|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #24546 — a resume body that carries BOTH `inputs` and `variables` is refused, |
| 5 | + * never resolved by dropping one of them. |
| 6 | + * |
| 7 | + * `variables` is an accepted alias of `inputs` on |
| 8 | + * `POST /automation/:name/runs/:runId/resume`, and the signal assembly reads |
| 9 | + * `b.inputs ?? b.variables`. A body carrying both therefore answered HTTP 200 |
| 10 | + * `success:true` with the `variables` bag never read — measured on stock |
| 11 | + * showcase through `showcase_reassign_wizard`, whose `collect` screen declares |
| 12 | + * one field, `new_assignee`: the run completed with the `inputs` value, while |
| 13 | + * the `variables` bag held an undeclared `bogus_key` that, sent alone, is |
| 14 | + * refused `400 INVALID_SCREEN_INPUT`. The same silent-drop family the closed |
| 15 | + * key set (commit a4331227b, `automation-resume-envelope.test.ts`) and the |
| 16 | + * value-shape check (#9416, `automation-resume-value-shape.test.ts`) closed, |
| 17 | + * on the one pair of accepted keys that name the same thing. |
| 18 | + * |
| 19 | + * The lane's pick on the card: REFUSE, with the route's own `400 |
| 20 | + * VALIDATION_FAILED` envelope (the shape its closed-set and value-shape |
| 21 | + * refusals already answer), naming both keys and the one to keep, with the |
| 22 | + * engine never consulted so the suspension stays parked for the retry. |
| 23 | + * |
| 24 | + * The scripted engine below plays the `collect` screen the card measured: it |
| 25 | + * judges the submitted bag against the declared `new_assignee` and refuses an |
| 26 | + * undeclared key with `INVALID_SCREEN_INPUT`, so each case reads what the |
| 27 | + * engine was actually handed. Its verdicts are the engine's own, pinned where |
| 28 | + * the engine lives (`service-automation`'s `screen-resume-validation.test.ts`); |
| 29 | + * this file pins only the door. Both directions are pinned, because a door that |
| 30 | + * refuses everything ships as green as a correct one: the both-keys body is |
| 31 | + * refused, and each key alone answers exactly as before. |
| 32 | + */ |
| 33 | + |
| 34 | +import { describe, it, expect, vi } from 'vitest'; |
| 35 | + |
| 36 | +import type { AutomationResult } from '@objectstack/spec/contracts'; |
| 37 | + |
| 38 | +import { HttpDispatcher } from '../http-dispatcher.js'; |
| 39 | +import { validationFailureDetails, VALIDATION_FAILED_STATUS } from '../validation-failure.js'; |
| 40 | + |
| 41 | +const DECLARED_FIELDS = ['new_assignee']; |
| 42 | + |
| 43 | +/** |
| 44 | + * The `collect` screen of `showcase_reassign_wizard`, scripted: one declared, |
| 45 | + * required field. An undeclared key is refused the way the engine refuses it; |
| 46 | + * a complete bag completes the run and echoes what was written. |
| 47 | + */ |
| 48 | +function makeDispatcher() { |
| 49 | + const resume = vi.fn(async (_runId: string, signal: { variables?: Record<string, unknown> }): Promise<AutomationResult> => { |
| 50 | + const bag = signal.variables ?? {}; |
| 51 | + const undeclared = Object.keys(bag).filter((k) => !DECLARED_FIELDS.includes(k)); |
| 52 | + if (undeclared.length > 0) { |
| 53 | + return { |
| 54 | + success: false, |
| 55 | + code: 'INVALID_SCREEN_INPUT', |
| 56 | + error: `Invalid screen input: Unknown screen field "${undeclared[0]}" — declared fields: 'new_assignee'`, |
| 57 | + } as AutomationResult; |
| 58 | + } |
| 59 | + if (bag.new_assignee === undefined) { |
| 60 | + return { |
| 61 | + success: false, |
| 62 | + code: 'INVALID_SCREEN_INPUT', |
| 63 | + error: 'Invalid screen input: Screen field "new_assignee" is required', |
| 64 | + } as AutomationResult; |
| 65 | + } |
| 66 | + return { success: true, output: { assignee: bag.new_assignee }, durationMs: 7 } as AutomationResult; |
| 67 | + }); |
| 68 | + const services: Record<string, unknown> = { automation: { resume } }; |
| 69 | + const resolve = (name: string) => services[name]; |
| 70 | + const kernel: any = { |
| 71 | + getService: resolve, |
| 72 | + getServiceAsync: async (name: string) => resolve(name), |
| 73 | + context: { getService: resolve }, |
| 74 | + }; |
| 75 | + return { dispatcher: new HttpDispatcher(kernel), resume }; |
| 76 | +} |
| 77 | + |
| 78 | +const CTX = () => ({ request: {}, executionContext: { userId: 'user_1' } } as any); |
| 79 | +const RESUME = '/showcase_reassign_wizard/runs/run_1/resume'; |
| 80 | + |
| 81 | +/** The card's measured body: both keys, the `variables` bag carrying an undeclared field. */ |
| 82 | +const CARD_BODY = { |
| 83 | + inputs: { new_assignee: 'inputs-win@example.com' }, |
| 84 | + variables: { new_assignee: 'variables-lost@example.com', bogus_key: 1 }, |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Drive the resume route with one body and report the refusal as the wire |
| 89 | + * would: the status is the one both dispatcher error exits derive for a thrown |
| 90 | + * validation failure carrying no `.status` of its own (#3918). |
| 91 | + */ |
| 92 | +async function refusalFor(body: unknown) { |
| 93 | + const { dispatcher, resume } = makeDispatcher(); |
| 94 | + let thrown: unknown; |
| 95 | + let response: unknown; |
| 96 | + try { |
| 97 | + response = (await dispatcher.handleAutomation(RESUME, 'POST', body, CTX())).response; |
| 98 | + } catch (e) { |
| 99 | + thrown = e; |
| 100 | + } |
| 101 | + expect( |
| 102 | + thrown, |
| 103 | + `resume body ${JSON.stringify(body)} was accepted (answered ${JSON.stringify(response)}) instead of refused`, |
| 104 | + ).toBeDefined(); |
| 105 | + const details = validationFailureDetails(thrown); |
| 106 | + const status = |
| 107 | + typeof (thrown as any)?.status === 'number' ? (thrown as any).status |
| 108 | + : details ? VALIDATION_FAILED_STATUS |
| 109 | + : 500; |
| 110 | + return { details, status, message: (thrown as Error).message, resume }; |
| 111 | +} |
| 112 | + |
| 113 | +/** Drive the resume route to its answer and hand back what the engine received. */ |
| 114 | +async function answerFor(body: unknown) { |
| 115 | + const { dispatcher, resume } = makeDispatcher(); |
| 116 | + const result = await dispatcher.handleAutomation(RESUME, 'POST', body, CTX()); |
| 117 | + return { status: result.response?.status, error: result.response?.body?.error, resume }; |
| 118 | +} |
| 119 | + |
| 120 | +// ───────────────────────────────────────────────────────────────────────────── |
| 121 | +// Direction 1 — the refusal |
| 122 | +// ───────────────────────────────────────────────────────────────────────────── |
| 123 | + |
| 124 | +describe('#24546 — a body carrying both `inputs` and `variables` is refused', () => { |
| 125 | + it('refuses the card\'s measured body — 400 VALIDATION_FAILED, both keys located, engine never consulted', async () => { |
| 126 | + const r = await refusalFor(CARD_BODY); |
| 127 | + // The envelope: the route's own validation-failure shape, the one its |
| 128 | + // closed-set and value-shape refusals answer. |
| 129 | + expect(r.status).toBe(VALIDATION_FAILED_STATUS); |
| 130 | + expect(r.details?.code).toBe('VALIDATION_FAILED'); |
| 131 | + // Located: BOTH keys named, each its own entry, with an ADR-0114 |
| 132 | + // catalog code (`invalid_value`: rejected for a reason no other |
| 133 | + // member names — the value is well-shaped, its sibling is the problem). |
| 134 | + expect(r.details?.fields).toMatchObject([ |
| 135 | + { field: 'inputs', code: 'invalid_value' }, |
| 136 | + { field: 'variables', code: 'invalid_value' }, |
| 137 | + ]); |
| 138 | + // The message names both keys and the one to keep. |
| 139 | + expect(r.message).toMatch(/`inputs`/); |
| 140 | + expect(r.message).toMatch(/`variables`/); |
| 141 | + expect(r.message).toMatch(/send `inputs` alone/); |
| 142 | + // The load-bearing half: nothing reached the engine, so the |
| 143 | + // suspension was not consumed and a corrected retry is legitimate. |
| 144 | + expect(r.resume).not.toHaveBeenCalled(); |
| 145 | + }); |
| 146 | + |
| 147 | + it.each([ |
| 148 | + ['identical bags', { inputs: { new_assignee: 'ada' }, variables: { new_assignee: 'ada' } }], |
| 149 | + ['two empty bags', { inputs: {}, variables: {} }], |
| 150 | + ['both keys beside `output` and `branchLabel`', { |
| 151 | + inputs: { new_assignee: 'ada' }, |
| 152 | + variables: { new_assignee: 'bob' }, |
| 153 | + output: { comment: 'ok' }, |
| 154 | + branchLabel: 'approve', |
| 155 | + }], |
| 156 | + ])('refuses %s too — the rule is "both keys present", never "the bags disagree"', async (_label, body) => { |
| 157 | + const r = await refusalFor(body); |
| 158 | + expect(r.status).toBe(VALIDATION_FAILED_STATUS); |
| 159 | + expect(r.details?.code).toBe('VALIDATION_FAILED'); |
| 160 | + expect(r.details?.fields).toMatchObject([{ field: 'inputs' }, { field: 'variables' }]); |
| 161 | + expect(r.resume).not.toHaveBeenCalled(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('keeps the earlier refusals first — an unknown key, then a mis-shaped value, each unchanged', async () => { |
| 165 | + // Ordering pin: the closed key set (commit a4331227b) and the value |
| 166 | + // shapes (#9416) still report exactly what they reported before, so |
| 167 | + // a body wrong in two ways gets the earlier correction first. |
| 168 | + const unknown = await refusalFor({ ...CARD_BODY, values: { x: 1 } }); |
| 169 | + expect(unknown.details?.fields).toMatchObject([{ field: 'values', code: 'unknown_field' }]); |
| 170 | + |
| 171 | + const misShaped = await refusalFor({ inputs: 'a string', variables: { new_assignee: 'ada' } }); |
| 172 | + expect(misShaped.details?.fields).toMatchObject([{ field: 'inputs', code: 'invalid_type' }]); |
| 173 | + }); |
| 174 | + |
| 175 | + it('reads an `undefined` value as ABSENT — no conflict, the same reading as the value-shape check', async () => { |
| 176 | + // `JSON.stringify` drops such a key, so no HTTP caller can produce |
| 177 | + // one; the in-process spelling `{ inputs, variables: maybeUndefined }` |
| 178 | + // means "no variables". |
| 179 | + const { status, resume } = await answerFor({ inputs: { new_assignee: 'ada' }, variables: undefined }); |
| 180 | + expect(status).toBe(200); |
| 181 | + expect(resume).toHaveBeenCalledWith('run_1', { variables: { new_assignee: 'ada' } }); |
| 182 | + }); |
| 183 | + |
| 184 | + it('escapes dispatch() as the recognized validation-failure shape', async () => { |
| 185 | + // dispatch() rethrows a domain's validation failure to the HTTP error |
| 186 | + // exits #3918 pinned (`dispatcher-validation-error.test.ts`: 400, |
| 187 | + // `error.code: 'VALIDATION_FAILED'`, `details.fields[]`). |
| 188 | + const { dispatcher, resume } = makeDispatcher(); |
| 189 | + (dispatcher as any).timedResolveExecutionContext = async () => ({ userId: 'user_1' }); |
| 190 | + let thrown: unknown; |
| 191 | + try { |
| 192 | + await dispatcher.dispatch( |
| 193 | + 'POST', `/automation${RESUME}`, |
| 194 | + CARD_BODY, {}, {} as any, |
| 195 | + ); |
| 196 | + } catch (e) { |
| 197 | + thrown = e; |
| 198 | + } |
| 199 | + expect(thrown, 'the refusal must reach the HTTP error exits').toBeDefined(); |
| 200 | + expect(validationFailureDetails(thrown)?.code).toBe('VALIDATION_FAILED'); |
| 201 | + expect(validationFailureDetails(thrown)?.fields).toMatchObject([{ field: 'inputs' }, { field: 'variables' }]); |
| 202 | + expect(resume).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | +}); |
| 205 | + |
| 206 | +// ───────────────────────────────────────────────────────────────────────────── |
| 207 | +// Direction 2 — the controls: either key ALONE answers exactly as before. |
| 208 | +// ───────────────────────────────────────────────────────────────────────────── |
| 209 | + |
| 210 | +describe('#24546 — a body carrying one of the two keys is unaffected', () => { |
| 211 | + it('`variables` alone with an undeclared field — still the engine\'s 400 INVALID_SCREEN_INPUT', async () => { |
| 212 | + // The alias is live, not dead: sent alone, its bag reaches the engine |
| 213 | + // and is judged — which is what made dropping it in combination a |
| 214 | + // silent drop rather than a dead key. |
| 215 | + const { status, error, resume } = await answerFor({ variables: CARD_BODY.variables }); |
| 216 | + expect(status).toBe(400); |
| 217 | + expect(error?.code).toBe('INVALID_SCREEN_INPUT'); |
| 218 | + expect(error?.message).toMatch(/bogus_key/); |
| 219 | + expect(resume).toHaveBeenCalledWith('run_1', { variables: CARD_BODY.variables }); |
| 220 | + }); |
| 221 | + |
| 222 | + it('`variables` alone, valid — 200, the bag forwarded as the submission', async () => { |
| 223 | + const { status, resume } = await answerFor({ variables: { new_assignee: 'variables-only@example.com' } }); |
| 224 | + expect(status).toBe(200); |
| 225 | + expect(resume).toHaveBeenCalledWith('run_1', { variables: { new_assignee: 'variables-only@example.com' } }); |
| 226 | + }); |
| 227 | + |
| 228 | + it('`inputs` alone, valid — 200, the bag forwarded as the submission', async () => { |
| 229 | + const { status, resume } = await answerFor({ inputs: { new_assignee: 'inputs-only@example.com' } }); |
| 230 | + expect(status).toBe(200); |
| 231 | + expect(resume).toHaveBeenCalledWith('run_1', { variables: { new_assignee: 'inputs-only@example.com' } }); |
| 232 | + }); |
| 233 | +}); |
0 commit comments