|
9 | 9 | // ?? null` — a resolved org id, or `null` when none resolved, or absent on |
10 | 10 | // rows written before stamping existed). |
11 | 11 |
|
| 12 | +import { readFileSync } from 'node:fs'; |
| 13 | +import { fileURLToPath } from 'node:url'; |
| 14 | + |
12 | 15 | import { describe, it, expect, expectTypeOf } from 'vitest'; |
13 | | -import type { ApprovalActionRow, ApprovalRequestRow } from './approval-service'; |
| 16 | +import type { |
| 17 | + ApprovalActionRow, |
| 18 | + ApprovalRequestRow, |
| 19 | + ApprovalStatus, |
| 20 | + IApprovalService, |
| 21 | +} from './approval-service'; |
14 | 22 |
|
15 | 23 | describe('approval row organization_id declaration (#10331)', () => { |
16 | 24 | it('is readable off ApprovalRequestRow without a cast, at the stamped shape', () => { |
@@ -54,3 +62,182 @@ describe('approval row organization_id declaration (#10331)', () => { |
54 | 62 | expect(read({ ...minimal, organization_id: 'o_plant' })).toBe('o_plant'); |
55 | 63 | }); |
56 | 64 | }); |
| 65 | + |
| 66 | +// --------------------------------------------------------------------------- |
| 67 | +// [#15389] `continueRestoredRun` — the approvals half of the operator repair |
| 68 | +// pair, declared on the contract. |
| 69 | +// |
| 70 | +// The maintainer ruling of 2026-09-09 (decision batch #106, item 3) settled |
| 71 | +// option A: the verb is declared on `IApprovalService` as an OPTIONAL member, |
| 72 | +// with the shape and docblock discipline #16495 gave |
| 73 | +// `IAutomationService.cancelRun` / `restoreConsumedSuspension`. These pins are |
| 74 | +// that block's sibling, and they hold the three things the ruling actually |
| 75 | +// decided: the member exists, it is optional, and it carries the ruled posture |
| 76 | +// in its docblock — including the note that promising a repair verb that will |
| 77 | +// refuse is worse than promising nothing. |
| 78 | +// |
| 79 | +// The type-level identities are exported aliases for the same reason the |
| 80 | +// #16495 block's are: an unread alias inside a test body is TS6196, and a pin |
| 81 | +// no program compiles is no pin at all. `check:test-typecheck` compiles this |
| 82 | +// file. |
| 83 | +// --------------------------------------------------------------------------- |
| 84 | + |
| 85 | +type Eq<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; |
| 86 | +type Assert<T extends true> = T; |
| 87 | +type ContinueRestoredRun = NonNullable<IApprovalService['continueRestoredRun']>; |
| 88 | + |
| 89 | +/** |
| 90 | + * `continueRestoredRun(requestId, options?)` — who asked and why travel |
| 91 | + * through the contract, exactly as they do on the engine verb this completes. |
| 92 | + */ |
| 93 | +export type ContinueTakesRequestIdAndOptions = Assert< |
| 94 | + Eq<Parameters<ContinueRestoredRun>, [requestId: string, options?: { requestedBy?: string; reason?: string }]> |
| 95 | +>; |
| 96 | + |
| 97 | +/** |
| 98 | + * The replay result: what moved, which run, which outcome and edge, and |
| 99 | + * whether the signal was the literal one or was rebuilt. A dropped or widened |
| 100 | + * member turns this alias red. |
| 101 | + */ |
| 102 | +export type ContinueAnswersTheReplayResult = Assert< |
| 103 | + Eq< |
| 104 | + Awaited<ReturnType<ContinueRestoredRun>>, |
| 105 | + { |
| 106 | + resumed: boolean; |
| 107 | + runId: string; |
| 108 | + decision: string; |
| 109 | + branchLabel?: string; |
| 110 | + source: 'journal' | 'reconstructed'; |
| 111 | + resumeError?: string; |
| 112 | + } |
| 113 | + > |
| 114 | +>; |
| 115 | + |
| 116 | +/** A conforming request row, at the minimum the contract requires. */ |
| 117 | +const requestRow = (id: string, status: ApprovalStatus): ApprovalRequestRow => ({ |
| 118 | + id, |
| 119 | + process_name: 'flow:expense_review', |
| 120 | + object_name: 'expense', |
| 121 | + record_id: 'rec_1', |
| 122 | + status, |
| 123 | +}); |
| 124 | + |
| 125 | +/** |
| 126 | + * The smallest thing that satisfies `IApprovalService` — every REQUIRED member |
| 127 | + * and nothing else. It is the population the optionality pin is about: a |
| 128 | + * service with no operator repair verb still conforms. |
| 129 | + */ |
| 130 | +const minimalService = (): IApprovalService => ({ |
| 131 | + listRequests: async () => [], |
| 132 | + countRequests: async () => 0, |
| 133 | + getRequest: async () => null, |
| 134 | + decide: async (requestId) => ({ request: requestRow(requestId, 'approved'), finalized: true, decision: 'approve' }), |
| 135 | + recall: async (requestId) => ({ request: requestRow(requestId, 'recalled') }), |
| 136 | + sendBack: async (requestId) => ({ request: requestRow(requestId, 'returned') }), |
| 137 | + resubmit: async (requestId) => ({ request: requestRow(requestId, 'returned') }), |
| 138 | + reassign: async (requestId) => ({ request: requestRow(requestId, 'pending') }), |
| 139 | + remind: async (requestId) => ({ request: requestRow(requestId, 'pending'), notified: 0 }), |
| 140 | + requestInfo: async (requestId) => ({ request: requestRow(requestId, 'pending') }), |
| 141 | + comment: async (requestId) => ({ request: requestRow(requestId, 'pending') }), |
| 142 | + listActions: async () => [], |
| 143 | +}); |
| 144 | + |
| 145 | +describe('[#15389] continueRestoredRun — the approvals operator repair verb, declared', () => { |
| 146 | + it('is optional: the minimal implementation still conforms and has no operator door', () => { |
| 147 | + const service = minimalService(); |
| 148 | + |
| 149 | + // The ruled optionality. A door standing in front of THIS service has |
| 150 | + // to probe and refuse fail-closed — it may not call and report success. |
| 151 | + expect(service.continueRestoredRun).toBeUndefined(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('carries the signature through the contract — who asked, and why, reach the implementation', async () => { |
| 155 | + const seen: Array<Record<string, unknown>> = []; |
| 156 | + const service: IApprovalService = { |
| 157 | + ...minimalService(), |
| 158 | + continueRestoredRun: async (requestId, options) => { |
| 159 | + seen.push({ requestId, ...options }); |
| 160 | + return requestId === 'req_journalled' |
| 161 | + ? { |
| 162 | + resumed: true, |
| 163 | + runId: 'run_stranded', |
| 164 | + decision: 'reject', |
| 165 | + branchLabel: 'reject', |
| 166 | + source: 'journal', |
| 167 | + } |
| 168 | + : { |
| 169 | + resumed: true, |
| 170 | + runId: 'run_stranded', |
| 171 | + decision: 'reject', |
| 172 | + branchLabel: 'reject', |
| 173 | + source: 'reconstructed', |
| 174 | + resumeError: 'a concurrent resume is already advancing this run', |
| 175 | + }; |
| 176 | + }, |
| 177 | + }; |
| 178 | + |
| 179 | + const exact = await service.continueRestoredRun!('req_journalled', { |
| 180 | + requestedBy: 'ops@example.com', |
| 181 | + reason: 'notify node fixed; re-issuing the recorded rejection', |
| 182 | + }); |
| 183 | + expect(exact.resumed).toBe(true); |
| 184 | + expect(exact.runId).toBe('run_stranded'); |
| 185 | + // The outcome is REPLAYED, never re-decided: it is the one already on |
| 186 | + // the row, and the edge it walks is the one it always walked. |
| 187 | + expect(exact.decision).toBe('reject'); |
| 188 | + expect(exact.branchLabel).toBe('reject'); |
| 189 | + // `journal` is the literal re-issue; `reconstructed` is the inferred |
| 190 | + // one. A caller that cannot tell them apart cannot say what it trusts. |
| 191 | + expect(exact.source).toBe('journal'); |
| 192 | + expect(exact.resumeError).toBeUndefined(); |
| 193 | + |
| 194 | + const rebuilt = await service.continueRestoredRun!('req_pre_journal'); |
| 195 | + expect(rebuilt.source).toBe('reconstructed'); |
| 196 | + expect(rebuilt.resumeError).toContain('concurrent resume'); |
| 197 | + |
| 198 | + // The optional parameters ARE the reason the signature follows the |
| 199 | + // implementation: an operator repair records who asked and why. |
| 200 | + expect(seen).toEqual([ |
| 201 | + { |
| 202 | + requestId: 'req_journalled', |
| 203 | + requestedBy: 'ops@example.com', |
| 204 | + reason: 'notify node fixed; re-issuing the recorded rejection', |
| 205 | + }, |
| 206 | + { requestId: 'req_pre_journal' }, |
| 207 | + ]); |
| 208 | + }); |
| 209 | + |
| 210 | + it('refuses a replay result that omits `source` (compile-time, under check:test-typecheck)', () => { |
| 211 | + const service: IApprovalService = { |
| 212 | + ...minimalService(), |
| 213 | + // @ts-expect-error — `source` is required: a caller told a run moved, but not whether the |
| 214 | + // signal was the literal one or a rebuild, cannot tell an exact replay from an inferred one. |
| 215 | + continueRestoredRun: async (_requestId) => ({ resumed: true, runId: 'run_stranded', decision: 'reject' }), |
| 216 | + }; |
| 217 | + |
| 218 | + expect(service.continueRestoredRun).toBeDefined(); |
| 219 | + }); |
| 220 | + |
| 221 | + it('the docblock carries the ruled posture: no re-decision, no door, and the promising-nothing note', () => { |
| 222 | + const source = readFileSync(fileURLToPath(new URL('./approval-service.ts', import.meta.url)), 'utf8'); |
| 223 | + const at = source.indexOf('continueRestoredRun?('); |
| 224 | + expect(at).toBeGreaterThan(-1); |
| 225 | + // The doc block immediately above the declaration — from its last `/**`. |
| 226 | + const doc = source.slice(source.lastIndexOf('/**', at), at); |
| 227 | + |
| 228 | + // The sibling this was ruled to copy, named where a later author reads it. |
| 229 | + expect(doc).toContain('#16495'); |
| 230 | + // What the engine verb leaves undone, which is the whole reason this exists. |
| 231 | + expect(doc).toMatch(/the continuation must be[\s*]+re-issued/); |
| 232 | + // ⛔ It replays a recorded outcome; it does not re-decide. |
| 233 | + expect(doc).toMatch(/does not re-open, re-decide or rewrite the request row/); |
| 234 | + expect(doc).toMatch(/does not relax the node's `resumeAuthority: 'service'`/); |
| 235 | + // Optional ⇒ absent means no door, and the door refuses fail-closed. |
| 236 | + expect(doc).toMatch(/NO[\s*]+operator door/); |
| 237 | + expect(doc).toMatch(/refuse[\s*]+fail-closed/); |
| 238 | + // The #16495 note, in its own words — the reason optionality is not a shrug. |
| 239 | + expect(doc).toMatch(/promising a repair verb that will refuse is worse[\s*]+than promising nothing/); |
| 240 | + // C was refused: declaring the member is not declaring a route. |
| 241 | + expect(doc).toMatch(/refused a REST\/CLI route/); |
| 242 | + }); |
| 243 | +}); |
0 commit comments