|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20679, ADR-0126 §2] `packagedBaseRefusal` — the `/meta` door's |
| 5 | + * locked-base verdict, as a value, for a second door onto the same artifact. |
| 6 | + * |
| 7 | + * It is not a new rule. It hands out the SAME verdict `saveMetaItem` and |
| 8 | + * `deleteMetaItem` reach — the package doors both now call, lifted out of them |
| 9 | + * unchanged, `refusePackagedBaseOverride` / `refusePackagedBaseRemoval` — so this file |
| 10 | + * pins two things and only two: |
| 11 | + * |
| 12 | + * 1. it answers exactly what those two methods throw for the same item (the |
| 13 | + * ONE-emitter claim: same code, status and sentence); |
| 14 | + * 2. its matrix is the metadata door's matrix, including the answers that are |
| 15 | + * deliberately `null` — a name no package ships, a Regime O overlay type, |
| 16 | + * the #6960 delete carve-out, and the operator hatch. |
| 17 | + * |
| 18 | + * The registry double serves only `getArtifactItem`, which is all the verdict |
| 19 | + * reads; what it returns is what the real `SchemaRegistry` returns for an |
| 20 | + * artifact a code package registered (`_packageId` stamped, package |
| 21 | + * provenance). `@objectstack/objectql` cannot be imported here: it depends on |
| 22 | + * this package. |
| 23 | + */ |
| 24 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 25 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 26 | +import { resetEnvWritableMetadataTypes } from './sys-metadata-repository.js'; |
| 27 | + |
| 28 | +const PACKAGE_ID = 'com.example.pkg'; |
| 29 | + |
| 30 | +const shipped = (name: string, extra: Record<string, unknown> = {}) => |
| 31 | + ({ name, label: name, _packageId: PACKAGE_ID, _provenance: 'package', ...extra }); |
| 32 | + |
| 33 | +/** Packaged artifacts of three regimes: behavioral (`flow`), overlay (`view`), rolled-back overlay (`page`). */ |
| 34 | +const ARTIFACTS = new Map<string, Map<string, unknown>>([ |
| 35 | + ['flow', new Map([['pkg_flow', shipped('pkg_flow', { type: 'autolaunched', nodes: [], edges: [] })]])], |
| 36 | + ['view', new Map([['pkg_view', shipped('pkg_view')]])], |
| 37 | + ['page', new Map([['pkg_page', shipped('pkg_page')]])], |
| 38 | +]); |
| 39 | + |
| 40 | +function protocolOn(environmentId: string | undefined): ObjectStackProtocolImplementation { |
| 41 | + const registry = { getArtifactItem: (type: string, name: string) => ARTIFACTS.get(type)?.get(name) }; |
| 42 | + return new ObjectStackProtocolImplementation({ registry } as never, () => new Map(), environmentId); |
| 43 | +} |
| 44 | + |
| 45 | +const shape = (e: any) => (e ? { code: e.code, status: e.status } : null); |
| 46 | + |
| 47 | +afterEach(() => { |
| 48 | + delete process.env.OS_METADATA_WRITABLE; |
| 49 | + ObjectStackProtocolImplementation.resetEnvWritableCache(); |
| 50 | + resetEnvWritableMetadataTypes(); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('packagedBaseRefusal — the /meta door\'s locked-base verdict, handed to a second door', () => { |
| 54 | + for (const environmentId of [undefined, 'env_1']) { |
| 55 | + it(`refuses a packaged flow's save AND removal on ${environmentId ? 'an environment' : 'a host-config'} kernel`, () => { |
| 56 | + const p = protocolOn(environmentId); |
| 57 | + expect(shape(p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'save' }))) |
| 58 | + .toEqual({ code: 'NOT_OVERRIDABLE', status: 403 }); |
| 59 | + expect(shape(p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'delete' }))) |
| 60 | + .toEqual({ code: 'NOT_OVERRIDABLE', status: 403 }); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + it('ONE emitter: the value equals what saveMetaItem / deleteMetaItem throw for the same item', async () => { |
| 65 | + const p = protocolOn('env_1'); |
| 66 | + const thrownBy = (run: Promise<unknown>) => run.then(() => undefined, (e: any) => e); |
| 67 | + |
| 68 | + const save = await thrownBy(p.saveMetaItem({ type: 'flow', name: 'pkg_flow', item: { name: 'pkg_flow', label: 'x' } })); |
| 69 | + const saveVerdict: any = p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'save' }); |
| 70 | + expect({ ...shape(save), message: save?.message }).toEqual({ ...shape(saveVerdict), message: saveVerdict?.message }); |
| 71 | + |
| 72 | + const del = await thrownBy(p.deleteMetaItem({ type: 'flow', name: 'pkg_flow' })); |
| 73 | + const delVerdict: any = p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'delete' }); |
| 74 | + expect({ ...shape(del), message: del?.message }).toEqual({ ...shape(delVerdict), message: delVerdict?.message }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('folds the type at the producer — a plural spelling cannot address around the lock', () => { |
| 78 | + const p = protocolOn(undefined); |
| 79 | + expect(shape(p.packagedBaseRefusal({ type: 'flows', name: 'pkg_flow', operation: 'save' }))) |
| 80 | + .toEqual({ code: 'NOT_OVERRIDABLE', status: 403 }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('a name no code package ships is not locked — creation is not this verdict\'s question', () => { |
| 84 | + const p = protocolOn(undefined); |
| 85 | + expect(p.packagedBaseRefusal({ type: 'flow', name: 'customer_flow', operation: 'save' })).toBeNull(); |
| 86 | + expect(p.packagedBaseRefusal({ type: 'flow', name: 'customer_flow', operation: 'delete' })).toBeNull(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('a Regime O overlay type (allowOrgOverride) is never refused on this ground', () => { |
| 90 | + const p = protocolOn(undefined); |
| 91 | + expect(p.packagedBaseRefusal({ type: 'view', name: 'pkg_view', operation: 'save' })).toBeNull(); |
| 92 | + expect(p.packagedBaseRefusal({ type: 'view', name: 'pkg_view', operation: 'delete' })).toBeNull(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('mirrors the #6960 carve-out: a rolled-back overlay type refuses the write but not the removal', () => { |
| 96 | + const p = protocolOn(undefined); |
| 97 | + expect(shape(p.packagedBaseRefusal({ type: 'page', name: 'pkg_page', operation: 'save' }))) |
| 98 | + .toEqual({ code: 'NOT_OVERRIDABLE', status: 403 }); |
| 99 | + expect(p.packagedBaseRefusal({ type: 'page', name: 'pkg_page', operation: 'delete' })).toBeNull(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('a lookup that FAILS is re-raised, never handed out as a verdict', () => { |
| 103 | + // The lifted helpers throw, as the inline code did; only the refusal is |
| 104 | + // turned into a value. A registry that cannot answer must not become a |
| 105 | + // well-formed "refused" (or "allowed") — it stays the fault it is. |
| 106 | + const registry = { getArtifactItem: () => { throw new Error('registry unreadable'); } }; |
| 107 | + const p = new ObjectStackProtocolImplementation({ registry } as never, () => new Map(), undefined); |
| 108 | + expect(() => p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'save' })) |
| 109 | + .toThrow('registry unreadable'); |
| 110 | + expect(() => p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'delete' })) |
| 111 | + .toThrow('registry unreadable'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('reads the operator hatch through the same predicate the metadata door reads', () => { |
| 115 | + process.env.OS_METADATA_WRITABLE = 'flow'; |
| 116 | + ObjectStackProtocolImplementation.resetEnvWritableCache(); |
| 117 | + const p = protocolOn(undefined); |
| 118 | + expect(p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'save' })).toBeNull(); |
| 119 | + expect(p.packagedBaseRefusal({ type: 'flow', name: 'pkg_flow', operation: 'delete' })).toBeNull(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments