-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtranslation.test.ts
More file actions
1545 lines (1406 loc) · 60.6 KB
/
Copy pathtranslation.test.ts
File metadata and controls
1545 lines (1406 loc) · 60.6 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect } from 'vitest';
import { z } from 'zod';
import { PAGE_COMPONENT_COPY_KEYS, FLOW_SCREEN_COPY_KEYS, FLOW_SCREEN_FIELD_COPY_KEYS } from './i18n-resolver';
import { FlowSchema } from '../automation/flow.zod';
import { ScreenConfigSchema, ScreenFieldConfigSchema } from '../automation/builtin-node-config.zod';
import {
TranslationDataSchema,
TranslationBundleSchema,
LocaleSchema,
FieldTranslationSchema,
ObjectTranslationDataSchema,
TranslationConfigSchema,
TranslationItemSchema,
defineTranslation,
TranslationDiffStatusSchema,
TranslationDiffItemSchema,
TranslationCoverageResultSchema,
CoverageBreakdownEntrySchema,
type TranslationBundle,
type ObjectTranslationData,
type TranslationConfig,
type TranslationItem,
type TranslationDiffItem,
type TranslationCoverageResult,
type CoverageBreakdownEntry,
} from './translation.zod';
describe('LocaleSchema', () => {
it('should accept valid locale strings', () => {
const validLocales = ['en-US', 'zh-CN', 'es-ES', 'fr-FR', 'de-DE', 'ja-JP'];
validLocales.forEach(locale => {
expect(() => LocaleSchema.parse(locale)).not.toThrow();
});
});
it('should accept simple language codes', () => {
const locales = ['en', 'zh', 'es', 'fr', 'de'];
locales.forEach(locale => {
expect(() => LocaleSchema.parse(locale)).not.toThrow();
});
});
});
describe('TranslationDataSchema', () => {
it('should accept empty translation data', () => {
const data = TranslationDataSchema.parse({});
expect(data).toBeDefined();
});
it('should accept object translations', () => {
const data = TranslationDataSchema.parse({
objects: {
account: {
label: 'Account',
pluralLabel: 'Accounts',
},
},
});
expect(data.objects?.account.label).toBe('Account');
});
it('should accept field translations', () => {
const data = TranslationDataSchema.parse({
objects: {
account: {
label: 'Account',
fields: {
name: {
label: 'Account Name',
help: 'Enter the name of the account',
},
status: {
label: 'Status',
options: {
active: 'Active',
inactive: 'Inactive',
},
},
},
},
},
});
expect(data.objects?.account.fields?.name.label).toBe('Account Name');
expect(data.objects?.account.fields?.status.options?.active).toBe('Active');
});
it('should accept app translations', () => {
const data = TranslationDataSchema.parse({
apps: {
sales: {
label: 'Sales',
description: 'Manage your sales pipeline',
},
},
});
expect(data.apps?.sales.label).toBe('Sales');
});
it('should accept message translations', () => {
const data = TranslationDataSchema.parse({
messages: {
'common.save': 'Save',
'common.cancel': 'Cancel',
'error.required': 'This field is required',
},
});
expect(data.messages?.['common.save']).toBe('Save');
});
it('should accept complete translation data', () => {
const data = TranslationDataSchema.parse({
objects: {
account: {
label: 'Account',
pluralLabel: 'Accounts',
fields: {
name: {
label: 'Name',
},
},
},
},
apps: {
sales: {
label: 'Sales',
},
},
messages: {
'common.save': 'Save',
},
});
expect(data.objects).toBeDefined();
expect(data.apps).toBeDefined();
expect(data.messages).toBeDefined();
});
});
describe('TranslationBundleSchema', () => {
it('should accept valid translation bundle', () => {
const bundle: TranslationBundle = {
'en-US': {
objects: {
account: {
label: 'Account',
pluralLabel: 'Accounts',
},
},
},
};
expect(() => TranslationBundleSchema.parse(bundle)).not.toThrow();
});
it('should accept multi-language bundle', () => {
const bundle = TranslationBundleSchema.parse({
'en-US': {
objects: {
account: {
label: 'Account',
},
},
messages: {
'common.save': 'Save',
},
},
'zh-CN': {
objects: {
account: {
label: '客户',
},
},
messages: {
'common.save': '保存',
},
},
});
expect(bundle['en-US'].objects?.account.label).toBe('Account');
expect(bundle['zh-CN'].objects?.account.label).toBe('客户');
});
it('should handle English translations', () => {
const bundle = TranslationBundleSchema.parse({
'en-US': {
objects: {
account: {
label: 'Account',
pluralLabel: 'Accounts',
fields: {
name: {
label: 'Account Name',
help: 'The name of the account',
},
type: {
label: 'Type',
options: {
customer: 'Customer',
partner: 'Partner',
vendor: 'Vendor',
},
},
},
},
},
apps: {
sales: {
label: 'Sales',
description: 'Manage your sales pipeline',
},
},
messages: {
'common.save': 'Save',
'common.cancel': 'Cancel',
'common.delete': 'Delete',
},
},
});
expect(bundle['en-US'].objects?.account.label).toBe('Account');
});
it('should handle Chinese translations', () => {
const bundle = TranslationBundleSchema.parse({
'zh-CN': {
objects: {
account: {
label: '客户',
pluralLabel: '客户',
fields: {
name: {
label: '客户名称',
help: '输入客户名称',
},
},
},
},
messages: {
'common.save': '保存',
'common.cancel': '取消',
},
},
});
expect(bundle['zh-CN'].objects?.account.label).toBe('客户');
});
it('should handle Spanish translations', () => {
const bundle = TranslationBundleSchema.parse({
'es-ES': {
objects: {
account: {
label: 'Cuenta',
pluralLabel: 'Cuentas',
},
},
messages: {
'common.save': 'Guardar',
'common.cancel': 'Cancelar',
},
},
});
expect(bundle['es-ES'].objects?.account.label).toBe('Cuenta');
});
it('should handle field option translations', () => {
const bundle = TranslationBundleSchema.parse({
'en-US': {
objects: {
opportunity: {
label: 'Opportunity',
fields: {
stage: {
label: 'Stage',
options: {
prospecting: 'Prospecting',
qualification: 'Qualification',
proposal: 'Proposal',
closed_won: 'Closed Won',
closed_lost: 'Closed Lost',
},
},
},
},
},
},
'zh-CN': {
objects: {
opportunity: {
label: '商机',
fields: {
stage: {
label: '阶段',
options: {
prospecting: '寻找客户',
qualification: '资格审查',
proposal: '提案',
closed_won: '成交',
closed_lost: '失败',
},
},
},
},
},
},
});
expect(bundle['en-US'].objects?.opportunity.fields?.stage.options?.prospecting).toBe('Prospecting');
expect(bundle['zh-CN'].objects?.opportunity.fields?.stage.options?.prospecting).toBe('寻找客户');
});
it('should handle app menu translations', () => {
const bundle = TranslationBundleSchema.parse({
'en-US': {
apps: {
sales: {
label: 'Sales',
description: 'Manage your sales pipeline and opportunities',
},
service: {
label: 'Service',
description: 'Handle customer support cases',
},
},
},
'fr-FR': {
apps: {
sales: {
label: 'Ventes',
description: 'Gérez votre pipeline de ventes',
},
service: {
label: 'Service',
description: 'Gérez les cas de support client',
},
},
},
});
expect(bundle['en-US'].apps?.sales.label).toBe('Sales');
expect(bundle['fr-FR'].apps?.sales.label).toBe('Ventes');
});
it('should handle UI message translations', () => {
const bundle = TranslationBundleSchema.parse({
'en-US': {
messages: {
'error.required': 'This field is required',
'error.invalid_email': 'Invalid email address',
'success.saved': 'Successfully saved',
'confirm.delete': 'Are you sure you want to delete this record?',
},
},
'de-DE': {
messages: {
'error.required': 'Dieses Feld ist erforderlich',
'error.invalid_email': 'Ungültige E-Mail-Adresse',
'success.saved': 'Erfolgreich gespeichert',
'confirm.delete': 'Möchten Sie diesen Datensatz wirklich löschen?',
},
},
});
expect(bundle['en-US'].messages?.['error.required']).toBe('This field is required');
expect(bundle['de-DE'].messages?.['error.required']).toBe('Dieses Feld ist erforderlich');
});
it('should accept empty locale data', () => {
const bundle = TranslationBundleSchema.parse({
'en-US': {},
'zh-CN': {},
});
expect(bundle['en-US']).toBeDefined();
expect(bundle['zh-CN']).toBeDefined();
});
it('should handle partial translations', () => {
const bundle = TranslationBundleSchema.parse({
'en-US': {
objects: {
account: {
label: 'Account',
},
},
messages: {
'common.save': 'Save',
},
},
'zh-CN': {
objects: {
account: {
label: '客户',
},
},
// messages not translated yet
},
});
expect(bundle['zh-CN'].objects?.account.label).toBe('客户');
expect(bundle['zh-CN'].messages).toBeUndefined();
});
});
// ============================================================================
// Translation validationMessages — RETIRED in 17.0.0 (#4667)
// ============================================================================
//
// This block used to assert the group parsed at both doors. It was never read
// by any resolver, so a translated rule message was stored and never shown;
// the rejection now lives in the "retired translation.validationMessages"
// describe at the bottom of this file. Validation messages are authored on the
// rule itself (`object.validations[].message`).
// ============================================================================
// FieldTranslationSchema
// ============================================================================
describe('FieldTranslationSchema', () => {
it('should accept label only', () => {
const result = FieldTranslationSchema.parse({ label: 'Account Name' });
expect(result.label).toBe('Account Name');
expect(result.help).toBeUndefined();
expect(result.options).toBeUndefined();
});
it('should accept label with help text', () => {
const result = FieldTranslationSchema.parse({
label: 'Industry',
help: 'Select the primary industry',
});
expect(result.help).toBe('Select the primary industry');
});
it('should accept field with options', () => {
const result = FieldTranslationSchema.parse({
label: 'Status',
options: { active: 'Active', inactive: 'Inactive' },
});
expect(result.options?.active).toBe('Active');
});
it('should accept empty object', () => {
const result = FieldTranslationSchema.parse({});
expect(result).toBeDefined();
});
it('should accept field with placeholder', () => {
const result = FieldTranslationSchema.parse({
label: 'Email',
placeholder: 'Enter your email address',
});
expect(result.placeholder).toBe('Enter your email address');
});
it('should accept field with all properties including placeholder', () => {
const result = FieldTranslationSchema.parse({
label: '邮箱',
help: '输入您的电子邮箱地址',
placeholder: '例如:user@example.com',
options: { work: '工作邮箱', personal: '个人邮箱' },
});
expect(result.label).toBe('邮箱');
expect(result.placeholder).toBe('例如:user@example.com');
});
});
// ============================================================================
// ObjectTranslationDataSchema — per-object file validation
// ============================================================================
describe('ObjectTranslationDataSchema', () => {
it('should accept minimal object translation', () => {
const data: ObjectTranslationData = {
label: 'Account',
};
const result = ObjectTranslationDataSchema.parse(data);
expect(result.label).toBe('Account');
expect(result.pluralLabel).toBeUndefined();
expect(result.fields).toBeUndefined();
});
it('should accept full object translation (en/account.json)', () => {
const data = ObjectTranslationDataSchema.parse({
label: 'Account',
pluralLabel: 'Accounts',
fields: {
name: { label: 'Account Name', help: 'Legal name of the company' },
type: {
label: 'Type',
options: { customer: 'Customer', partner: 'Partner', vendor: 'Vendor' },
},
industry: { label: 'Industry' },
},
});
expect(data.label).toBe('Account');
expect(data.pluralLabel).toBe('Accounts');
expect(data.fields?.name.label).toBe('Account Name');
expect(data.fields?.type.options?.customer).toBe('Customer');
});
it('should accept Chinese object translation (zh-CN/account.json)', () => {
const data = ObjectTranslationDataSchema.parse({
label: '客户',
pluralLabel: '客户',
fields: {
name: { label: '客户名称', help: '公司或组织的法定名称' },
type: {
label: '类型',
options: { customer: '正式客户', partner: '合作伙伴' },
},
},
});
expect(data.label).toBe('客户');
expect(data.fields?.name.help).toBe('公司或组织的法定名称');
});
it('should accept a partial object translation with no label', () => {
// Partial translation is the normal state — see the schema's note on why
// `label` is optional.
const data = ObjectTranslationDataSchema.parse({
fields: { name: { label: 'Account Name' } },
});
expect(data.label).toBeUndefined();
expect(data.fields?.name.label).toBe('Account Name');
});
it('should compose into TranslationDataSchema via objects record', () => {
const localeData = TranslationDataSchema.parse({
objects: {
account: { label: 'Account', pluralLabel: 'Accounts' },
contact: { label: 'Contact' },
},
});
expect(localeData.objects?.account.label).toBe('Account');
expect(localeData.objects?.contact.label).toBe('Contact');
});
});
// ============================================================================
// TranslationConfigSchema
// ============================================================================
describe('TranslationConfigSchema', () => {
it('should accept minimal config', () => {
const config: TranslationConfig = TranslationConfigSchema.parse({
defaultLocale: 'en',
supportedLocales: ['en'],
});
expect(config.defaultLocale).toBe('en');
expect(config.supportedLocales).toEqual(['en']);
expect(config.fallbackLocale).toBeUndefined();
});
it('should accept full multi-language config', () => {
const config = TranslationConfigSchema.parse({
defaultLocale: 'en',
supportedLocales: ['en', 'zh-CN', 'ja-JP', 'es-ES'],
fallbackLocale: 'en',
});
expect(config.supportedLocales).toHaveLength(4);
expect(config.fallbackLocale).toBe('en');
});
it('should reject config without defaultLocale', () => {
expect(() =>
TranslationConfigSchema.parse({
supportedLocales: ['en'],
}),
).toThrow();
});
it('should reject config without supportedLocales', () => {
expect(() =>
TranslationConfigSchema.parse({
defaultLocale: 'en',
}),
).toThrow();
});
});
// ============================================================================
// TranslationItemSchema — the runtime-authored `translation` metadata type
// ============================================================================
describe('TranslationItemSchema', () => {
it('should accept a single-locale item in the runtime `objects.` shape', () => {
const item: TranslationItem = defineTranslation({
locale: 'zh-CN',
objects: {
account: {
label: '客户',
fields: { name: { label: '客户名称' } },
_views: { all_accounts: { label: '全部客户' } },
_actions: { merge: { label: '合并客户', confirmText: '确认合并?' } },
},
},
apps: { crm: { label: '客户关系管理' } },
messages: { 'common.save': '保存' },
});
expect(item.locale).toBe('zh-CN');
expect(item.objects?.account.label).toBe('客户');
expect(item.objects?.account._actions?.merge.confirmText).toBe('确认合并?');
expect(item.apps?.crm.label).toBe('客户关系管理');
});
it('should require a locale — an unresolvable one is silently skipped at runtime', () => {
const result = TranslationItemSchema.safeParse({
objects: { account: { label: '客户' } },
});
expect(result.success).toBe(false);
expect(result.error?.issues.some((i) => i.path[0] === 'locale')).toBe(true);
});
it('should accept a partial item that translates one field and nothing else', () => {
const item = TranslationItemSchema.parse({
locale: 'ja-JP',
objects: { account: { fields: { name: { label: '取引先名' } } } },
});
expect(item.objects?.account.label).toBeUndefined();
expect(item.objects?.account.fields?.name.label).toBe('取引先名');
});
it.each([
['o', 'objects.<object_name>'],
['app', 'apps.<app_name>'],
['nav', 'apps.<app_name>.navigation'],
['dashboard', 'dashboards.<dashboard_name>'],
['_globalOptions', 'objects.<object_name>.fields.<field_name>.options'],
['_meta', "top-level 'locale'"],
['namespace', 'omit it'],
])('should reject the retired object-first key `%s` with an actionable message', (key, hint) => {
const result = TranslationItemSchema.safeParse({
locale: 'zh-CN',
[key]: { account: { label: '客户' } },
});
expect(result.success).toBe(false);
// The prescriptions used to come from a bespoke `z.preprocess` that scanned
// for these ten keys; since #4001 they ride the strict unknown-key error as
// `guidance`, so the issue is `unrecognized_keys` naming the key rather than
// a custom issue at `path: [key]`. The message is what an author reads, and
// it still has to carry the destination.
const issue = result.error?.issues.find((i) => i.code === 'unrecognized_keys');
expect(issue).toBeDefined();
expect((issue as { keys?: string[] } | undefined)?.keys).toContain(key);
expect(issue?.message).toContain(hint);
});
it('should not offer `app` → `apps` as a rename — the inner shapes differ', () => {
// Edit distance 1, so without a `guidance` entry the suggester would call
// this a typo. It is not: the object-first `app.<object>` and the current
// `apps.<app_name>` hold different content, so the fix is a rewrite. A
// guidance entry suppresses the rename, which is the point of having one.
const result = TranslationItemSchema.safeParse({ locale: 'en', app: { account: { label: 'Account' } } });
expect(result.success).toBe(false);
const message = result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message ?? '';
expect(message).not.toContain('Did you mean');
expect(message).toContain('apps.<app_name>');
});
it('should reject an unknown key the retired-key list never named (#4001)', () => {
// What the #3778 guard could not do. It enumerated ten keys someone had
// thought of; `object` for `objects` was not one of them, and a bundle
// written that way saved clean and resolved to nothing.
const result = TranslationItemSchema.safeParse({
locale: 'zh-CN',
object: { account: { label: '客户' } },
});
expect(result.success).toBe(false);
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain('`object` → `objects`');
});
it('should round-trip its own identity keys (the Studio create seed sends both)', () => {
// `metadata-create-seeds.ts` ships `{ name, label, locale, objects }` as the
// authoritative minimal create shape, and its test asserts every seed is
// spec-valid — the canonical guard for "designer create shape ≠ spec".
// That guard could only ever catch a MISSING required key: an extra key the
// schema did not declare was stripped, so the seed validated while two
// thirds of it was being thrown away. Closing the shape made it fail, which
// is the guard finally working in both directions.
const item = TranslationItemSchema.parse({ name: 'zh-CN', label: '简体中文', locale: 'zh-CN' });
expect(item.name).toBe('zh-CN');
expect(item.label).toBe('简体中文');
});
it('should declare the ADR-0010 protection envelope', () => {
// The loader stamps these on every registered type; undeclared, they were
// dropped on each parse, and `authored-translation-sync` strips them by
// hand on the read side because the schema could not hold them.
const item = TranslationItemSchema.parse({
locale: 'en',
objects: { account: { label: 'Account' } },
_packageId: 'com.example.pkg',
_provenance: 'package',
_lock: 'no-overlay',
});
expect(item._packageId).toBe('com.example.pkg');
expect(item._lock).toBe('no-overlay');
});
it('should reject the retired shape rather than silently stripping it (#3778)', () => {
// The pre-fix failure mode: Zod strips undeclared keys, so an `o.`-shaped
// item saved cleanly and then resolved to nothing. A save that succeeds
// must be a save that renders.
const result = TranslationItemSchema.safeParse({
locale: 'zh-CN',
o: { account: { label: '客户' } },
});
expect(result.success).toBe(false);
});
it('should not leak the retired keys into a parsed item', () => {
const item = TranslationItemSchema.parse({
locale: 'en',
objects: { account: { label: 'Account' } },
});
expect(Object.keys(item)).toEqual(expect.arrayContaining(['locale', 'objects']));
for (const key of ['o', 'app', 'nav', 'dashboard', '_meta', 'namespace']) {
expect(item).not.toHaveProperty(key);
}
});
it('should not advertise the retired keys in its generated JSON Schema', () => {
// The JSON Schema is what `/meta/types/:type` hands the Studio editor and
// any agent authoring metadata — listing a retired key there would teach
// the shape the refinement then rejects.
for (const io of ['input', 'output'] as const) {
const json = z.toJSONSchema(TranslationItemSchema, { io, unrepresentable: 'any' }) as {
properties?: Record<string, unknown>;
required?: string[];
};
expect(json.required).toContain('locale');
expect(Object.keys(json.properties ?? {})).toEqual(
expect.not.arrayContaining(['o', 'app', 'nav', 'dashboard', '_meta', 'namespace']),
);
expect(json.properties).toHaveProperty('objects');
}
});
it('should compose into a TranslationBundle entry (item == one bundle locale)', () => {
const item = TranslationItemSchema.parse({
locale: 'zh-CN',
objects: { account: { label: '客户' } },
});
const { locale, ...data } = item;
const bundle = TranslationBundleSchema.parse({ [locale]: data });
expect(bundle['zh-CN'].objects?.account.label).toBe('客户');
});
});
// ============================================================================
// Unknown-key strictness across the translation groups (#4001)
// ============================================================================
describe('translation unknown-key strictness (#4001)', () => {
// The failure this file is closing is unusually cruel: a translation that
// resolves to nothing is indistinguishable from a translation nobody wrote.
// There is no wrong string on screen to notice — just the source language,
// forever, exactly as if the key were still on the backlog.
it('rejects a retired key on the file-authored BUNDLE path, which the guard never covered', () => {
// #3778's `z.preprocess` ran on the item door only. The examples and the
// platform apps author bundles (`defineTranslationBundle`), and the same
// ten keys were stripped there in silence. Same asymmetry #4522 found in
// #1535's object guard: closed at one door, open at the other.
const result = TranslationBundleSchema.safeParse({
en: { o: { account: { label: 'Account' } } },
});
expect(result.success).toBe(false);
const message = result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message ?? '';
expect(message).toContain('objects.<object_name>');
});
it.each([
['a top-level group', { messsages: { 'common.save': 'Save' } }, 'messages'],
['an app translation', { apps: { crm: { label: 'CRM', nagivation: {} } } }, 'navigation'],
['a page translation', { pages: { home: { subtitel: 'Welcome' } } }, 'subtitle'],
['a dashboard widget', { dashboards: { sales: { widgets: { rev: { titel: 'Revenue' } } } } }, 'title'],
['a settings key', { settings: { mail: { keys: { host: { lable: 'Host' } } } } }, 'label'],
['a metadata form field', { metadataForms: { object: { fields: { name: { helpTxt: 'x' } } } } }, 'helpText'],
])('rejects a typo in %s and names the key it meant', (_what, body, expected) => {
const result = TranslationDataSchema.safeParse(body);
expect(result.success).toBe(false);
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain(`→ \`${expected}\``);
});
it.each([
['object', { objects: { account: { views: {} } } }, '_views'],
['object', { objects: { account: { actions: {} } } }, '_actions'],
['object', { objects: { account: { sections: {} } } }, '_sections'],
])('points the un-prefixed %s group at its `_`-prefixed name', (_what, body, expected) => {
const result = TranslationDataSchema.safeParse(body);
expect(result.success).toBe(false);
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain(`→ \`${expected}\``);
});
it('sends `help` on an action param to `helpText`, the spelling that surface uses', () => {
// `help` is correct on a FIELD translation and on a settings key; on an
// action param it is `helpText`. Borrowing from a neighbouring surface
// reads as a real word, not a slip, so edit distance cannot rule on it —
// this is what the alias tables are for.
const result = TranslationDataSchema.safeParse({
globalActions: { export_csv: { params: { format: { help: 'CSV or XLSX' } } } },
});
expect(result.success).toBe(false);
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain('`help` → `helpText`');
});
// ──────────────────────────────────────────────────────────────────────────
// #6080 — `pages.<name>.components.<id>`, the page half of `dashboards.widgets`
// ──────────────────────────────────────────────────────────────────────────
describe('page component copy (#6080)', () => {
const parse = (components: unknown) =>
TranslationDataSchema.safeParse({ pages: { sales_home_page: { label: 'Sales', components } } });
it('accepts the measured key face, keyed by component id', () => {
const result = parse({
quick_create: { title: 'Quick Create' },
kpi_revenue_won: { label: 'Revenue (Won)' },
ai_briefing: { title: 'Ask the AI', description: 'Open the panel.' },
lead_picker: { placeholder: 'Search…', emptyText: 'No records' },
});
expect(result.success).toBe(true);
});
it('refuses `submitLabel` with the retirement prescription (#10926)', () => {
// Flipped, not deleted: until #10926 this case pinned `submitLabel` as
// an accepted copy key (latterly on a bespoke component type, after
// #9249 retired `element:form`, its only spec-declared carrier). The
// maintainer ruled retire over re-anchor, so the same authored shape now
// pins the rejection — and the rejection must carry the upgrade.
const result = parse({ new_lead_form: { submitLabel: 'Create' } });
expect(result.success).toBe(false);
const message = result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message;
expect(message).toContain('`submitLabel` was removed in @objectstack/spec 17 (ADR-0049)');
expect(message).toContain('`submitText`');
});
it('refuses the retired `submit` alias spelling with the same story', () => {
// `submit` was an alias (rejection-path suggestion) pointing at
// `submitLabel`; with the target retired the alias converts to guidance
// so the spelling lands on the prescription instead of a dangling
// rename suggestion.
const result = parse({ new_lead_form: { submit: 'Create' } });
expect(result.success).toBe(false);
const message = result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message;
expect(message).toContain('`submit` was the alias spelling of `submitLabel`');
});
it('stays `.strict()` — an invented key is still refused', () => {
const result = parse({ quick_create: { tooltip: 'Create a record' } });
expect(result.success).toBe(false);
expect(result.error?.issues.some((i) => i.code === 'unrecognized_keys')).toBe(true);
});
it('sends `help` to `description` rather than declaring a key no component has', () => {
// The issue proposed `help` in this face. No component in
// `ComponentPropsMap` declares it, so declaring it would parse clean and
// translate nothing (ADR-0078). It is an alias instead.
const result = parse({ ai_briefing: { help: 'Open the panel.' } });
expect(result.success).toBe(false);
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain('`help` → `description`');
});
it('keeps `subtitle` at the page level — one string, one spelling', () => {
// `page:header` is `subtitle`'s only declarer and is addressed by page
// name, so a per-component `subtitle` would be a second route to it.
expect(parse({ some_header: { subtitle: 'Welcome back' } }).success).toBe(false);
expect(TranslationDataSchema.safeParse({
pages: { sales_home_page: { subtitle: 'Welcome back' } },
}).success).toBe(true);
});
it('names this surface in the error, not the dashboard widget one', () => {
const result = parse({ quick_create: { titel: 'Quick Create' } });
expect(result.error?.issues[0]?.message).toContain('this page component translation');
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain('→ `title`');
});
it('declares exactly the keys the resolver and the extractor act on', () => {
// `PAGE_COMPONENT_COPY_KEYS` drives both `translatePage`'s overlay and
// the CLI's skeleton extraction. If this schema declared a key missing
// from that list the extractor would offer a slot nothing reads; if the
// list carried one this schema lacks, `.strict()` would reject the very
// key the extractor just wrote. Pin the two together.
for (const key of PAGE_COMPONENT_COPY_KEYS) {
expect(parse({ some_component: { [key]: 'x' } }).success, `\`${key}\` must be declared`).toBe(true);
}
const declared = Object.keys(
(TranslationDataSchema.safeParse({
pages: { p: { components: { c: Object.fromEntries(PAGE_COMPONENT_COPY_KEYS.map((k) => [k, 'x'])) } } },
}) as { success: true; data: any }).data.pages.p.components.c,
).sort();
expect(declared).toEqual([...PAGE_COMPONENT_COPY_KEYS].sort());
});
it('carries the `label`/`title` trap alias the dashboard widget face carries', () => {
// A page's headline is `label`; a component's is `title`. One level
// apart, opposite spellings — the same trap `dashboards.widgets` names.
const result = parse({ quick_create: { name: 'Quick Create' } });
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain('`name` → `title`');
});
});
// ──────────────────────────────────────────────────────────────────────────
// #7862 — `dashboards.<name>.widgets.<id>.subCaption`, the metric widget's
// sub-caption (`options.description`). #5428 item 4: two authored fields,
// two keys — `description` translates `widget.description`, `subCaption`
// translates `widget.options.description`; sharing one key is forbidden.
// ──────────────────────────────────────────────────────────────────────────
describe('dashboard widget sub-caption (#7862)', () => {
const parse = (widgets: unknown) =>
TranslationDataSchema.safeParse({ dashboards: { sales: { widgets } } });
it('accepts `subCaption` on the widget node, alongside title/description', () => {
const result = parse({
rev: { title: '营收', description: '本季度确认的营收', subCaption: '较上季度' },
});
expect(result.success, JSON.stringify(result.error?.issues)).toBe(true);
});
it('keeps `title`/`description` byte-identical through the parse', () => {
const body = { rev: { title: 'Revenue', description: 'Recognized revenue this quarter' } };
const result = parse(body);
expect(result.success).toBe(true);
expect((result as { success: true; data: any }).data.dashboards.sales.widgets)
.toEqual(body);
});
it('stays `.strict()` — an invented key is still refused, with substance', () => {
const result = parse({ rev: { footnote: 'x' } });
expect(result.success).toBe(false);
const message = result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message ?? '';
expect(message).toContain('this widget translation');
expect(message).toContain('`footnote`');
});
it('sends `subtitle` to `subCaption`, not to `description`', () => {
// On a metric widget the string an author calls the "subtitle" is the
// sub-caption under the number (`options.description`). Pointing it at
// `description` would steer authors to precisely the shared key the
// #5428 ruling forbids (「两个作者字段两个 key」).
const result = parse({ rev: { subtitle: '较上季度' } });
expect(result.success).toBe(false);
const message = result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message ?? '';
expect(message).toContain('`subtitle` → `subCaption`');
expect(message).not.toContain('`subtitle` → `description`');
});
it('suggests `subCaption` for a near-miss spelling', () => {
const result = parse({ rev: { subCaptoin: '较上季度' } });
expect(result.success).toBe(false);
expect(result.error?.issues.find((i) => i.code === 'unrecognized_keys')?.message)
.toContain('→ `subCaption`');
});
});
// ──────────────────────────────────────────────────────────────────────────
// #7646 — `flows.<name>.screens.<nodeId>`, the screen-flow wizard's copy
// ──────────────────────────────────────────────────────────────────────────
describe('screen-flow copy (#7646)', () => {
const parse = (flows: unknown) => TranslationDataSchema.safeParse({ flows });
it('accepts a fully-populated flow entry', () => {
const result = parse({
lead_conversion: {
label: '转化线索',
screens: {
conversion_details: {
title: '转化详情',
fields: {
create_opportunity: { label: '创建商机?' },
opportunity_name: { label: '商机名称', placeholder: '输入商机名称' },
opportunity_amount: { label: '商机金额' },
},
},
},
},
});
expect(result.success, JSON.stringify(result.error?.issues)).toBe(true);
});
it('accepts partial entries at every level — the family\'s partial-locale semantics', () => {
// Every key on this surface is optional for the same reason
// `ObjectTranslationDataSchema.label` is: partial translation is the