Skip to content

Commit 72093f0

Browse files
committed
test: add scripts/generate-locale-matrix.test
1 parent 5608085 commit 72093f0

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Simple tests for the locale matrix generation scripts
5+
* Usage: node generate-locale-matrix.test.js
6+
*/
7+
8+
const { test } = require('node:test');
9+
const assert = require('node:assert');
10+
const {
11+
processManualTrigger,
12+
processAutoTrigger,
13+
isLocaleEnabled,
14+
getLocaleConfig,
15+
} = require('./generate-locale-matrix.js');
16+
17+
// Mock locale config for testing
18+
const mockLocaleConfig = {
19+
en: {
20+
secret_project_id: 'VERCEL_PROJECT_EN_ID',
21+
orama_private_api_key: 'ORAMA_PRIVATE_API_KEY_EN',
22+
enabled: true,
23+
},
24+
'zh-hans': {
25+
secret_project_id: 'VERCEL_PROJECT_ZH_HANS_ID',
26+
orama_private_api_key: 'ORAMA_PRIVATE_API_KEY_ZH_HANS',
27+
enabled: true,
28+
},
29+
fr: {
30+
secret_project_id: 'VERCEL_PROJECT_FR_ID',
31+
orama_private_api_key: 'ORAMA_PRIVATE_API_KEY_FR',
32+
enabled: false,
33+
},
34+
};
35+
36+
test('isLocaleEnabled should return correct boolean values', () => {
37+
assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'en'), true);
38+
assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'zh-hans'), true);
39+
assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'fr'), false);
40+
assert.strictEqual(isLocaleEnabled(mockLocaleConfig, 'nonexistent'), false);
41+
});
42+
43+
test('getLocaleConfig should return correct configuration values', () => {
44+
assert.strictEqual(
45+
getLocaleConfig(mockLocaleConfig, 'en', 'secret_project_id'),
46+
'VERCEL_PROJECT_EN_ID',
47+
);
48+
assert.strictEqual(
49+
getLocaleConfig(mockLocaleConfig, 'en', 'orama_private_api_key'),
50+
'ORAMA_PRIVATE_API_KEY_EN',
51+
);
52+
assert.strictEqual(
53+
getLocaleConfig(mockLocaleConfig, 'nonexistent', 'secret_project_id'),
54+
'',
55+
);
56+
assert.strictEqual(
57+
getLocaleConfig(mockLocaleConfig, 'en', 'nonexistent_field'),
58+
'',
59+
);
60+
});
61+
62+
test('processManualTrigger should handle empty manual locales', () => {
63+
const result = processManualTrigger(mockLocaleConfig, '');
64+
65+
// Should include all enabled locales
66+
assert.strictEqual(result.matrixInclude.length, 2);
67+
assert.strictEqual(result.hasChanges, true);
68+
69+
const locales = result.matrixInclude.map((item) => item.locale);
70+
assert.ok(locales.includes('en'));
71+
assert.ok(locales.includes('zh-hans'));
72+
assert.ok(!locales.includes('fr')); // fr is disabled
73+
});
74+
75+
test('processManualTrigger should handle specific manual locales', () => {
76+
const result = processManualTrigger(mockLocaleConfig, 'en');
77+
78+
// Should include only the specified locale
79+
assert.strictEqual(result.matrixInclude.length, 1);
80+
assert.strictEqual(result.hasChanges, true);
81+
assert.strictEqual(result.matrixInclude[0].locale, 'en');
82+
});
83+
84+
test('processManualTrigger should skip disabled locales', () => {
85+
const result = processManualTrigger(mockLocaleConfig, 'fr');
86+
87+
// Should not include disabled locale
88+
assert.strictEqual(result.matrixInclude.length, 0);
89+
assert.strictEqual(result.hasChanges, false);
90+
});
91+
92+
test('processAutoTrigger should handle core changes', () => {
93+
const mockChanges = {
94+
core_any_changed: 'true',
95+
en_any_changed: 'false',
96+
zh_hans_any_changed: 'false',
97+
fr_any_changed: 'false',
98+
};
99+
100+
const result = processAutoTrigger(mockLocaleConfig, mockChanges);
101+
102+
// Should include all enabled locales when core changes
103+
assert.strictEqual(result.matrixInclude.length, 2);
104+
assert.strictEqual(result.hasChanges, true);
105+
106+
const locales = result.matrixInclude.map((item) => item.locale);
107+
assert.ok(locales.includes('en'));
108+
assert.ok(locales.includes('zh-hans'));
109+
});
110+
111+
test('processAutoTrigger should handle specific locale changes', () => {
112+
const mockChanges = {
113+
core_any_changed: 'false',
114+
en_any_changed: 'true',
115+
zh_hans_any_changed: 'false',
116+
fr_any_changed: 'false',
117+
};
118+
119+
const result = processAutoTrigger(mockLocaleConfig, mockChanges);
120+
121+
// Should include only the changed locale
122+
assert.strictEqual(result.matrixInclude.length, 1);
123+
assert.strictEqual(result.hasChanges, true);
124+
assert.strictEqual(result.matrixInclude[0].locale, 'en');
125+
});
126+
127+
test('processAutoTrigger should handle no changes', () => {
128+
const mockChanges = {
129+
core_any_changed: 'false',
130+
en_any_changed: 'false',
131+
zh_hans_any_changed: 'false',
132+
fr_any_changed: 'false',
133+
};
134+
135+
const result = processAutoTrigger(mockLocaleConfig, mockChanges);
136+
137+
// Should include no locales when no changes
138+
assert.strictEqual(result.matrixInclude.length, 0);
139+
assert.strictEqual(result.hasChanges, false);
140+
});
141+
142+
console.log('All tests passed! ✅');

0 commit comments

Comments
 (0)