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>
This commit is contained in:
10 files changed
+11835
-3
No files matched your search
@@ -119,3 +119,93 @@ export function generateScenarios(seed: number = GENERATOR_SEED): Scenario[] {
|
||||
return { id, note: `generated by ${GENERATOR_VERSION}, seed ${seed}`, declarations, requests };
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Version 2: the same seed and helpers, plus rules that force shared prefixes, so a meaningful share
|
||||
* of cases puts static, param, constrained and catch-all siblings under one prefix and makes the
|
||||
* lookup leave a branch it has already walked into. Frozen in `frozen/matcher-generated-v2.json`;
|
||||
* v1 above and its frozen file are unchanged.
|
||||
*/
|
||||
export const GENERATOR_V2_VERSION = 'router-parity-gen-v2';
|
||||
|
||||
export const GENERATOR_V2_RULES = [
|
||||
'PRNG: mulberry32(seed), fresh for v2; every choice below draws from it in program order.',
|
||||
'40 scenarios; each has a shared static prefix of 0-3 segments (a|b|c|x), so siblings sit at depth 1-4.',
|
||||
'Each scenario has 3-6 sibling declarations: prefix + one sibling segment + a tail. The first sibling segment is ' +
|
||||
'static, the second a plain or constrained param; the rest are static (p=0.35), plain (p=0.25), constrained ' +
|
||||
'(p=0.25) or a final catch-all (p=0.15). Tails are 0-2 segments, static/plain/constrained, none after a catch-all.',
|
||||
'Then 0-3 unrelated declarations made by the v1 declaration rule.',
|
||||
'Methods and transports follow the v1 declaration rule.',
|
||||
'Requests (12): p=0.5 instantiate a sibling; p=0.25 cross two siblings (prefix and sibling segment of one, tail ' +
|
||||
'of another) to force a partial match; else prefix + 0-3 pool values. v1 decoration and request methods apply.',
|
||||
'Unsupported patterns are never produced: every generated pattern must pass compilePattern.',
|
||||
];
|
||||
|
||||
function siblingSegment(rng: Rng, depth: number, index: number): string {
|
||||
if (index === 0) return pick(rng, STATICS);
|
||||
if (index === 1) return rng() < 0.5 ? `:p${depth}` : `:p${depth}(${pick(rng, CONSTRAINTS)})`;
|
||||
const roll = rng();
|
||||
if (roll < 0.35) return pick(rng, STATICS);
|
||||
if (roll < 0.6) return `:p${depth}`;
|
||||
if (roll < 0.85) return `:p${depth}(${pick(rng, CONSTRAINTS)})`;
|
||||
return `:p${depth}*`;
|
||||
}
|
||||
|
||||
function siblingDeclaration(rng: Rng, id: string, prefix: string[], index: number): Declaration {
|
||||
const sibling = siblingSegment(rng, prefix.length, index);
|
||||
const segments = [...prefix, sibling];
|
||||
if (!sibling.endsWith('*')) {
|
||||
const tail = int(rng, 0, 2);
|
||||
for (let t = 0; t < tail; t++) {
|
||||
const depth = segments.length;
|
||||
const roll = rng();
|
||||
segments.push(
|
||||
roll < 0.5 ? pick(rng, STATICS) : roll < 0.75 ? `:p${depth}` : `:p${depth}(${pick(rng, CONSTRAINTS)})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const method = pick(rng, DECL_METHODS);
|
||||
const transport: Transport = method === 'GET' && rng() >= 0.7 ? pick(rng, STREAMING) : 'buffer';
|
||||
const pattern = `/${segments.join('/')}`;
|
||||
compilePattern(pattern);
|
||||
return { id, method, pattern, transport };
|
||||
}
|
||||
|
||||
function siblingRequest(rng: Rng, prefixLength: number, siblings: Declaration[]): Request {
|
||||
const roll = rng();
|
||||
let segments: string[];
|
||||
|
||||
if (roll < 0.5) {
|
||||
segments = instantiate(rng, pick(rng, siblings));
|
||||
} else if (roll < 0.75) {
|
||||
const head = instantiate(rng, pick(rng, siblings)).slice(0, prefixLength + 1);
|
||||
const tail = instantiate(rng, pick(rng, siblings)).slice(prefixLength + 1);
|
||||
segments = [...head, ...tail];
|
||||
} else {
|
||||
const prefix = instantiate(rng, siblings[0]).slice(0, prefixLength);
|
||||
segments = [...prefix, ...Array.from({ length: int(rng, 0, 3) }, () => pick(rng, VALUES))];
|
||||
}
|
||||
|
||||
return { method: pick(rng, REQUEST_METHODS), raw: decorate(rng, segments) };
|
||||
}
|
||||
|
||||
export function generateScenariosV2(seed: number = GENERATOR_SEED): Scenario[] {
|
||||
const rng = mulberry32(seed);
|
||||
|
||||
return Array.from({ length: 40 }, (_, index): Scenario => {
|
||||
const id = `S${String(index + 1).padStart(2, '0')}`;
|
||||
const prefix = Array.from({ length: int(rng, 0, 3) }, () => pick(rng, STATICS));
|
||||
const siblings = Array.from({ length: int(rng, 3, 6) }, (_, s) =>
|
||||
siblingDeclaration(rng, `${id}.s${s + 1}`, prefix, s),
|
||||
);
|
||||
const unrelated = Array.from({ length: int(rng, 0, 3) }, (_, u) => declaration(rng, `${id}.u${u + 1}`));
|
||||
const requests = Array.from({ length: 12 }, () => siblingRequest(rng, prefix.length, siblings));
|
||||
return {
|
||||
id,
|
||||
note: `generated by ${GENERATOR_V2_VERSION}, seed ${seed}, shared prefix /${prefix.join('/')}`,
|
||||
declarations: [...siblings, ...unrelated],
|
||||
requests,
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user