/** * Reads the frozen router-parity baseline. Read README.md in this directory before touching it. * * Every expected output under `frozen/` was captured from the pinned revision below, by running * these tests with `ROUTER_PARITY_CAPTURE=1` while the file did not exist yet. Nothing here ever * overwrites a file: a capture only fills a gap, and `vitest -u` is refused outright, because a * baseline that a flag can rewrite is a baseline a new implementation can quietly move. */ import { existsSync, readFileSync, writeFileSync } from 'fs'; import { join } from 'path'; import { expect } from 'vitest'; /** The `@green-tea/core` commit every frozen output was captured from (develop after #117 and #122). */ export const PINNED_REVISION = 'ed47836c9820c47bf4ed5e902d91e019abdb110a'; const FROZEN_DIR = join(__dirname, 'frozen'); interface FrozenFile { revision: string; data: T; } function refuseSnapshotUpdate(name: string): void { // `_updateSnapshot` is vitest's private record of `-u` ('all') versus a normal run ('new'/'none'). const state = expect.getState().snapshotState as unknown as { _updateSnapshot?: string } | undefined; if (state?._updateSnapshot !== 'all') return; throw new Error( `router-parity: '${name}' is a frozen baseline captured at ${PINNED_REVISION} and is never ` + 'rewritten by `vitest -u`. A behaviour difference against it is a compatibility failure. ' + 'If the change is deliberate, it needs its own maintainer approval and a reviewed edit of ' + `test/router-parity/frozen/${name}.json — see test/router-parity/README.md.`, ); } /** * Returns the frozen data for `name`. When the file is missing it fails, unless * `ROUTER_PARITY_CAPTURE=1` is set, in which case `capture()` runs against the current code and its * result is written. An existing file is never replaced. */ export async function frozen(name: string, capture: () => T | Promise): Promise { refuseSnapshotUpdate(name); const file = join(FROZEN_DIR, `${name}.json`); if (!existsSync(file)) { if (process.env.ROUTER_PARITY_CAPTURE !== '1') throw new Error( `router-parity: frozen file ${file} is missing — the baseline cannot be regenerated by a test run`, ); const data = JSON.parse(JSON.stringify(await capture())) as T; const record: FrozenFile = { revision: PINNED_REVISION, data }; writeFileSync(file, `${JSON.stringify(record, null, 2)}\n`); return data; } const record = JSON.parse(readFileSync(file, 'utf8')) as FrozenFile; expect(record.revision, `${name}.json was captured from a different revision`).toBe(PINNED_REVISION); return record.data; } /** * Structural equality that also holds array order and object key order. `toEqual` alone ignores key * order, which is observable in `openapi().paths` and in serialized introspection output. */ export function expectFrozenEqual(actual: unknown, expected: unknown, label: string): void { const plain = JSON.parse(JSON.stringify(actual)); expect(plain, label).toEqual(expected); expect(JSON.stringify(plain), `${label} (key and array order)`).toBe(JSON.stringify(expected)); }