test(router): prove misses after index build recompile nothing (CARD-10, #109)

The linear matcher answered every 404, 405 and automatic OPTIONS by compiling
the whole route table. The radix index compiles each pattern once, when the
index for an array is built. Nothing asserted that, so a regression to
per-request compilation or per-request index builds would only show up as a
slower benchmark.

compilePattern is called inside router.ts, where a spy on the module export
cannot see it, so the router gains two internal counters (compilePattern calls,
index builds), exposed as @internal routerSeamCounts() next to walkStackSize.
router.ts is not a package entry. The new test warms the index, then repeats
404, 405 and OPTIONS misses through resolveRoute/allowedMethods, buildFetch and
app.fetch, and asserts both counts stay constant while status and Allow stay
correct.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-12 15:12:40 -05:00
1 parent 72f591dd21
commit 7a9932f095
2 files changed
+146

No files matched your search

+12
View File
@@ -139,8 +139,19 @@ function segmentShape(segment: CompiledSegment): string {
return segment.constraintSource ? `r:${segment.constraintSource}` : 'p';
}
/**
* Calls into the compile and index-build seams. `compilePattern` is called inside this module, where a
* spy on the export cannot see it, so the counts are kept here; they let a test prove that misses
* after the index is built recompile nothing and rebuild nothing (FPRD-001 CARD-10).
*/
const seamCounts = { compilePattern: 0, indexBuilds: 0 };
/** @internal Calls so far to `compilePattern` (cache hits included) and route-index builds. For tests. */
export const routerSeamCounts = (): Readonly<typeof seamCounts> => ({ ...seamCounts });
/** Compiles and validates one route pattern. Results are cached by source string. */
export function compilePattern(pattern: string): CompiledPattern {
seamCounts.compilePattern++;
const cached = compiledCache.get(pattern);
if (cached) return cached;
if (!pattern.startsWith('/')) throw new Error(`invalid route pattern '${pattern}': pattern must start with '/'`);
@@ -490,6 +501,7 @@ function routeIndex(routes: RouteDef[]): RouteIndex {
const cached = routeIndexCache.get(routes);
if (cached?.size === routes.length) return cached;
seamCounts.indexBuilds++;
const local: Array<{ def: RouteDef; order: number }> = [];
const remote: Array<{ def: RouteDef; order: number }> = [];
routes.forEach((def, order) => (remoteRouteDefs.has(def) ? remote : local).push({ def, order }));