|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0010] The BUILD door of the collection-item owner-stamp rule: the stack |
| 5 | + * schema refuses an item whose body carries a `_packageId` other than the |
| 6 | + * package that ships it (`applyCollectionItemOwnerStampGate`, `stack.zod.ts`, |
| 7 | + * calling `judgeCollectionItemOwnerStamp`). |
| 8 | + * |
| 9 | + * Before this gate such a stack parsed, and only the registration door refused |
| 10 | + * it, at boot or install. `defineStack`, `os validate`, `os build` and the |
| 11 | + * artifact door all run this one parse, so it is pinned here on the parse and |
| 12 | + * through `defineStack`, with the envelope each answers: the issue's `code` and |
| 13 | + * path at the parse, `STACK_SCHEMA_INVALID` / 422 from `defineStack`. |
| 14 | + * |
| 15 | + * The judge reads the PARSED stack, so a key the parse dropped would make it |
| 16 | + * unreachable: every judged collection's item schema declares `_packageId` |
| 17 | + * (`MetadataProtectionFields`, held by the authorable-surface ratchet), and the |
| 18 | + * last suite drives one item of each shape class (a plain item, a transformed |
| 19 | + * action, a seed dataset, a view container) through the parse to the judge. |
| 20 | + */ |
| 21 | +import { describe, it, expect } from 'vitest'; |
| 22 | +import { composeStacks, defineStack, ObjectStackDefinitionSchema } from './stack.zod'; |
| 23 | +import { judgeCollectionItemOwnerStamp } from './kernel/collection-item-owner-stamp'; |
| 24 | + |
| 25 | +type Envelope = Error & { code?: string; status?: number; issues?: Array<{ code?: string; path?: PropertyKey[]; message?: string }> }; |
| 26 | + |
| 27 | +const CRM = 'com.acme.crm'; |
| 28 | +const OTHER = 'com.acme.other'; |
| 29 | + |
| 30 | +const connector = (name: string, stamp?: unknown) => ({ |
| 31 | + name, |
| 32 | + label: 'Probe', |
| 33 | + type: 'api', |
| 34 | + provider: 'openapi', |
| 35 | + providerConfig: { spec: './specs/billing.json' }, |
| 36 | + ...(stamp === undefined ? {} : { _packageId: stamp }), |
| 37 | +}); |
| 38 | + |
| 39 | +const manifest = (id: string, extra: Record<string, unknown> = {}) => ({ |
| 40 | + id, |
| 41 | + name: id, |
| 42 | + namespace: 'crm', |
| 43 | + version: '1.0.0', |
| 44 | + type: 'app' as const, |
| 45 | + ...extra, |
| 46 | +}); |
| 47 | + |
| 48 | +function refusal(config: unknown): Envelope | null { |
| 49 | + try { |
| 50 | + defineStack(config as never); |
| 51 | + return null; |
| 52 | + } catch (e) { |
| 53 | + return e as Envelope; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** Every issue the parse raises at a `_packageId`, as `[path, code, message]`. */ |
| 58 | +function stampIssues(config: unknown): Array<[string, string, string]> { |
| 59 | + const result = ObjectStackDefinitionSchema.safeParse(config); |
| 60 | + if (result.success) return []; |
| 61 | + return result.error.issues |
| 62 | + .filter((i) => i.path[i.path.length - 1] === '_packageId') |
| 63 | + .map((i) => [i.path.join('.'), i.code, i.message]); |
| 64 | +} |
| 65 | + |
| 66 | +describe('[ADR-0010] defineStack refuses a collection item whose body `_packageId` is not the stack\'s package', () => { |
| 67 | + it('a connector naming another package: STACK_SCHEMA_INVALID / 422, the issue at its `_packageId`, in the judge\'s build words', () => { |
| 68 | + const error = refusal({ manifest: manifest(CRM), connectors: [connector('probe_conn', OTHER)] }); |
| 69 | + expect(error, 'the stack must be refused').not.toBeNull(); |
| 70 | + expect(error).toMatchObject({ code: 'STACK_SCHEMA_INVALID', status: 422 }); |
| 71 | + const issue = error!.issues!.find((i) => i.path?.join('.') === 'connectors.0._packageId'); |
| 72 | + expect(issue, 'located at the item\'s key').toMatchObject({ code: 'custom' }); |
| 73 | + expect(issue!.message).toBe( |
| 74 | + judgeCollectionItemOwnerStamp({ _packageId: OTHER }, 'connector', 'probe_conn', CRM, 'build')!.message, |
| 75 | + ); |
| 76 | + expect(issue!.message.indexOf("Drop `_packageId` from connector 'probe_conn'")).toBe(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('CONTROL: the same stack without the stamp, and with a stamp equal to `manifest.id`, is accepted', () => { |
| 80 | + expect(refusal({ manifest: manifest(CRM), connectors: [connector('probe_conn')] })).toBeNull(); |
| 81 | + expect(refusal({ manifest: manifest(CRM), connectors: [connector('probe_conn', CRM)] })).toBeNull(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('`\'\'` is a stamp: refused by the judge; `null` is a stamp too, refused first by the item schema at the same key', () => { |
| 85 | + expect(stampIssues({ manifest: manifest(CRM), connectors: [connector('probe_conn', '')] })) |
| 86 | + .toEqual([['connectors.0._packageId', 'custom', expect.stringContaining('carries _packageId "", but this stack ships it')]]); |
| 87 | + expect(stampIssues({ manifest: manifest(CRM), connectors: [connector('probe_conn', null)] })) |
| 88 | + .toEqual([['connectors.0._packageId', 'invalid_type', expect.any(String)]]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('a stack with no `manifest` ships its items from no package: any stamp is refused', () => { |
| 92 | + for (const stamp of [OTHER, CRM]) { |
| 93 | + expect(stampIssues({ connectors: [connector('probe_conn', stamp)] }), stamp) |
| 94 | + .toEqual([['connectors.0._packageId', 'custom', expect.stringContaining('from no package (the stack declares no `manifest`)')]]); |
| 95 | + } |
| 96 | + expect(stampIssues({ connectors: [connector('probe_conn')] })).toEqual([]); |
| 97 | + }); |
| 98 | + |
| 99 | + it('a map-format collection is judged on the array it normalises to', () => { |
| 100 | + const error = refusal({ |
| 101 | + manifest: manifest(CRM), |
| 102 | + connectors: { probe_conn: { label: 'Probe', type: 'api', provider: 'openapi', providerConfig: { spec: './s.json' }, _packageId: OTHER } }, |
| 103 | + }); |
| 104 | + expect(error).toMatchObject({ code: 'STACK_SCHEMA_INVALID', status: 422 }); |
| 105 | + expect(error!.issues!.map((i) => i.path?.join('.'))).toContain('connectors.0._packageId'); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('[ADR-0010] the parse keeps the stamp for the judge, across shape classes', () => { |
| 110 | + const stack = (extra: Record<string, unknown>) => ({ manifest: manifest(CRM), ...extra }); |
| 111 | + |
| 112 | + it('an action (a transformed item schema) is judged at its `_packageId`', () => { |
| 113 | + const action = { name: 'crm_go', label: 'Go', type: 'script', target: 'go' }; |
| 114 | + expect(stampIssues(stack({ actions: [{ ...action, _packageId: OTHER }] })).map(([p, c]) => [p, c])) |
| 115 | + .toEqual([['actions.0._packageId', 'custom']]); |
| 116 | + expect(stampIssues(stack({ actions: [action] }))).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('a seed dataset is judged under the type word `data`, named by its object', () => { |
| 120 | + const data = { object: 'crm_task', records: [] }; |
| 121 | + expect(stampIssues(stack({ data: [{ ...data, _packageId: OTHER }] }))) |
| 122 | + .toEqual([['data.0._packageId', 'custom', expect.stringContaining("Drop `_packageId` from data 'crm_task'")]]); |
| 123 | + expect(stampIssues(stack({ data: [data] }))).toEqual([]); |
| 124 | + }); |
| 125 | + |
| 126 | + it('a view container is judged, named by the object it binds to', () => { |
| 127 | + const view = { object: 'crm_task', list: { type: 'grid', columns: ['name'] } }; |
| 128 | + expect(stampIssues(stack({ views: [{ ...view, _packageId: OTHER }] }))) |
| 129 | + .toEqual([['views.0._packageId', 'custom', expect.stringContaining("Drop `_packageId` from view 'crm_task'")]]); |
| 130 | + expect(stampIssues(stack({ views: [view] }))).toEqual([]); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('[ADR-0010] a release artifact\'s items are judged against the package that registers each one', () => { |
| 135 | + /** Two packages composed as `os build` does (ADR-0130 D4), the crm connector's stamp as the variable. */ |
| 136 | + // The artifact's own manifest is the LAST input's (`'preserve'` keeps the |
| 137 | + // `'last'` pick), so it is the orders module's, never the package under test. |
| 138 | + const twoPackages = (crmStamp?: string) => composeStacks([ |
| 139 | + defineStack({ manifest: manifest(CRM), connectors: [connector('crm_conn', crmStamp)] } as never, { strict: false }), |
| 140 | + defineStack({ manifest: manifest('com.acme.orders', { type: 'module' }), connectors: [connector('orders_conn')] } as never), |
| 141 | + ], { manifest: 'preserve' }) as unknown as { |
| 142 | + manifest: { id: string }; |
| 143 | + packages: Array<{ manifest: Record<string, unknown> }>; |
| 144 | + [key: string]: unknown; |
| 145 | + }; |
| 146 | + |
| 147 | + it('a body\'s item is judged against that body\'s own id', () => { |
| 148 | + const artifact = twoPackages(OTHER); |
| 149 | + const at = artifact.packages.findIndex((p) => p.manifest.id === CRM); |
| 150 | + expect(stampIssues(artifact).map(([path]) => path)).toEqual([`packages.${at}.manifest.connectors.0._packageId`]); |
| 151 | + // CONTROL: the stamp naming the body that ships it, and no stamp. |
| 152 | + expect(stampIssues(twoPackages(CRM))).toEqual([]); |
| 153 | + expect(stampIssues(twoPackages())).toEqual([]); |
| 154 | + }); |
| 155 | + |
| 156 | + it('a top-level item no body declares is the residual, judged against the artifact\'s `manifest.id`', () => { |
| 157 | + const artifact = { ...twoPackages(), connectors: [connector('stray_conn', OTHER)] }; |
| 158 | + const owner = artifact.manifest.id; |
| 159 | + expect(stampIssues(artifact)).toEqual([['connectors.0._packageId', 'custom', expect.stringContaining(`from package '${owner}'`)]]); |
| 160 | + // CONTROL: the residual stamped with the artifact's own id. |
| 161 | + expect(stampIssues({ ...twoPackages(), connectors: [connector('stray_conn', owner)] })).toEqual([]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('CONTROL: a top-level COPY of a body\'s item is not judged against the artifact\'s id', () => { |
| 165 | + const artifact = twoPackages(CRM); |
| 166 | + const flattened = { ...artifact, connectors: [connector('crm_conn', CRM), connector('orders_conn')] }; |
| 167 | + expect(flattened.manifest.id).not.toBe(CRM); |
| 168 | + expect(stampIssues(flattened)).toEqual([]); |
| 169 | + }); |
| 170 | +}); |
0 commit comments