|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Ruling 6677709817, step (c): a `sys_metadata_history` snapshot written by |
| 5 | + * `SysMetadataRepository.put` withholds every position the type's redactor |
| 6 | + * marks withheld AND not carried forward — the connector's undeclared `auth` |
| 7 | + * keys the save door drains from the live row — so no new history row carries |
| 8 | + * such a value, whichever write produced the body. |
| 9 | + * |
| 10 | + * `put` is the one history writer behind the `/meta` save door, publish |
| 11 | + * (`promoteDraft`) and revert (`restoreVersion`). The save door already drains |
| 12 | + * the body it hands `put`, so the bodies that still reach `put` holding such a |
| 13 | + * key are the ones no door re-judged: a direct `put`, and a revert to a |
| 14 | + * snapshot written before this step. Both are pinned below. The live row is |
| 15 | + * NOT asserted on: the ruling binds what history stores. |
| 16 | + * |
| 17 | + * The placeholder below is a fixture string, not a credential. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 21 | +import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate } from '@objectstack/metadata-core'; |
| 22 | +import { SysMetadataRepository } from './sys-metadata-repository.js'; |
| 23 | + |
| 24 | +interface Row { [k: string]: unknown } |
| 25 | + |
| 26 | +const LEGACY_AUTH_VALUE = 'TEST_PLACEHOLDER_LEGACY_AUTH_VALUE'; |
| 27 | + |
| 28 | +/** Minimal engine fake that stores exactly what it is handed (the recorded-by suite's shape). */ |
| 29 | +function makeFakeEngine() { |
| 30 | + const rows = new Map<string, Row>(); |
| 31 | + const historyRows: Row[] = []; |
| 32 | + |
| 33 | + const keyOf = (w: Record<string, unknown>) => |
| 34 | + `${String(w.type)}|${String(w.name)}|${String(w.organization_id ?? 'null')}|${String(w.state ?? 'active')}`; |
| 35 | + |
| 36 | + const findRow = (where: Record<string, unknown>) => { |
| 37 | + if (where.id !== undefined) { |
| 38 | + for (const [k, r] of rows) if (r.id === where.id) return { key: k, row: r }; |
| 39 | + return null; |
| 40 | + } |
| 41 | + const k = keyOf(where); |
| 42 | + const r = rows.get(k); |
| 43 | + return r ? { key: k, row: r } : null; |
| 44 | + }; |
| 45 | + |
| 46 | + const matchesHistory = (h: Row, where: Record<string, unknown>): boolean => |
| 47 | + Object.entries(where).every(([k, v]) => { |
| 48 | + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); |
| 49 | + return v === undefined || h[k] === v; |
| 50 | + }); |
| 51 | + |
| 52 | + return { |
| 53 | + rows, |
| 54 | + historyRows, |
| 55 | + async find(table: string, opts: { where: Record<string, unknown>; limit?: number }) { |
| 56 | + const found = table === 'sys_metadata_history' |
| 57 | + ? historyRows.filter((h) => matchesHistory(h, opts.where)) |
| 58 | + : Array.from(rows.values()).filter((r) => { |
| 59 | + if (opts.where.type && r.type !== opts.where.type) return false; |
| 60 | + if (opts.where.organization_id !== undefined && r.organization_id !== opts.where.organization_id) return false; |
| 61 | + if (opts.where.state && r.state !== opts.where.state) return false; |
| 62 | + return true; |
| 63 | + }); |
| 64 | + // The caller's bound, applied after the filter (check:objectql-double-limit). |
| 65 | + return typeof opts?.limit === 'number' ? found.slice(0, opts.limit) : found; |
| 66 | + }, |
| 67 | + async findOne(table: string, opts: { where: Record<string, unknown> }) { |
| 68 | + assertEngineFindOnePredicate(table, opts); |
| 69 | + if (table === 'sys_metadata_history') return historyRows.find((h) => matchesHistory(h, opts.where)) ?? null; |
| 70 | + return findRow(opts.where)?.row ?? null; |
| 71 | + }, |
| 72 | + async insert(table: string, data: Record<string, unknown>) { |
| 73 | + if (table === 'sys_metadata_history') { |
| 74 | + const h: Row = { ...data }; |
| 75 | + if (!h.id) h.id = `h_${historyRows.length + 1}`; |
| 76 | + historyRows.push(h); |
| 77 | + return { id: h.id as string }; |
| 78 | + } |
| 79 | + const k = keyOf(data); |
| 80 | + const row: Row = { id: `r_${rows.size + 1}`, ...data }; |
| 81 | + rows.set(k, row); |
| 82 | + return { id: row.id as string }; |
| 83 | + }, |
| 84 | + async update(_t: string, data: Record<string, unknown>, opts: { where: Record<string, unknown> }) { |
| 85 | + assertEngineUpdateDispatch(data, opts); |
| 86 | + const found = findRow(opts.where); |
| 87 | + if (!found) throw new Error('not found'); |
| 88 | + rows.set(found.key, { ...found.row, ...data }); |
| 89 | + return { id: found.row.id as string }; |
| 90 | + }, |
| 91 | + async delete(_t: string, opts: { where: Record<string, unknown> }) { |
| 92 | + assertEngineDeleteDispatch(opts); |
| 93 | + const found = findRow(opts.where); |
| 94 | + if (!found) return { deleted: 0 }; |
| 95 | + rows.delete(found.key); |
| 96 | + return { deleted: 1 }; |
| 97 | + }, |
| 98 | + async transaction<T>(cb: (ctx: any, info: { owned: boolean }) => Promise<T>): Promise<T> { return cb(undefined, { owned: true }); }, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +/** A connector whose `auth` holds a key its `bearer` kind does not declare. */ |
| 103 | +function legacyConnector(label = 'Vendor API'): Row { |
| 104 | + return { |
| 105 | + name: 'vendor_api', |
| 106 | + label, |
| 107 | + type: 'api', |
| 108 | + provider: 'rest', |
| 109 | + // A DECLARED credential position (withheld on read, carried forward on save): kept in history. |
| 110 | + providerConfig: { baseUrl: 'https://api.vendor.example/v1', headers: { Accept: 'application/json' } }, |
| 111 | + auth: { type: 'bearer', credentialRef: 'VENDOR_API_TOKEN', token: LEGACY_AUTH_VALUE }, |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +const DECLARED_AUTH = { type: 'bearer', credentialRef: 'VENDOR_API_TOKEN' }; |
| 116 | + |
| 117 | +describe('[ruling 6677709817 (c)] SysMetadataRepository history snapshots withhold the drained connector keys', () => { |
| 118 | + let engine: ReturnType<typeof makeFakeEngine>; |
| 119 | + let repo: SysMetadataRepository; |
| 120 | + // `connector` is not in the repository's typed `MetaRef` union; the protocol |
| 121 | + // builds its refs the same way (`as Parameters<typeof repo.put>[0]`). |
| 122 | + const ref = { org: 'org_alpha', type: 'connector', name: 'vendor_api' } as unknown as Parameters<SysMetadataRepository['put']>[0]; |
| 123 | + |
| 124 | + beforeEach(() => { |
| 125 | + engine = makeFakeEngine(); |
| 126 | + repo = new SysMetadataRepository({ engine, orgLabel: 'org_alpha' }); |
| 127 | + }); |
| 128 | + |
| 129 | + // `runtime-only`: a connector is a runtime-created item here, not an override |
| 130 | + // of one a managed package ships (which the repository seals). |
| 131 | + const snapshot = (h: Row): Row => JSON.parse(h.metadata as string) as Row; |
| 132 | + |
| 133 | + it('a put of a connector holding an undeclared `auth` key writes a snapshot without it', async () => { |
| 134 | + await repo.put(ref, legacyConnector(), { parentVersion: null, actor: null, intent: 'runtime-only' }); |
| 135 | + |
| 136 | + expect(engine.historyRows).toHaveLength(1); |
| 137 | + expect(engine.historyRows[0]!.metadata as string).not.toContain(LEGACY_AUTH_VALUE); |
| 138 | + expect(snapshot(engine.historyRows[0]!).auth).toEqual(DECLARED_AUTH); |
| 139 | + }); |
| 140 | + |
| 141 | + it('CONTROL: every declared position is kept in the snapshot exactly as written', async () => { |
| 142 | + await repo.put(ref, legacyConnector(), { parentVersion: null, actor: null, intent: 'runtime-only' }); |
| 143 | + |
| 144 | + const { auth: _auth, ...rest } = legacyConnector(); |
| 145 | + const { auth, ...snapshotRest } = snapshot(engine.historyRows[0]!); |
| 146 | + expect(auth).toEqual(DECLARED_AUTH); |
| 147 | + expect(snapshotRest).toEqual(rest); |
| 148 | + }); |
| 149 | + |
| 150 | + it('a revert to a snapshot written before this step appends a revert snapshot without it', async () => { |
| 151 | + const first = await repo.put(ref, legacyConnector(), { parentVersion: null, actor: null, intent: 'runtime-only' }); |
| 152 | + await repo.put(ref, legacyConnector('Vendor API (edited)'), { parentVersion: first.version, actor: null, intent: 'runtime-only' }); |
| 153 | + // The version-1 snapshot as a write before this step stored it: the key held. |
| 154 | + const v1 = engine.historyRows.find((h) => h.version === 1)!; |
| 155 | + v1.metadata = JSON.stringify(legacyConnector()); |
| 156 | + |
| 157 | + await repo.restoreVersion(ref, 1, { actor: null, intent: 'runtime-only' }); |
| 158 | + |
| 159 | + const revert = engine.historyRows.find((h) => h.operation_type === 'revert')!; |
| 160 | + expect(revert).toBeDefined(); |
| 161 | + expect(revert.metadata as string).not.toContain(LEGACY_AUTH_VALUE); |
| 162 | + expect(snapshot(revert).auth).toEqual(DECLARED_AUTH); |
| 163 | + }); |
| 164 | + |
| 165 | + it('CONTROL: a non-connector type\'s snapshot is byte-identical to the body written', async () => { |
| 166 | + const viewRef = { org: 'org_alpha', type: 'view' as const, name: 'case_grid' }; |
| 167 | + const body = { name: 'case_grid', label: 'Cases', object: 'case', columns: [{ field: 'name' }] }; |
| 168 | + await repo.put(viewRef, body, { parentVersion: null, actor: null, intent: 'runtime-only' }); |
| 169 | + |
| 170 | + expect(engine.historyRows).toHaveLength(1); |
| 171 | + expect(engine.historyRows[0]!.metadata).toBe(JSON.stringify(body)); |
| 172 | + }); |
| 173 | + |
| 174 | + it('CONTROL: a connector with nothing undeclared is byte-identical to the body written', async () => { |
| 175 | + const { auth: _auth, ...rest } = legacyConnector(); |
| 176 | + const body = { ...rest, auth: { ...DECLARED_AUTH } }; |
| 177 | + await repo.put(ref, body, { parentVersion: null, actor: null, intent: 'runtime-only' }); |
| 178 | + |
| 179 | + expect(engine.historyRows[0]!.metadata).toBe(JSON.stringify(body)); |
| 180 | + }); |
| 181 | +}); |
0 commit comments