-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
126 lines (105 loc) · 3.73 KB
/
test.js
File metadata and controls
126 lines (105 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Unit Tests for DoughnutChartGenerator PCF Component
*/
const crypto = require('crypto');
function computeHmacSha256Sync(secretKey, message) {
return crypto.createHmac('sha256', secretKey).update(message).digest('hex');
}
function normalizeColors(colors) {
if (!colors) return '';
return colors.split('|').map(c => {
const trimmed = c.trim();
const withoutHash = trimmed.startsWith('#') ? trimmed.slice(1) : trimmed;
return /^[0-9A-Fa-f]{6}$/.test(withoutHash) ? withoutHash.toUpperCase() : '';
}).filter(c => c !== '').join('|');
}
function parseDataValues(data) {
if (!data) return [];
const trimmed = data.trim();
const separator = trimmed.includes('|') ? '|' : ',';
return trimmed.split(separator).map(v => parseFloat(v.trim())).filter(v => !isNaN(v));
}
function formatDataAwesome(values) {
if (values.length === 0) return '';
return 'a:' + values.join(',');
}
function parseLabels(labels) {
if (!labels) return [];
const trimmed = labels.trim();
const separator = trimmed.includes('|') ? '|' : ',';
return trimmed.split(separator).map(l => l.trim()).filter(l => l !== '');
}
function buildDoughnutChartUrl(params) {
const { accountId, secretKey, privateCloudDomain, data, labels, colors, title, chartSize } = params;
const host = privateCloudDomain || 'image-charts.com';
const dataValues = parseDataValues(data);
// cht=pd for doughnut (pie donut)
const queryParts = ['cht=pd', 'chs=' + (chartSize || '400x300'), 'chd=' + formatDataAwesome(dataValues)];
if (labels) {
const labelArr = parseLabels(labels);
if (labelArr.length > 0) queryParts.push('chl=' + labelArr.join('|'));
}
if (colors) {
const normalizedColors = normalizeColors(colors);
if (normalizedColors) queryParts.push('chco=' + normalizedColors);
}
if (title) queryParts.push('chtt=' + title);
if (accountId && !privateCloudDomain) queryParts.push('icac=' + accountId);
const queryString = queryParts.join('&');
if (accountId && secretKey && !privateCloudDomain) {
const signature = computeHmacSha256Sync(secretKey, queryString);
return 'https://' + host + '/chart?' + queryString + '&ichm=' + signature;
}
return 'https://' + host + '/chart?' + queryString;
}
describe('Doughnut Chart URL Building', () => {
test('should build doughnut chart URL with cht=pd', () => {
const url = buildDoughnutChartUrl({
accountId: 'test_account',
secretKey: 'test_secret',
data: '30,40,30',
labels: 'Red,Green,Blue'
});
expect(url).toContain('cht=pd');
expect(url).toContain('chd=a:30,40,30');
expect(url).toContain('chl=Red|Green|Blue');
expect(url).toContain('ichm=');
});
test('should include colors', () => {
const url = buildDoughnutChartUrl({
accountId: 'test_account',
secretKey: 'test_secret',
data: '30,40,30',
colors: 'FF0000|00FF00|0000FF'
});
expect(url).toContain('chco=FF0000|00FF00|0000FF');
});
test('should include title', () => {
const url = buildDoughnutChartUrl({
accountId: 'test_account',
secretKey: 'test_secret',
data: '50,50',
title: 'Market Share'
});
expect(url).toContain('chtt=Market Share');
});
test('should handle Private Cloud mode', () => {
const url = buildDoughnutChartUrl({
privateCloudDomain: 'charts.mycompany.com',
data: '30,40,30'
});
expect(url).toContain('https://charts.mycompany.com/chart');
expect(url).not.toContain('ichm=');
});
});
describe('Doughnut vs Pie', () => {
test('should use pd chart type (not p)', () => {
const url = buildDoughnutChartUrl({
accountId: 'test',
secretKey: 'test',
data: '50,50'
});
expect(url).toContain('cht=pd');
expect(url).not.toContain('cht=p&');
});
});