test(router): freeze the differential, application, WS and boot-error corpus

The parity corpus from ROUTER-CORPUS §2, with every expected output captured
from ed47836 rather than written by hand.

Matcher: 24 hand-picked scenarios (precedence, constraint ties, exact versus
catch-all, equal-rank and same-pattern ties with reversed twins, implicit and
explicit HEAD, streaming GETs, canonical Allow, encoding, case, Unicode and
normalization) plus 40 scenarios from a fixed-seed generator
(router-parity-gen-v1, seed 10908). The generated file stores the seed, the
rules and every generated input; the test regenerates and requires a match,
so changing the generator cannot move the baseline. Each scenario runs through
the oracle and the live router and both must equal the capture, compared by
declaration ID, decoded params, implicitHead, ordered Allow, or the
normalization error.

Application level: status, Allow, headers and body through app.fetch and a
Node socket, including HEAD body suppression, 400/404/405 and OPTIONS.

WebSocket, separate from HTTP: first-registered selection, an HTTP route at the
same path not taking the upgrade, and the reserved mesh control path, through
app.upgrade and the Node listener. This freezes one adapter disagreement as
observed: a user @Ws('/__mesh__/control') on an app without mesh is refused by
app.upgrade (1008) and served by the Node listener.

Boot-time rejections: pattern compilation, local ambiguity and mesh splicing
(duplicate exports, same-shape exports, local-over-remote precedence with its
warning, and a remote static outranking a local parameter route).

Refs #109

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-10 16:59:45 -05:00
1 parent adce82beaa
commit c30cf7e457
18 files changed
+16595

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
/**
* Differential matcher parity (CARD-08 → CARD-07, #109).
*
* Every scenario runs through the frozen oracle and through the live `src/http/router` exports, and
* both must equal the outputs captured at the pinned revision. Today the live router *is* the
* linear matcher, so this proves the oracle is a faithful copy; once CARD-07 swaps the internals
* for a radix index behind the same exports, the same test compares oracle against radix with no
* edit here.
*/
import { describe, it, expect, beforeAll } from 'vitest';
import * as live from '../../src/http/router';
import { matchWsRoute as liveMatchWsRoute } from '../../src/http/ws-core';
import type { WsRouteDef } from '../../src/http/types';
import * as oracle from './oracle';
import { runScenario, type MatcherImpl, type Outcome, type Scenario } from './matcher';
import { HAND_SCENARIOS } from './cases';
import { generateScenarios, GENERATOR_RULES, GENERATOR_SEED, GENERATOR_VERSION } from './generator';
import { frozen, expectFrozenEqual } from './frozen';
const LIVE: MatcherImpl = live;
const ORACLE: MatcherImpl = oracle;
interface CapturedScenario extends Scenario {
outcomes: Outcome[];
}
interface GeneratedFile {
generator: string;
seed: number;
rules: string[];
scenarios: CapturedScenario[];
}
const capture = (scenarios: Scenario[]): CapturedScenario[] =>
scenarios.map((scenario) => ({ ...scenario, outcomes: runScenario(LIVE, scenario) }));
function checkScenario(scenario: Scenario, expected: CapturedScenario | undefined): void {
expect(expected, `${scenario.id} is missing from the frozen fixture`).toBeDefined();
const { outcomes, ...inputs } = expected!;
expectFrozenEqual(scenario, inputs, `${scenario.id} inputs`);
expectFrozenEqual(runScenario(ORACLE, scenario), outcomes, `${scenario.id} oracle`);
expectFrozenEqual(runScenario(LIVE, scenario), outcomes, `${scenario.id} live router`);
}
describe('router parity: hand-picked matcher cases', () => {
let expected: Map<string, CapturedScenario>;
beforeAll(async () => {
const data = await frozen('matcher-hand', () => capture(HAND_SCENARIOS));
expect(data.map((scenario) => scenario.id)).toEqual(HAND_SCENARIOS.map((scenario) => scenario.id));
expected = new Map(data.map((scenario) => [scenario.id, scenario]));
});
for (const scenario of HAND_SCENARIOS) {
it(`${scenario.id}: ${scenario.note}`, () => checkScenario(scenario, expected.get(scenario.id)));
}
});
describe(`router parity: generated matcher cases (${GENERATOR_VERSION}, seed ${GENERATOR_SEED})`, () => {
const generated = generateScenarios(GENERATOR_SEED);
let expected: Map<string, CapturedScenario>;
beforeAll(async () => {
const data = await frozen<GeneratedFile>('matcher-generated', () => ({
generator: GENERATOR_VERSION,
seed: GENERATOR_SEED,
rules: GENERATOR_RULES,
scenarios: capture(generated),
}));
expect({ generator: data.generator, seed: data.seed, rules: data.rules }).toEqual({
generator: GENERATOR_VERSION,
seed: GENERATOR_SEED,
rules: GENERATOR_RULES,
});
expect(data.scenarios.map((scenario) => scenario.id)).toEqual(generated.map((scenario) => scenario.id));
expected = new Map(data.scenarios.map((scenario) => [scenario.id, scenario]));
});
for (const scenario of generated) {
it(`${scenario.id}`, () => checkScenario(scenario, expected.get(scenario.id)));
}
});
describe('router parity: WebSocket matcher (first registered wins)', () => {
// WS selection is a separate contract from HTTP: no specificity ranking, registration order only.
// Paths are fed already normalized, as both upgrade adapters do since #117.
type WsDecl = WsRouteDef & { id: string };
type WsMatcher = (
routes: WsRouteDef[],
path: string,
) => { def: WsRouteDef; params: Record<string, string> | undefined } | undefined;
const ws = (id: string, pattern: string): WsDecl => ({ id, pattern, open: async () => (async function* () {})() });
const overlap = [ws('param', '/ws/:id'), ws('static', '/ws/static'), ws('rest', '/ws/:rest*')];
const scenarios = [
{ id: 'W01-overlap', routes: overlap },
{ id: 'W02-overlap-reversed', routes: [...overlap].reverse() },
{ id: 'W03-catch-all-first', routes: [ws('rest', '/w/:rest*'), ws('exact', '/w')] },
];
const paths = ['/ws/static', '/ws/x', '/ws/a%20b', '/ws', '/ws/x/y', '/w', '/w/a%2Fb/c', '/nope'];
const run = (matchWs: WsMatcher) =>
scenarios.map(({ id, routes }) => ({
id,
declarations: routes.map((route) => ({ id: route.id, pattern: route.pattern })),
outcomes: paths.map((path) => {
const hit = matchWs(routes, path);
return { path, route: hit ? { id: (hit.def as WsDecl).id, params: hit.params } : null };
}),
}));
it('oracle and live matchWsRoute equal the frozen capture', async () => {
const expected = await frozen('matcher-ws', () => run(liveMatchWsRoute));
expectFrozenEqual(run(oracle.matchWsRoute), expected, 'ws oracle');
expectFrozenEqual(run(liveMatchWsRoute), expected, 'ws live');
});
});