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>
108 lines
3.3 KiB
TypeScript
108 lines
3.3 KiB
TypeScript
/**
|
|
* The differential matcher harness: one ordered declaration array and one request list, run through
|
|
* any implementation of the router's lookup functions, reduced to what a caller can observe —
|
|
* which declaration answered (by stable ID, never by pattern text), its decoded params, whether HEAD
|
|
* was implicit, the ordered `Allow` list, and the normalization error if the path never got that far.
|
|
*/
|
|
import type { RouteDef } from '../../src/http/types';
|
|
|
|
export type Transport = 'buffer' | 'sse' | 'ndjson' | 'negotiate';
|
|
|
|
export interface Declaration {
|
|
id: string;
|
|
method: string;
|
|
pattern: string;
|
|
transport: Transport;
|
|
}
|
|
|
|
export interface Request {
|
|
method: string;
|
|
raw: string;
|
|
}
|
|
|
|
export interface Scenario {
|
|
id: string;
|
|
note: string;
|
|
declarations: Declaration[];
|
|
requests: Request[];
|
|
}
|
|
|
|
/** The lookup surface CARD-07 replaces. The live router and the frozen oracle both satisfy it. */
|
|
export interface MatcherImpl {
|
|
normalizeRequestPath(path: string): string;
|
|
matchRoute(
|
|
routes: RouteDef[],
|
|
method: string,
|
|
path: string,
|
|
): { def: unknown; params: Record<string, string> } | undefined;
|
|
resolveRoute(
|
|
routes: RouteDef[],
|
|
method: string,
|
|
path: string,
|
|
): { def: unknown; params: Record<string, string>; implicitHead: boolean } | undefined;
|
|
allowedMethods(routes: RouteDef[], path: string): string[];
|
|
}
|
|
|
|
export interface ErrorOutcome {
|
|
name: string;
|
|
message: string;
|
|
}
|
|
|
|
export interface Outcome {
|
|
method: string;
|
|
raw: string;
|
|
normalized?: string;
|
|
error?: ErrorOutcome;
|
|
match?: { id: string; params: Record<string, string> } | null;
|
|
resolve?: { id: string; params: Record<string, string>; implicitHead: boolean } | null;
|
|
allow?: string[];
|
|
}
|
|
|
|
export function errorOutcome(error: unknown): ErrorOutcome {
|
|
const err = error as Error;
|
|
return { name: err.name, message: err.message };
|
|
}
|
|
|
|
/** Builds fresh `RouteDef` objects for one run, so no implementation can see another's cached table. */
|
|
function buildRoutes(declarations: Declaration[]): { routes: RouteDef[]; idOf: Map<unknown, string> } {
|
|
const idOf = new Map<unknown, string>();
|
|
const routes = declarations.map((decl): RouteDef => {
|
|
const def: RouteDef = {
|
|
method: decl.method,
|
|
pattern: decl.pattern,
|
|
transport: decl.transport,
|
|
handler: async () => ({ status: 200, headers: {}, body: decl.id }),
|
|
};
|
|
idOf.set(def, decl.id);
|
|
return def;
|
|
});
|
|
return { routes, idOf };
|
|
}
|
|
|
|
export function runScenario(impl: MatcherImpl, scenario: Scenario): Outcome[] {
|
|
const { routes, idOf } = buildRoutes(scenario.declarations);
|
|
const id = (def: unknown): string => idOf.get(def) ?? '<unknown declaration>';
|
|
|
|
return scenario.requests.map(({ method, raw }): Outcome => {
|
|
let normalized: string;
|
|
|
|
try {
|
|
normalized = impl.normalizeRequestPath(raw);
|
|
} catch (error) {
|
|
return { method, raw, error: errorOutcome(error) };
|
|
}
|
|
|
|
const match = impl.matchRoute(routes, method, normalized);
|
|
const resolved = impl.resolveRoute(routes, method, normalized);
|
|
|
|
return {
|
|
method,
|
|
raw,
|
|
normalized,
|
|
match: match ? { id: id(match.def), params: match.params } : null,
|
|
resolve: resolved ? { id: id(resolved.def), params: resolved.params, implicitHead: resolved.implicitHead } : null,
|
|
allow: impl.allowedMethods(routes, normalized),
|
|
};
|
|
});
|
|
}
|