From b2af0d77e056f475e15321746fb05a70144467b3 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:54:38 -0500 Subject: [PATCH] test(routes): consolidate JavaScript route behavior coverage --- .../JavaScriptNextAppContext.test.ts | 126 +--- .../javascriptRouteInventory.test.ts | 673 ++++++++---------- .../javascript/javascriptRouteScan.test.ts | 77 +- .../javascriptNextRouteContext.test.ts | 19 +- .../javascriptRouteAndRpcIdentity.test.ts | 84 ++- 5 files changed, 447 insertions(+), 532 deletions(-) diff --git a/src/application/javascript/JavaScriptNextAppContext.test.ts b/src/application/javascript/JavaScriptNextAppContext.test.ts index c0136bf6a4..0dea362213 100644 --- a/src/application/javascript/JavaScriptNextAppContext.test.ts +++ b/src/application/javascript/JavaScriptNextAppContext.test.ts @@ -6,12 +6,11 @@ import { createJavaScriptNextAppContext } from "./JavaScriptNextAppContext.js"; const contextFor = ( files: readonly JavaScriptArtifactFile[], - documents: JavaScriptJsonDocuments, directories: ReadonlySet, ) => createJavaScriptNextAppContext( { files, unavailable_relevant_files: [] }, - documents, + new JavaScriptJsonDocuments(), directories, { scope: "container-entries", status: "complete" }, ); @@ -27,7 +26,7 @@ const file = (path: string, text = ""): JavaScriptArtifactFile => ({ text: { included: true, value: text }, }); -it("keeps package applicability and its root cache scoped to the source container", () => { +it("keeps readable package applicability scoped to its source container", () => { const manifest = file("package.json", '{"dependencies":{"next":"16.4.2"}}'); const foreign = { ...file("package.json", '{"name":"plain"}'), @@ -37,7 +36,6 @@ it("keeps package applicability and its root cache scoped to the source containe const foreignPage = { ...page, container_sha256: foreign.container_sha256 }; const context = contextFor( [manifest, foreign, page, foreignPage], - new JavaScriptJsonDocuments(), new Set(["app"]), ); expect(context(page)).toMatchObject({ @@ -48,80 +46,20 @@ it("keeps package applicability and its root cache scoped to the source containe kind: "unknown", package: { container_sha256: foreign.container_sha256 }, }); - expect(context(page).kind).toBe("app-directory"); -}); - -it("uses the nearest declared Next package and root directory precedence", () => { - const files = [ - file("package.json", '{"dependencies":{"next":"16.4.2"}}'), - file("app/page.js"), - file("src/app/control/page.js"), - file("pages/app/page.js"), - file("components/app/page.js"), - file("node_modules/ordinary/package.json", '{"name":"ordinary"}'), - file("node_modules/ordinary/app/page.js"), - ]; - const context = contextFor( - files, - new JavaScriptJsonDocuments(), - new Set([ - "app", - "src/app", - "pages", - "components/app", - "node_modules/ordinary/app", - ]), - ); - expect(context(files[1]!)).toMatchObject({ - kind: "app-directory", - directory: "app", - package: { path: "package.json" }, - }); - for (const source of files.slice(2, 5)) - expect(context(source).kind).toBe("outside-app-directory"); - expect(context(files[6]!)).toMatchObject({ - kind: "unknown", - package: { path: "node_modules/ordinary/package.json" }, - }); }); it("keeps default compiled output under its declared package and unrelated chunk-like paths unknown", () => { const manifest = file("package.json", '{"dependencies":{"next":"16.4.2"}}'); const chunk = file(".next/static/chunks/app/build/page-abc.js"); - const context = contextFor( - [manifest, chunk], - new JavaScriptJsonDocuments(), - new Set(), - ); + const context = contextFor([manifest, chunk], new Set()); expect(context(chunk)).toMatchObject({ kind: "compiled-directory", directory: ".next/static/chunks/app", }); - expect( - contextFor([chunk], new JavaScriptJsonDocuments(), new Set())(chunk), - ).toMatchObject({ kind: "unknown" }); -}); - -it("supports src/app only when app is not inventoried and retains missing framework identity", () => { - const manifest = file( - "package.json", - '{"devDependencies":{"next":"16.4.2"}}', - ); - const page = file("src/app/control/page.js"); - expect( - contextFor( - [manifest, page], - new JavaScriptJsonDocuments(), - new Set(["src/app"]), - )(page), - ).toMatchObject({ kind: "app-directory", directory: "src/app" }); - expect( - contextFor( - [page], - new JavaScriptJsonDocuments(), - new Set(["src/app"]), - )(page), - ).toMatchObject({ kind: "unknown", package: null }); + expect(contextFor([chunk], new Set())(chunk)).toMatchObject({ + kind: "unknown", + package: null, + }); }); it("does not infer absent manifests or higher-priority directories from incomplete traversal", () => { @@ -140,14 +78,7 @@ it("does not infer absent manifests or higher-priority directories from incomple reason: expect.stringContaining("src/app/control/package.json"), }); const rootPage = file("page.js"); - expect( - createJavaScriptNextAppContext( - files, - new JavaScriptJsonDocuments(), - new Set(["src/app"]), - { scope: "container-entries", status: "unknown" }, - )(rootPage), - ).toMatchObject({ + expect(context(rootPage)).toMatchObject({ kind: "unknown", package: { path: "package.json" }, reason: expect.stringContaining("higher-priority app directory"), @@ -167,39 +98,28 @@ it("retains the nearest unavailable manifest's evidence instead of borrowing an kind: "package-json" as const, limitations: ["The unpacked package companion is missing."], }; + const foreignContainer = "c".repeat(64); + const foreignParent = { ...parent, container_sha256: foreignContainer }; + const foreignPage = { ...page, container_sha256: foreignContainer }; const context = createJavaScriptNextAppContext( - { files: [parent, page], unavailable_relevant_files: [unavailable] }, - new JavaScriptJsonDocuments(), - new Set(["app", "app/widget/app"]), - { scope: "container-entries", status: "complete" }, - ); - expect(context(page)).toMatchObject({ - kind: "unknown", - package: null, - reason: expect.stringContaining(unavailable.path), - }); - const result = context(page); - if (result.kind !== "unknown") - throw new Error("Expected unknown package ownership"); - expect(result.reason).toContain(unavailable.inventory_occurrence_id); - expect(result.reason).toContain(unavailable.limitations[0]); - expect(context(file("app/page.js"))).toMatchObject({ - kind: "app-directory", - package: { path: parent.path }, - }); - const foreign = createJavaScriptNextAppContext( { - files: [parent, page], - unavailable_relevant_files: [ - { ...unavailable, container_sha256: "c".repeat(64) }, - ], + files: [parent, page, foreignParent, foreignPage], + unavailable_relevant_files: [unavailable], }, new JavaScriptJsonDocuments(), new Set(["app", "app/widget/app"]), { scope: "container-entries", status: "complete" }, ); - expect(foreign(page)).toMatchObject({ + const result = context(page); + expect(result).toMatchObject({ kind: "unknown", package: null }); + if (result.kind !== "unknown") + throw new Error("Expected unknown package ownership"); + expect(result.reason).toContain(unavailable.path); + expect(result.reason).toContain(unavailable.inventory_occurrence_id); + expect(result.reason).toContain(unavailable.limitations[0]); + expect(context(foreignPage)).toMatchObject({ kind: "app-directory", - package: { path: parent.path }, + directory: "app", + package: { path: parent.path, container_sha256: foreignContainer }, }); }); diff --git a/src/domain/javascript/javascriptRouteInventory.test.ts b/src/domain/javascript/javascriptRouteInventory.test.ts index 5db3f77b77..04b49f4474 100644 --- a/src/domain/javascript/javascriptRouteInventory.test.ts +++ b/src/domain/javascript/javascriptRouteInventory.test.ts @@ -81,7 +81,6 @@ it.each([ expect(result.routes).toMatchObject([ { path: `/${segment}`, source_count: 3, occurrence_count: 3 }, ]); - expect(result.routes).toHaveLength(1); expect(result.occurrences.map(({ source_id }) => source_id).sort()).toEqual( result.sources.map(({ id }) => id).sort(), ); @@ -92,33 +91,13 @@ it.each([ ]), ); expect(result.unknowns).toEqual([]); - expect(result.coverage).toEqual({ - scope: "provided-code-sources", + expect(result.coverage).toMatchObject({ source_parsing: "complete", candidate_enumeration: "complete", - application_routes: "unknown", }); }, ); -it.each(["%00", "%09", "%0A", "%7F"])( - "retains an interpretation gap for a Next filename containing %s", - (encoded) => { - const url = `https://example.test/_next/static/chunks/app/hello${encoded}world/page-abc.js`; - const result = build([source("captured.js", "", url)]); - expect(result.routes).toEqual([]); - expect(result.unknowns).toMatchObject([ - { - source_id: result.sources[0]!.id, - kind: "interpretation", - reason: expect.stringContaining("control character"), - }, - ]); - expect(result.sources[0]!.url).toBe(url); - expect(result.coverage.candidate_enumeration).toBe("partial"); - }, -); - it.each(["app/page.js", "app/hello\nworld/page.js"])( "does not promote page-shaped path %j without source-router applicability", (path) => { @@ -162,6 +141,7 @@ it("coalesces exact patterns while retaining independent occurrences and stable "two.js", `createBrowserRouter([{path:'/reports',children:[{path:'settings'}]}]);`, ), + source("three.js", `createBrowserRouter([{path:'/reports'}]);`), ]; const result = build(inputs), repeated = build([...inputs].reverse()); @@ -170,8 +150,8 @@ it("coalesces exact patterns while retaining independent occurrences and stable "/reports/settings", ]); expect(result.routes[0]).toMatchObject({ - occurrence_count: 2, - source_count: 2, + occurrence_count: 3, + source_count: 3, }); const reportOccurrences = result.occurrences.filter( ({ route_id }) => route_id === result.routes[0]!.id, @@ -190,227 +170,162 @@ it("coalesces exact patterns while retaining independent occurrences and stable mechanism: "navigation-target", segments: ["/reports"], }, + { + source: "three.js", + mechanism: "nested-route-object", + segments: ["/reports"], + }, { source: "two.js", mechanism: "nested-route-object", segments: ["/reports"], }, ]); - expect(new Set(result.occurrences.map(({ id }) => id)).size).toBe( - result.occurrences.length, - ); - expect(result.routes.map(({ id }) => id)).toEqual( - repeated.routes.map(({ id }) => id), - ); - expect(result.source_manifest_sha256).toBe(repeated.source_manifest_sha256); - expect(result.inventory_id).toBe(repeated.inventory_id); - expect(result.coverage.application_routes).toBe("unknown"); + expect(repeated).toEqual(result); }); -it("retains resolved index routes with source provenance and complete enumeration", () => { +it("publishes the unresolved index flag alongside its usable child route", () => { + const prefix = "createBrowserRouter([{index:"; const result = build([ source( - "routes.ts", - `const INDEX=true; createBrowserRouter([{index:(INDEX as boolean),Component:Home},{path:'/parent',children:[{index:INDEX,Component:Home}]}]);`, + "routes.js", + `const INDEX=choose();\n${prefix}INDEX,children:[{path:'child'}]}]);`, ), ]); - expect(result.routes.map(({ path }) => path)).toEqual(["/", "/parent"]); - expect(result.occurrences).toHaveLength(3); - expect(result.occurrences).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - route_id: result.routes[0]!.id, - unprefixed_path: "/", - origin: "source-syntax", - mechanism: "nested-route-object", - source_range: expect.objectContaining({ start: expect.any(Object) }), - }), - ]), - ); - expect(result.unknowns).toEqual([]); + expect(result.routes.map(({ path }) => path)).toEqual(["/child"]); + expect(result.unknowns).toMatchObject([ + { + source_id: result.sources[0]?.id, + kind: "interpretation", + reason: expect.stringContaining("index-route flag"), + source_range: { + start: { line: 2, column: prefix.length }, + end: { line: 2, column: prefix.length + "INDEX".length }, + }, + }, + ]); expect(result.coverage).toMatchObject({ source_parsing: "complete", - candidate_enumeration: "complete", - application_routes: "unknown", + candidate_enumeration: "partial", }); }); -it.each([ - "const INDEX=choose();", - "var INDEX=true;globalThis.INDEX=false;", - "var INDEX=true;const host=globalThis;host.INDEX=false;", -])( - "marks unproven index interpretation as partial with child paths: %s", - (prefix) => { - const result = build([ - source( - "routes.js", - `${prefix}createBrowserRouter([{index:INDEX,children:[{path:'child'}]}]);`, - ), - ]); - expect(result.routes.map(({ path }) => path)).toEqual(["/child"]); - expect(result.unknowns).toEqual([ - expect.objectContaining({ - kind: "interpretation", - reason: expect.stringContaining("index-route flag"), - source_range: expect.objectContaining({ start: expect.any(Object) }), - }), - ]); - expect(result.coverage).toMatchObject({ - source_parsing: "complete", - candidate_enumeration: "partial", - application_routes: "unknown", - }); - }, -); -it("chooses deterministic primary provenance when sources have equal confidence", () => { - const inputs = [ - source("one.js", `router.push('/reports');`), - source("two.js", `router.push('/reports');`), - ]; - expect(build(inputs)).toEqual(build([...inputs].reverse())); -}); -it("binds ordinary constants to application segments across sources and excludes framework filesystem noise", () => { - const result = build([ - source("navigation.js", `router.push('/reports');`), - source( - "runtime.js", - `globalThis.TURBOPACK=[];const routes=['/reports/:id','/ROOT/node_modules/next/process/','/_not-found'];`, - ), - ]); - expect(result.routes.map(({ path }) => path)).toEqual([ - "/reports", - "/reports/:id", - ]); - expect(result.statistics.unanchored_literals_excluded).toBe(2); -}); -it.each(["api", "assets", "static", "_next"])( - "retains constants under an anchored %s segment without promoting unanchored paths", - (prefix) => { - const result = build([ - source("route.js", `router.push('/${prefix}/status');`), - source( - "runtime.js", - `globalThis.TURBOPACK=[];const same='/${prefix}/status';const help='/${prefix}/help';const other='/unanchored/noise';`, - ), - ]); - const anchored = result.routes.find( - ({ path }) => path === `/${prefix}/status`, - ); - const descendant = result.routes.find( - ({ path }) => path === `/${prefix}/help`, - ); - expect(anchored).toMatchObject({ occurrence_count: 2, source_count: 2 }); - expect(descendant).toMatchObject({ - confidence: "low", - classification: "unknown", - }); - expect(result.routes.some(({ path }) => path === "/unanchored/noise")).toBe( - false, - ); - expect(result.coverage.application_routes).toBe("unknown"); - }, -); -it("retains inferred Next script bases and the unprefixed path", () => { - const result = build([ - source( - "script", - `router.push('/planner');`, - "https://example.test/app/_next/static/chunks/123.js", - ), - ]); - expect(result.routes.map(({ path }) => path)).toEqual(["/app/planner"]); - expect(result.occurrences[0]).toMatchObject({ - unprefixed_path: "/planner", - inferred_base_path: "/app", - confidence: "low", - }); - expect(result.sources[0]!.url).toBe( - "https://example.test/app/_next/static/chunks/123.js", - ); - expect(result.unknowns.some(({ kind }) => kind === "inferred-base")).toBe( - true, - ); -}); -it("reports missing source facts as partial and preserves filename-only discoveries without fake AST ranges", () => { +it("retains filename route provenance when source syntax is available or missing", () => { + const available = "app/(account)/reports/[id]/page.tsx"; + const unavailable = "app/reports/[id]/page.js"; const result = build([ + source(available, "export default function Page() { return null; }"), { - ...source("app/reports/[id]/page.js", ""), + ...source(unavailable, ""), status: "unavailable", reason: "source was not selected", facts: null, }, ]); + expect(result.routes).toMatchObject([ + { + path: "/reports/:id", + classification: "page", + classification_basis: "next-app-page", + source_count: 2, + occurrence_count: 2, + }, + ]); + for (const [path, status] of [ + [available, "complete"], + [unavailable, "unavailable"], + ] as const) { + const input = result.sources.find(({ key }) => key === path); + expect(input).toMatchObject({ path, status }); + expect(result.occurrences).toContainEqual( + expect.objectContaining({ + route_id: result.routes[0]?.id, + source_id: input?.id, + origin: "source-path", + source_range: null, + }), + ); + } + expect(result.unknowns).toMatchObject([ + { + source_id: result.sources.find(({ key }) => key === unavailable)?.id, + kind: "source-unavailable", + reason: "source was not selected", + source_range: null, + }, + ]); expect(result.coverage).toMatchObject({ source_parsing: "partial", candidate_enumeration: "partial", - application_routes: "unknown", - }); - expect(result.routes[0]!.path).toBe("/reports/:id"); - expect(result.occurrences[0]).toMatchObject({ - origin: "source-path", - source_range: null, }); }); -it("does not interpret the URL authority or file URL prefix as an App Router directory or routing base", () => { - const result = build([ - source("script", `router.push('/reports');`, "https://app/reports/page.js"), +it("keeps host names and file URL paths from becoming deployment bases", () => { + const urls = ["https://app/reports/page.js", "file:///tmp/_next/123.js"]; + const result = build( + urls.map((url, index) => + source(`script-${index}`, "router.push('/reports');", url), + ), + ); + expect(result.routes).toMatchObject([ + { path: "/reports", occurrence_count: 2, source_count: 2 }, ]); - expect(result.routes.map(({ path }) => path)).toEqual(["/reports"]); - const local = build([ - source("file", `router.push('/reports');`, "file:///tmp/_next/123.js"), - ]); - expect(local.sources[0]!.base_path).toBeNull(); - expect(local.routes[0]!.path).toBe("/reports"); + expect(result.sources).toMatchObject( + urls.map((url) => ({ url, base_path: null })), + ); + expect(result.unknowns).toEqual([]); }); it("rejects forged aggregate counts and detached provenance", () => { const result = build([source("one.js", `router.push('/reports');`)]); + const badCount = javaScriptRouteInventorySchema.safeParse({ + ...result, + routes: result.routes.map((route) => ({ ...route, occurrence_count: 99 })), + }); + const detachedOccurrence = javaScriptRouteInventorySchema.safeParse({ + ...result, + sources: [], + statistics: { ...result.statistics, sources_retained: 0 }, + }); + expect(badCount.error?.issues.map(({ path }) => path)).toContainEqual([ + "routes", + ]); expect( - javaScriptRouteInventorySchema.safeParse({ - ...result, - routes: result.routes.map((route) => ({ - ...route, - occurrence_count: 99, - })), - }).success, - ).toBe(false); - expect( - javaScriptRouteInventorySchema.safeParse({ ...result, sources: [] }) - .success, - ).toBe(false); + detachedOccurrence.error?.issues.map(({ path }) => path), + ).toContainEqual(["occurrences"]); }); -it("retains a bounded prefix with explicit capacity and counts only retained facts", () => { - const inputs = [ - source( - "many.js", - Array.from( - { length: 100 }, - (_, index) => `router.push('/reports/${index}');`, - ).join("\n"), - ), - ]; - const result = build(inputs, 8192); - expect(result.routes.length).toBeGreaterThan(0); - expect(result.routes.length).toBeLessThan(100); - expect(result.coverage.candidate_enumeration).toBe("partial"); - expect(result.capacity?.limit_bytes).toBe(8192); - expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(8192); - expect(() => build(inputs, 1)).toThrow(RangeError); +it("retains the first complete route rows that fit the result budget", () => { + const paths = Array.from( + { length: 100 }, + (_, index) => `/reports/${String(index).padStart(2, "0")}`, + ); + const input = source( + "many.js", + paths.map((path) => `router.push('${path}');`).join("\n"), + ); + const limit = 8192; + const result = build([input], limit); + const retainedPaths = result.routes.map(({ path }) => path); + expect(retainedPaths.length).toBeGreaterThan(0); + expect(retainedPaths.length).toBeLessThan(paths.length); + expect(retainedPaths).toEqual(paths.slice(0, retainedPaths.length)); + expect( + result.occurrences.map(({ unprefixed_path }) => unprefixed_path).sort(), + ).toEqual(retainedPaths); + expect(result.coverage).toMatchObject({ + source_parsing: "partial", + candidate_enumeration: "partial", + }); + expect(result.capacity?.limit_bytes).toBe(limit); + expect(result.capacity?.accounted_bytes).toBeLessThanOrEqual(limit); + expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual( + result.capacity?.accounted_bytes ?? 0, + ); + expect(() => build([input], 1)).toThrow( + expect.objectContaining({ + name: "RangeError", + message: expect.stringContaining("envelope"), + }), + ); }); -it("anchors generic constants before applying an inferred asset base", () => { - const result = build([ - source( - "prefixed.js", - `globalThis.TURBOPACK=[];createBrowserRouter([{path:'/reports'}]);const paths=['/reports/:id','/noise'];`, - "https://example.test/app/_next/static/chunks/one.js", - ), - ]); - expect(result.routes.map((row) => row.path)).toEqual([ - "/app/reports", - "/app/reports/:id", - ]); - expect(result.statistics.unanchored_literals_excluded).toBe(1); -}); it("classifies typed page configuration while generic server route calls remain unknown", () => { const result = build([ source( @@ -429,25 +344,6 @@ it("classifies typed page configuration while generic server route calls remain }); }); -it("keeps TypeScript page filename provenance separate from syntax ranges", () => { - const result = build([ - source( - "app/(account)/reports/[id]/page.tsx", - "export default function Page() { return null; }", - ), - ]); - expect(result.routes).toHaveLength(1); - expect(result.routes[0]).toMatchObject({ - path: "/reports/:id", - classification: "page", - classification_basis: "next-app-page", - }); - expect(result.occurrences[0]).toMatchObject({ - origin: "source-path", - source_range: null, - }); -}); - it("coalesces inferred deployment roots without rewriting explicit trailing slashes", () => { const result = build([ source( @@ -489,35 +385,60 @@ it("keeps enumeration partial when a recognized navigation target is unresolved" ]); }); -it.each(["/app/reports", "/reports"])( - "matches based and unbased constants against application segments for %s", - (anchor) => { - const result = build([ - source( - "mixed.js", - `globalThis.TURBOPACK=[];router.push('${anchor}');const paths=['/reports/settings','/app/reports/history','/noise','/app/noise','/ROOT/node_modules/framework'];`, - "https://example.test/app/_next/static/chunks/one.js", - ), - ]); - expect(result.routes.map(({ path }) => path)).toEqual([ - "/app/reports", - "/app/reports/history", - "/app/reports/settings", - ]); - expect(result.occurrences).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - unprefixed_path: "/reports/settings", - inferred_base_path: "/app", - }), - expect.objectContaining({ - unprefixed_path: "/app/reports/history", - inferred_base_path: null, - }), - ]), +it("anchors based and unbased constants while preserving original paths and deployment evidence", () => { + const reportsUrl = "https://example.test/app/_next/static/chunks/reports.js"; + const plannerUrl = "https://example.test/app/_next/static/chunks/planner.js"; + const result = build([ + source( + "reports.js", + `globalThis.TURBOPACK=[];router.push('/app/reports');const paths=['/reports/settings','/app/reports/history','/noise','/app/noise','/ROOT/node_modules/framework'];`, + reportsUrl, + ), + source( + "planner.js", + `globalThis.TURBOPACK=[];createBrowserRouter([{path:'/planner'}]);const help='/planner/help';`, + plannerUrl, + ), + ]); + expect(result.routes.map(({ path }) => path)).toEqual([ + "/app/planner", + "/app/planner/help", + "/app/reports", + "/app/reports/history", + "/app/reports/settings", + ]); + expect(result.sources).toMatchObject( + [plannerUrl, reportsUrl].map((url) => ({ + url, + base_path: { value: "/app", basis: "next-script-url", status: "inferred" }, + })), + ); + const [planner, reports] = result.sources; + for (const [path, input, unprefixedPath, basePath] of [ + ["/app/planner", planner, "/planner", "/app"], + ["/app/planner/help", planner, "/planner/help", "/app"], + ["/app/reports", reports, "/app/reports", null], + ["/app/reports/history", reports, "/app/reports/history", null], + ["/app/reports/settings", reports, "/reports/settings", "/app"], + ] as const) { + expect(result.occurrences, path).toContainEqual( + expect.objectContaining({ + route_id: result.routes.find((route) => route.path === path)?.id, + source_id: input?.id, + unprefixed_path: unprefixedPath, + inferred_base_path: basePath, + ...(basePath === null ? {} : { confidence: "low" }), + }), ); - }, -); + } + expect( + result.unknowns + .filter(({ kind }) => kind === "inferred-base") + .map(({ source_id }) => source_id) + .sort(), + ).toEqual(result.sources.map(({ id }) => id).sort()); + expect(result.statistics.unanchored_literals_excluded).toBe(3); +}); it("keeps filename and module-derived anchors application-relative when a deployment base matches an application segment", () => { const result = build([ @@ -567,111 +488,145 @@ it("reports generated typed configurations as partial even without retained cand expect(result.unknowns[0]!.kind).toBe("interpretation"); }); -it.each([ - ["app/feed/(.)photo/page.tsx", null], - [ - "page.js", - "https://example.test/_next/static/chunks/app/feed/(.)photo/page-123.js", - ], -] as const)( - "retains intercepted filename metadata as partial: %s", - (path, url) => { - const result = build([ - source(path, "export default function Photo(){return null;}", url), - ]); - expect(result.routes).toEqual([]); - expect(result.coverage.candidate_enumeration).toBe("partial"); - expect(result.unknowns).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - kind: "interpretation", - reason: expect.stringContaining("intercepted"), - source_range: null, - }), - ]), - ); - }, -); - -it("decodes captured Next script URL segments once while retaining source URL identity", () => { +it("retains intercepted local and captured filenames as separate interpretation gaps", () => { + const path = "app/feed/(.)photo/page.tsx"; const url = - "https://example.test/base/_next/static/chunks/app/%28shop%29/blog/%5Bid%5D/page-abc.js"; - const result = build([ - source("captured-page", "", url), - source("app/blog/%5Bid%5D/page.js", ""), - ]); - expect(result.routes.map(({ path }) => path)).toEqual([ - "/base/blog/:id", - "/blog/%5Bid%5D", - ]); - expect(result.sources.find((entry) => entry.url === url)?.url).toBe(url); - expect(result.occurrences).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - origin: "source-url", - unprefixed_path: "/blog/:id", - inferred_base_path: "/base", - }), - ]), - ); -}); - -it.each(["team%2Fadmin", "team%5Cadmin", "%E0%A4%A"])( - "keeps malformed Next URL segment %s unknown alongside usable script facts", - (segment) => { - const url = `https://example.test/_next/static/chunks/app/${segment}/page-abc.js`; - const result = build([source("page", "router.push('/usable');", url)]); - expect(result.routes.map(({ path }) => path)).toEqual(["/usable"]); - expect(result.sources[0]?.url).toBe(url); - expect(result.coverage.candidate_enumeration).toBe("partial"); - expect(result.unknowns).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - kind: "interpretation", - reason: expect.stringContaining("Next script URL"), - }), - ]), - ); - }, -); - -it("preserves literal percent encoding after one script URL decode", () => { - const result = build([ - source( - "page", - "", - "https://example.test/_next/static/chunks/app/%255Bid%255D/page-abc.js", - ), - ]); - expect(result.routes.map(({ path }) => path)).toEqual(["/%5Bid%5D"]); -}); - -it.each([ - "page%3Fnotes.js", - "page%23notes.js", - "page.js%3Fnotes", - "page.js%23notes", -])("keeps encoded Next script filename delimiters literal: %s", (leaf) => { - const url = `https://example.test/_next/static/chunks/app/dashboard/${leaf}`; - const result = build([source("captured-page", "", url)]); + "https://example.test/_next/static/chunks/app/feed/(.)photo/page-123.js"; + const result = build([source(path, ""), source("captured", "", url)]); expect(result.routes).toEqual([]); - expect(result.sources[0]?.url).toBe(url); + expect(result.occurrences).toEqual([]); + expect(result.coverage.candidate_enumeration).toBe("partial"); + expect(result.sources).toMatchObject([ + { key: path, path, url: null }, + { key: "captured", path: null, url }, + ]); + expect(result.unknowns).toHaveLength(2); + for (const { id } of result.sources) + expect(result.unknowns).toContainEqual( + expect.objectContaining({ + source_id: id, + kind: "interpretation", + reason: expect.stringContaining("intercepted"), + source_range: null, + }), + ); }); -it.each(["?cache=1", "#section", "?cache=1#section"])( - "parses actual Next script URL suffix %s before classifying the filename", - (suffix) => { - const url = `https://example.test/_next/static/chunks/app/dashboard/page-abc.js${suffix}`; - const result = build([source("captured-page", "", url)]); - expect(result.routes.map(({ path }) => path)).toEqual(["/dashboard"]); - expect(result.sources[0]?.url).toBe(url); - }, -); +it("decodes captured script paths once and retains the original source identities", () => { + const basedUrl = + "https://example.test/base/_next/static/chunks/app/%28shop%29/blog/%5Bid%5D/page-abc.js"; + const escapedUrl = + "https://example.test/_next/static/chunks/app/blog/%255Bid%255D/page-abc.js"; + const local = "app/blog/%5Bid%5D/page.js"; + const result = build([ + source("captured", "", basedUrl), + source("escaped", "", escapedUrl), + source(local, ""), + ]); + expect(result.routes).toMatchObject([ + { path: "/base/blog/:id", source_count: 1, occurrence_count: 1 }, + { path: "/blog/%5Bid%5D", source_count: 2, occurrence_count: 2 }, + ]); + expect(result.sources).toHaveLength(3); + for (const [key, url, routePath, unprefixed, base] of [ + [local, null, "/blog/%5Bid%5D", "/blog/%5Bid%5D", null], + ["captured", basedUrl, "/base/blog/:id", "/blog/:id", "/base"], + ["escaped", escapedUrl, "/blog/%5Bid%5D", "/blog/%5Bid%5D", null], + ] as const) { + const input = result.sources.find((row) => row.key === key); + expect(input).toMatchObject({ path: url === null ? local : null, url }); + expect(result.occurrences).toContainEqual( + expect.objectContaining({ + route_id: result.routes.find(({ path }) => path === routePath)?.id, + source_id: input?.id, + origin: url === null ? "source-path" : "source-url", + unprefixed_path: unprefixed, + inferred_base_path: base, + source_range: null, + }), + ); + } +}); -it.each(["page?notes.js", "page#notes.js", "page.js?notes", "page.js#notes"])( - "keeps filesystem Next page filename delimiters literal: %s", - (leaf) => { - const result = build([source(`app/dashboard/${leaf}`, "")]); - expect(result.routes).toEqual([]); - }, -); +it("reports invalid Next filename interpretations while retaining usable script routes", () => { + const cases = [ + ["nul", "hello%00world", "control character"], + ["tab", "hello%09world", "control character"], + ["line-feed", "hello%0Aworld", "control character"], + ["delete", "hello%7Fworld", "control character"], + ["slash", "team%2Fadmin", "encoded path separator"], + ["backslash", "team%5Cadmin", "encoded path separator"], + ["malformed", "%E0%A4%A", "malformed percent encoding"], + ] as const; + const captured = cases.map(([key, segment, reason]) => ({ + key, + url: `https://example.test/_next/static/chunks/app/${segment}/page-abc.js`, + reason, + })); + const result = build( + captured.map(({ key, url }) => source(key, "router.push('/usable');", url)), + ); + expect(result.routes).toMatchObject([ + { + path: "/usable", + source_count: cases.length, + occurrence_count: cases.length, + }, + ]); + expect(result.sources).toHaveLength(cases.length); + expect(result.unknowns).toHaveLength(cases.length); + for (const { key, url, reason } of captured) { + const input = result.sources.find((row) => row.key === key); + expect(input, key).toMatchObject({ path: null, url }); + expect(result.unknowns, key).toContainEqual( + expect.objectContaining({ + source_id: input?.id, + kind: "interpretation", + reason: expect.stringContaining(reason), + }), + ); + } + expect(result.coverage.candidate_enumeration).toBe("partial"); +}); + +it("distinguishes URL suffixes from literal Next script filename bytes", () => { + const baseUrl = "https://example.test/_next/static/chunks/app/dashboard/"; + const captured = [ + [`${baseUrl}page-abc.js?cache=1`, true], + [`${baseUrl}page-abc.js#section`, true], + [`${baseUrl}page-abc.js?cache=1#section`, true], + [`${baseUrl}page%3Fnotes.js`, false], + [`${baseUrl}page%23notes.js`, false], + [`${baseUrl}page.js%3Fnotes`, false], + [`${baseUrl}page.js%23notes`, false], + ] as const; + const local = [ + "app/dashboard/page?notes.js", + "app/dashboard/page#notes.js", + "app/dashboard/page.js?notes", + "app/dashboard/page.js#notes", + ]; + const result = build([ + ...captured.map(([url], index) => source(`captured-${index}`, "", url)), + ...local.map((path) => source(path, "")), + ]); + expect(result.routes).toMatchObject([ + { path: "/dashboard", source_count: 3, occurrence_count: 3 }, + ]); + const sourcesByUrl = new Map(result.sources.map((row) => [row.url, row])); + const expectedOccurrences = []; + for (const [url, resolves] of captured) { + const row = sourcesByUrl.get(url); + expect(row, url).toMatchObject({ path: null, url }); + if (resolves) expectedOccurrences.push(row?.id); + } + expect(result.occurrences.map(({ source_id }) => source_id).sort()).toEqual( + expectedOccurrences.sort(), + ); + expect( + result.sources + .filter(({ url }) => url === null) + .map(({ path }) => path) + .sort(), + ).toEqual([...local].sort()); +}); diff --git a/src/domain/javascript/javascriptRouteScan.test.ts b/src/domain/javascript/javascriptRouteScan.test.ts index 315650997c..3160196ae7 100644 --- a/src/domain/javascript/javascriptRouteScan.test.ts +++ b/src/domain/javascript/javascriptRouteScan.test.ts @@ -90,32 +90,16 @@ it("reads route-record keys at most once beyond inspecting each property", () => })), ), ); - expect(propertyCount).toBe(count * 2 + 1); - expect(keyReads).toBeGreaterThanOrEqual(propertyCount); expect(keyReads).toBeLessThanOrEqual(propertyCount * 2); }); -it.each(["true", "(true as const)", "INDEX"])( - "retains an index route with flag %s", - (flag) => { - const facts = analyze( - `const INDEX=true;createBrowserRouter([{index:${flag},Component:Home}]);`, - ); - expect(facts.candidates.map(({ path }) => path)).toEqual(["/"]); - expect(facts.unresolved).toEqual([]); - }, -); - -it.each([ - "createBrowserRouter([{index:INDEX,Component:Home}]);const INDEX=true;", - "const INDEX=runtime;createBrowserRouter([{index:INDEX,Component:Home}]);", -])("retains unknown index flags without inventing a route: %s", (source) => { - const facts = analyze(source); +it("keeps an index flag unknown before its initializer runs", () => { + const facts = analyze( + "createBrowserRouter([{index:INDEX,Component:Home}]);const INDEX=true;", + ); expect(facts.candidates).toEqual([]); - expect(facts.unresolved).toEqual([ - expect.objectContaining({ - reason: expect.stringContaining("index-route flag"), - }), + expect(facts.unresolved).toMatchObject([ + { reason: expect.stringContaining("index-route flag") }, ]); }); @@ -148,26 +132,20 @@ it.each([ }, ); -it.each([ - "var routes=[{path:'/reports'}];createBrowserRouter(routes);", - "var ROUTES=[{path:'/reports',children:[{path:'child'}]}];createBrowserRouter(ROUTES);", -] as const)( - "keeps a program-var route configuration readable while disclosing the gap: %s", - (source) => { - const facts = analyze(source); - expect(facts.candidates.map(({ path }) => path)).toEqual( - expect.arrayContaining(["/reports"]), - ); - expect(facts.unresolved).toEqual([ - expect.objectContaining({ - reason: expect.stringContaining("unisolated program variable"), - }), - ]); - }, -); +it("retains a program-var route tree and reports its unresolved ownership", () => { + const facts = analyze( + "var ROUTES=[{path:'/reports',children:[{path:'child'}]}];createBrowserRouter(ROUTES);", + ); + expect(facts.candidates.map(({ path }) => path)).toEqual([ + "/reports", + "/reports/child", + ]); + expect(facts.unresolved).toMatchObject([ + { reason: expect.stringContaining("unisolated program variable") }, + ]); +}); it.each([ - "var PATH='/a';globalThis.PATH='/b';createBrowserRouter([{path:PATH}]);", "var PATH='/a';const host=globalThis;host.PATH='/b';createBrowserRouter([{path:PATH}]);", "var PATH='/a';createBrowserRouter([{path:PATH}]);", ] as const)( @@ -207,19 +185,12 @@ it.each([ }, ); -it.each([ - "var SHARED={query:{path:'/data',Component:View}};const HOLD=SHARED;router.push({pathname:'/reports',query:HOLD.query});", - "var SHARED={query:{path:'/data'}};router.push({pathname:'/reports',query:SHARED.query});", -] as const)( - "never promotes unisolated navigation data to a route: %s", - (source) => { - // The slot is data, not a route record. Rejecting its runtime identity must - // not release the object back to the independent route-literal scan. - const facts = analyze(source); - expect(facts.candidates.map(({ path }) => path)).toEqual(["/reports"]); - expect(facts.candidates.some(({ path }) => path === "/data")).toBe(false); - }, -); +it("keeps a navigation data path out of the route candidates", () => { + const facts = analyze( + "var SHARED={query:{path:'/data'}};router.push({pathname:'/reports',query:SHARED.query});", + ); + expect(facts.candidates.map(({ path }) => path)).toEqual(["/reports"]); +}); it("keeps unrelated routes that share the unisolated data object", () => { // Rejecting the runtime identity must not claim the whole source object: diff --git a/tests/acceptance/applications/javascriptNextRouteContext.test.ts b/tests/acceptance/applications/javascriptNextRouteContext.test.ts index 6e96b4be01..c1daee9476 100644 --- a/tests/acceptance/applications/javascriptNextRouteContext.test.ts +++ b/tests/acceptance/applications/javascriptNextRouteContext.test.ts @@ -58,6 +58,23 @@ cliTest.for(["CLI", "stdio MCP"] as const)( directory: "app", package: { path: "package.json" }, }); + expect( + inventory?.sources.find( + ({ key }) => key === "workspace/src/app/control/page.js", + )?.next_app_context, + ).toMatchObject({ + kind: "app-directory", + directory: "workspace/src/app", + package: { path: "workspace/package.json" }, + }); + expect( + inventory?.sources.find( + ({ key }) => key === "node_modules/ordinary/app/dependency-only/page.js", + )?.next_app_context, + ).toMatchObject({ + kind: "unknown", + package: { path: "node_modules/ordinary/package.json" }, + }); const unproven = inventory?.sources.find( ({ key }) => key === "plain/components/app/module-only/page.js", ); @@ -132,8 +149,6 @@ cliTest.for(["CLI", "stdio MCP"] as const)( directory: "app.asar/app/widget/app", package: { path: `app.asar/${nestedManifest}` }, }); - expect(nested?.id).toEqual(expect.any(String)); - expect(recovered?.id).toEqual(expect.any(String)); expect(recovered?.id).not.toBe(nested?.id); expect( restored.route_inventory?.routes.map(({ path, source_count }) => ({ diff --git a/tests/acceptance/applications/javascriptRouteAndRpcIdentity.test.ts b/tests/acceptance/applications/javascriptRouteAndRpcIdentity.test.ts index b9c78599cf..4eac6e246c 100644 --- a/tests/acceptance/applications/javascriptRouteAndRpcIdentity.test.ts +++ b/tests/acceptance/applications/javascriptRouteAndRpcIdentity.test.ts @@ -12,7 +12,10 @@ cliTest.for(["CLI", "stdio MCP"] as const)( await writeFixtureFiles(root, { "entry.ts": ` const INDEX=true; - createBrowserRouter([{index:INDEX,Component:Home}, {path:'/wrapped',children:[{index:(true as const),Component:Home}]}]); + createBrowserRouter([ + {index:INDEX,Component:Home}, + {path:'/wrapped',children:[{index:(true as const),Component:Home}]} + ]); function clientScope() { const history=createTRPCClient({links:[httpBatchLink({url:'/api/trpc'})]}); history.archive.query();history.history.list.query(); @@ -23,10 +26,29 @@ cliTest.for(["CLI", "stdio MCP"] as const)( }); const result = await analyzeJavaScriptDirectory(surface, cli, root); expect(result.statistics.parse_failures).toBe(0); - expect(result.route_inventory?.routes.map(({ path }) => path)).toEqual([ - "/", - "/wrapped", + const inventory = result.route_inventory; + expect(inventory?.routes).toMatchObject([ + { path: "/", source_count: 1, occurrence_count: 1 }, + { path: "/wrapped", source_count: 1, occurrence_count: 2 }, ]); + expect(inventory?.occurrences).toContainEqual( + expect.objectContaining({ + route_id: inventory?.routes[0]?.id, + source_id: inventory?.sources.find(({ key }) => key === "entry.ts")?.id, + unprefixed_path: "/", + origin: "source-syntax", + mechanism: "nested-route-object", + source_range: { + start: { line: 4, column: 10 }, + end: { line: 4, column: 38 }, + }, + }), + ); + expect(inventory?.coverage).toMatchObject({ + source_parsing: "complete", + candidate_enumeration: "complete", + }); + expect(inventory?.unknowns).toEqual([]); const operations = result.graph.nodes .filter(({ kind }) => kind === "endpoint") .flatMap(({ observations }) => observations) @@ -43,20 +65,52 @@ cliTest.for(["CLI", "stdio MCP"] as const)( "retains anchored reserved-prefix route constants through %s", async (surface, { cli }) => { const root = await createTestTempDirectory("rea-route-reserved-prefix-"); + const patterns = [ + ["/_next/status", "/_next/help"], + ["/api/status", "/api/help"], + ["/assets/status", "/assets/help"], + ["/reports", "/reports/:id"], + ["/static/status", "/static/help"], + ] as const; await writeFixtureFiles(root, { - "entry.js": `globalThis.TURBOPACK=[]; + "entry.js": ` import {createBrowserRouter} from 'react-router-dom'; - export const router=createBrowserRouter([{path:'/static',element:null}]); - const help='/static/help';globalThis.helpLink=help; - const unrelated='/api/unanchored';globalThis.other=unrelated;`, + export const router=createBrowserRouter(${JSON.stringify( + patterns.map(([path]) => ({ path, element: null })), + )});`, + "runtime.js": `globalThis.TURBOPACK=[]; + globalThis.candidates=${JSON.stringify([ + ...patterns.flat(), + "/ROOT/node_modules/next/process/", + "/_not-found", + "/unanchored/noise", + ])};`, }); const result = await analyzeJavaScriptDirectory(surface, cli, root); - const routes = result.route_inventory?.routes ?? []; - expect(routes.find(({ path }) => path === "/static/help")).toMatchObject({ - confidence: "low", - classification: "unknown", - }); - expect(routes.some(({ path }) => path === "/api/unanchored")).toBe(false); - expect(result.route_inventory?.coverage.application_routes).toBe("unknown"); + const inventory = result.route_inventory; + const routes = inventory?.routes ?? []; + expect(routes.map(({ path }) => path).sort()).toEqual( + patterns.flat().sort(), + ); + const runtime = inventory?.sources.find(({ key }) => key === "runtime.js"); + for (const [anchor, descendant] of patterns) { + expect(routes.find(({ path }) => path === anchor), anchor).toMatchObject({ + occurrence_count: 2, + source_count: 2, + }); + const route = routes.find(({ path }) => path === descendant); + expect(route, descendant).toMatchObject({ + confidence: "low", + classification: "unknown", + }); + expect(inventory?.occurrences, descendant).toContainEqual( + expect.objectContaining({ + route_id: route?.id, + source_id: runtime?.id, + mechanism: "route-string-constant", + }), + ); + } + expect(inventory?.statistics.unanchored_literals_excluded).toBe(3); }, );