|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `announceOpenMigrationGates` against the REAL registry (ADR-0104 D1 rollout / |
| 5 | + * #3438, correcting #4235 and #4253). |
| 6 | + * |
| 7 | + * The announcement itself is covered in `engine.test.ts`, which mocks |
| 8 | + * `./registry` away. That mock is why these cases live in their own file: the |
| 9 | + * genuine `registerApp → registry` path INJECTS covered-type fields into every |
| 10 | + * object it registers, and a mocked registry hands the engine exactly the |
| 11 | + * fields the test wrote. The two defects pinned below are both invisible under |
| 12 | + * the mock and unconditional in production, so the suite that can see them has |
| 13 | + * to build its objects the way a deployment does. |
| 14 | + * |
| 15 | + * The risk with any such line is not silence, it is speaking where the line is |
| 16 | + * untrue or useless — that trains readers to ignore it. So each silent case is |
| 17 | + * pinned, not just the speaking one. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 21 | +import { ObjectQL } from './engine'; |
| 22 | +import type { IDataDriver } from '@objectstack/spec/contracts'; |
| 23 | + |
| 24 | +/** Minimal driver whose `find` serves whatever rows the test seeded. */ |
| 25 | +function makeDriver(seed: Record<string, Array<Record<string, unknown>>> = {}): IDataDriver { |
| 26 | + const store = new Map<string, Array<Record<string, unknown>>>(Object.entries(seed)); |
| 27 | + return { |
| 28 | + name: 'default', |
| 29 | + version: '1.0.0', |
| 30 | + async connect() {}, |
| 31 | + async disconnect() {}, |
| 32 | + async find(object: string) { return store.get(object) ?? []; }, |
| 33 | + async findOne() { return null; }, |
| 34 | + async count() { return 0; }, |
| 35 | + async create(_o: string, data: any) { return data; }, |
| 36 | + async update(_o: string, id: string, data: any) { return { ...data, id }; }, |
| 37 | + async delete() { return true; }, |
| 38 | + async bulkCreate(_o: string, rows: any[]) { return rows; }, |
| 39 | + async syncSchema() {}, |
| 40 | + async dropTable() {}, |
| 41 | + } as unknown as IDataDriver; |
| 42 | +} |
| 43 | + |
| 44 | +const COVERED = { |
| 45 | + name: 'contact', |
| 46 | + fields: { |
| 47 | + id: { type: 'text' }, |
| 48 | + account: { type: 'lookup', reference: 'account' }, |
| 49 | + geo: { type: 'location' }, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +/** Declares no covered field of its own — but the registry will inject four. */ |
| 54 | +const UNCOVERED = { |
| 55 | + name: 'note', |
| 56 | + fields: { id: { type: 'text' }, body: { type: 'textarea' } }, |
| 57 | +}; |
| 58 | + |
| 59 | +/** The `sys_migration` shape the flag reader needs to find a row at all. */ |
| 60 | +const FLAG_OBJECT = { |
| 61 | + name: 'sys_migration', |
| 62 | + fields: { |
| 63 | + id: { type: 'text' }, |
| 64 | + last_run_at: { type: 'datetime' }, |
| 65 | + verified_at: { type: 'datetime' }, |
| 66 | + blocking: { type: 'number' }, |
| 67 | + }, |
| 68 | +}; |
| 69 | + |
| 70 | +function makeEngine(opts: { |
| 71 | + objects?: any[]; |
| 72 | + flagRows?: Array<Record<string, unknown>>; |
| 73 | +} = {}) { |
| 74 | + const engine = new ObjectQL(); |
| 75 | + engine.registerDriver( |
| 76 | + makeDriver(opts.flagRows ? { sys_migration: opts.flagRows } : {}), |
| 77 | + true, |
| 78 | + ); |
| 79 | + engine.registerApp({ |
| 80 | + id: 'advisory_pkg', |
| 81 | + name: 'Advisory', |
| 82 | + objects: opts.objects ?? [COVERED, UNCOVERED], |
| 83 | + } as any); |
| 84 | + return engine; |
| 85 | +} |
| 86 | + |
| 87 | +/** Everything the announcement logged, as one string. */ |
| 88 | +async function announce(engine: ObjectQL): Promise<string> { |
| 89 | + const info = vi.spyOn((engine as any).logger, 'info'); |
| 90 | + await engine.announceOpenMigrationGates(); |
| 91 | + return info.mock.calls.map((c: any[]) => String(c[0])).join('\n'); |
| 92 | +} |
| 93 | + |
| 94 | +afterEach(() => { |
| 95 | + vi.restoreAllMocks(); |
| 96 | + delete process.env.OS_DATA_VALUE_SHAPE_STRICT_ENABLED; |
| 97 | + delete process.env.OS_ALLOW_LAX_VALUE_SHAPES; |
| 98 | + delete process.env.OS_ALLOW_LAX_MEDIA_VALUES; |
| 99 | +}); |
| 100 | + |
| 101 | +describe('announceOpenMigrationGates, real registry (ADR-0104 D1 / #3438)', () => { |
| 102 | + it('names the command that ends warn mode on a warn-first deployment', async () => { |
| 103 | + expect(await announce(makeEngine())).toMatch(/os migrate value-shapes/); |
| 104 | + }); |
| 105 | + |
| 106 | + it('stays silent when no object declares a covered field — the gate is moot here', async () => { |
| 107 | + // The regression guard for the predicate the dormancy short-circuit counts |
| 108 | + // with. `note` declares only text, but the REGISTRY injects |
| 109 | + // `organization_id` and `owner_id` (both `system`) plus `created_by` / |
| 110 | + // `updated_by` (both in `SKIP_FIELDS`) — four `lookup`s, a covered TYPE |
| 111 | + // apiece. `validateRecord` skips every one of them before it reaches the |
| 112 | + // value-shape check, so counting by raw type answered true for literally |
| 113 | + // every object that exists: the short-circuit never fired, its WeakMap |
| 114 | + // memoized a constant, and this line fired on every deployment there is. |
| 115 | + // Counting by the validator's own `isScannableValueShapeField` — the |
| 116 | + // predicate the scanner already imports — is what makes it mean something. |
| 117 | + // |
| 118 | + // Unreachable through `engine.test.ts`: under its registry mock `note` |
| 119 | + // carries the two fields written above and nothing else. |
| 120 | + const said = await announce(makeEngine({ objects: [UNCOVERED] })); |
| 121 | + expect(said).not.toMatch(/os migrate/); |
| 122 | + }); |
| 123 | + |
| 124 | + it('stays silent under the all-class opt-in — "NOT enforced here" would be false', async () => { |
| 125 | + // `VALUE_SHAPE_STRICT()` short-circuits both `*StrictEffective` functions |
| 126 | + // ahead of the deployment flag, so enforcement is already on and the flag |
| 127 | + // this line reports on decides nothing. |
| 128 | + process.env.OS_DATA_VALUE_SHAPE_STRICT_ENABLED = '1'; |
| 129 | + expect(await announce(makeEngine())).not.toMatch(/os migrate/); |
| 130 | + }); |
| 131 | + |
| 132 | + it('stays silent under the value-shape escape hatch — the operator opted out on purpose', async () => { |
| 133 | + // Running the scan cannot change this deployment's posture, so naming it is |
| 134 | + // noise, not help. |
| 135 | + process.env.OS_ALLOW_LAX_VALUE_SHAPES = '1'; |
| 136 | + expect(await announce(makeEngine())).not.toMatch(/os migrate value-shapes/); |
| 137 | + }); |
| 138 | + |
| 139 | + it('applies each opt-out to its own class only', async () => { |
| 140 | + // The two opt-outs are per-class, so silencing one must not silence the |
| 141 | + // other. `contact` declares a `location`; `note` gets a `file` here so both |
| 142 | + // gates are live and exactly one is switched off. |
| 143 | + process.env.OS_ALLOW_LAX_MEDIA_VALUES = '1'; |
| 144 | + const said = await announce(makeEngine({ |
| 145 | + objects: [COVERED, { name: 'note', fields: { id: { type: 'text' }, doc: { type: 'file' } } }], |
| 146 | + })); |
| 147 | + expect(said).not.toMatch(/os migrate files-to-references/); |
| 148 | + expect(said).toMatch(/os migrate value-shapes/); |
| 149 | + }); |
| 150 | + |
| 151 | + it('stays silent once the gate is open (verified row)', async () => { |
| 152 | + const said = await announce(makeEngine({ |
| 153 | + objects: [COVERED, UNCOVERED, FLAG_OBJECT], |
| 154 | + flagRows: [ |
| 155 | + { |
| 156 | + id: 'adr-0104-value-shapes', |
| 157 | + last_run_at: '2026-07-30T00:00:00.000Z', |
| 158 | + verified_at: '2026-07-30T00:00:00.000Z', |
| 159 | + blocking: 0, |
| 160 | + }, |
| 161 | + ], |
| 162 | + })); |
| 163 | + expect(said).not.toMatch(/os migrate value-shapes/); |
| 164 | + }); |
| 165 | + |
| 166 | + it('still speaks when a row exists but did not pass its self-check', async () => { |
| 167 | + // A failed run is not permission — the same "absent evidence is not |
| 168 | + // permission" rule the gate itself runs on, so the advice still applies. |
| 169 | + const said = await announce(makeEngine({ |
| 170 | + objects: [COVERED, UNCOVERED, FLAG_OBJECT], |
| 171 | + flagRows: [ |
| 172 | + { |
| 173 | + id: 'adr-0104-value-shapes', |
| 174 | + last_run_at: '2026-07-30T00:00:00.000Z', |
| 175 | + verified_at: null, |
| 176 | + blocking: 3, |
| 177 | + }, |
| 178 | + ], |
| 179 | + })); |
| 180 | + expect(said).toMatch(/os migrate value-shapes/); |
| 181 | + }); |
| 182 | +}); |
0 commit comments