-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaction.test.ts
More file actions
1183 lines (1049 loc) · 36.5 KB
/
Copy pathaction.test.ts
File metadata and controls
1183 lines (1049 loc) · 36.5 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 { ActionSchema, ActionParamSchema, Action, type Action as ActionType, ACTION_LOCATIONS, ActionLocationSchema, type ActionLocation } from './action.zod';
describe('ActionParamSchema', () => {
it('should accept minimal action parameter', () => {
const param = {
name: 'comment',
label: 'Comment',
type: 'text' as const,
};
const result = ActionParamSchema.parse(param);
expect(result.required).toBe(false);
});
it('should accept required parameter', () => {
const param = {
name: 'reason',
label: 'Reason',
type: 'textarea' as const,
required: true,
};
expect(() => ActionParamSchema.parse(param)).not.toThrow();
});
it('should accept parameter with options', () => {
const param = {
name: 'priority',
label: 'Priority',
type: 'select' as const,
options: [
{ label: 'Low', value: 'low' },
{ label: 'Medium', value: 'medium' },
{ label: 'High', value: 'high' },
],
};
expect(() => ActionParamSchema.parse(param)).not.toThrow();
});
// objectui ADR-0059 — params render through the shared form field-widget
// renderer, so inline params can carry the widget config the form widgets
// consume (field-backed params inherit these from the field at runtime).
it('accepts a file param with upload widget config (multiple/accept/maxSize)', () => {
const result = ActionParamSchema.parse({
name: 'attachments',
label: 'Attachments',
type: 'file' as const,
multiple: true,
accept: ['application/pdf', 'image/*'],
maxSize: 5 * 1024 * 1024,
});
expect(result.multiple).toBe(true);
expect(result.accept).toEqual(['application/pdf', 'image/*']);
expect(result.maxSize).toBe(5 * 1024 * 1024);
});
it('leaves widget config undefined when not declared (runtime inherits from the field)', () => {
const result = ActionParamSchema.parse({ field: 'avatar_file' });
expect(result.multiple).toBeUndefined();
expect(result.accept).toBeUndefined();
expect(result.maxSize).toBeUndefined();
});
it('rejects a non-positive or fractional maxSize', () => {
expect(() => ActionParamSchema.parse({ name: 'f', type: 'file', maxSize: 0 })).toThrow();
expect(() => ActionParamSchema.parse({ name: 'f', type: 'file', maxSize: -1 })).toThrow();
expect(() => ActionParamSchema.parse({ name: 'f', type: 'file', maxSize: 1.5 })).toThrow();
});
it('accepts rich field types for params (rendered by the shared widget renderer)', () => {
for (const type of ['file', 'image', 'richtext', 'color', 'date', 'address', 'code'] as const) {
expect(() => ActionParamSchema.parse({ name: 'p', label: 'P', type })).not.toThrow();
}
});
// #3405 — an inline record-picker param carries its own target object.
// Before this, `reference` was an unknown key: zod stripped it silently and
// the param dialog degraded to a "paste the record id (UUID)" text input,
// with no signal that the authored config had been dropped.
describe('inline lookup reference target (#3405)', () => {
it('keeps `reference` on an inline lookup param', () => {
const result = ActionParamSchema.parse({
name: 'inspector',
label: 'Inspector',
type: 'lookup' as const,
reference: 'sys_user',
required: true,
});
expect(result.reference).toBe('sys_user');
});
it('rejects an inline lookup/master_detail param with no reference target', () => {
for (const type of ['lookup', 'master_detail'] as const) {
const result = ActionParamSchema.safeParse({ name: 'owner', label: 'Owner', type });
expect(result.success).toBe(false);
expect(result.error?.issues[0]?.path).toEqual(['reference']);
}
});
it('allows a field-backed lookup param to omit it (inherited from the field at runtime)', () => {
expect(() => ActionParamSchema.parse({ field: 'inspector', type: 'lookup' as const })).not.toThrow();
expect(() => ActionParamSchema.parse({ field: 'inspector' })).not.toThrow();
});
it('leaves `reference` undefined for non-picker params', () => {
expect(ActionParamSchema.parse({ name: 'note', type: 'textarea' as const }).reference).toBeUndefined();
});
});
});
// #2874 P1 — declarative `requiresFeature` sugar, lowered at parse time into
// the canonical `visible` CEL predicate (semantics per the
// PUBLIC_AUTH_FEATURES registry) and stripped from the output.
describe('requiresFeature lowering', () => {
it('lowers an opt-in flag to == true on a param', () => {
const result = ActionParamSchema.parse({ name: 'phoneNumber', requiresFeature: 'phoneNumber' });
expect(result.visible).toEqual({ dialect: 'cel', source: 'features.phoneNumber == true' });
expect(result).not.toHaveProperty('requiresFeature');
});
it('lowers a default-on flag to != false on an action', () => {
const result = ActionSchema.parse({
name: 'invite_user',
label: 'Invite',
type: 'api',
target: '/api/v1/auth/organization/invite-member',
requiresFeature: 'organization',
});
expect(result.visible).toEqual({ dialect: 'cel', source: 'features.organization != false' });
expect(result).not.toHaveProperty('requiresFeature');
});
it('AND-composes with an explicit string visible — existing first, gate last', () => {
const result = ActionSchema.parse({
name: 'transfer_ownership',
label: 'Transfer Ownership',
type: 'api',
target: '/api/v1/auth/organization/update-member-role',
visible: "record.role != 'owner'",
requiresFeature: 'organization',
});
expect(result.visible).toEqual({
dialect: 'cel',
source: "(record.role != 'owner') && features.organization != false",
});
});
it('AND-composes with an explicit envelope visible, preserving meta', () => {
const result = ActionParamSchema.parse({
name: 'x',
visible: { dialect: 'cel', source: 'record.a == 1', meta: { rationale: 'why' } },
requiresFeature: 'admin',
});
expect(result.visible).toEqual({
dialect: 'cel',
source: '(record.a == 1) && features.admin == true',
meta: { rationale: 'why' },
});
});
it('rejects an unknown flag name at parse time', () => {
expect(() => ActionParamSchema.parse({ name: 'x', requiresFeature: 'phoneNumbr' })).toThrow();
});
it('rejects composition with an AST-only visible loudly (ADR-0078)', () => {
const result = ActionParamSchema.safeParse({
name: 'x',
visible: { dialect: 'cel', ast: { kind: 'literal' } },
requiresFeature: 'admin',
});
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues.some((i) => i.path.includes('requiresFeature'))).toBe(true);
}
});
it('leaves sugar-free inputs untouched', () => {
const result = ActionParamSchema.parse({ name: 'x', visible: 'features.phoneNumber == true' });
expect(result.visible).toEqual({ dialect: 'cel', source: 'features.phoneNumber == true' });
const bare = ActionParamSchema.parse({ name: 'y' });
expect(bare.visible).toBeUndefined();
expect(bare).not.toHaveProperty('requiresFeature');
});
});
describe('ActionSchema', () => {
describe('Basic Action Properties', () => {
it('should accept minimal action', () => {
// A `script` action (the default type) must be bound to something
// runnable — here a `target` naming a registered handler.
const action: ActionType = {
name: 'approve',
label: 'Approve',
target: 'approve_handler',
};
const result = ActionSchema.parse(action);
expect(result.type).toBe('script');
expect(result.refreshAfter).toBe(false);
});
it('should enforce snake_case for action name', () => {
const validNames = ['approve_record', 'send_email', 'close_case'];
validNames.forEach(name => {
expect(() => ActionSchema.parse({ name, label: 'Test', target: 'h' })).not.toThrow();
});
const invalidNames = ['approveRecord', 'Approve-Record', '123action', '_internal'];
invalidNames.forEach(name => {
expect(() => ActionSchema.parse({ name, label: 'Test', target: 'h' })).toThrow();
});
});
it('should accept action with icon', () => {
const action: ActionType = {
name: 'delete_record',
label: 'Delete',
icon: 'trash-2',
target: 'delete_handler',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
});
describe('Action Types', () => {
it('should accept all action types with target', () => {
const types = ['script', 'url', 'modal', 'flow', 'api'] as const;
types.forEach(type => {
const action: ActionType = {
name: 'test_action',
label: 'Test',
type,
target: 'test_handler',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
});
it('should accept a script action bound by inline body (no target)', () => {
expect(() => ActionSchema.parse({
name: 'test_action',
label: 'Test',
type: 'script',
body: { language: 'expression', source: 'true' },
})).not.toThrow();
});
it('should reject a script action with neither body nor target', () => {
// Regression guard for #2169: a body-less, target-less script action
// registers no runtime handler and fails on invocation.
expect(() => ActionSchema.parse({
name: 'test_action',
label: 'Test',
type: 'script',
})).toThrow(/body|target/);
});
it('should reject url/flow/modal/api types without target', () => {
const targetRequiredTypes = ['url', 'flow', 'modal', 'api'] as const;
targetRequiredTypes.forEach(type => {
expect(() => ActionSchema.parse({
name: 'test_action',
label: 'Test',
type,
})).toThrow(/target/);
});
});
it('should default to script type', () => {
const action = {
name: 'custom_action',
label: 'Custom',
target: 'custom_handler',
};
const result = ActionSchema.parse(action);
expect(result.type).toBe('script');
});
});
describe('Action Locations', () => {
it('should accept valid locations', () => {
const locations = [
'list_toolbar',
'list_item',
'record_header',
'record_more',
'record_related',
'global_nav',
] as const;
const action: ActionType = {
name: 'multi_location',
label: 'Multi Location',
target: 'noop',
locations,
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
it('should accept single location', () => {
const action: ActionType = {
name: 'toolbar_action',
label: 'Toolbar Action',
target: 'noop',
locations: ['list_toolbar'],
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
});
describe('objectName', () => {
it('should accept action with objectName', () => {
const action = {
name: 'approve_task',
label: 'Approve Task',
target: 'noop',
objectName: 'task',
};
const result = ActionSchema.parse(action);
expect(result.objectName).toBe('task');
});
it('should accept action without objectName (global action)', () => {
const action = {
name: 'global_search',
label: 'Global Search',
target: 'noop',
};
const result = ActionSchema.parse(action);
expect(result.objectName).toBeUndefined();
});
it('should enforce snake_case for objectName', () => {
expect(() => ActionSchema.parse({
name: 'test_action',
label: 'Test',
objectName: 'myObject',
})).toThrow();
expect(() => ActionSchema.parse({
name: 'test_action',
label: 'Test',
objectName: 'my_object',
target: 'noop',
})).not.toThrow();
});
});
describe('Action Targets', () => {
it('should accept URL action with target', () => {
const action: ActionType = {
name: 'open_external',
label: 'Open External',
type: 'url',
target: 'https://example.com/api',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
it('should accept flow action with target', () => {
const action: ActionType = {
name: 'run_approval_flow',
label: 'Run Approval',
type: 'flow',
target: 'approval_workflow',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
it('should accept API action with target', () => {
const action: ActionType = {
name: 'call_api',
label: 'Call API',
type: 'api',
target: '/api/custom-endpoint',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
});
describe('Action Parameters', () => {
it('should accept action with parameters', () => {
const action: ActionType = {
name: 'transfer_ownership',
label: 'Transfer Ownership',
target: 'noop',
type: 'script',
params: [
{
name: 'new_owner',
label: 'New Owner',
type: 'lookup',
reference: 'sys_user',
required: true,
},
{
name: 'notify',
label: 'Notify Old Owner',
type: 'boolean',
required: false,
},
],
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
it('should accept action with select parameter', () => {
const action: ActionType = {
name: 'change_status',
label: 'Change Status',
target: 'noop',
params: [
{
name: 'status',
label: 'New Status',
type: 'select',
required: true,
options: [
{ label: 'Active', value: 'active' },
{ label: 'Inactive', value: 'inactive' },
],
},
],
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
});
describe('UX Behavior', () => {
it('should accept action with confirmation', () => {
const action: ActionType = {
name: 'delete_all',
label: 'Delete All',
target: 'noop',
confirmText: 'Are you sure you want to delete all records? This cannot be undone.',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
it('should accept action with success message', () => {
const action: ActionType = {
name: 'send_notification',
label: 'Send Notification',
target: 'noop',
successMessage: 'Notification sent successfully!',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
it('should accept action that refreshes view', () => {
const action: ActionType = {
name: 'update_status',
label: 'Update Status',
target: 'noop',
refreshAfter: true,
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
it('should accept action with all UX properties', () => {
const action: ActionType = {
name: 'complete_task',
label: 'Complete Task',
target: 'noop',
confirmText: 'Mark this task as complete?',
successMessage: 'Task completed successfully!',
refreshAfter: true,
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
});
describe('Visibility Control', () => {
it('should accept action with visibility formula', () => {
const action: ActionType = {
name: 'approve',
label: 'Approve',
target: 'approve_handler',
visible: 'status == "pending" && user.can_approve',
};
expect(() => ActionSchema.parse(action)).not.toThrow();
});
});
describe('Real-World Action Examples', () => {
it('should accept approve opportunity action', () => {
const approveAction: ActionType = {
name: 'approve_opportunity',
label: 'Approve',
icon: 'check-circle',
type: 'script',
locations: ['record_header', 'record_more'],
target: 'approveOpportunity',
confirmText: 'Are you sure you want to approve this opportunity?',
successMessage: 'Opportunity approved successfully!',
refreshAfter: true,
visible: 'status == "pending_approval" && user.has_permission("approve_opportunities")',
};
expect(() => ActionSchema.parse(approveAction)).not.toThrow();
});
it('should accept transfer case action with parameters', () => {
const transferAction: ActionType = {
name: 'transfer_case',
label: 'Transfer Case',
icon: 'arrow-right',
type: 'modal',
target: 'transfer_case_modal',
locations: ['record_more'],
params: [
{
name: 'new_owner',
label: 'New Owner',
type: 'lookup',
reference: 'sys_user',
required: true,
},
{
name: 'reason',
label: 'Transfer Reason',
type: 'textarea',
required: false,
},
{
name: 'notify_customer',
label: 'Notify Customer',
type: 'boolean',
required: false,
},
],
successMessage: 'Case transferred successfully!',
refreshAfter: true,
};
expect(() => ActionSchema.parse(transferAction)).not.toThrow();
});
it('should accept send email action', () => {
const emailAction: ActionType = {
name: 'send_quote',
label: 'Send Quote',
icon: 'mail',
type: 'flow',
target: 'send_quote_flow',
locations: ['record_header', 'list_item'],
params: [
{
name: 'recipient',
label: 'Send To',
type: 'email',
required: true,
},
{
name: 'template',
label: 'Email Template',
type: 'select',
required: true,
options: [
{ label: 'Standard Quote', value: 'standard_quote' },
{ label: 'Premium Quote', value: 'premium_quote' },
],
},
],
successMessage: 'Quote sent!',
};
expect(() => ActionSchema.parse(emailAction)).not.toThrow();
});
it('should accept export to Excel action', () => {
const exportAction: ActionType = {
name: 'export_excel',
label: 'Export to Excel',
icon: 'file-spreadsheet',
type: 'api',
target: '/api/export/excel',
locations: ['list_toolbar'],
successMessage: 'Export started. You will receive an email when ready.',
};
expect(() => ActionSchema.parse(exportAction)).not.toThrow();
});
it('should accept delete action with confirmation', () => {
const deleteAction: ActionType = {
name: 'delete_record',
label: 'Delete',
icon: 'trash-2',
type: 'script',
locations: ['record_more'],
target: 'deleteRecord',
confirmText: 'Are you sure you want to delete this record? This action cannot be undone.',
successMessage: 'Record deleted successfully!',
refreshAfter: true,
visible: 'user.has_permission("delete_records")',
};
expect(() => ActionSchema.parse(deleteAction)).not.toThrow();
});
it('should accept clone record action', () => {
const cloneAction: ActionType = {
name: 'clone_record',
label: 'Clone',
icon: 'copy',
type: 'script',
locations: ['record_more', 'list_item'],
target: 'cloneRecord',
params: [
{
name: 'include_children',
label: 'Include Related Records',
type: 'boolean',
required: false,
},
],
successMessage: 'Record cloned successfully!',
refreshAfter: true,
};
expect(() => ActionSchema.parse(cloneAction)).not.toThrow();
});
it('should accept open external link action', () => {
const linkAction: ActionType = {
name: 'view_on_map',
label: 'View on Map',
icon: 'map-pin',
type: 'url',
target: 'https://maps.google.com/?q={address}',
locations: ['record_related'],
visible: 'address != null',
};
expect(() => ActionSchema.parse(linkAction)).not.toThrow();
});
});
});
// ============================================================================
// openIn — declarative new-tab control for static type:'url' targets (#2043)
// ============================================================================
describe('ActionSchema - openIn', () => {
it('should accept openIn: "new-tab" on a static url action', () => {
const result = ActionSchema.parse({
name: 'print_a3',
label: 'Print A3',
type: 'url',
target: '/print/a3?id=${record.id}',
openIn: 'new-tab',
});
expect(result.openIn).toBe('new-tab');
});
it('should accept openIn: "self"', () => {
const result = ActionSchema.parse({
name: 'open_inline',
label: 'Open Inline',
type: 'url',
target: '/somewhere',
openIn: 'self',
});
expect(result.openIn).toBe('self');
});
it('should leave openIn undefined when omitted', () => {
const result = ActionSchema.parse({
name: 'plain_url',
label: 'Plain URL',
type: 'url',
target: 'https://example.com',
});
expect(result.openIn).toBeUndefined();
});
it('should preserve openIn through defineAction (not stripped at build)', () => {
// Regression guard for #2043 counterpart: a plain z.object() stripped
// unknown keys, so openIn never reached objectui. It must survive parse.
const result = ActionSchema.parse({
name: 'print_a3',
label: '打印总表(A3)',
type: 'url',
target: '/print/a3?id=${record.id}',
openIn: 'new-tab',
});
expect(result.openIn).toBe('new-tab');
});
it('should reject an invalid openIn value', () => {
expect(() => ActionSchema.parse({
name: 'bad_openin',
label: 'Bad',
type: 'url',
target: '/x',
openIn: 'newtab',
})).toThrow();
});
});
describe('Action Factory', () => {
it('should create action with default values via factory', () => {
const action = Action.create({
name: 'test_action',
label: 'Test Action',
target: 'noop',
});
expect(action.name).toBe('test_action');
expect(action.label).toBe('Test Action');
expect(action.type).toBe('script');
expect(action.refreshAfter).toBe(false);
});
it('should create action without refreshAfter property (uses default)', () => {
const action = Action.create({
name: 'send_email',
label: 'Send Email',
type: 'flow',
target: 'email_flow',
});
expect(action.refreshAfter).toBe(false);
});
it('should create action with explicit refreshAfter', () => {
const action = Action.create({
name: 'update_record',
label: 'Update',
target: 'noop',
refreshAfter: true,
});
expect(action.refreshAfter).toBe(true);
});
it('should validate snake_case name in factory', () => {
expect(() => Action.create({
name: 'invalidName',
label: 'Invalid',
})).toThrow();
expect(() => Action.create({
name: 'valid_name',
label: 'Valid',
target: 'noop',
})).not.toThrow();
});
});
describe('Action I18n Integration', () => {
it('should reject i18n object as action label', () => {
expect(() => ActionSchema.parse({
name: 'i18n_action',
label: { key: 'actions.approve', defaultValue: 'Approve' },
})).toThrow();
});
it('should reject i18n as confirmText and successMessage', () => {
expect(() => ActionSchema.parse({
name: 'i18n_confirm',
label: 'Delete',
confirmText: { key: 'actions.confirm_delete', defaultValue: 'Are you sure?' },
successMessage: { key: 'actions.delete_success', defaultValue: 'Deleted!' },
})).toThrow();
});
it('should reject i18n in param labels', () => {
expect(() => ActionParamSchema.parse({
name: 'reason',
label: { key: 'params.reason', defaultValue: 'Reason' },
type: 'textarea',
})).toThrow();
});
it('should reject i18n in param option labels', () => {
expect(() => ActionParamSchema.parse({
name: 'priority',
label: 'Priority',
type: 'select',
options: [
{ label: { key: 'options.high', defaultValue: 'High' }, value: 'high' },
{ label: { key: 'options.low', defaultValue: 'Low' }, value: 'low' },
],
})).toThrow();
});
});
// ============================================================================
// ADR-0011: AI exposure block (opt-in)
// ============================================================================
describe('ActionSchema - ai block (ADR-0011)', () => {
const longDescription =
'Classify a support case and suggest a priority, category, and queue for the agent.';
it('defaults ai.exposed to false when an ai block is supplied without it', () => {
const result = ActionSchema.parse({
name: 'maybe_expose',
label: 'Maybe',
target: 'noop',
ai: {},
});
expect(result.ai?.exposed).toBe(false);
});
it('accepts an action with no ai block (not exposed)', () => {
const result = ActionSchema.parse({ name: 'plain', label: 'Plain', target: 'noop' });
expect(result.ai).toBeUndefined();
});
it('requires a description when exposed is true', () => {
expect(() =>
ActionSchema.parse({
name: 'expose_no_desc',
label: 'Expose',
ai: { exposed: true },
}),
).toThrow(/ai\.description/);
});
it('rejects a description shorter than 40 chars', () => {
expect(() =>
ActionSchema.parse({
name: 'expose_short',
label: 'Expose',
ai: { exposed: true, description: 'too short' },
}),
).toThrow();
});
it('accepts a fully-specified ai block', () => {
const result = ActionSchema.parse({
name: 'triage_case',
label: 'Triage Case',
target: 'noop',
objectName: 'crm_case',
params: [{ name: 'priority', type: 'text' }],
ai: {
exposed: true,
description: longDescription,
category: 'analytics',
paramHints: { priority: { description: 'P0-P3', enum: ['P0', 'P1', 'P2', 'P3'] } },
outputSchema: { type: 'object', properties: { priority: { type: 'string' } } },
requiresConfirmation: false,
},
});
expect(result.ai?.exposed).toBe(true);
expect(result.ai?.category).toBe('analytics');
expect(result.ai?.requiresConfirmation).toBe(false);
});
it('rejects an invalid ai.category value', () => {
expect(() =>
ActionSchema.parse({
name: 'bad_category',
label: 'Bad',
ai: { exposed: true, description: longDescription, category: 'not_a_category' },
}),
).toThrow();
});
it('rejects paramHints keys that do not match a declared param', () => {
expect(() =>
ActionSchema.parse({
name: 'bad_hint',
label: 'Bad Hint',
params: [{ name: 'priority', type: 'text' }],
ai: { exposed: true, description: longDescription, paramHints: { nonexistent: { description: 'x' } } },
}),
).toThrow(/paramHints/);
});
it('allows paramHints to reference the injected recordId', () => {
expect(() =>
ActionSchema.parse({
name: 'hint_record_id',
label: 'Hint',
target: 'noop',
objectName: 'task',
locations: ['record_header'],
ai: { exposed: true, description: longDescription, paramHints: { recordId: { description: 'The task id.' } } },
}),
).not.toThrow();
});
it('does not require a description when exposed is false', () => {
expect(() =>
ActionSchema.parse({ name: 'opted_out', label: 'Out', target: 'noop', ai: { exposed: false } }),
).not.toThrow();
});
});
describe('Action ARIA Integration', () => {
it('should accept action with ARIA attributes', () => {
expect(() => ActionSchema.parse({
name: 'accessible_action',
label: 'Delete',
target: 'noop',
aria: { ariaLabel: 'Delete this record permanently', role: 'button' },
})).not.toThrow();
});
});
// ============================================================================
// Protocol Improvement Tests: Action variant
// ============================================================================
describe('ActionSchema - variant', () => {
it('should accept all valid variants', () => {
const variants = ['primary', 'secondary', 'danger', 'ghost', 'link'] as const;
for (const variant of variants) {
const result = ActionSchema.parse({
name: 'test_action',
label: 'Test',
target: 'noop',
variant,
});
expect(result.variant).toBe(variant);
}
});
it('should accept action without variant (optional)', () => {
const result = ActionSchema.parse({
name: 'no_variant',
label: 'Action',
target: 'noop',
});
expect(result.variant).toBeUndefined();
});
it('should reject invalid variant value', () => {
expect(() => ActionSchema.parse({
name: 'bad_variant',
label: 'Action',
variant: 'invalid',
})).toThrow();
});
it('should combine variant with other action properties', () => {
const result = ActionSchema.parse({
name: 'delete_record',
label: 'Delete',
target: 'delete_handler',
variant: 'danger',
confirmText: 'Are you sure?',
icon: 'trash',
});
expect(result.variant).toBe('danger');
expect(result.confirmText).toBe('Are you sure?');
});
});
describe('ActionSchema - order', () => {
it('should accept a numeric order (incl. negative and zero)', () => {
for (const order of [-100, -1, 0, 5, 999]) {
const result = ActionSchema.parse({
name: 'ordered_action',
label: 'Ordered',
target: 'noop',
order,
});
expect(result.order).toBe(order);
}
});
it('should accept action without order (optional)', () => {
const result = ActionSchema.parse({
name: 'no_order',
label: 'Action',
target: 'noop',
});
expect(result.order).toBeUndefined();
});
it('should reject a non-numeric order', () => {
expect(() => ActionSchema.parse({
name: 'bad_order',
label: 'Action',
target: 'noop',
order: 'first',
})).toThrow();
});