CARD-07 replaces the linear HTTP matcher with a radix index. Before any of that code changes, the behaviour it must preserve has to exist somewhere the new code cannot quietly move it. manifest.json names the existing tests retained unchanged (ROUTER-CORPUS §1), pinned toed47836instead of42376c6: #117 and #122 landed after the corpus was written. Every named file and test was verified to exist and pass there; none was renamed. Three upgrade-path tests from #117 are listed separately as proposed additions, pending D5. manifest.test.ts fails if a named test disappears. oracle.ts is a verbatim copy of the matcher ated47836(matchRoute, resolveRoute, allowedMethods, normalizeRequestPath, compilePattern, matchWsRoute), with local route types and its own caches. Its header says it must not be edited to fit a new implementation, and the manifest test fails if anything under src/ imports it. frozen.ts reads the captured baseline. It refuses `vitest -u`, and the capture switch only fills a missing file, never overwrites one. Refs #109 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
70 lines
3.1 KiB
TypeScript
70 lines
3.1 KiB
TypeScript
/**
|
|
* 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<T> {
|
|
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<T>(name: string, capture: () => T | Promise<T>): Promise<T> {
|
|
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<T> = { revision: PINNED_REVISION, data };
|
|
writeFileSync(file, `${JSON.stringify(record, null, 2)}\n`);
|
|
return data;
|
|
}
|
|
|
|
const record = JSON.parse(readFileSync(file, 'utf8')) as FrozenFile<T>;
|
|
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));
|
|
}
|