-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathobject.test.ts
More file actions
1410 lines (1279 loc) · 45.3 KB
/
Copy pathobject.test.ts
File metadata and controls
1410 lines (1279 loc) · 45.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
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 { ObjectSchema, ObjectCapabilities, IndexSchema, ObjectFieldGroupSchema, ObjectExternalBindingSchema, ObjectAccessConfigSchema, LifecycleSchema, TenancyConfigSchema, resolveCrudAffordances, type ServiceObject } from './object.zod';
describe('ObjectCapabilities', () => {
it('should apply default values correctly', () => {
const result = ObjectCapabilities.parse({});
expect(result.trackHistory).toBe(false);
expect(result.searchable).toBe(true);
expect(result.apiEnabled).toBe(true);
expect(result.files).toBe(false);
// feeds/activities are opt-OUT capabilities (#2707): default on, consumers
// gate on explicit `false` only — same posture as trash/mru/clone.
expect(result.feeds).toBe(true);
expect(result.activities).toBe(true);
expect(result.trash).toBe(true);
expect(result.mru).toBe(true);
expect(result.clone).toBe(true);
});
it('should accept custom capability values', () => {
const capabilities = {
trackHistory: true,
searchable: false,
apiEnabled: true,
files: true,
feeds: true,
activities: false,
trash: false,
mru: true,
clone: true,
};
const result = ObjectCapabilities.parse(capabilities);
expect(result).toEqual(capabilities);
});
});
describe('LifecycleSchema (ADR-0057)', () => {
it('accepts the ADR §3.2 telemetry rotation shape', () => {
const result = LifecycleSchema.safeParse({
class: 'telemetry',
retention: { maxAge: '14d' },
storage: { strategy: 'rotation', shards: 14, unit: 'day' },
reclaim: true,
});
expect(result.success).toBe(true);
});
it('accepts the ADR §3.2 audit archive-then-delete shape', () => {
const result = LifecycleSchema.safeParse({
class: 'audit',
retention: { maxAge: '90d' },
archive: { after: '90d', to: 'datalake', keep: '7y' },
});
expect(result.success).toBe(true);
});
it('accepts the ADR §3.2 transient ttl shape', () => {
const result = LifecycleSchema.safeParse({
class: 'transient',
ttl: { field: 'created_at', expireAfter: '7d' },
});
expect(result.success).toBe(true);
});
it('accepts a bare record class (permanent, no policies)', () => {
expect(LifecycleSchema.safeParse({ class: 'record' }).success).toBe(true);
});
it('rejects a non-record class with no bounding policy (§3.5 enforce-or-remove)', () => {
for (const cls of ['audit', 'telemetry', 'transient', 'event'] as const) {
const result = LifecycleSchema.safeParse({ class: cls });
expect(result.success).toBe(false);
}
});
it('rejects retention/ttl/storage/archive on a record class', () => {
const result = LifecycleSchema.safeParse({
class: 'record',
retention: { maxAge: '30d' },
});
expect(result.success).toBe(false);
});
it('rejects an archive window that does not start where the hot window ends', () => {
const result = LifecycleSchema.safeParse({
class: 'audit',
retention: { maxAge: '90d' },
archive: { after: '30d', to: 'datalake' },
});
expect(result.success).toBe(false);
});
it('rejects malformed duration literals', () => {
for (const bad of ['14', 'd14', '14 days', '2mo', '-3d', '1.5d']) {
const result = LifecycleSchema.safeParse({
class: 'telemetry',
retention: { maxAge: bad },
});
expect(result.success).toBe(false);
}
});
it('accepts retention.onlyWhen with scalar and $in predicates (#2834 mixed tables)', () => {
const result = LifecycleSchema.safeParse({
class: 'telemetry',
retention: {
maxAge: '30d',
onlyWhen: { status: { $in: ['completed', 'failed'] }, archived: true },
},
});
expect(result.success).toBe(true);
});
it('rejects onlyWhen operators other than $in and empty $in lists', () => {
for (const bad of [
{ status: { $nin: ['paused'] } }, // unsupported operator
{ status: { $in: [] } }, // empty list matches nothing — surely a mistake
{ status: { $in: ['a'], extra: 1 } }, // strict object: no extra keys
]) {
const result = LifecycleSchema.safeParse({
class: 'telemetry',
retention: { maxAge: '30d', onlyWhen: bad },
});
expect(result.success).toBe(false);
}
});
it('rejects onlyWhen combined with rotation storage (shard DROPs ignore filters)', () => {
const result = LifecycleSchema.safeParse({
class: 'telemetry',
retention: { maxAge: '14d', onlyWhen: { status: 'done' } },
storage: { strategy: 'rotation', shards: 14, unit: 'day' },
});
expect(result.success).toBe(false);
});
it('rejects onlyWhen combined with archive (the Archiver moves rows by age alone)', () => {
const result = LifecycleSchema.safeParse({
class: 'audit',
retention: { maxAge: '90d', onlyWhen: { status: 'done' } },
archive: { after: '90d', to: 'datalake' },
});
expect(result.success).toBe(false);
});
it('is accepted as an object-level property by ObjectSchema.create', () => {
const obj = ObjectSchema.create({
name: 'my_trace',
fields: {},
lifecycle: {
class: 'telemetry',
retention: { maxAge: '14d' },
},
});
expect(obj.lifecycle?.class).toBe('telemetry');
expect(obj.lifecycle?.retention?.maxAge).toBe('14d');
});
it('objects without a lifecycle block stay back-compatible (undefined = record semantics)', () => {
const obj = ObjectSchema.create({ name: 'plain_object', fields: {} });
expect(obj.lifecycle).toBeUndefined();
});
});
describe('IndexSchema', () => {
it('should accept basic index definition', () => {
const index = {
fields: ['email'],
};
expect(() => IndexSchema.parse(index)).not.toThrow();
});
it('should accept index with all properties', () => {
const index = {
name: 'idx_email_status',
fields: ['email', 'status'],
unique: true,
};
expect(() => IndexSchema.parse(index)).not.toThrow();
});
it('should accept composite index', () => {
const index = {
fields: ['tenant_id', 'created_at', 'status'],
unique: false,
};
expect(() => IndexSchema.parse(index)).not.toThrow();
});
it('should reject index without fields', () => {
expect(() => IndexSchema.parse({})).toThrow();
});
});
describe('ObjectSchema', () => {
describe('Basic Object Properties', () => {
it('should accept minimal valid object', () => {
const validObject: ServiceObject = {
name: 'account',
fields: {},
};
const result = ObjectSchema.safeParse(validObject);
expect(result.success).toBe(true);
});
it('should enforce snake_case for object name', () => {
const validNames = ['account', 'project_task', 'user_profile', '_system'];
validNames.forEach(name => {
expect(() => ObjectSchema.parse({ name, fields: {} })).not.toThrow();
});
const invalidNames = ['Account', 'project-task', 'UserProfile', '123object'];
invalidNames.forEach(name => {
expect(() => ObjectSchema.parse({ name, fields: {} })).toThrow();
});
});
it('should apply default values', () => {
const object = {
name: 'test_object',
fields: {},
};
const result = ObjectSchema.parse(object);
expect(result.datasource).toBe('default');
expect(result.isSystem).toBe(false);
});
});
describe('Object with Fields', () => {
it('should accept object with multiple fields', () => {
const objectWithFields: ServiceObject = {
name: 'contact',
label: 'Contact',
pluralLabel: 'Contacts',
fields: {
first_name: {
label: 'First Name',
type: 'text',
required: true,
maxLength: 50,
},
last_name: {
label: 'Last Name',
type: 'text',
required: true,
maxLength: 50,
},
email: {
label: 'Email',
type: 'email',
unique: true,
},
phone: {
label: 'Phone',
type: 'phone',
},
},
};
expect(() => ObjectSchema.parse(objectWithFields)).not.toThrow();
});
it('should enforce snake_case for field names', () => {
// Valid snake_case field names
const validFieldNames = ['first_name', 'last_name', 'email', 'company_name', 'annual_revenue', '_system_id'];
validFieldNames.forEach(fieldName => {
const obj = {
name: 'test_object',
fields: {
[fieldName]: {
type: 'text' as const,
label: 'Test Field',
},
},
};
expect(() => ObjectSchema.parse(obj)).not.toThrow();
});
});
it('should reject PascalCase field names', () => {
const invalidObject = {
name: 'lead',
fields: {
FirstName: {
type: 'text' as const,
label: '名',
},
},
};
expect(() => ObjectSchema.parse(invalidObject)).toThrow();
expect(() => ObjectSchema.parse(invalidObject)).toThrow(/Field names must be lowercase snake_case/);
});
it('should reject camelCase field names', () => {
const invalidObject = {
name: 'lead',
fields: {
firstName: {
type: 'text' as const,
label: 'First Name',
},
},
};
expect(() => ObjectSchema.parse(invalidObject)).toThrow();
expect(() => ObjectSchema.parse(invalidObject)).toThrow(/Field names must be lowercase snake_case/);
});
it('should reject kebab-case field names', () => {
const invalidObject = {
name: 'lead',
fields: {
'first-name': {
type: 'text' as const,
label: 'First Name',
},
},
};
expect(() => ObjectSchema.parse(invalidObject)).toThrow();
expect(() => ObjectSchema.parse(invalidObject)).toThrow(/Field names must be lowercase snake_case/);
});
it('should reject field names with spaces', () => {
const invalidObject = {
name: 'lead',
fields: {
'first name': {
type: 'text' as const,
label: 'First Name',
},
},
};
expect(() => ObjectSchema.parse(invalidObject)).toThrow();
expect(() => ObjectSchema.parse(invalidObject)).toThrow(/Field names must be lowercase snake_case/);
});
it('should reject field names starting with numbers', () => {
const invalidObject = {
name: 'lead',
fields: {
'123field': {
type: 'text' as const,
label: 'Field',
},
},
};
expect(() => ObjectSchema.parse(invalidObject)).toThrow();
expect(() => ObjectSchema.parse(invalidObject)).toThrow(/Field names must be lowercase snake_case/);
});
it('should reject mixed-case field names like in AI-generated objects', () => {
// This is the exact problem from the issue
const aiGeneratedObject = {
name: 'lead',
label: '线索',
fields: {
FirstName: {
type: 'text' as const,
label: '名',
maxLength: 40,
},
LastName: {
type: 'text' as const,
label: '姓',
required: true,
maxLength: 80,
},
Company: {
type: 'text' as const,
label: '公司',
required: true,
maxLength: 255,
},
},
};
expect(() => ObjectSchema.parse(aiGeneratedObject)).toThrow();
expect(() => ObjectSchema.parse(aiGeneratedObject)).toThrow(/Field names must be lowercase snake_case/);
});
});
describe('Object Metadata', () => {
it('should accept object with full metadata', () => {
const fullObject: ServiceObject = {
name: 'opportunity',
label: 'Opportunity',
pluralLabel: 'Opportunities',
description: 'Sales opportunities tracking',
icon: 'target',
datasource: 'salesforce',
isSystem: false,
nameField: 'opportunity_name',
fields: {
opportunity_name: {
label: 'Opportunity Name',
type: 'text',
},
},
};
expect(() => ObjectSchema.parse(fullObject)).not.toThrow();
});
it('should accept object with field-level columnName for storage decoupling', () => {
const object = ObjectSchema.parse({
name: 'user',
fields: {
email: {
type: 'email',
columnName: 'email_address',
},
created_at: {
type: 'datetime',
columnName: 'createdAt',
},
},
});
expect(object.name).toBe('user');
expect(object.fields.email.columnName).toBe('email_address');
expect(object.fields.created_at.columnName).toBe('createdAt');
});
});
describe('Object with Indexes', () => {
it('should accept object with indexes', () => {
const objectWithIndexes: ServiceObject = {
name: 'user',
fields: {
email: {
label: 'Email',
type: 'email',
},
username: {
label: 'Username',
type: 'text',
},
},
indexes: [
{
name: 'idx_email',
fields: ['email'],
unique: true,
},
{
name: 'idx_username',
fields: ['username'],
unique: true,
},
{
fields: ['email', 'username'],
},
],
};
expect(() => ObjectSchema.parse(objectWithIndexes)).not.toThrow();
});
});
describe('Object Capabilities', () => {
it('should accept object with custom capabilities', () => {
const objectWithCapabilities: ServiceObject = {
name: 'case',
fields: {},
enable: {
trackHistory: true,
searchable: true,
apiEnabled: true,
files: true,
feedEnabled: true,
trash: true,
},
};
expect(() => ObjectSchema.parse(objectWithCapabilities)).not.toThrow();
});
it('should merge default capabilities with custom values', () => {
const object = {
name: 'task',
fields: {},
enable: {
trackHistory: true,
files: true,
},
};
const result = ObjectSchema.parse(object);
expect(result.enable?.trackHistory).toBe(true);
expect(result.enable?.files).toBe(true);
expect(result.enable?.searchable).toBe(true); // default
expect(result.enable?.apiEnabled).toBe(true); // default
});
});
describe('Complete Real-World Examples', () => {
it('should accept CRM Account object', () => {
const accountObject: ServiceObject = {
name: 'account',
label: 'Account',
pluralLabel: 'Accounts',
description: 'Companies and organizations',
icon: 'building-2',
nameField: 'account_name',
fields: {
account_name: {
label: 'Account Name',
type: 'text',
required: true,
maxLength: 255,
},
account_number: {
label: 'Account Number',
type: 'text',
unique: true,
externalId: true,
},
website: {
label: 'Website',
type: 'url',
},
industry: {
label: 'Industry',
type: 'select',
options: [
{ label: 'Technology', value: 'tech' },
{ label: 'Finance', value: 'finance' },
{ label: 'Healthcare', value: 'healthcare' },
],
},
annual_revenue: {
label: 'Annual Revenue',
type: 'currency',
precision: 18,
scale: 2,
},
owner_id: {
label: 'Account Owner',
type: 'lookup',
reference: 'user',
},
},
indexes: [
{
name: 'idx_account_number',
fields: ['account_number'],
unique: true,
},
],
enable: {
trackHistory: true,
searchable: true,
apiEnabled: true,
files: true,
feedEnabled: true,
trash: true,
},
};
expect(() => ObjectSchema.parse(accountObject)).not.toThrow();
});
it('should accept Task object with parent relationship', () => {
const taskObject: ServiceObject = {
name: 'task',
label: 'Task',
pluralLabel: 'Tasks',
icon: 'check-square',
nameField: 'subject',
fields: {
subject: {
label: 'Subject',
type: 'text',
required: true,
},
status: {
label: 'Status',
type: 'select',
options: [
{ label: 'Not Started', value: 'not_started', default: true },
{ label: 'In Progress', value: 'in_progress' },
{ label: 'Completed', value: 'completed' },
],
},
priority: {
label: 'Priority',
type: 'select',
options: [
{ label: 'Low', value: 'low', color: '#00FF00' },
{ label: 'Medium', value: 'medium', color: '#FFA500', default: true },
{ label: 'High', value: 'high', color: '#FF0000' },
],
},
environment_id: {
label: 'Project',
type: 'master_detail',
reference: 'project',
deleteBehavior: 'cascade',
},
assigned_to: {
label: 'Assigned To',
type: 'lookup',
reference: 'user',
},
due_date: {
label: 'Due Date',
type: 'date',
},
completed_at: {
label: 'Completed At',
type: 'datetime',
},
},
enable: {
trackHistory: false,
searchable: true,
apiEnabled: true,
files: false,
feedEnabled: false,
trash: true,
},
};
expect(() => ObjectSchema.parse(taskObject)).not.toThrow();
});
// ADR-0020: record state machines are no longer a standalone
// `object.stateMachines` map. They converge onto a single
// `state_machine` validation rule on the object — a flat
// field + transitions table enforced on the write path.
it('should validate an object with a state_machine validation rule', () => {
const objectWithState = {
name: 'leave_request',
fields: {
status: { type: 'text' },
},
validations: [
{
type: 'state_machine',
name: 'leave_flow',
field: 'status',
message: 'Invalid status transition.',
transitions: {
draft: ['pending'],
pending: ['approved', 'draft'],
approved: [],
},
},
],
};
const result = ObjectSchema.parse(objectWithState);
const rule = result.validations!.find((v) => v.name === 'leave_flow');
expect(rule).toBeDefined();
expect(rule!.type).toBe('state_machine');
expect((rule as { field: string }).field).toBe('status');
expect((rule as { transitions: Record<string, string[]> }).transitions.draft).toEqual([
'pending',
]);
});
it('should allow multiple state_machine rules over distinct fields', () => {
const order = {
name: 'order',
fields: {
status: { type: 'text' },
payment_status: { type: 'text' },
},
validations: [
{
type: 'state_machine',
name: 'lifecycle',
field: 'status',
message: 'Invalid status transition.',
transitions: {
draft: ['submitted'],
submitted: ['confirmed'],
confirmed: [],
},
},
{
type: 'state_machine',
name: 'payment',
field: 'payment_status',
message: 'Invalid payment_status transition.',
transitions: {
unpaid: ['partial', 'paid'],
partial: ['paid'],
paid: [],
},
},
],
};
const result = ObjectSchema.parse(order);
const machines = result.validations!.filter((v) => v.type === 'state_machine');
expect(machines.map((m) => m.name)).toEqual(['lifecycle', 'payment']);
});
});
});
// ============================================================================
// Protocol Improvement Tests: displayNameField and recordName
// ============================================================================
describe('ObjectSchema - displayNameField', () => {
it('should accept displayNameField', () => {
const result = ObjectSchema.parse({
name: 'ticket',
fields: {
title: { type: 'text' },
},
displayNameField: 'title',
});
expect(result.displayNameField).toBe('title');
});
it('should accept object without displayNameField (optional)', () => {
const result = ObjectSchema.parse({
name: 'ticket',
fields: {
name: { type: 'text' },
},
});
expect(result.displayNameField).toBeUndefined();
});
// ADR-0079: `nameField` is the canonical pointer; `displayNameField` is a
// deprecated alias that the schema maps onto `nameField` on parse.
it('should accept the canonical nameField pointer', () => {
const result = ObjectSchema.parse({
name: 'ticket',
fields: { title: { type: 'text' } },
nameField: 'title',
});
expect(result.nameField).toBe('title');
});
it('should map deprecated displayNameField onto nameField (back-compat alias)', () => {
const result = ObjectSchema.parse({
name: 'ticket',
fields: { title: { type: 'text' } },
displayNameField: 'title',
});
expect(result.nameField).toBe('title');
expect(result.displayNameField).toBe('title'); // preserved for cross-repo consumers
});
it('should map the alias through ObjectSchema.create() as well', () => {
const result = ObjectSchema.create({
name: 'ticket',
fields: { title: { type: 'text' } },
displayNameField: 'title',
});
expect(result.nameField).toBe('title');
});
it('explicit nameField takes precedence over displayNameField alias', () => {
const result = ObjectSchema.parse({
name: 'ticket',
fields: { a: { type: 'text' }, b: { type: 'text' } },
nameField: 'a',
displayNameField: 'b',
});
expect(result.nameField).toBe('a');
});
});
describe('ObjectSchema - recordName', () => {
it('should accept recordName with autonumber config', () => {
const result = ObjectSchema.parse({
name: 'invoice',
fields: {
total: { type: 'number' },
},
recordName: {
type: 'autonumber',
displayFormat: 'INV-{YYYY}-{0000}',
startNumber: 1,
},
});
expect(result.recordName?.type).toBe('autonumber');
expect(result.recordName?.displayFormat).toBe('INV-{YYYY}-{0000}');
expect(result.recordName?.startNumber).toBe(1);
});
it('should accept recordName with text type', () => {
const result = ObjectSchema.parse({
name: 'account',
fields: {
name: { type: 'text' },
},
recordName: {
type: 'text',
},
});
expect(result.recordName?.type).toBe('text');
expect(result.recordName?.displayFormat).toBeUndefined();
});
it('should accept object without recordName (optional)', () => {
const result = ObjectSchema.parse({
name: 'task',
fields: {
title: { type: 'text' },
},
});
expect(result.recordName).toBeUndefined();
});
});
describe('ObjectSchema.create()', () => {
it('should auto-generate label from snake_case name', () => {
const result = ObjectSchema.create({
name: 'project_task',
fields: {
title: { type: 'text' },
},
});
expect(result.label).toBe('Project Task');
});
it('should preserve explicitly provided label', () => {
const result = ObjectSchema.create({
name: 'project_task',
label: 'My Custom Label',
fields: {
title: { type: 'text' },
},
});
expect(result.label).toBe('My Custom Label');
});
it('should auto-generate label from single-word name', () => {
const result = ObjectSchema.create({
name: 'account',
fields: {
name: { type: 'text' },
},
});
expect(result.label).toBe('Account');
});
it('should validate and apply defaults', () => {
const result = ObjectSchema.create({
name: 'task',
fields: {
title: { type: 'text' },
},
});
expect(result.active).toBe(true);
expect(result.isSystem).toBe(false);
expect(result.abstract).toBe(false);
expect(result.datasource).toBe('default');
});
it('should throw on invalid name format', () => {
expect(() => ObjectSchema.create({
name: 'InvalidName',
fields: { title: { type: 'text' } },
})).toThrow();
});
it('should throw on invalid field name format', () => {
expect(() => ObjectSchema.create({
name: 'task',
fields: { InvalidField: { type: 'text' } },
})).toThrow();
});
// ADR-0032 "no silent failure" for metadata shape (issue #1535): unknown
// top-level keys used to be stripped silently, shipping dead metadata.
describe('unknown-key rejection (#1535)', () => {
it('rejects object-level `workflows` with guidance toward hooks/record_change', () => {
expect(() => ObjectSchema.create({
name: 'demo',
fields: { status: { type: 'text' } },
// @ts-expect-error — `workflows` is not an ObjectSchema field
workflows: [{ name: 'stamp', triggerType: 'on_update', actions: [] }],
})).toThrow(/workflows/);
});
it('error message points at the supported mechanism, not just "unknown key"', () => {
let message = '';
try {
ObjectSchema.create({
name: 'demo',
fields: { status: { type: 'text' } },
// @ts-expect-error — `workflows` is not an ObjectSchema field
workflows: [],
});
} catch (e) {
message = (e as Error).message;
}
expect(message).toContain('lifecycle hook');
expect(message).toContain('record_change');
expect(message).toContain('#1535');
});
// Tombstones: a RETIRED key's rejection must carry the upgrade
// prescription — the compile/validation error is the one channel every
// upgrading consumer (human or agent) is guaranteed to hit.
it('tombstone: retired compactLayout names its replacement and versions', () => {
let message = '';
try {
ObjectSchema.create({
name: 'demo',
fields: {},
// @ts-expect-error — compactLayout was retired (#2536)
compactLayout: ['name'],
});
} catch (e) {
message = (e as Error).message;
}
expect(message).toContain('highlightFields');
expect(message).toContain('11.7.0');
expect(message).toContain('#2536');
});
it('tombstone: removed detail block routes each job to its semantic role', () => {
let message = '';
try {
ObjectSchema.create({
name: 'demo',
fields: {},
// @ts-expect-error — the detail block was removed (ADR-0085)
detail: { stageField: 'status' },
});
} catch (e) {
message = (e as Error).message;
}
expect(message).toContain('stageField');
expect(message).toContain('highlightFields');
expect(message).toContain('fieldGroups');
expect(message).toContain('ADR-0085');
});
it('tombstone: object-level views dialect points at semantic roles + listViews', () => {
let message = '';
try {
ObjectSchema.create({
name: 'demo',
fields: {},
// @ts-expect-error — object-level views.* was never a spec key
views: { form: { sections: [] } },
});
} catch (e) {
message = (e as Error).message;
}
expect(message).toContain('listViews');
expect(message).toContain('ADR-0085');
});
it('suggests the intended key on a typo (`validation` → `validations`)', () => {
expect(() => ObjectSchema.create({
name: 'demo',
fields: { status: { type: 'text' } },
// @ts-expect-error — typo'd key
validation: [],
})).toThrow(/did you mean `validations`/);
});
it('does not strip — a supported key like `validations` still parses', () => {
const obj = ObjectSchema.create({
name: 'demo',
fields: { status: { type: 'text' } },
validations: [],
});
expect(obj.validations).toEqual([]);
});
});
});
// ============================================================================
// Namespace removal (D4) — Object identity is single-sourced on `name`.
// ============================================================================
describe('ObjectSchema name-as-identity', () => {
it('does not surface a namespace property on parsed objects', () => {
const result = ObjectSchema.safeParse({
name: 'sys_user',
fields: {},
});
expect(result.success).toBe(true);
if (result.success) {
expect((result.data as Record<string, unknown>).namespace).toBeUndefined();
}
});