-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapproval-service.test.ts
More file actions
1490 lines (1319 loc) · 71.9 KB
/
Copy pathapproval-service.test.ts
File metadata and controls
1490 lines (1319 loc) · 71.9 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Node-era approval service tests (ADR-0019).
*
* Approval is a flow node — there is no standalone process engine. These tests
* exercise the service directly: opening a node-driven request, recording
* decisions (first_response / unanimous), the public `decide()` resume bridge,
* the read API, and the global record-lock hook.
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { ApprovalService, REMIND_COOLDOWN_MS } from './approval-service.js';
import { bindApprovalLockHook, bindDelegationWriteGuard, unbindAllHooks } from './lifecycle-hooks.js';
interface FakeRow { [k: string]: any }
function makeFakeEngine() {
const tables: Record<string, FakeRow[]> = {};
const ensure = (n: string) => (tables[n] ??= []);
const hooks: Record<string, Array<{ handler: (ctx: any) => any | Promise<any>; object?: string | string[]; packageId?: string }>> = {};
function matches(row: FakeRow, filter: any): boolean {
if (!filter || typeof filter !== 'object') return true;
for (const [k, v] of Object.entries(filter)) {
if (k === '$or') {
if (!(v as any[]).some(sub => matches(row, sub))) return false;
continue;
}
const rv = row[k];
if (v != null && typeof v === 'object' && '$in' in (v as any)) {
if (!(v as any).$in.includes(rv)) return false;
continue;
}
if (v != null && typeof v === 'object' && '$ne' in (v as any)) {
if (rv === (v as any).$ne) return false;
continue;
}
if (v != null && typeof v === 'object' && '$contains' in (v as any)) {
if (!String(rv ?? '').includes(String((v as any).$contains))) return false;
continue;
}
if (rv !== v) return false;
}
return true;
}
return {
_tables: tables,
_hooks: hooks,
async find(object: string, options?: any) {
const rows = ensure(object).filter(r => matches(r, options?.filter ?? options?.where));
if (options?.orderBy?.[0]) {
// Canonical SortNode key only (spec/data/query.zod.ts): a sloppy
// `direction:` key must fall through to the schema default (asc),
// exactly like the real engine — that's how the remind() cool-down
// regression stayed invisible when this mock honored both keys.
const { field, order } = options.orderBy[0];
rows.sort((a, b) => {
const av = a[field]; const bv = b[field];
if (av === bv) return 0;
const cmp = av > bv ? 1 : -1;
return order === 'desc' ? -cmp : cmp;
});
}
const start = options?.offset ?? 0;
return rows.slice(start, start + (options?.limit ?? 1000));
},
async insert(object: string, data: any) {
ensure(object).push({ ...data });
return { ...data };
},
async update(object: string, idOrData: any, _opts?: any) {
const data = typeof idOrData === 'object' ? idOrData : _opts;
const id = typeof idOrData === 'object' ? idOrData.id : idOrData;
const table = ensure(object);
const i = table.findIndex(r => r.id === id);
if (i >= 0) table[i] = { ...table[i], ...data };
return table[i];
},
async delete(object: string, options?: any) {
const table = ensure(object);
const id = options?.where?.id ?? options?.id;
const i = table.findIndex(r => r.id === id);
if (i >= 0) table.splice(i, 1);
return { id };
},
// ── hook surface (for the record-lock hook) ──
registerHook(event: string, handler: (ctx: any) => any, options?: any) {
(hooks[event] ??= []).push({ handler, object: options?.object, packageId: options?.packageId });
},
unregisterHooksByPackage(packageId: string): number {
let n = 0;
for (const ev of Object.keys(hooks)) {
const before = hooks[ev].length;
hooks[ev] = hooks[ev].filter(h => h.packageId !== packageId);
n += before - hooks[ev].length;
}
return n;
},
async fire(event: string, ctx: any) {
for (const h of hooks[event] ?? []) {
if (h.object) {
const objs = Array.isArray(h.object) ? h.object : [h.object];
if (!objs.includes(ctx.object)) continue;
}
await h.handler(ctx);
}
},
};
}
const CTX = { userId: 'u1', tenantId: 't1', positions: [], permissions: [] } as any;
const SYS = { isSystem: true, positions: [], permissions: [] } as any;
function nodeConfig(approvers: string[], extra: Record<string, any> = {}) {
return {
approvers: approvers.map(v => ({ type: 'user' as const, value: v })),
behavior: 'first_response' as const,
lockRecord: true,
...extra,
};
}
function openInput(approvers: string[], extra: Record<string, any> = {}, configExtra: Record<string, any> = {}) {
return {
object: 'opportunity',
recordId: 'opp1',
runId: 'run_1',
nodeId: 'approve_step',
flowName: 'deal_approval',
config: nodeConfig(approvers, configExtra),
record: { id: 'opp1', amount: 100 },
...extra,
};
}
describe('ApprovalService (node era)', () => {
let engine: ReturnType<typeof makeFakeEngine>;
let svc: ApprovalService;
let n = 0;
const baseTime = new Date('2026-01-15T10:00:00Z').getTime();
beforeEach(() => {
engine = makeFakeEngine();
n = 0;
svc = new ApprovalService({
engine: engine as any,
clock: { now: () => new Date(baseTime + (n++) * 1000) },
});
});
// ── openNodeRequest ─────────────────────────────────────────────
it('openNodeRequest: creates a pending request + submit action with flow correlation', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
expect(req.status).toBe('pending');
expect(req.process_name).toBe('flow:deal_approval');
expect(req.flow_run_id).toBe('run_1');
expect(req.flow_node_id).toBe('approve_step');
expect(req.pending_approvers).toEqual(['u9']);
expect(engine._tables['sys_approval_request']).toHaveLength(1);
expect(engine._tables['sys_approval_action'][0].action).toBe('submit');
});
it('openNodeRequest: snapshots the node config on the row', async () => {
await svc.openNodeRequest(openInput(['u9']), CTX);
const raw = engine._tables['sys_approval_request'][0];
expect(JSON.parse(raw.node_config_json)).toMatchObject({ behavior: 'first_response', lockRecord: true });
});
it('openNodeRequest: deduplicates a pending request per (object, record)', async () => {
await svc.openNodeRequest(openInput(['u9']), CTX);
await expect(svc.openNodeRequest(openInput(['u9'], { runId: 'run_2' }), CTX))
.rejects.toThrow(/DUPLICATE_REQUEST/);
});
it('openNodeRequest: requires object, recordId, runId', async () => {
await expect(svc.openNodeRequest(openInput(['u9'], { object: '' }), CTX)).rejects.toThrow(/VALIDATION_FAILED/);
await expect(svc.openNodeRequest(openInput(['u9'], { recordId: '' }), CTX)).rejects.toThrow(/VALIDATION_FAILED/);
await expect(svc.openNodeRequest(openInput(['u9'], { runId: '' }), CTX)).rejects.toThrow(/VALIDATION_FAILED/);
});
it('openNodeRequest: mirrors status onto the business record when configured', async () => {
engine._tables['opportunity'] = [{ id: 'opp1', amount: 100 }];
await svc.openNodeRequest(openInput(['u9'], {}, { approvalStatusField: 'approval_status' }), CTX);
expect(engine._tables['opportunity'][0].approval_status).toBe('pending');
});
// ── approver expansion: position (ADR-0090 D3) ──────────────────
const positionInput = (extra: Record<string, any> = {}) => ({
...openInput([]),
config: {
approvers: [{ type: 'position' as const, value: 'sales_manager' }],
behavior: 'first_response' as const,
lockRecord: true,
},
...extra,
});
it('position approver: expands via sys_user_position, org-scoped', async () => {
engine._tables['sys_user_position'] = [
{ id: 'up1', user_id: 'u5', position: 'sales_manager', organization_id: 't1' },
{ id: 'up2', user_id: 'u6', position: 'sales_manager', organization_id: 't1' },
{ id: 'up3', user_id: 'u7', position: 'sales_manager', organization_id: 't2' }, // other tenant
{ id: 'up4', user_id: 'u8', position: 'cfo', organization_id: 't1' }, // other position
];
const req = await svc.openNodeRequest(positionInput(), CTX);
expect(req.pending_approvers.sort()).toEqual(['u5', 'u6']);
});
it('position approver: unions the sys_member.role transition source (ADR-0057 D4)', async () => {
engine._tables['sys_user_position'] = [
{ id: 'up1', user_id: 'u5', position: 'sales_manager', organization_id: 't1' },
];
engine._tables['sys_member'] = [
{ id: 'm1', user_id: 'u6', role: 'sales_manager', organization_id: 't1' },
{ id: 'm2', user_id: 'u5', role: 'sales_manager', organization_id: 't1' }, // deduped
];
const req = await svc.openNodeRequest(positionInput(), CTX);
expect(req.pending_approvers.sort()).toEqual(['u5', 'u6']);
});
it('position approver: falls back to a position: literal when nobody holds it', async () => {
const req = await svc.openNodeRequest(positionInput(), CTX);
expect(req.pending_approvers).toEqual(['position:sales_manager']);
});
// ── approver expansion: org_membership_level + its deprecated `role` alias
// (ADR-0090 D3) ────────────────────────────────────────────────────────
// `recordId` is parameterised: the service rejects a second pending request
// on the same record, and the alias test deliberately opens two.
const tierInput = (type: 'org_membership_level' | 'role', recordId = 'opp1') => ({
...openInput([]),
recordId,
record: { id: recordId, amount: 100 },
config: {
approvers: [{ type: type as any, value: 'admin' }],
behavior: 'first_response' as const,
lockRecord: true,
},
});
it('org_membership_level approver: expands the better-auth tier, org-scoped', async () => {
engine._tables['sys_member'] = [
{ id: 'm1', user_id: 'u1', role: 'admin', organization_id: 't1' },
{ id: 'm2', user_id: 'u2', role: 'admin', organization_id: 't1' },
{ id: 'm3', user_id: 'u3', role: 'admin', organization_id: 't2' }, // other tenant
{ id: 'm4', user_id: 'u4', role: 'member', organization_id: 't1' }, // other tier
];
const req = await svc.openNodeRequest(tierInput('org_membership_level'), CTX);
expect(req.pending_approvers.sort()).toEqual(['u1', 'u2']);
});
it('deprecated `role` alias resolves IDENTICALLY to org_membership_level', async () => {
engine._tables['sys_member'] = [
{ id: 'm1', user_id: 'u1', role: 'admin', organization_id: 't1' },
{ id: 'm2', user_id: 'u4', role: 'member', organization_id: 't1' },
];
const canonical = await svc.openNodeRequest(tierInput('org_membership_level', 'opp_canon'), CTX);
const deprecated = await svc.openNodeRequest(tierInput('role', 'opp_depr'), CTX);
expect(deprecated.pending_approvers).toEqual(canonical.pending_approvers);
expect(deprecated.pending_approvers).toEqual(['u1']);
});
// The fallback literal keeps the AUTHORED spelling: `sys_approval_approver`
// rows and `pending_approvers` slots written by 15.x carry `role:<v>`, and
// canonicalising the literal here would orphan every one of them.
it('deprecated `role` alias keeps its legacy literal on fallback (no orphaned slots)', async () => {
const req = await svc.openNodeRequest(tierInput('role'), CTX);
expect(req.pending_approvers).toEqual(['role:admin']);
});
it('org_membership_level falls back to its own canonical literal', async () => {
const req = await svc.openNodeRequest(tierInput('org_membership_level'), CTX);
expect(req.pending_approvers).toEqual(['org_membership_level:admin']);
});
it("department approver: honors the spec enum value 'department' (not just the business_unit dialect)", async () => {
engine._tables['sys_business_unit'] = [
{ id: 'bu1', organization_id: 't1', active: true },
{ id: 'bu2', parent_business_unit_id: 'bu1', organization_id: 't1', active: true },
];
engine._tables['sys_business_unit_member'] = [
{ id: 'bm1', business_unit_id: 'bu1', user_id: 'u5' },
{ id: 'bm2', business_unit_id: 'bu2', user_id: 'u6' },
];
const req = await svc.openNodeRequest(positionInput({
config: {
approvers: [{ type: 'department' as const, value: 'bu1' }],
behavior: 'first_response' as const,
lockRecord: true,
},
}), CTX);
expect(req.pending_approvers.sort()).toEqual(['u5', 'u6']);
});
// ── decideNode ──────────────────────────────────────────────────
it('decideNode: first_response approve finalizes immediately', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS);
expect(out.finalized).toBe(true);
expect(out.decision).toBe('approve');
expect(out.runId).toBe('run_1');
expect(out.nodeId).toBe('approve_step');
expect(out.request.status).toBe('approved');
});
it('decideNode: reject finalizes as rejected', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.decideNode(req.id, { decision: 'reject', actorId: 'u9', comment: 'no' }, SYS);
expect(out.finalized).toBe(true);
expect(out.request.status).toBe('rejected');
});
it('decideNode: unanimous holds until every approver acts', async () => {
const req = await svc.openNodeRequest(openInput(['u1', 'u2'], {}, { behavior: 'unanimous' }), CTX);
const first = await svc.decideNode(req.id, { decision: 'approve', actorId: 'u1' }, SYS);
expect(first.finalized).toBe(false);
expect(first.request.pending_approvers).toEqual(['u2']);
const second = await svc.decideNode(req.id, { decision: 'approve', actorId: 'u2' }, SYS);
expect(second.finalized).toBe(true);
expect(second.request.status).toBe('approved');
});
it('decideNode: blocks a non-approver in a non-system context', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await expect(
svc.decideNode(req.id, { decision: 'approve', actorId: 'mallory' }, { isSystem: false, positions: [], permissions: [] } as any),
).rejects.toThrow(/FORBIDDEN/);
});
it('decideNode: rejects a decision on a non-pending request', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS);
await expect(svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS)).rejects.toThrow(/INVALID_STATE/);
});
it('decideNode: mirrors the terminal status onto the business record', async () => {
engine._tables['opportunity'] = [{ id: 'opp1', amount: 100 }];
const req = await svc.openNodeRequest(openInput(['u9'], {}, { approvalStatusField: 'approval_status' }), CTX);
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS);
expect(engine._tables['opportunity'][0].approval_status).toBe('approved');
});
// ── decide(): public contract + resume bridge ───────────────────
it('decide: resumes the owning run down the matching branch on finalize', async () => {
const resumed: any[] = [];
svc.attachAutomation({ async resume(runId, signal) { resumed.push({ runId, signal }); } });
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.decide(req.id, { decision: 'approve', actorId: 'u9' }, SYS);
expect(out.finalized).toBe(true);
expect(out.resumed).toBe(true);
expect(out.runId).toBe('run_1');
expect(resumed).toHaveLength(1);
expect(resumed[0]).toMatchObject({ runId: 'run_1', signal: { branchLabel: 'approve' } });
});
it('decide: does not resume while a unanimous request is still pending', async () => {
const resumed: any[] = [];
svc.attachAutomation({ async resume(runId) { resumed.push(runId); } });
const req = await svc.openNodeRequest(openInput(['u1', 'u2'], {}, { behavior: 'unanimous' }), CTX);
const out = await svc.decide(req.id, { decision: 'approve', actorId: 'u1' }, SYS);
expect(out.finalized).toBe(false);
expect(out.resumed).toBe(false);
expect(resumed).toHaveLength(0);
});
it('decide: finalizes even when no automation is attached (resumed=false)', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.decide(req.id, { decision: 'reject', actorId: 'u9' }, SYS);
expect(out.finalized).toBe(true);
expect(out.resumed).toBe(false);
});
// ── read API ────────────────────────────────────────────────────
it('listRequests: filters by approver and status', async () => {
await svc.openNodeRequest(openInput(['u9']), CTX);
const pending = await svc.listRequests({ status: 'pending', approverId: 'u9' }, SYS);
expect(pending).toHaveLength(1);
const none = await svc.listRequests({ approverId: 'nobody' }, SYS);
expect(none).toHaveLength(0);
});
it('listRequests: approverId accepts a list and matches ANY identity', async () => {
await svc.openNodeRequest(openInput(['u9']), CTX);
// None of these identities individually except the last is the approver.
const hit = await svc.listRequests(
{ status: 'pending', approverId: ['someone-else', 'user@example.com', 'u9'] },
SYS,
);
expect(hit).toHaveLength(1);
// A list with no matching identity returns nothing.
const miss = await svc.listRequests({ approverId: ['a', 'b', 'role:viewer'] }, SYS);
expect(miss).toHaveLength(0);
// Empty / whitespace-only ids are ignored, not treated as a match-all.
const ignored = await svc.listRequests({ approverId: ['', ' '] }, SYS);
expect(ignored).toHaveLength(1);
});
it('listActions: returns the audit trail for a request', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS);
const actions = await svc.listActions(req.id, SYS);
expect(actions.map(a => a.action)).toEqual(['submit', 'approve']);
});
it('getRequest: returns null for an unknown id', async () => {
expect(await svc.getRequest('nope', SYS)).toBeNull();
});
// ── viewer capability (#3310) ───────────────────────────────────
it('getRequest: viewer.can_act is true for a pending approver, false for the submitter', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX); // submitter u1, approver u9
const asApprover = await svc.getRequest(req.id, { userId: 'u9', tenantId: 't1' } as any);
expect(asApprover!.viewer).toEqual({ can_act: true, is_submitter: false });
const asSubmitter = await svc.getRequest(req.id, { userId: 'u1', tenantId: 't1' } as any);
expect(asSubmitter!.viewer).toEqual({ can_act: false, is_submitter: true });
const asOther = await svc.getRequest(req.id, { userId: 'u_stranger', tenantId: 't1' } as any);
expect(asOther!.viewer).toEqual({ can_act: false, is_submitter: false });
});
it('getRequest: viewer.can_act drops to false once the request is finalized', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS); // → approved
const after = await svc.getRequest(req.id, { userId: 'u9', tenantId: 't1' } as any);
expect(after!.status).toBe('approved');
expect(after!.viewer!.can_act).toBe(false);
});
it('listRequests: attaches viewer to every row from the caller context', async () => {
await svc.openNodeRequest(openInput(['u9']), CTX);
const rows = await svc.listRequests({ status: 'pending' }, { userId: 'u9', tenantId: 't1' } as any);
expect(rows.length).toBeGreaterThan(0);
expect(rows.every(r => r.viewer != null)).toBe(true);
expect(rows[0].viewer!.can_act).toBe(true);
});
// ── recall ──────────────────────────────────────────────────────
it('recall: submitter withdraws a pending request', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.recall(req.id, { actorId: 'u1', comment: 'changed my mind' }, CTX);
expect(out.request.status).toBe('recalled');
expect(out.request.completed_at).toBeTruthy();
expect(out.request.pending_approvers).toEqual([]);
const actions = await svc.listActions(req.id, SYS);
expect(actions.map(a => a.action)).toEqual(['submit', 'recall']);
expect(actions[1].comment).toBe('changed my mind');
});
it('recall: blocks a non-submitter in a non-system context', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await expect(svc.recall(req.id, { actorId: 'u9' }, { positions: [], permissions: [] } as any))
.rejects.toThrow(/FORBIDDEN/);
});
it('recall: rejects a recall on a non-pending request', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS);
await expect(svc.recall(req.id, { actorId: 'u1' }, SYS)).rejects.toThrow(/INVALID_STATE/);
});
it('recall: resumes the owning run down the reject branch with decision=recall', async () => {
const resumed: any[] = [];
svc.attachAutomation({ async resume(runId, signal) { resumed.push({ runId, signal }); } });
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.recall(req.id, { actorId: 'u1' }, CTX);
expect(out.resumed).toBe(true);
expect(resumed[0]).toMatchObject({
runId: 'run_1',
signal: { branchLabel: 'reject', output: { decision: 'recall' } },
});
});
it('recall: mirrors `recalled` onto the business record when configured', async () => {
engine._tables['opportunity'] = [{ id: 'opp1', amount: 100 }];
const req = await svc.openNodeRequest(openInput(['u9'], {}, { approvalStatusField: 'approval_status' }), CTX);
await svc.recall(req.id, { actorId: 'u1' }, CTX);
expect(engine._tables['opportunity'][0].approval_status).toBe('recalled');
});
// ── inbox display fields ────────────────────────────────────────
it('rows expose submitted_at as an alias of created_at', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
expect(req.submitted_at).toBeTruthy();
expect(req.submitted_at).toBe(req.created_at);
const listed = await svc.listRequests({ status: 'pending' }, SYS);
expect(listed[0].submitted_at).toBe(listed[0].created_at);
});
it('rows carry authored flow/node labels when provided', async () => {
const req = await svc.openNodeRequest(
openInput(['u9'], { flowLabel: 'Deal Approval', nodeLabel: 'Manager Review' }), CTX,
);
expect(req.process_label).toBe('Deal Approval');
expect(req.step_label).toBe('Manager Review');
});
it('rows fall back to prettified machine names when labels are absent', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
expect(req.process_label).toBe('Deal Approval'); // from `flow:deal_approval`
expect(req.step_label).toBe('Approve Step'); // from `approve_step`
});
it('listRequests enriches record_title and submitter_name', async () => {
engine._tables['opportunity'] = [{ id: 'opp1', name: 'Acme Renewal', amount: 100 }];
engine._tables['sys_user'] = [{ id: 'u1', name: 'Ada Lovelace', email: 'ada@example.com' }];
await svc.openNodeRequest(openInput(['u9']), CTX); // submitter_id = u1 (CTX.userId)
const rows = await svc.listRequests({ status: 'pending' }, SYS);
expect(rows[0].record_title).toBe('Acme Renewal');
expect(rows[0].submitter_name).toBe('Ada Lovelace');
});
it('enrichment falls back to the payload snapshot when the record is gone', async () => {
await svc.openNodeRequest(
openInput(['u9'], { record: { id: 'opp1', name: 'Snapshot Title', amount: 1 } }), CTX,
);
const rows = await svc.listRequests({ status: 'pending' }, SYS);
expect(rows[0].record_title).toBe('Snapshot Title');
});
it('enrichment resolves lookup foreign keys in the payload to record titles', async () => {
(engine as any).getSchema = (name: string) =>
name === 'opportunity'
? { label: 'Opportunity', fields: { name: {}, account: { type: 'lookup', reference: 'account' } } }
: name === 'account' ? { label: 'Account', fields: { name: {} } } : undefined;
engine._tables['opportunity'] = [{ id: 'opp1', name: 'Acme Renewal', account: 'acc1' }];
engine._tables['account'] = [{ id: 'acc1', name: 'Acme Corp' }];
await svc.openNodeRequest(openInput(['u9'], { record: { id: 'opp1', name: 'Acme Renewal', account: 'acc1' } }), CTX);
const rows = await svc.listRequests({ status: 'pending' }, SYS);
expect(rows[0].object_label).toBe('Opportunity');
expect(rows[0].payload_display).toEqual({ account: 'Acme Corp' });
});
it('enrichment maps user-id approvers to display names', async () => {
engine._tables['sys_user'] = [{ id: 'u9', name: 'Grace Hopper', email: 'grace@example.com' }];
await svc.openNodeRequest(openInput(['u9']), CTX);
const rows = await svc.listRequests({ status: 'pending' }, SYS);
expect(rows[0].pending_approver_names).toEqual({ u9: 'Grace Hopper' });
});
it('listActions resolves actor display names', async () => {
engine._tables['sys_user'] = [
{ id: 'u1', name: 'Ada Lovelace', email: 'ada@example.com' },
{ id: 'u9', name: 'Grace Hopper', email: 'grace@example.com' },
];
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u9' }, SYS);
const actions = await svc.listActions(req.id, SYS);
expect(actions.map(a => (a as any).actor_name)).toEqual(['Ada Lovelace', 'Grace Hopper']);
});
// ── thread interactions ─────────────────────────────────────────
it('reassign: hands the slot to a new approver and audits the move', async () => {
const req = await svc.openNodeRequest(openInput(['u9', 'u2']), CTX);
const out = await svc.reassign(req.id, { actorId: 'u9', to: 'u7' }, CTX);
expect(out.request.pending_approvers).toEqual(['u7', 'u2']);
const actions = await svc.listActions(req.id, SYS);
expect(actions.at(-1)).toMatchObject({ action: 'reassign', actor_id: 'u9', comment: 'u9 → u7' });
});
it('reassign: notifies the new approver via messaging', async () => {
const emitted: any[] = [];
svc.attachMessaging({ async emit(input) { emitted.push(input); } });
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.reassign(req.id, { actorId: 'u9', to: 'u7' }, CTX);
expect(emitted).toHaveLength(1);
expect(emitted[0]).toMatchObject({ topic: 'approval.reassigned', audience: ['u7'] });
});
it('reassign: blocks a non-holder and duplicate targets', async () => {
const req = await svc.openNodeRequest(openInput(['u9', 'u2']), CTX);
await expect(svc.reassign(req.id, { actorId: 'intruder', to: 'u7' }, CTX)).rejects.toThrow(/FORBIDDEN/);
await expect(svc.reassign(req.id, { actorId: 'u9', to: 'u2' }, CTX)).rejects.toThrow(/VALIDATION_FAILED/);
});
it('remind: notifies pending approvers, audits, and throttles repeats', async () => {
const emitted: any[] = [];
svc.attachMessaging({ async emit(input) { emitted.push(input); } });
const req = await svc.openNodeRequest(openInput(['u9', 'u2']), CTX);
const out = await svc.remind(req.id, { actorId: 'u1' }, CTX); // u1 = submitter (CTX.userId)
expect(out.notified).toBe(2);
// ADR-0043: per-approver fan-out so each reminder carries personal links.
const reminders = emitted.filter(e => e.topic === 'approval.reminder');
expect(reminders.map(r => r.audience)).toEqual([['u9'], ['u2']]);
const actions = await svc.listActions(req.id, SYS);
expect(actions.at(-1)?.action).toBe('remind');
// The fake clock steps 1s per call — well inside the 4h cool-down.
await expect(svc.remind(req.id, { actorId: 'u1' }, CTX)).rejects.toThrow(/THROTTLED/);
});
it('remind: cool-down measures from the NEWEST reminder, not the first', async () => {
// Regression: the throttle query sorted with the non-canonical
// `direction: 'desc'` key, which SortNode strips — so it sorted asc and
// compared against the FIRST reminder ever sent. Once 4h passed after
// reminder #1, every later remind() sailed through unthrottled.
let nowMs = baseTime;
const localSvc = new ApprovalService({
engine: engine as any,
clock: { now: () => new Date(nowMs += 1000) },
});
const req = await localSvc.openNodeRequest(openInput(['u9']), CTX);
await localSvc.remind(req.id, { actorId: 'u1' }, CTX);
// Jump past the cool-down: a second reminder is legitimately allowed.
nowMs += REMIND_COOLDOWN_MS;
await localSvc.remind(req.id, { actorId: 'u1' }, CTX);
// Immediately after reminder #2 the throttle must bite again — with the
// wrong sort key it compared against reminder #1 (now >4h old) and let
// unlimited reminders through.
await expect(localSvc.remind(req.id, { actorId: 'u1' }, CTX)).rejects.toThrow(/THROTTLED/);
});
it('remind: only the submitter may nudge', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await expect(svc.remind(req.id, { actorId: 'u9' }, { positions: [], permissions: [] } as any))
.rejects.toThrow(/FORBIDDEN/);
});
it('requestInfo: keeps the request pending and notifies the submitter', async () => {
const emitted: any[] = [];
svc.attachMessaging({ async emit(input) { emitted.push(input); } });
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.requestInfo(req.id, { actorId: 'u9', comment: 'Need the Q3 numbers' }, CTX);
expect(out.request.status).toBe('pending');
expect(out.request.pending_approvers).toEqual(['u9']);
expect(emitted[0]).toMatchObject({ topic: 'approval.request_info', audience: ['u1'] });
const actions = await svc.listActions(req.id, SYS);
expect(actions.at(-1)).toMatchObject({ action: 'request_info', comment: 'Need the Q3 numbers' });
});
it('comment: submitter and approver may reply; outsiders may not', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.comment(req.id, { actorId: 'u1', comment: 'Numbers attached.' }, CTX);
await svc.comment(req.id, { actorId: 'u9', comment: 'Thanks, reviewing.' }, CTX);
await expect(svc.comment(req.id, { actorId: 'outsider', comment: 'hi' }, { positions: [], permissions: [] } as any))
.rejects.toThrow(/FORBIDDEN/);
const actions = await svc.listActions(req.id, SYS);
expect(actions.filter(a => a.action === 'comment')).toHaveLength(2);
});
// ── actionable links (ADR-0043) ─────────────────────────────────
it('issueActionTokens: stores hashes only and binds approver + action', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const tokens = await svc.issueActionTokens(req.id, 'u9');
expect(tokens.approve).not.toBe(tokens.reject);
const rows = engine._tables['sys_approval_token'];
expect(rows).toHaveLength(2);
expect(rows.every(r => r.token_hash.length === 64)).toBe(true); // sha256 hex, never the raw token
expect(rows.every(r => !JSON.stringify(r).includes(tokens.approve))).toBe(true);
await expect(svc.issueActionTokens(req.id, 'stranger')).rejects.toThrow(/FORBIDDEN/);
});
it('redeem: approves as the bound approver and burns the token (single-use)', async () => {
const resumed: any[] = [];
svc.attachAutomation({ async resume(runId, signal) { resumed.push({ runId, signal }); } });
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const { approve } = await svc.issueActionTokens(req.id, 'u9');
const out = await svc.redeemActionToken(approve);
expect(out).toMatchObject({ ok: true, action: 'approve', approverId: 'u9' });
expect((out as any).request.status).toBe('approved');
expect(resumed[0]?.signal?.branchLabel).toBe('approve');
const acts = await svc.listActions(req.id, SYS);
expect(acts.at(-1)).toMatchObject({ action: 'approve', actor_id: 'u9', comment: 'Via action link' });
// replay
expect(await svc.redeemActionToken(approve)).toMatchObject({ ok: false, reason: 'consumed' });
});
it('peek: validates without consuming (GET never mutates)', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const { reject } = await svc.issueActionTokens(req.id, 'u9');
expect(await svc.peekActionToken(reject)).toMatchObject({ ok: true, action: 'reject' });
expect(await svc.peekActionToken(reject)).toMatchObject({ ok: true }); // still live
const fresh = await svc.getRequest(req.id, SYS);
expect(fresh?.status).toBe('pending');
});
it('redeem: dead tokens — invalid, expired, decided request, reassigned slot', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
expect(await svc.redeemActionToken('garbage')).toMatchObject({ ok: false, reason: 'invalid' });
const short = await svc.issueActionTokens(req.id, 'u9', { ttlMs: 1 });
// fake clock advances 1s per call — far beyond a 1ms TTL
expect(await svc.redeemActionToken(short.approve)).toMatchObject({ ok: false, reason: 'expired' });
const live = await svc.issueActionTokens(req.id, 'u9');
await svc.reassign(req.id, { actorId: 'u9', to: 'u7' }, CTX);
expect(await svc.redeemActionToken(live.approve)).toMatchObject({ ok: false, reason: 'not_approver' });
const forU7 = await svc.issueActionTokens(req.id, 'u7');
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u7' }, SYS);
expect(await svc.redeemActionToken(forU7.reject)).toMatchObject({ ok: false, reason: 'not_pending' });
});
it('remind: each concrete approver gets their own action links', async () => {
const emitted: any[] = [];
svc.attachMessaging({ async emit(input) { emitted.push(input); } });
const req = await svc.openNodeRequest(openInput(['u9', 'ada@example.com']), CTX);
await svc.remind(req.id, { actorId: 'u1' }, CTX);
const reminders = emitted.filter(e => e.topic === 'approval.reminder');
expect(reminders).toHaveLength(2);
for (const r of reminders) {
expect(r.audience).toHaveLength(1);
expect(r.payload.actions).toHaveLength(2);
expect(r.payload.actions[0].url).toContain('/api/v1/approvals/act?token=');
}
const urls = reminders.flatMap(r => r.payload.actions.map((a: any) => a.url));
expect(new Set(urls).size).toBe(4); // every link is personal + per-action
});
// ── pagination + search pushdown (#1745) ────────────────────────
async function openMany(n: number) {
for (let i = 0; i < n; i++) {
await svc.openNodeRequest(openInput(['u9'], {
recordId: `opp${i}`, record: { id: `opp${i}`, name: `Deal ${i}` },
}), CTX);
}
}
it('listRequests: windows pushable queries newest-first with limit/offset', async () => {
await openMany(5);
const page1 = await svc.listRequests({ limit: 2, offset: 0 }, SYS);
const page2 = await svc.listRequests({ limit: 2, offset: 2 }, SYS);
expect(page1.map(r => r.record_id)).toEqual(['opp4', 'opp3']); // created_at desc
expect(page2.map(r => r.record_id)).toEqual(['opp2', 'opp1']);
});
it('listRequests: q matches the payload snapshot (record titles) via pushdown', async () => {
await openMany(3);
const hit = await svc.listRequests({ q: 'Deal 1', limit: 10 }, SYS);
expect(hit.map(r => r.record_id)).toEqual(['opp1']);
const miss = await svc.listRequests({ q: 'no-such-thing', limit: 10 }, SYS);
expect(miss).toHaveLength(0);
});
it('countRequests: returns the unwindowed total for a filter', async () => {
await openMany(4);
expect(await svc.countRequests({ status: 'pending' }, SYS)).toBe(4);
expect(await svc.countRequests({ q: 'Deal 2' }, SYS)).toBe(1);
});
it('listRequests: approver queries resolve via the index and window engine-side', async () => {
await openMany(4); // approver u9 on all
await svc.openNodeRequest(openInput(['someone-else'], {
recordId: 'oppX', record: { id: 'oppX', name: 'Other' },
}), CTX);
const page = await svc.listRequests({ approverId: 'u9', limit: 2, offset: 2 }, SYS);
expect(page).toHaveLength(2);
expect(page.every(r => r.pending_approvers?.includes('u9'))).toBe(true);
expect(await svc.countRequests({ approverId: 'u9' }, SYS)).toBe(4);
});
it('listRequests/countRequests: status arrays push down as $in', async () => {
await openMany(3);
const all = await svc.listRequests({ status: 'pending' }, SYS);
await svc.decideNode(all[0].id, { decision: 'approve', actorId: 'u9' }, SYS);
await svc.decideNode(all[1].id, { decision: 'reject', actorId: 'u9' }, SYS);
const done = await svc.listRequests({ status: ['approved', 'rejected'] }, SYS);
expect(done.map(r => r.status).sort()).toEqual(['approved', 'rejected']);
expect(await svc.countRequests({ status: ['approved', 'rejected'] }, SYS)).toBe(2);
expect(await svc.countRequests({ status: ['recalled'] }, SYS)).toBe(0);
});
// ── pending-approver index (#1745 join table) ───────────────────
const indexRows = () => (engine._tables['sys_approval_approver'] ?? [])
.map(r => ({ request_id: r.request_id, approver: r.approver }));
it('openNodeRequest mirrors every approver identity into the index', async () => {
const req = await svc.openNodeRequest(openInput(['u9', 'ada@example.com', 'role:finance']), CTX);
expect(indexRows()).toEqual([
{ request_id: req.id, approver: 'u9' },
{ request_id: req.id, approver: 'ada@example.com' },
{ request_id: req.id, approver: 'role:finance' },
]);
});
it('decide and recall clear the request\'s index rows', async () => {
const a = await svc.openNodeRequest(openInput(['u9']), CTX);
await svc.decideNode(a.id, { decision: 'approve', actorId: 'u9' }, SYS);
expect(indexRows()).toHaveLength(0);
const b = await svc.openNodeRequest(openInput(['u9'], { recordId: 'opp2', record: { id: 'opp2' } }), CTX);
await svc.recall(b.id, { actorId: 'u1' }, CTX);
expect(indexRows()).toHaveLength(0);
});
it('unanimous partial approval shrinks the index to the still-pending set', async () => {
const req = await svc.openNodeRequest(openInput(['u1', 'u2'], {}, { behavior: 'unanimous' }), CTX);
await svc.decideNode(req.id, { decision: 'approve', actorId: 'u1' }, SYS);
expect(indexRows()).toEqual([{ request_id: req.id, approver: 'u2' }]);
});
it('reassign and SLA-reassign rewrite the index rows', async () => {
const req = await svc.openNodeRequest(
openInput(['u9', 'u2'], {}, { escalation: { timeoutHours: 1, action: 'reassign', escalateTo: 'boss', notifySubmitter: false } }), CTX,
);
await svc.reassign(req.id, { actorId: 'u9', to: 'u7' }, SYS);
expect(indexRows().map(r => r.approver).sort()).toEqual(['u2', 'u7']);
makeOverdue(req.id);
await svc.runEscalations();
expect(indexRows()).toEqual([{ request_id: req.id, approver: 'boss' }]);
});
it('approver-filtered pages stay correct past the old 500-row scan window', async () => {
// 30 u9 requests are the OLDEST rows, buried under 510 newer non-matching
// ones — the pre-#1745 bounded scan (limit 500, newest-first) could never
// reach them. Seeded directly: 540 openNodeRequest round-trips are noise.
const reqs = (engine._tables['sys_approval_request'] ??= []);
const idx = (engine._tables['sys_approval_approver'] ??= []);
const ts = (i: number) => new Date(baseTime + i * 1000).toISOString();
for (let i = 0; i < 30; i++) {
reqs.push({
id: `match_${i}`, process_name: 'flow:f', object_name: 'o', record_id: `m${i}`,
status: 'pending', pending_approvers: 'u9', created_at: ts(i), updated_at: ts(i),
});
idx.push({ id: `aapr_m${i}`, request_id: `match_${i}`, approver: 'u9', organization_id: null, created_at: ts(i) });
}
for (let i = 0; i < 510; i++) {
reqs.push({
id: `noise_${i}`, process_name: 'flow:f', object_name: 'o', record_id: `n${i}`,
status: 'pending', pending_approvers: 'someone-else', created_at: ts(100 + i), updated_at: ts(100 + i),
});
idx.push({ id: `aapr_n${i}`, request_id: `noise_${i}`, approver: 'someone-else', organization_id: null, created_at: ts(100 + i) });
}
expect(await svc.countRequests({ approverId: 'u9' }, SYS)).toBe(30);
const page = await svc.listRequests({ approverId: 'u9', limit: 10, offset: 20 }, SYS);
// Newest-first within the matches: offset 20 of 30 → match_9 … match_0.
expect(page.map(r => r.id)).toEqual(Array.from({ length: 10 }, (_, k) => `match_${9 - k}`));
expect(page.every(r => r.pending_approvers?.includes('u9'))).toBe(true);
});
it('rebuildApproverIndex backfills legacy rows, drops orphans + stale entries, and is idempotent', async () => {
const reqs = (engine._tables['sys_approval_request'] ??= []);
const idx = (engine._tables['sys_approval_approver'] ??= []);
const ts = new Date(baseTime).toISOString();
// Legacy pending row written before the index existed.
reqs.push({
id: 'legacy_1', process_name: 'flow:f', object_name: 'o', record_id: 'r1',
status: 'pending', pending_approvers: 'u1,u2', created_at: ts, updated_at: ts,
});
// Completed row whose index rows were never cleaned (orphan).
reqs.push({
id: 'done_1', process_name: 'flow:f', object_name: 'o', record_id: 'r2',
status: 'approved', pending_approvers: null, created_at: ts, updated_at: ts,
});
idx.push({ id: 'aapr_orphan', request_id: 'done_1', approver: 'u3', organization_id: null, created_at: ts });
// Pending row whose index drifted (holds an approver no longer in the CSV).
reqs.push({
id: 'drift_1', process_name: 'flow:f', object_name: 'o', record_id: 'r3',
status: 'pending', pending_approvers: 'u5', created_at: ts, updated_at: ts,
});
idx.push({ id: 'aapr_stale', request_id: 'drift_1', approver: 'u4', organization_id: null, created_at: ts });
const out = await svc.rebuildApproverIndex();
expect(out).toEqual({ requests: 2, inserted: 3, deleted: 2 }); // +u1 +u2 +u5 / -orphan -stale
expect(indexRows().sort((a, b) => a.approver.localeCompare(b.approver))).toEqual([
{ request_id: 'legacy_1', approver: 'u1' },
{ request_id: 'legacy_1', approver: 'u2' },
{ request_id: 'drift_1', approver: 'u5' },
]);
const again = await svc.rebuildApproverIndex();
expect(again).toEqual({ requests: 2, inserted: 0, deleted: 0 });
});
// ── SLA escalation (ADR-0042) ───────────────────────────────────
function makeOverdue(reqId: string) {
// Push created_at into the past so a small timeoutHours is breached.
const row = engine._tables['sys_approval_request'].find(r => r.id === reqId)!;
row.created_at = new Date(baseTime - 10 * 3600_000).toISOString();
}
it('runEscalations: notify action messages approvers + escalateTo + submitter, once', async () => {
const emitted: any[] = [];
svc.attachMessaging({ async emit(input) { emitted.push(input); } });
const req = await svc.openNodeRequest(
openInput(['u9'], {}, { escalation: { timeoutHours: 2, action: 'notify', escalateTo: 'boss', notifySubmitter: true } }), CTX,
);
makeOverdue(req.id);
const first = await svc.runEscalations();
expect(first.escalated).toBe(1);
expect(emitted.map(e => e.topic)).toEqual(['approval.sla_breached', 'approval.sla_breached']);
expect(emitted[0].audience).toEqual(['u9', 'boss']);
expect(emitted[1].audience).toEqual(['u1']); // submitter
const actions = await svc.listActions(req.id, SYS);
expect(actions.at(-1)).toMatchObject({ action: 'escalate', actor_id: 'system:sla', comment: 'notify → boss' });
// Single-shot: second sweep is a no-op.
const second = await svc.runEscalations();
expect(second.escalated).toBe(0);
expect(emitted).toHaveLength(2);
});
it('runEscalations: auto_approve decides as system:sla and resumes the flow', async () => {
const resumed: any[] = [];
svc.attachAutomation({ async resume(runId, signal) { resumed.push({ runId, signal }); } });
const req = await svc.openNodeRequest(
openInput(['u9'], {}, { escalation: { timeoutHours: 1, action: 'auto_approve', notifySubmitter: false } }), CTX,
);
makeOverdue(req.id);
const out = await svc.runEscalations();
expect(out.escalated).toBe(1);
const fresh = await svc.getRequest(req.id, SYS);
expect(fresh?.status).toBe('approved');
expect(resumed[0]).toMatchObject({ runId: 'run_1', signal: { branchLabel: 'approve' } });
const actions = await svc.listActions(req.id, SYS);
expect(actions.map(a => a.action)).toEqual(['submit', 'escalate', 'approve']);
expect(actions.at(-1)?.actor_id).toBe('system:sla');
});
it('runEscalations: auto_reject decides as system:sla', async () => {
const req = await svc.openNodeRequest(
openInput(['u9'], {}, { escalation: { timeoutHours: 1, action: 'auto_reject', notifySubmitter: false } }), CTX,
);
makeOverdue(req.id);
await svc.runEscalations();
const fresh = await svc.getRequest(req.id, SYS);
expect(fresh?.status).toBe('rejected');
});
it('runEscalations: reassign replaces the approver set with escalateTo', async () => {
const req = await svc.openNodeRequest(
openInput(['u9', 'u2'], {}, { escalation: { timeoutHours: 1, action: 'reassign', escalateTo: 'boss', notifySubmitter: false } }), CTX,
);
makeOverdue(req.id);
await svc.runEscalations();
const fresh = await svc.getRequest(req.id, SYS);
expect(fresh?.status).toBe('pending');
expect(fresh?.pending_approvers).toEqual(['boss']);
});
it('runEscalations: reassign expands a position escalateTo to its holders (ADR-0090 D3)', async () => {
engine._tables['sys_user_position'] = [
{ id: 'up1', user_id: 'u5', position: 'approvals_supervisor', organization_id: 't1' },
{ id: 'up2', user_id: 'u6', position: 'approvals_supervisor', organization_id: 't1' },
{ id: 'up3', user_id: 'u7', position: 'approvals_supervisor', organization_id: 't2' }, // other tenant
];
const req = await svc.openNodeRequest(
openInput(['u9'], {}, { escalation: { timeoutHours: 1, action: 'reassign', escalateTo: 'approvals_supervisor', notifySubmitter: false } }), CTX,
);
makeOverdue(req.id);
await svc.runEscalations();
const fresh = await svc.getRequest(req.id, SYS);
expect(fresh?.status).toBe('pending');
expect(fresh?.pending_approvers?.slice().sort()).toEqual(['u5', 'u6']);
// The audit trail keeps the AUTHORED target, not the expansion.
const actions = await svc.listActions(req.id, SYS);
expect(actions.find(a => a.action === 'escalate')?.comment).toBe('reassign → approvals_supervisor');
});
it('runEscalations: notify expands a position escalateTo into the audience', async () => {
engine._tables['sys_user_position'] = [
{ id: 'up1', user_id: 'u5', position: 'approvals_supervisor', organization_id: 't1' },
];
const emitted: any[] = [];
svc.attachMessaging({ async emit(input) { emitted.push(input); } });
const req = await svc.openNodeRequest(
openInput(['u9'], {}, { escalation: { timeoutHours: 2, action: 'notify', escalateTo: 'approvals_supervisor', notifySubmitter: false } }), CTX,
);
makeOverdue(req.id);
await svc.runEscalations();
expect(emitted).toHaveLength(1);
expect(emitted[0].audience).toEqual(['u9', 'u5']);
});
it('runEscalations: skips requests that are not yet due or have no SLA', async () => {
await svc.openNodeRequest(
openInput(['u9'], {}, { escalation: { timeoutHours: 1000, action: 'auto_approve' } }), CTX,
);
await svc.openNodeRequest(openInput(['u9'], { recordId: 'opp2', record: { id: 'opp2' } }), CTX);
const out = await svc.runEscalations();
expect(out.scanned).toBe(2);
expect(out.escalated).toBe(0);
});
// ── SLA + flow steps ────────────────────────────────────────────
it('rows expose sla_due_at when the node declares escalation.timeoutHours', async () => {
const req = await svc.openNodeRequest(
openInput(['u9'], {}, { escalation: { timeoutHours: 48, action: 'notify', notifySubmitter: true } }), CTX,
);
expect(req.sla_due_at).toBe(new Date(Date.parse(req.created_at!) + 48 * 3600_000).toISOString());
const noSla = await svc.openNodeRequest(openInput(['u9'], { recordId: 'opp2', record: { id: 'opp2' } }), CTX);
expect(noSla.sla_due_at).toBeUndefined();
});
it('getRequest attaches flow_steps from the owning flow graph', async () => {
svc.attachAutomation({