|
28 | 28 |
|
29 | 29 | import { describe, it, expect } from 'vitest'; |
30 | 30 | import { WorkCalendarService } from '@objectstack/objectql'; |
| 31 | +import { AutomationEngine } from '@objectstack/service-automation'; |
31 | 32 | import { ApprovalService } from './approval-service.js'; |
| 33 | +import { registerApprovalNode } from './approval-node.js'; |
32 | 34 |
|
33 | 35 | interface Row { [k: string]: any } |
34 | 36 |
|
@@ -390,3 +392,115 @@ describe("ApprovalEscalation.clock 'working_time' counts the hours on a working |
390 | 392 | expect(row?.sla_due_refusal).toBeUndefined(); |
391 | 393 | }); |
392 | 394 | }); |
| 395 | + |
| 396 | +// ── a calendar read that ERRORS, as opposed to one that refuses ───────────── |
| 397 | +// |
| 398 | +// A refusal (`isWorkCalendarRefusal`) is served as `sla_due_refusal` (D9). Any |
| 399 | +// other error — a driver failure reading `sys_work_calendar` — leaves that one |
| 400 | +// row with no deadline served: it never fails a request open that has already |
| 401 | +// written the row and told the approvers, never fails the rest of a list, and |
| 402 | +// never falls back to wall-clock hours (D2). The sweep skips the row and asks |
| 403 | +// again next pass. |
| 404 | + |
| 405 | +const CN_BACKUP: Row = { ...CN_CALENDAR, id: 'cal_cn_backup', code: 'cn_backup', is_default: false }; |
| 406 | + |
| 407 | +/** The real calendar service, except that a read for a calendar code in `failing` throws a plain Error. */ |
| 408 | +function erroringCalendarServiceAt(iso: string, failing: Set<string>) { |
| 409 | + let nowMs = Date.parse(iso); |
| 410 | + const engine = makeEngine(); |
| 411 | + const reads = calendarReads([CN_CALENDAR, CN_BACKUP], CN_EXCEPTIONS); |
| 412 | + const real = new WorkCalendarService({ getEngine: () => reads, posture: () => 'isolated', ttlMs: 0 }); |
| 413 | + const warned: Array<{ msg: string; meta: any }> = []; |
| 414 | + const logger = { info() {}, debug() {}, error() {}, warn: (msg: string, meta: any) => { warned.push({ msg, meta }); } }; |
| 415 | + const svc = new ApprovalService({ |
| 416 | + engine: engine as any, |
| 417 | + logger: logger as any, |
| 418 | + clock: { now: () => new Date(nowMs) }, |
| 419 | + workCalendar: () => ({ |
| 420 | + async addWorkingHours(scope, start, hours) { |
| 421 | + if (scope.calendar !== undefined && failing.has(scope.calendar)) { |
| 422 | + throw new Error('sys_work_calendar read failed: connection reset'); |
| 423 | + } |
| 424 | + return real.addWorkingHours(scope, start, hours); |
| 425 | + }, |
| 426 | + }), |
| 427 | + }); |
| 428 | + return { svc, engine, warned, setNow: (at: string) => { nowMs = Date.parse(at); } }; |
| 429 | +} |
| 430 | + |
| 431 | +describe("ApprovalEscalation.clock 'working_time': a calendar read that errors costs only that row its deadline", () => { |
| 432 | + const created = sh('2026-04-03', '17:00'); |
| 433 | + |
| 434 | + it('the open still returns the request it wrote and told the approvers about, with no deadline served', async () => { |
| 435 | + const { svc, engine, warned } = erroringCalendarServiceAt(created, new Set(['cn_standard'])); |
| 436 | + const req = await openPending(svc, workingTimeInput('wt_error_open', 4, { calendar: 'cn_standard' })); |
| 437 | + |
| 438 | + expect(engine._tables.sys_approval_request?.map((r) => r.id)).toEqual([req.id]); |
| 439 | + expect(req.status).toBe('pending'); |
| 440 | + expect(req.sla_due_at).toBeUndefined(); |
| 441 | + expect(req.sla_due_refusal).toBeUndefined(); |
| 442 | + expect(warned.some((w) => w.meta?.request === req.id)).toBe(true); |
| 443 | + }); |
| 444 | + |
| 445 | + it('the approval node suspends its run instead of failing it', async () => { |
| 446 | + const { svc } = erroringCalendarServiceAt(created, new Set(['cn_standard'])); |
| 447 | + const automation = new AutomationEngine({ info() {}, warn() {}, error() {}, debug() {} } as any); |
| 448 | + svc.attachAutomation(automation); |
| 449 | + registerApprovalNode(automation, svc); |
| 450 | + automation.registerFlow('wt_flow', { |
| 451 | + name: 'wt_flow', |
| 452 | + label: 'Working-time approval', |
| 453 | + type: 'autolaunched', |
| 454 | + nodes: [ |
| 455 | + { id: 'start', type: 'start', label: 'Start' }, |
| 456 | + { id: 'approve_step', type: 'approval', label: 'Approve', config: workingTimeInput('approve_step', 4, { calendar: 'cn_standard' }).config }, |
| 457 | + { id: 'end', type: 'end', label: 'End' }, |
| 458 | + ], |
| 459 | + edges: [ |
| 460 | + { id: 'e1', source: 'start', target: 'approve_step' }, |
| 461 | + { id: 'e2', source: 'approve_step', target: 'end', label: 'approve' }, |
| 462 | + ], |
| 463 | + } as any); |
| 464 | + |
| 465 | + const result = await automation.execute('wt_flow', { object: 'opportunity', record: { id: 'opp1' }, userId: 'u1' }); |
| 466 | + expect(result.status).toBe('paused'); |
| 467 | + expect(automation.listSuspendedRuns()).toHaveLength(1); |
| 468 | + }); |
| 469 | + |
| 470 | + it('a list and a single read keep every other row and its deadline', async () => { |
| 471 | + const failing = new Set<string>(); |
| 472 | + const { svc } = erroringCalendarServiceAt(created, failing); |
| 473 | + const good = await openPending(svc, workingTimeInput('wt_good', 4, { calendar: 'cn_standard' })); |
| 474 | + const bad = await openPending(svc, workingTimeInput('wt_bad', 4, { calendar: 'cn_backup' })); |
| 475 | + failing.add('cn_backup'); |
| 476 | + |
| 477 | + const list = await svc.listRequests({ status: 'pending' }, SYS); |
| 478 | + expect(list.map((r) => r.id).sort()).toEqual([good.id, bad.id].sort()); |
| 479 | + expect(list.find((r) => r.id === good.id)?.sla_due_at).toBe(sh('2026-04-07', '12:00')); |
| 480 | + const listedBad = list.find((r) => r.id === bad.id); |
| 481 | + expect(listedBad?.sla_due_at).toBeUndefined(); |
| 482 | + expect(listedBad?.sla_due_refusal).toBeUndefined(); |
| 483 | + |
| 484 | + const read = await svc.getRequest(bad.id, SYS); |
| 485 | + expect(read?.id).toBe(bad.id); |
| 486 | + expect(read?.sla_due_at).toBeUndefined(); |
| 487 | + }); |
| 488 | + |
| 489 | + it('the sweep skips the row, asks again next pass, and still escalates it once', async () => { |
| 490 | + const failing = new Set<string>(); |
| 491 | + const { svc, setNow } = erroringCalendarServiceAt(created, failing); |
| 492 | + const good = await openPending(svc, workingTimeInput('wt_sweep_good', 4, { calendar: 'cn_standard' })); |
| 493 | + const bad = await openPending(svc, workingTimeInput('wt_sweep_bad', 4, { calendar: 'cn_backup' })); |
| 494 | + failing.add('cn_backup'); |
| 495 | + |
| 496 | + setNow(sh('2026-04-20', '12:00')); |
| 497 | + expect(await svc.runEscalations()).toMatchObject({ escalated: 1 }); |
| 498 | + expect((await svc.listActions(good.id, SYS)).some((a) => a.action === 'escalate')).toBe(true); |
| 499 | + expect((await svc.listActions(bad.id, SYS)).some((a) => a.action === 'escalate')).toBe(false); |
| 500 | + |
| 501 | + failing.delete('cn_backup'); |
| 502 | + expect(await svc.runEscalations()).toMatchObject({ escalated: 1 }); |
| 503 | + expect((await svc.listActions(bad.id, SYS)).filter((a) => a.action === 'escalate')).toHaveLength(1); |
| 504 | + expect(await svc.runEscalations()).toMatchObject({ escalated: 0 }); |
| 505 | + }); |
| 506 | +}); |
0 commit comments