|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `defineStack`'s cross-reference refusal carries an ADR-0112 envelope |
| 5 | + * (`code` + `status`), not a bare `Error`. |
| 6 | + * |
| 7 | + * ## What was wrong |
| 8 | + * |
| 9 | + * `validateCrossReferences` collects every dangling reference a stack declares |
| 10 | + * — an item naming an object the stack does not define — and `defineStack` |
| 11 | + * raised the collected set as `new Error(message)`. `code` and `status` were |
| 12 | + * both `undefined`, so the five REFUSED item classes of the ADR-0130 matrix |
| 13 | + * (action `objectName`, view `data.object`, permission-set `objects`, seed |
| 14 | + * dataset `object`, import mapping `targetObject`) plus the `hooks[].object` |
| 15 | + * rule were distinguishable only by MESSAGE TEXT. ADR-0112 makes `code` / |
| 16 | + * `status` the machine-readable half of a refusal precisely so that prose does |
| 17 | + * not have to be load-bearing; `os validate`, `os build` and any AI author |
| 18 | + * reading the refusal had nothing else to match on. |
| 19 | + * |
| 20 | + * ## Why ONE code and not five |
| 21 | + * |
| 22 | + * There is exactly ONE raise site: `validateCrossReferences` returns a |
| 23 | + * `string[]` and `defineStack` throws the whole set as a single aggregated |
| 24 | + * error. A refusal can therefore carry issues from SEVERAL classes at once, |
| 25 | + * which is why the code names the rule family (the cross-reference gate) and |
| 26 | + * not one member of it — a per-class code on an aggregate throw would have to |
| 27 | + * pick one of several true answers. The individual classes stay legible in |
| 28 | + * `issues`, one entry per finding, which is the machine-readable form of what |
| 29 | + * previously existed only as newline-joined prose. |
| 30 | + * |
| 31 | + * The set is also WIDER than "undefined object": the same aggregate carries the |
| 32 | + * duplicate-action-key and global-`update`-action findings, and the mapping |
| 33 | + * `javascript`-transform refusal. `STACK_CROSS_REFERENCE_UNDEFINED_OBJECT` |
| 34 | + * would be false for those, so the family spelling is the honest one. |
| 35 | + * |
| 36 | + * ## What is pinned |
| 37 | + * |
| 38 | + * The ENVELOPE (`code`, `status`), never the message alone — a bare |
| 39 | + * `toThrow()` cannot tell "refused for the right reason" from "refused because |
| 40 | + * the fixture is broken", and both precedents for this defect class |
| 41 | + * (`ObjectOwnershipConflictError`, `NamespaceConflictError`) are asserted the |
| 42 | + * same way. The message text is pinned as UNCHANGED beside it: this change adds |
| 43 | + * fields, it does not reword a sentence, and five message-substring pins |
| 44 | + * elsewhere in the tree read it. |
| 45 | + */ |
| 46 | +import { describe, it, expect } from 'vitest'; |
| 47 | +import { defineStack } from './stack.zod'; |
| 48 | + |
| 49 | +const manifest = { |
| 50 | + id: 'com.example.crossrefenvelope', |
| 51 | + name: 'cross-reference-envelope-test', |
| 52 | + version: '1.0.0', |
| 53 | + type: 'app' as const, |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * The stack's ONE declared object. Every fixture below names `missing_object` |
| 58 | + * instead — the single difference between a refused stack and an accepted one. |
| 59 | + */ |
| 60 | +const declared = { |
| 61 | + name: 'probe_item', |
| 62 | + label: 'Probe Item', |
| 63 | + fields: { title: { type: 'text' as const } }, |
| 64 | +}; |
| 65 | + |
| 66 | +/** The name no fixture declares, so every reference to it dangles. */ |
| 67 | +const MISSING = 'missing_object'; |
| 68 | + |
| 69 | +/** The error shape every assertion below reads — the ADR-0112 envelope. */ |
| 70 | +type Envelope = Error & { code?: string; status?: number; issues?: readonly string[] }; |
| 71 | + |
| 72 | +/** The thrown value, or `null` when the stack is accepted. */ |
| 73 | +function refusal(config: Parameters<typeof defineStack>[0]): Envelope | null { |
| 74 | + try { |
| 75 | + defineStack(config); |
| 76 | + return null; |
| 77 | + } catch (e) { |
| 78 | + return e as Envelope; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +const stackWith = (extra: Record<string, unknown>) => |
| 83 | + ({ manifest, objects: [declared], ...extra }) as unknown as Parameters<typeof defineStack>[0]; |
| 84 | + |
| 85 | +/** |
| 86 | + * One row per refused item class. `message` is the verbatim line the aggregate |
| 87 | + * must still contain — the byte-for-byte fence on the prose. |
| 88 | + */ |
| 89 | +const rows: Array<{ label: string; config: Record<string, unknown>; message: string }> = [ |
| 90 | + { |
| 91 | + label: 'hooks[].object (#14122 §4 rule R4)', |
| 92 | + config: { |
| 93 | + hooks: [{ name: 'probe_hook', object: MISSING, events: ['afterInsert'], handler: 'noop' }], |
| 94 | + }, |
| 95 | + message: `Hook 'probe_hook' references object '${MISSING}' which is not defined in objects.`, |
| 96 | + }, |
| 97 | + { |
| 98 | + label: 'view data.object', |
| 99 | + config: { |
| 100 | + views: [ |
| 101 | + { |
| 102 | + name: 'probe_view', |
| 103 | + label: 'Probe View', |
| 104 | + list: { columns: [{ field: 'title' }], data: { provider: 'object', object: MISSING } }, |
| 105 | + }, |
| 106 | + ], |
| 107 | + }, |
| 108 | + message: `View[0].list references object '${MISSING}' which is not defined in objects.`, |
| 109 | + }, |
| 110 | + { |
| 111 | + label: 'seed dataset object', |
| 112 | + config: { data: [{ object: MISSING, records: [] }] }, |
| 113 | + message: `Seed data references object '${MISSING}' which is not defined in objects.`, |
| 114 | + }, |
| 115 | + { |
| 116 | + label: 'import mapping targetObject', |
| 117 | + config: { |
| 118 | + mappings: [{ name: 'probe_mapping', targetObject: MISSING, fieldMapping: [] }], |
| 119 | + }, |
| 120 | + message: `Mapping 'probe_mapping' targets object '${MISSING}' which is not defined in objects.`, |
| 121 | + }, |
| 122 | + { |
| 123 | + label: 'permission set objects', |
| 124 | + config: { |
| 125 | + permissions: [{ name: 'probe_perm', label: 'Probe Perm', objects: { [MISSING]: { allowRead: true } } }], |
| 126 | + }, |
| 127 | + message: `Permission 'probe_perm' grants on object '${MISSING}' which is not defined in objects.`, |
| 128 | + }, |
| 129 | + { |
| 130 | + label: 'action objectName', |
| 131 | + config: { |
| 132 | + actions: [{ name: 'probe_action', label: 'Probe Action', type: 'script', target: 'noop', objectName: MISSING }], |
| 133 | + }, |
| 134 | + message: `Action 'probe_action' references object '${MISSING}' which is not defined in objects.`, |
| 135 | + }, |
| 136 | +]; |
| 137 | + |
| 138 | +describe('#14552 — defineStack cross-reference refusals carry an ADR-0112 envelope', () => { |
| 139 | + for (const row of rows) { |
| 140 | + describe(row.label, () => { |
| 141 | + it('refuses with code STACK_CROSS_REFERENCE_INVALID and status 422', () => { |
| 142 | + const refused = refusal(stackWith(row.config)); |
| 143 | + expect(refused).toBeInstanceOf(Error); |
| 144 | + expect(refused?.code).toBe('STACK_CROSS_REFERENCE_INVALID'); |
| 145 | + expect(refused?.status).toBe(422); |
| 146 | + }); |
| 147 | + |
| 148 | + it('keeps the message text byte-for-byte, header and line', () => { |
| 149 | + const refused = refusal(stackWith(row.config)); |
| 150 | + expect(refused?.message).toContain('defineStack cross-reference validation failed'); |
| 151 | + expect(refused?.message).toContain(row.message); |
| 152 | + }); |
| 153 | + |
| 154 | + it('carries the finding in `issues`, one entry per finding', () => { |
| 155 | + const refused = refusal(stackWith(row.config)); |
| 156 | + expect(refused?.issues).toContain(row.message); |
| 157 | + }); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + it('the same object declared makes the stack ACCEPTED — the fixtures differ by one name', () => { |
| 162 | + // The control: without it, a fixture broken in some unrelated way would |
| 163 | + // satisfy every refusal assertion above for the wrong reason. |
| 164 | + const accepted = refusal( |
| 165 | + stackWith({ data: [{ object: declared.name, records: [] }] }), |
| 166 | + ); |
| 167 | + expect(accepted).toBeNull(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('an aggregate spanning TWO classes carries one code and BOTH findings', () => { |
| 171 | + // Why the code names the rule family and not one item class: a single |
| 172 | + // throw can carry findings from several classes at once. |
| 173 | + const refused = refusal( |
| 174 | + stackWith({ |
| 175 | + data: [{ object: MISSING, records: [] }], |
| 176 | + mappings: [{ name: 'probe_mapping', targetObject: MISSING, fieldMapping: [] }], |
| 177 | + }), |
| 178 | + ); |
| 179 | + expect(refused?.code).toBe('STACK_CROSS_REFERENCE_INVALID'); |
| 180 | + expect(refused?.issues).toHaveLength(2); |
| 181 | + }); |
| 182 | +}); |
0 commit comments