@fgv/ts-res-cli 5.0.0-3 → 5.0.0-30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,341 +0,0 @@
1
- /*
2
- * CLI Context Filter Working Tests - Tests that demonstrate working functionality
3
- */
4
-
5
- import '@fgv/ts-utils-jest';
6
- import { promises as fs } from 'fs';
7
- import * as path from 'path';
8
- import { spawn } from 'child_process';
9
- import * as TsRes from '@fgv/ts-res';
10
-
11
- describe('CLI Context Filter Working Tests', () => {
12
- let tempDir: string;
13
- let cliBin: string;
14
-
15
- beforeEach(async () => {
16
- tempDir = await fs.mkdtemp(path.join(__dirname, '../../temp/test-temp-working-'));
17
- cliBin = path.resolve(__dirname, '../../bin/ts-res-compile.js');
18
- });
19
-
20
- afterEach(async () => {
21
- await fs.rm(tempDir, { recursive: true, force: true });
22
- });
23
-
24
- async function runCli(args: string[]): Promise<{ exitCode: number; stdout: string; stderr: string }> {
25
- return new Promise((resolve) => {
26
- const child = spawn('node', [cliBin, ...args], {
27
- stdio: ['pipe', 'pipe', 'pipe']
28
- });
29
-
30
- let stdout = '';
31
- let stderr = '';
32
-
33
- child.stdout.on('data', (data) => {
34
- stdout += data.toString();
35
- });
36
-
37
- child.stderr.on('data', (data) => {
38
- stderr += data.toString();
39
- });
40
-
41
- child.on('close', (exitCode) => {
42
- resolve({ exitCode: exitCode || 0, stdout, stderr });
43
- });
44
- });
45
- }
46
-
47
- test('context filter successfully filters language candidates', async () => {
48
- const inputFile = path.join(tempDir, 'test.json');
49
- const outputFile = path.join(tempDir, 'output.json');
50
-
51
- const resources = {
52
- resources: [
53
- {
54
- id: 'welcome.message',
55
- resourceTypeName: 'json',
56
- candidates: [
57
- {
58
- json: { text: 'Welcome!' },
59
- conditions: { language: 'en' }
60
- },
61
- {
62
- json: { text: '¡Bienvenido!' },
63
- conditions: { language: 'es' }
64
- },
65
- {
66
- json: { text: 'Willkommen!' },
67
- conditions: { language: 'de' }
68
- }
69
- ]
70
- }
71
- ]
72
- };
73
-
74
- await fs.writeFile(inputFile, JSON.stringify(resources, null, 2));
75
-
76
- const result = await runCli([
77
- 'compile',
78
- '--input',
79
- inputFile,
80
- '--output',
81
- outputFile,
82
- '--context-filter',
83
- 'language=en',
84
- '--format',
85
- 'source'
86
- ]);
87
-
88
- expect(result.exitCode).toBe(0);
89
- expect(result.stdout).toMatch(/Successfully compiled resources/);
90
-
91
- const outputContent = await fs.readFile(outputFile, 'utf-8');
92
- const output = JSON.parse(outputContent);
93
-
94
- expect(TsRes.ResourceJson.Convert.resourceCollectionDecl.convert(output.resources)).toSucceedAndSatisfy(
95
- (resources) => {
96
- expect(resources?.resources?.length).toBeGreaterThan(0);
97
-
98
- const welcomeResource = resources?.resources?.find((r) => r.id === 'test.welcome.message');
99
- expect(welcomeResource).toBeDefined();
100
- expect(welcomeResource?.candidates?.length).toBe(1);
101
- expect(welcomeResource?.candidates?.[0]?.conditions).toEqual([
102
- { qualifierName: 'language', value: 'en' }
103
- ]);
104
- expect(welcomeResource?.candidates?.[0]?.json).toEqual({ text: 'Welcome!' });
105
-
106
- return true;
107
- }
108
- );
109
- });
110
-
111
- test('context filter works with territory qualifier', async () => {
112
- const inputFile = path.join(tempDir, 'territory.json');
113
- const outputFile = path.join(tempDir, 'territory-output.json');
114
-
115
- const resources = {
116
- resources: [
117
- {
118
- id: 'currency.symbol',
119
- resourceTypeName: 'json',
120
- candidates: [
121
- {
122
- json: { symbol: '$', name: 'US Dollar' },
123
- conditions: { currentTerritory: 'US' }
124
- },
125
- {
126
- json: { symbol: '€', name: 'Euro' },
127
- conditions: { currentTerritory: 'DE' }
128
- }
129
- ]
130
- }
131
- ]
132
- };
133
-
134
- await fs.writeFile(inputFile, JSON.stringify(resources, null, 2));
135
-
136
- const result = await runCli([
137
- 'compile',
138
- '--input',
139
- inputFile,
140
- '--output',
141
- outputFile,
142
- '--context-filter',
143
- 'currentTerritory=US',
144
- '--format',
145
- 'source'
146
- ]);
147
-
148
- expect(result.exitCode).toBe(0);
149
-
150
- const outputContent = await fs.readFile(outputFile, 'utf-8');
151
- const output = JSON.parse(outputContent);
152
-
153
- expect(TsRes.ResourceJson.Convert.resourceCollectionDecl.convert(output.resources)).toSucceedAndSatisfy(
154
- (resources) => {
155
- expect(resources?.resources?.length).toBeGreaterThan(0);
156
-
157
- const currencyResource = resources?.resources?.find((r) => r.id === 'territory.currency.symbol');
158
- expect(currencyResource).toBeDefined();
159
- expect(currencyResource?.candidates?.length).toBe(1);
160
- expect(currencyResource?.candidates?.[0]?.conditions).toEqual([
161
- { qualifierName: 'currentTerritory', value: 'US' }
162
- ]);
163
- expect(currencyResource?.candidates?.[0]?.json).toEqual({ symbol: '$', name: 'US Dollar' });
164
-
165
- return true;
166
- }
167
- );
168
- });
169
-
170
- test('context filter works with multiple qualifiers', async () => {
171
- const inputFile = path.join(tempDir, 'multi.json');
172
- const outputFile = path.join(tempDir, 'multi-output.json');
173
-
174
- const resources = {
175
- resources: [
176
- {
177
- id: 'greeting.formal',
178
- resourceTypeName: 'json',
179
- candidates: [
180
- {
181
- json: { text: 'Good morning' },
182
- conditions: { language: 'en', currentTerritory: 'US' }
183
- },
184
- {
185
- json: { text: 'Good day' },
186
- conditions: { language: 'en', currentTerritory: 'GB' }
187
- },
188
- {
189
- json: { text: 'Buenos días' },
190
- conditions: { language: 'es', currentTerritory: 'ES' }
191
- }
192
- ]
193
- }
194
- ]
195
- };
196
-
197
- await fs.writeFile(inputFile, JSON.stringify(resources, null, 2));
198
-
199
- const result = await runCli([
200
- 'compile',
201
- '--input',
202
- inputFile,
203
- '--output',
204
- outputFile,
205
- '--context-filter',
206
- 'language=en|currentTerritory=US',
207
- '--format',
208
- 'source'
209
- ]);
210
-
211
- expect(result.exitCode).toBe(0);
212
-
213
- const outputContent = await fs.readFile(outputFile, 'utf-8');
214
- const output = JSON.parse(outputContent);
215
-
216
- expect(TsRes.ResourceJson.Convert.resourceCollectionDecl.convert(output.resources)).toSucceedAndSatisfy(
217
- (resources) => {
218
- expect(resources?.resources?.length).toBeGreaterThan(0);
219
-
220
- const greetingResource = resources?.resources?.find((r) => r.id === 'multi.greeting.formal');
221
- expect(greetingResource).toBeDefined();
222
- expect(greetingResource?.candidates?.length).toBe(1);
223
- expect(greetingResource?.candidates?.[0]?.conditions).toEqual(
224
- expect.arrayContaining([
225
- { qualifierName: 'language', value: 'en' },
226
- { qualifierName: 'currentTerritory', value: 'US' }
227
- ])
228
- );
229
- expect(greetingResource?.candidates?.[0]?.json).toEqual({ text: 'Good morning' });
230
-
231
- return true;
232
- }
233
- );
234
- });
235
-
236
- test('context filter works with JSON format for comparison', async () => {
237
- const inputFile = path.join(tempDir, 'json-format.json');
238
- const outputFile = path.join(tempDir, 'json-format-output.json');
239
-
240
- const resources = {
241
- resources: [
242
- {
243
- id: 'test.comparison',
244
- resourceTypeName: 'json',
245
- candidates: [
246
- {
247
- json: { text: 'English' },
248
- conditions: { language: 'en' }
249
- },
250
- {
251
- json: { text: 'Spanish' },
252
- conditions: { language: 'es' }
253
- }
254
- ]
255
- }
256
- ]
257
- };
258
-
259
- await fs.writeFile(inputFile, JSON.stringify(resources, null, 2));
260
-
261
- const result = await runCli([
262
- 'compile',
263
- '--input',
264
- inputFile,
265
- '--output',
266
- outputFile,
267
- '--context',
268
- '{"language": "en"}',
269
- '--format',
270
- 'source'
271
- ]);
272
-
273
- expect(result.exitCode).toBe(0);
274
-
275
- const outputContent = await fs.readFile(outputFile, 'utf-8');
276
- const output = JSON.parse(outputContent);
277
-
278
- expect(TsRes.ResourceJson.Convert.resourceCollectionDecl.convert(output.resources)).toSucceedAndSatisfy(
279
- (resources) => {
280
- expect(resources?.resources?.length).toBeGreaterThan(0);
281
-
282
- const testResource = resources?.resources?.find((r) => r.id === 'json-format.test.comparison');
283
- expect(testResource).toBeDefined();
284
- expect(testResource?.candidates?.length).toBeGreaterThanOrEqual(1);
285
- const englishCandidate = testResource?.candidates?.find((c) =>
286
- c.conditions?.some((cond) => cond.qualifierName === 'language' && cond.value === 'en')
287
- );
288
- expect(englishCandidate).toBeDefined();
289
- expect(englishCandidate?.conditions).toEqual([{ qualifierName: 'language', value: 'en' }]);
290
- expect(englishCandidate?.json).toEqual({ text: 'English' });
291
-
292
- return true;
293
- }
294
- );
295
- });
296
-
297
- test('info command works with context filter', async () => {
298
- const inputFile = path.join(tempDir, 'info.json');
299
-
300
- const resources = {
301
- resources: [
302
- {
303
- id: 'info.test1',
304
- resourceTypeName: 'json',
305
- candidates: [
306
- {
307
- json: { message: 'Hello' },
308
- conditions: { language: 'en' }
309
- }
310
- ]
311
- },
312
- {
313
- id: 'info.test2',
314
- resourceTypeName: 'json',
315
- candidates: [
316
- {
317
- json: { message: 'Hola' },
318
- conditions: { language: 'es' }
319
- }
320
- ]
321
- }
322
- ]
323
- };
324
-
325
- await fs.writeFile(inputFile, JSON.stringify(resources, null, 2));
326
-
327
- const result = await runCli(['info', '--input', inputFile, '--context-filter', 'language=en']);
328
-
329
- expect(result.exitCode).toBe(0);
330
-
331
- const info = JSON.parse(result.stdout);
332
- expect(info).toHaveProperty('totalResources');
333
- expect(info).toHaveProperty('filteredResources');
334
- expect(info).toHaveProperty('totalCandidates');
335
- expect(info).toHaveProperty('filteredCandidates');
336
-
337
- expect(info.totalResources).toBe(2);
338
- expect(info.filteredResources).toBeLessThanOrEqual(info.totalResources); // Resources should be filtered
339
- expect(info.filteredCandidates).toBe(1); // Only English candidate should be included
340
- });
341
- });