Files
core/test/router-parity/branches.ts
T
chrnx a47c6564ab test(router): add owner-requested parity cases and a shared-prefix generator
Additions requested in CARD-08 review, captured from ed47836 like the rest of
the corpus and frozen in their own files, so the first capture is untouched.

- Backtracking: a static branch failing one level down, at depth 5 (and a
  param branch at depth 3), and with a constrained param on the fallback.
- Cross-pattern Allow: GET :id plus POST b, with and without DELETE :rest*,
  at matcher level and through app.fetch and Node.
- Encoded slashes in plain and constrained params. A constraint cannot itself
  contain a slash: the pattern splits there and is rejected as malformed.
- Zero-segment catch-alls without an exact sibling, and a root catch-all.
- The mesh HTTP table across a peer's lifetime. It is built once at boot and
  never changes: a peer that leaves keeps its routes (503 on hit, 405 Allow
  unchanged) and a peer that arrives after boot is never spliced in (404).
  CARD-10 can reuse this for its no-rebuild-per-request check.
- router-parity-gen-v2, same seed 10908, forces static, param, constrained
  and catch-all siblings under shared prefixes at depths 1-4. branches.ts
  measures the effect and the result is frozen with the fixture: 71.9% of v2
  requests reach sibling branches and 67.3% backtrack, against 36.5% and
  26.7% for v1. v1 and its fixture are unchanged.

Refs #109

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-10 17:27:11 -05:00

95 lines
3.4 KiB
TypeScript

/**
* Measures how much of a corpus exercises sibling branches, so "the generator covers backtracking"
* is a number rather than a hope. Method-agnostic: a tree is walked by path before a method is
* chosen, so any declaration that shares the walk counts.
*
* - sibling hit: two declarations both match the request's leading segments through a segment
* where their patterns differ — the lookup has two branches to choose between at that point.
* - backtrack: a sibling hit where at least one of those declarations then fails to match the
* whole path — a branch the lookup walks into and has to leave.
*/
import { compilePattern, normalizeRequestPath, type CompiledSegment } from './oracle';
import type { Scenario } from './matcher';
function segmentMatches(segment: CompiledSegment, value: string): boolean {
if (segment.kind === 'static') return segment.value === value;
if (segment.kind === 'catchAll') return true;
const decoded = value.includes('%') ? decodeURIComponent(value) : value;
return !segment.constraint || segment.constraint.test(decoded);
}
/** Leading segments matched, and whether the whole path matched. */
function walk(pattern: string, segs: string[]): { depth: number; full: boolean } {
const { segments } = compilePattern(pattern);
for (let i = 0; i < segments.length; i++) {
if (segments[i].kind === 'catchAll') return { depth: segments.length, full: true };
if (i >= segs.length || !segmentMatches(segments[i], segs[i])) return { depth: i, full: false };
}
return { depth: segments.length, full: segments.length === segs.length };
}
const sameSegment = (a: CompiledSegment, b: CompiledSegment): boolean =>
JSON.stringify({ ...a, constraint: undefined }) === JSON.stringify({ ...b, constraint: undefined });
export interface BranchStats {
requests: number;
validPaths: number;
siblingHits: number;
backtracks: number;
siblingShare: string;
backtrackShare: string;
}
export function branchStats(scenarios: Scenario[]): BranchStats {
let requests = 0;
let validPaths = 0;
let siblingHits = 0;
let backtracks = 0;
for (const scenario of scenarios) {
const patterns = [...new Set(scenario.declarations.map((d) => d.pattern))];
for (const { raw } of scenario.requests) {
requests++;
let path: string;
try {
path = normalizeRequestPath(raw);
} catch {
continue;
}
validPaths++;
const segs = path.split('/').filter(Boolean);
const walks = patterns.map((pattern) => ({ segments: compilePattern(pattern).segments, ...walk(pattern, segs) }));
let sibling = false;
let backtrack = false;
for (let i = 0; i < walks.length; i++) {
for (let j = i + 1; j < walks.length; j++) {
const [a, b] = [walks[i], walks[j]];
const shared = Math.min(a.depth, b.depth, a.segments.length, b.segments.length);
const diverge = Array.from({ length: shared }, (_, d) => d).find(
(d) => !sameSegment(a.segments[d], b.segments[d]),
);
if (diverge === undefined) continue;
sibling = true;
if (!a.full || !b.full) backtrack = true;
}
}
if (sibling) siblingHits++;
if (backtrack) backtracks++;
}
}
const pct = (n: number) => `${((n / requests) * 100).toFixed(1)}%`;
return {
requests,
validPaths,
siblingHits,
backtracks,
siblingShare: pct(siblingHits),
backtrackShare: pct(backtracks),
};
}