-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathi18n-extract.test.ts
More file actions
315 lines (297 loc) · 12.3 KB
/
Copy pathi18n-extract.test.ts
File metadata and controls
315 lines (297 loc) · 12.3 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import {
collectExpectedEntries,
extractTranslations,
renderTranslationModule,
} from '../src/utils/i18n-extract';
const config: any = {
objects: [
{
name: 'sys_position',
label: 'Role',
pluralLabel: 'Roles',
description: 'Role definitions for RBAC',
fields: {
label: { label: 'Display Name' },
active: { label: 'Active' },
status: {
label: 'Status',
options: [
{ value: 'on', label: 'On' },
{ value: 'off', label: 'Off' },
],
},
kind: {
label: 'Kind',
options: { internal: 'Internal', external: 'External' },
},
},
listViews: {
active: { label: 'Active', name: 'active' },
all: { label: 'All' },
},
actions: [
{
name: 'set_password',
label: 'Set Password',
resultDialog: {
title: 'Password Updated',
description: 'Copy the temporary password now — it is shown only once.',
acknowledge: 'Done',
fields: [
{ path: 'user.email', label: 'Email', format: 'text' },
{ path: 'temporaryPassword', label: 'Temporary Password', format: 'secret' },
{ path: 'unlabeled' }, // no label → no entry
],
},
params: [
{ field: 'label' },
{ field: 'active', label: 'Enabled Override' },
{
name: 'generatePassword',
label: 'Generate Temporary Password',
type: 'boolean',
helpText: 'Leave checked to auto-generate.',
},
{
name: 'mode',
type: 'select',
placeholder: 'Pick a mode',
options: [
{ value: 'auto', label: 'Auto' },
{ value: 'manual', label: 'Manual' },
],
},
],
},
],
},
],
actions: [
{
name: 'merge',
label: 'Merge',
objectName: 'sys_position',
confirmText: 'Merge?',
successMessage: 'Merged.',
},
{
name: 'export_csv',
label: 'Export CSV',
successMessage: 'Done.',
params: [{ name: 'delimiter', label: 'Delimiter' }],
},
],
translations: [
{
en: {
objects: {
sys_position: {
label: 'Role',
// pluralLabel missing intentionally
fields: { active: { label: 'Active' } },
},
},
},
},
],
};
describe('collectExpectedEntries', () => {
it('walks objects, fields, options (array + record), listViews, and actions', () => {
const entries = collectExpectedEntries(config);
const paths = entries.map((e) => e.path.join('.'));
expect(paths).toContain('objects.sys_position.label');
expect(paths).toContain('objects.sys_position.pluralLabel');
expect(paths).toContain('objects.sys_position.description');
expect(paths).toContain('objects.sys_position.fields.label.label');
expect(paths).toContain('objects.sys_position.fields.status.options.on');
expect(paths).toContain('objects.sys_position.fields.status.options.off');
expect(paths).toContain('objects.sys_position.fields.kind.options.internal');
expect(paths).toContain('objects.sys_position.fields.kind.options.external');
expect(paths).toContain('objects.sys_position._views.active.label');
expect(paths).toContain('objects.sys_position._views.all.label');
expect(paths).toContain('objects.sys_position._actions.merge.label');
expect(paths).toContain('objects.sys_position._actions.merge.confirmText');
expect(paths).toContain('objects.sys_position._actions.merge.successMessage');
expect(paths).toContain('globalActions.export_csv.label');
expect(paths).toContain('globalActions.export_csv.successMessage');
expect(paths).toContain('metadataForms.flow.fields.name.label');
});
it('carries source values from the schema', () => {
const entries = collectExpectedEntries(config);
const byPath = Object.fromEntries(entries.map((e) => [e.path.join('.'), e.sourceValue]));
expect(byPath['objects.sys_position.label']).toBe('Role');
expect(byPath['objects.sys_position.fields.status.options.on']).toBe('On');
expect(byPath['objects.sys_position.fields.kind.options.internal']).toBe('Internal');
expect(byPath['objects.sys_position._actions.merge.label']).toBe('Merge');
expect(byPath['metadataForms.flow.fields.name.label']).toBe('Name');
});
it('emits action param entries (inline + top-level), skipping field-backed labels without overrides', () => {
const entries = collectExpectedEntries(config);
const byPath = Object.fromEntries(entries.map((e) => [e.path.join('.'), e.sourceValue]));
const base = 'objects.sys_position._actions.set_password.params';
// Field-backed param with no literal override → no label entry (field
// translations cover it at runtime).
expect(byPath[`${base}.label.label`]).toBeUndefined();
// Field-backed param WITH a literal override → entry under the field name.
expect(byPath[`${base}.active.label`]).toBe('Enabled Override');
// Inline params emit label / helpText / placeholder / options.
expect(byPath[`${base}.generatePassword.label`]).toBe('Generate Temporary Password');
expect(byPath[`${base}.generatePassword.helpText`]).toBe('Leave checked to auto-generate.');
expect(byPath[`${base}.mode.label`]).toBe('mode'); // no label → falls back to name
expect(byPath[`${base}.mode.placeholder`]).toBe('Pick a mode');
expect(byPath[`${base}.mode.options.auto`]).toBe('Auto');
expect(byPath[`${base}.mode.options.manual`]).toBe('Manual');
// Top-level (global) actions get the same treatment.
expect(byPath['globalActions.export_csv.params.delimiter.label']).toBe('Delimiter');
});
it('emits resultDialog entries keyed by the literal field path (dots preserved)', () => {
const entries = collectExpectedEntries(config);
const base = 'objects.sys_position._actions.set_password.resultDialog';
const byPath = Object.fromEntries(entries.map((e) => [e.path.join('.'), e.sourceValue]));
expect(byPath[`${base}.title`]).toBe('Password Updated');
expect(byPath[`${base}.description`]).toBe('Copy the temporary password now — it is shown only once.');
expect(byPath[`${base}.acknowledge`]).toBe('Done');
expect(byPath[`${base}.fields.temporaryPassword`]).toBe('Temporary Password');
// The dotted path stays ONE segment ('user.email'), never split.
const emailEntry = entries.find(
(e) => e.path.join('\u0000') === ['objects', 'sys_position', '_actions', 'set_password', 'resultDialog', 'fields', 'user.email'].join('\u0000'),
);
expect(emailEntry?.sourceValue).toBe('Email');
// Fields without a label emit nothing.
expect(byPath[`${base}.fields.unlabeled`]).toBeUndefined();
});
it('walks pages and their page:header copy (objectstack#3589)', () => {
const pageConfig: any = {
pages: [
{
name: 'connect_agent',
label: 'Connect an Agent',
description: 'Agent onboarding',
regions: [
{
name: 'header',
components: [
{
type: 'page:header',
// `title` duplicates `label` — resolved by the label
// fallback, so it must NOT emit its own entry.
properties: { title: 'Connect an Agent', subtitle: 'Governed MCP access.', icon: 'bot' },
},
],
},
{ name: 'main', components: [{ type: 'mcp:connect-agent', properties: {} }] },
],
},
{
name: 'renamed_header',
label: 'Nav Label',
regions: [
{ name: 'header', components: [{ type: 'page:header', properties: { title: 'Different Title' } }] },
],
},
{ name: 'bare_page', label: 'Bare' },
],
};
const entries = collectExpectedEntries(pageConfig);
const byPath = Object.fromEntries(entries.map((e) => [e.path.join('.'), e.sourceValue]));
expect(byPath['pages.connect_agent.label']).toBe('Connect an Agent');
expect(byPath['pages.connect_agent.description']).toBe('Agent onboarding');
expect(byPath['pages.connect_agent.subtitle']).toBe('Governed MCP access.');
// title === label → no redundant entry for translators to fill twice.
expect(byPath['pages.connect_agent.title']).toBeUndefined();
// A header title that genuinely differs from the label does emit.
expect(byPath['pages.renamed_header.title']).toBe('Different Title');
// Non-header components and non-translatable props (icon) contribute
// nothing — the page namespace holds only the four translatable keys.
const pagePaths = Object.keys(byPath).filter((p) => p.startsWith('pages.'));
expect(pagePaths.sort()).toEqual([
'pages.bare_page.label',
'pages.connect_agent.description',
'pages.connect_agent.label',
'pages.connect_agent.subtitle',
'pages.renamed_header.label',
'pages.renamed_header.title',
]);
});
});
describe('extractTranslations', () => {
it('fills the default locale from schema and emits empty strings for other locales', () => {
const { bundles, counts } = extractTranslations(config, {
defaultLocale: 'en',
locales: ['zh-CN'],
fill: 'empty',
mergeExisting: false,
});
expect(counts.en).toBeGreaterThan(0);
expect(counts['zh-CN']).toBe(counts.en);
expect(bundles.en.objects?.sys_position?.label).toBe('Role');
expect(bundles['zh-CN'].objects?.sys_position?.label).toBe('');
expect(bundles['zh-CN'].objects?.sys_position?.fields?.status?.options?.on).toBe('');
});
it('supports --fill=default (copy from source) and --fill=todo (prefix)', () => {
const { bundles } = extractTranslations(config, {
defaultLocale: 'en',
locales: ['zh-CN'],
fill: 'default',
});
expect(bundles['zh-CN'].objects?.sys_position?.label).toBe('Role');
const { bundles: todoBundles } = extractTranslations(config, {
defaultLocale: 'en',
locales: ['zh-CN'],
fill: 'todo',
});
expect(todoBundles['zh-CN'].objects?.sys_position?.label).toBe('[TODO] Role');
});
it('mergeExisting carries through values already translated in the input bundle', () => {
const { bundles } = extractTranslations(config, {
defaultLocale: 'en',
locales: ['en'],
mergeExisting: true,
});
// Existing translations are preserved verbatim so the generated file
// is a complete, self-contained bundle (not just a delta).
expect(bundles.en.objects?.sys_position?.label).toBe('Role');
expect(bundles.en.objects?.sys_position?.fields?.active?.label).toBe('Active');
// Missing keys are still filled from schema defaults.
expect(bundles.en.objects?.sys_position?.pluralLabel).toBe('Roles');
expect(bundles.en.objects?.sys_position?.fields?.label?.label).toBe('Display Name');
});
it('filters by object name regex', () => {
const cfg = {
objects: [
{ name: 'sys_position', label: 'Role', fields: {} },
{ name: 'crm_account', label: 'Account', fields: {} },
],
};
const { bundles, totalExpected } = extractTranslations(cfg, {
defaultLocale: 'en',
filter: /^sys_/,
mergeExisting: false,
});
expect(bundles.en.objects?.sys_position).toBeDefined();
expect(bundles.en.objects?.crm_account).toBeUndefined();
expect(totalExpected).toBe(1);
});
});
describe('renderTranslationModule', () => {
it('emits a TypeScript module with a typed default export', () => {
const { bundles } = extractTranslations(config, {
defaultLocale: 'en',
mergeExisting: false,
});
const ts = renderTranslationModule(bundles.en, { locale: 'en' });
expect(ts).toContain("import type { TranslationData } from '@objectstack/spec/system'");
expect(ts).toContain('export const enObjects:');
expect(ts).toContain('sys_position:');
expect(ts).toContain('label: "Role"');
});
it('quotes non-identifier keys (e.g. hyphenated values)', () => {
const ts = renderTranslationModule(
{ objects: { 'foo-bar': { label: 'Foo Bar', fields: {} } } as any },
{ locale: 'en' },
);
expect(ts).toContain('"foo-bar":');
});
});