test(router): run the parity differential on Deno, Bun and workerd

The matcher comparison does not depend on the runtime, so it should run on every
runtime core supports. Only the Node suite ran it before.

The comparison and the app.fetch fixtures move into differential.ts and
app-fixture.ts, which use neither vitest nor a file API. Each runtime passes in
a loader for the frozen JSON. The vitest files use the same modules, plus a
Node check that the neutral runners report no mismatch, so the two paths cannot
drift.

- Deno and Bun: test/{deno,bun}/router-parity.test.ts run all 1158 frozen
  matcher cases (hand-picked, owner, generated v1 and v2, WS) through the oracle
  and the live router, plus the 55 app.fetch cases.
- workerd: test/edge/parity-worker.ts bundles the frozen JSON and runs the
  same checks; test/edge/run.ts adds the phase after its existing assertions.

The Node-socket, WS-upgrade, boot and mesh captures stay Node-only: they need
http.request, the ws client, or a mesh peer on a port.

Refs #109

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-10 17:38:36 -05:00
1 parent a4562b2b23
commit 684795a2de
11 files changed
+553 -247

No files matched your search

+14 -30
View File
@@ -7,10 +7,10 @@
* for a radix index behind the same exports, the same test compares oracle against radix with no
* edit here.
*/
import { readFileSync } from 'fs';
import { join } from 'path';
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, OWNER_SCENARIOS } from './cases';
@@ -25,6 +25,7 @@ import {
GENERATOR_V2_VERSION,
} from './generator';
import { frozen, expectFrozenEqual } from './frozen';
import { runDifferential, runWsMatcher, LIVE_WS_MATCHER, ORACLE_WS_MATCHER } from './differential';
const LIVE: MatcherImpl = live;
const ORACLE: MatcherImpl = oracle;
@@ -139,34 +140,17 @@ describe(`router parity: generated shared-prefix cases (${GENERATOR_V2_VERSION},
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');
const expected = await frozen('matcher-ws', () => runWsMatcher(LIVE_WS_MATCHER));
expectFrozenEqual(runWsMatcher(ORACLE_WS_MATCHER), expected, 'ws oracle');
expectFrozenEqual(runWsMatcher(LIVE_WS_MATCHER), expected, 'ws live');
});
});
describe('router parity: runtime-neutral differential (the one Deno, Bun and workerd run)', () => {
it('reports no mismatch on Node', async () => {
const dir = join(__dirname, 'frozen');
const report = await runDifferential((name) => JSON.parse(readFileSync(join(dir, `${name}.json`), 'utf8')));
expect(report.mismatches).toEqual([]);
});
});