From 69e263b0b179da6bf47e84fedc2380954fa5658d Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:02:01 -0500 Subject: [PATCH] test(runtime): consolidate Inspector and reconciliation coverage --- ...ascriptNodeTargetReconciliationCli.test.ts | 49 +- .../javascriptRuntimeWideLoadStates.test.ts | 7 +- .../javascriptRuntimeContentPriority.test.ts | 538 ++++---- .../javascriptRuntimeReconciliation.test.ts | 1211 +++++++++-------- .../javascriptRuntimeNodeTarget.test.ts | 76 +- .../javascriptRuntimeObservation.test.ts | 441 +++--- .../javascriptRuntimeObservationMcp.test.ts | 74 +- ...riptRuntimeReconciliationAmbiguity.test.ts | 123 +- 8 files changed, 1269 insertions(+), 1250 deletions(-) diff --git a/tests/acceptance/applications/javascriptNodeTargetReconciliationCli.test.ts b/tests/acceptance/applications/javascriptNodeTargetReconciliationCli.test.ts index 357bc04959..97052ada91 100644 --- a/tests/acceptance/applications/javascriptNodeTargetReconciliationCli.test.ts +++ b/tests/acceptance/applications/javascriptNodeTargetReconciliationCli.test.ts @@ -1,6 +1,7 @@ import { writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect } from "vitest"; +import { parseEvidence } from "../../../src/domain/evidence.js"; import { javascriptRuntimeReconciliationResultSchema } from "../../../src/domain/javascript/javascriptRuntimeReconciliationSchemas.js"; import { nodeReconciliationFixture } from "../../fixtures/nodeRuntimeReconciliation.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -28,13 +29,39 @@ cliTest.for(["CLI", "stdio MCP"] as const)( expect(response.isError, JSON.stringify(response)).not.toBe(true); document = response.structuredContent; } + const evidence = parseEvidence(document); + expect(evidence.operation).toBe("reconcile_javascript_runtime"); const result = javascriptRuntimeReconciliationResultSchema.parse( - (document as { normalized_result: unknown }).normalized_result, + evidence.normalized_result, ); + const appEvidenceId = input.static_layers[0]?.analysis.evidence_id; + const runtimeEvidenceId = input.runtime_observations[0]?.evidence_id; + const asset = result.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some( + ({ properties }) => properties.path === "renderer.js", + ), + ); + const [capture] = result.runtime_captures; + if (asset === undefined || capture === undefined) + throw new Error("Missing renderer asset or runtime capture"); + expect(result.evidence_links).toEqual( + [appEvidenceId, runtimeEvidenceId].sort(), + ); + expect(capture).toMatchObject({ + evidence_id: runtimeEvidenceId, + kind: "v8-inspector", + scripts: 0, + }); expect(result.reconciliations).toEqual([ expect.objectContaining({ entity_kind: "target", status: "matched", + basis: "artifact-path", + static_node_id: asset.node_id, + runtime_node_id: capture.target_node_id, + runtime_evidence_id: runtimeEvidenceId, reason: "unique-location-match", confidence: "medium", candidate_static_count: 1, @@ -47,6 +74,24 @@ cliTest.for(["CLI", "stdio MCP"] as const)( }); expect( result.graph.edges.filter(({ relation }) => relation === "observed_as"), - ).toHaveLength(1); + ).toMatchObject([ + { + source_node_id: asset.node_id, + target_node_id: capture.target_node_id, + evidence: { + state: "inferred", + evidence_ids: [appEvidenceId, runtimeEvidenceId].sort(), + }, + }, + ]); + expect( + result.static_load_states.find( + ({ static_node_id }) => static_node_id === asset.node_id, + ), + ).toMatchObject({ + status: "unknown", + runtime_node_ids: [], + reason: "static-or-runtime-coverage-incomplete", + }); }, ); diff --git a/tests/boundary/cli/javascriptRuntimeWideLoadStates.test.ts b/tests/boundary/cli/javascriptRuntimeWideLoadStates.test.ts index 0c566a4aaa..0d6071598c 100644 --- a/tests/boundary/cli/javascriptRuntimeWideLoadStates.test.ts +++ b/tests/boundary/cli/javascriptRuntimeWideLoadStates.test.ts @@ -93,9 +93,10 @@ const wideRequest = (count: number) => { }; }; -processTest.for([1_000, 16_000])( - "retains every static load state for %s added assets through the compiled CLI", - async (count, { processes }) => { +processTest( + "retains every static load state for a wide graph through the compiled CLI", + async ({ processes }) => { + const count = 16_000; const root = await createTestTempDirectory("rea-runtime-wide-states-"); const path = join(root, "request.json"); const { input, assetIds, analysisId } = wideRequest(count); diff --git a/tests/boundary/filesystem/javascript-applications/javascriptRuntimeContentPriority.test.ts b/tests/boundary/filesystem/javascript-applications/javascriptRuntimeContentPriority.test.ts index 058a0abe65..5b66ab532e 100644 --- a/tests/boundary/filesystem/javascript-applications/javascriptRuntimeContentPriority.test.ts +++ b/tests/boundary/filesystem/javascript-applications/javascriptRuntimeContentPriority.test.ts @@ -30,82 +30,80 @@ const readInput = ( return result.value; }; -const collisionInput = async ( - alternatives: number, - explicitMapping: boolean, -) => { +it("retains the mapped disagreement despite equal bytes at other locations", async () => { const root = await applicationFixture(); - if (alternatives > 0) await writeFile(join(root, "other.js"), CAPTURED); - const live = explicitMapping - ? await createTestTempDirectory("rea-runtime-content-live-") - : root; + const live = await createTestTempDirectory("rea-runtime-content-live-"); const extra = await createTestTempDirectory("rea-runtime-content-extra-"); - await writeFile(join(extra, "app.js"), CAPTURED); - return readInput( - reconcileJavaScriptRuntimeInputSchema.parse({ - static_layers: [ - { - role: "application", - analysis: await analyzeFixture(root), - runtime_mappings: explicitMapping - ? [{ kind: "file-root", root: live, artifact_prefix: "" }] - : [], - }, - ...(alternatives > 1 - ? [{ role: "assets", analysis: await analyzeFixture(extra) }] - : []), - ], - runtime_observations: [ - electronRuntimeEvidence(live, CAPTURED, { includeWorker: false }), - ], + await Promise.all([ + writeFile(join(root, "other.js"), CAPTURED), + writeFile(join(extra, "app.js"), CAPTURED), + ]); + const [application, assets] = await Promise.all([ + analyzeFixture(root), + analyzeFixture(extra), + ]); + const captures = [root, live].map((path, index) => + electronRuntimeEvidence(path, CAPTURED, { + includeWorker: false, + targetId: `mismatch-${index}`, }), ); -}; - -it.each([ - [0, false], - [1, false], - [2, false], - [0, true], - [1, true], - [2, true], -] as const)( - "preserves mapped disagreement with %i other content matches (explicit mapping: %s)", - async (alternatives, explicitMapping) => { - const parsed = await collisionInput(alternatives, explicitMapping); - const result = reconcileJavaScriptRuntime(parsed); + const result = reconcileJavaScriptRuntime( + readInput( + reconcileJavaScriptRuntimeInputSchema.parse({ + static_layers: [ + { + role: "application", + analysis: application, + runtime_mappings: [ + { kind: "file-root", root: live, artifact_prefix: "" }, + ], + }, + { role: "assets", analysis: assets }, + ], + runtime_observations: captures, + }), + ), + ); + const layer = result.static_layers.find(({ role }) => role === "application"); + const node = result.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.length === 1 && + observations.some(({ properties }) => properties.path === "app.js"), + ); + if (layer === undefined || node === undefined) + throw new Error("Missing fixture application asset"); + for (const [index, basis] of [ + "artifact-path", + "operator-file-mapping", + ].entries()) { const script = result.reconciliations.find( - ({ entity_kind }) => entity_kind === "script", + ({ entity_kind, runtime_evidence_id }) => + entity_kind === "script" && + runtime_evidence_id === captures[index]?.evidence_id, ); expect(script).toMatchObject({ status: "unmatched", - basis: explicitMapping ? "operator-file-mapping" : "artifact-path", + basis, confidence: "high", reason: "captured-content-disagrees-with-static-location", static_layer_id: null, static_node_id: null, candidate_static_count: 1, + candidate_static_nodes: [ + { static_layer_id: layer.layer_id, static_node_id: node.node_id }, + ], }); - const candidate = script?.candidate_static_nodes[0]; - const selected = result.graph.nodes.find( - ({ node_id }) => node_id === candidate?.static_node_id, - ); - expect(selected?.observations).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - properties: expect.objectContaining({ path: "app.js" }), - }), - ]), - ); expect( result.graph.edges.some( - (edge) => - edge.relation === "observed_as" && - edge.target_node_id === script?.runtime_node_id, + ({ relation, target_node_id }) => + relation === "observed_as" && + target_node_id === script?.runtime_node_id, ), ).toBe(false); - }, -); + } +}); it("restricts content-and-location ambiguity to mapped candidates", async () => { const application = await applicationFixture(); @@ -139,46 +137,70 @@ it("restricts content-and-location ambiguity to mapped candidates", async () => reason: "ambiguous-static-candidates", candidate_static_count: 2, }); - const unmapped = result.static_layers.find(({ role }) => role === "assets"); - expect( - script?.candidate_static_nodes.map( - ({ static_layer_id }) => static_layer_id, - ), - ).not.toContain(unmapped?.layer_id); + const app = result.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === "app.js"), + ); + if (app === undefined) + throw new Error("Missing fixture shared application asset"); + expect(script?.candidate_static_nodes).toEqual( + result.static_layers + .filter(({ role }) => role === "application" || role === "cache") + .map(({ layer_id }) => ({ + static_layer_id: layer_id, + static_node_id: app.node_id, + })), + ); }); -it.each([false, true])( - "retains digest-only matching without a mapped candidate (mapping: %s)", - async (mapped) => { - const root = await applicationFixture(); - await writeFile(join(root, "other.js"), CAPTURED); - const live = await createTestTempDirectory( - "rea-runtime-content-unlocated-", - ); - const result = reconcileJavaScriptRuntime( - readInput( - reconcileJavaScriptRuntimeInputSchema.parse({ - static_layers: [ - { - role: "application", - analysis: await analyzeFixture(root), - runtime_mappings: mapped - ? [{ kind: "file-root", root: live, artifact_prefix: "" }] - : [], - }, - ], - runtime_observations: [ - electronRuntimeEvidence(live, CAPTURED, { - scriptFile: "absent.js", - includeWorker: false, - }), - ], - }), - ), - ); +it("matches exact bytes when either a mapped or unmapped location has no candidate", async () => { + const root = await applicationFixture(); + await writeFile(join(root, "other.js"), CAPTURED); + const mapped = await createTestTempDirectory( + "rea-runtime-content-unlocated-", + ); + const unmapped = await createTestTempDirectory( + "rea-runtime-content-unmapped-", + ); + const analysis = await analyzeFixture(root); + const captures = [mapped, unmapped].map((path, index) => + electronRuntimeEvidence(path, CAPTURED, { + scriptFile: "absent.js", + includeWorker: false, + targetId: `digest-only-${index}`, + }), + ); + const result = reconcileJavaScriptRuntime( + readInput( + reconcileJavaScriptRuntimeInputSchema.parse({ + static_layers: [ + { + role: "application", + analysis, + runtime_mappings: [ + { kind: "file-root", root: mapped, artifact_prefix: "" }, + ], + }, + ], + runtime_observations: captures, + }), + ), + ); + const expected = result.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === "other.js"), + ); + if (expected === undefined) throw new Error("Missing fixture digest match"); + const scripts = result.reconciliations.filter( + ({ entity_kind }) => entity_kind === "script", + ); + expect(scripts).toHaveLength(2); + for (const { evidence_id } of captures) expect( - result.reconciliations.find( - ({ entity_kind }) => entity_kind === "script", + scripts.find( + ({ runtime_evidence_id }) => runtime_evidence_id === evidence_id, ), ).toMatchObject({ status: "matched", @@ -186,12 +208,30 @@ it.each([false, true])( confidence: "high", reason: "unique-content-match", candidate_static_count: 1, + static_node_id: expected.node_id, }); - }, -); +}); it("keeps unavailable mapped digests unknown instead of borrowing unrelated matches", async () => { - const parsed = await collisionInput(1, true); + const root = await applicationFixture(); + await writeFile(join(root, "other.js"), CAPTURED); + const live = await createTestTempDirectory("rea-runtime-content-unknown-"); + const parsed = readInput( + reconcileJavaScriptRuntimeInputSchema.parse({ + static_layers: [ + { + role: "application", + analysis: await analyzeFixture(root), + runtime_mappings: [ + { kind: "file-root", root: live, artifact_prefix: "" }, + ], + }, + ], + runtime_observations: [ + electronRuntimeEvidence(live, CAPTURED, { includeWorker: false }), + ], + }), + ); const candidates = collectStaticRuntimeCandidates(parsed.layers).map( (candidate) => candidate.paths.includes("app.js") && candidate.category === "javascript" @@ -241,201 +281,147 @@ it("keeps unavailable mapped digests unknown instead of borrowing unrelated matc const UNRELATED = "export const unrelated = 17;\n"; -type LayerSetup = { - readonly root: string; - readonly mapping: "none" | "explicit"; -}; - -/** Reconcile one captured script, located at absent.js under live, against layers. */ -const reconcileCapturedScript = async (input: { - readonly layers: readonly LayerSetup[]; - readonly live: string; - readonly source: string; - readonly includeWorker?: boolean; -}) => { +it("reports absence only where a layer authorizes the capture location", async () => { + const [application, cache, mappedApplication, mappedCache, unmapped] = + await Promise.all([ + applicationFixture(), + applicationFixture(), + createTestTempDirectory("rea-runtime-application-mapped-"), + createTestTempDirectory("rea-runtime-cache-mapped-"), + createTestTempDirectory("rea-runtime-unmapped-"), + ]); + const [applicationEvidence, cacheEvidence] = await Promise.all([ + analyzeFixture(application), + analyzeFixture(cache), + ]); + const captures = [application, mappedApplication, mappedCache, unmapped].map( + (path, index) => + electronRuntimeEvidence(path, UNRELATED, { + scriptFile: "absent.js", + includeWorker: index === 3, + targetId: `no-match-${index}`, + }), + ); const result = reconcileJavaScriptRuntime( readInput( - reconcileJavaScriptRuntimeInputSchema.parse({ - static_layers: await Promise.all( - input.layers.map(async ({ root, mapping }, index) => ({ - role: (["application", "cache", "assets"] as const)[index], - analysis: await analyzeFixture(root), - runtime_mappings: - mapping === "explicit" - ? [{ kind: "file-root", root: input.live, artifact_prefix: "" }] - : [], - })), - ), - runtime_observations: [ - electronRuntimeEvidence(input.live, input.source, { - scriptFile: "absent.js", - includeWorker: input.includeWorker ?? false, - }), - ], - }), - ), - ); - const item = (kind: string) => - result.reconciliations.find(({ entity_kind }) => entity_kind === kind); - return { result, script: item("script"), worker: item("worker") }; -}; - -it("keeps an unmapped captured script without a content match unknown", async () => { - const root = await applicationFixture(); - const live = await createTestTempDirectory("rea-runtime-unmapped-"); - const { result, script, worker } = await reconcileCapturedScript({ - layers: [{ root, mapping: "none" }], - live, - source: UNRELATED, - includeWorker: true, - }); - const unknown = { - status: "unknown", - basis: "none", - confidence: "unknown", - reason: "no-authorized-location-mapping", - static_layer_id: null, - static_node_id: null, - candidate_static_count: 0, - }; - expect(script).toMatchObject(unknown); - // A source-free worker at an unmapped location shares the same rule. - expect(worker).toMatchObject(unknown); - expect(result.summary).toMatchObject({ matched: 0, unmatched: 0 }); - expect( - result.graph.edges.some(({ relation }) => relation === "observed_as"), - ).toBe(false); -}); - -it.each(["explicit", "implicit-root"] as const)( - "reports a mapped captured script without a content match as unmatched (%s)", - async (mapping) => { - const root = await applicationFixture(); - const live = - mapping === "explicit" - ? await createTestTempDirectory("rea-runtime-mapped-absent-") - : root; - const { result, script } = await reconcileCapturedScript({ - layers: [{ root, mapping: mapping === "explicit" ? "explicit" : "none" }], - live, - source: UNRELATED, - }); - expect(script).toMatchObject({ - status: "unmatched", - basis: "none", - confidence: "high", - reason: "no-static-content-match", - candidate_static_count: 0, - }); - expect(result.summary.unmatched).toBe(1); - }, -); - -it.each([ - ["only the second", ["none", "explicit"], "unmatched"], - ["only the first", ["explicit", "none"], "unmatched"], - ["neither", ["none", "none"], "unknown"], -] as const)( - "classifies an unmatched captured script when %s of two layers maps it", - async (_label, mappings, status) => { - const live = await createTestTempDirectory("rea-runtime-layer-mapping-"); - const { script } = await reconcileCapturedScript({ - layers: await Promise.all( - mappings.map(async (mapping) => ({ - root: await applicationFixture(), - mapping, - })), - ), - live, - source: UNRELATED, - }); - expect(script).toMatchObject({ - status, - reason: - status === "unknown" - ? "no-authorized-location-mapping" - : "no-static-content-match", - }); - }, -); - -it.each([ - [1, "matched", "unique-content-match"], - [2, "ambiguous", "ambiguous-static-candidates"], -] as const)( - "keeps unmapped content-only correspondence with %i digest candidates", - async (copies, status, reason) => { - const live = await createTestTempDirectory("rea-runtime-unmapped-content-"); - const { result, script } = await reconcileCapturedScript({ - layers: await Promise.all( - Array.from({ length: copies }, async () => { - const root = await applicationFixture(); - await writeFile(join(root, "other.js"), CAPTURED); - return { root, mapping: "none" as const }; - }), - ), - live, - source: CAPTURED, - }); - expect(script).toMatchObject({ - status, - basis: copies === 1 ? "content-sha256" : "none", - reason, - candidate_static_count: copies, - }); - expect(result.summary.unmatched).toBe(0); - }, -); - -it.each([ - ["https://example.test/assets/absent.js", "unmatched"], - ["https://other.test/assets/absent.js", "unknown"], -] as const)( - "applies URL-prefix mapping presence to captured script %s", - async (url, status) => { - const root = await applicationFixture(); - const parsed = readInput( reconcileJavaScriptRuntimeInputSchema.parse({ static_layers: [ { role: "application", - analysis: await analyzeFixture(root), + analysis: applicationEvidence, runtime_mappings: [ { - kind: "url-prefix", - prefix: "https://example.test/assets/", + kind: "file-root", + root: mappedApplication, artifact_prefix: "", }, ], }, + { + role: "cache", + analysis: cacheEvidence, + runtime_mappings: [ + { kind: "file-root", root: mappedCache, artifact_prefix: "" }, + ], + }, ], - runtime_observations: [ - electronRuntimeEvidence(root, UNRELATED, { includeWorker: false }), - ], + runtime_observations: captures, }), + ), + ); + const unmatched = { + status: "unmatched", + basis: "none", + confidence: "high", + reason: "no-static-content-match", + static_layer_id: null, + static_node_id: null, + candidate_static_count: 0, + }; + const unknown = { + ...unmatched, + status: "unknown", + confidence: "unknown", + reason: "no-authorized-location-mapping", + }; + const code = result.reconciliations.filter( + ({ entity_kind }) => entity_kind === "script" || entity_kind === "worker", + ); + expect(code).toHaveLength(5); + for (const [index, { evidence_id }] of captures.entries()) { + const rows = code.filter( + ({ runtime_evidence_id }) => runtime_evidence_id === evidence_id, ); - // Model a URL-located capture at the matcher boundary; browser and - // Inspector projections supply URL locations to the same classifier. - const entities = projectRuntimeCaptures(parsed.captures).entities.map( - (entity) => - entity.kind === "script" - ? { ...entity, location: { kind: "url" as const, value: url } } - : entity, - ); + expect( + rows.find(({ entity_kind }) => entity_kind === "script"), + ).toMatchObject(index < 3 ? unmatched : unknown); + if (index === 3) + expect( + rows.find(({ entity_kind }) => entity_kind === "worker"), + ).toMatchObject(unknown); + } + const runtimeIds = new Set( + code.map(({ runtime_node_id }) => runtime_node_id), + ); + expect( + result.graph.edges.some( + ({ relation, target_node_id }) => + relation === "observed_as" && runtimeIds.has(target_node_id), + ), + ).toBe(false); +}); + +it("applies URL-prefix presence to captured scripts independently of origin lookalikes", async () => { + const root = await applicationFixture(); + const parsed = readInput( + reconcileJavaScriptRuntimeInputSchema.parse({ + static_layers: [ + { + role: "application", + analysis: await analyzeFixture(root), + runtime_mappings: [ + { + kind: "url-prefix", + prefix: "https://example.test/assets/", + artifact_prefix: "", + }, + ], + }, + ], + runtime_observations: [ + electronRuntimeEvidence(root, UNRELATED, { includeWorker: false }), + ], + }), + ); + const { entities } = projectRuntimeCaptures(parsed.captures); + const candidates = collectStaticRuntimeCandidates(parsed.layers); + // Browser and Inspector projections supply URLs to this same matcher. + for (const [url, status, confidence, reason] of [ + [ + "https://example.test/assets/absent.js", + "unmatched", + "high", + "no-static-content-match", + ], + [ + "https://other.test/assets/absent.js", + "unknown", + "unknown", + "no-authorized-location-mapping", + ], + ] as const) { const result = reconcileRuntimeEntities({ - entities, - candidates: collectStaticRuntimeCandidates(parsed.layers), + entities: entities.map((entity) => + entity.kind === "script" + ? { ...entity, location: { kind: "url", value: url } } + : entity, + ), + candidates, layers: parsed.layers, }); expect( result.items.find(({ entity_kind }) => entity_kind === "script"), - ).toMatchObject({ - status, - confidence: status === "unknown" ? "unknown" : "high", - reason: - status === "unknown" - ? "no-authorized-location-mapping" - : "no-static-content-match", - }); - }, -); + url, + ).toMatchObject({ status, confidence, reason, candidate_static_count: 0 }); + } +}); diff --git a/tests/boundary/filesystem/javascript-applications/javascriptRuntimeReconciliation.test.ts b/tests/boundary/filesystem/javascript-applications/javascriptRuntimeReconciliation.test.ts index 1affba27cf..3618647f85 100644 --- a/tests/boundary/filesystem/javascript-applications/javascriptRuntimeReconciliation.test.ts +++ b/tests/boundary/filesystem/javascript-applications/javascriptRuntimeReconciliation.test.ts @@ -24,10 +24,7 @@ import { reconcileJavaScriptRuntimeInputSchema, javascriptRuntimeReconciliationResultSchema, } from "../../../../src/domain/javascript/javascriptRuntimeReconciliationSchemas.js"; -import { - electronActiveObservationInputSchema, - type ElectronActiveObservationInput, -} from "../../../../src/domain/javascript/electronActiveObservation.js"; +import { electronActiveObservationInputSchema } from "../../../../src/domain/javascript/electronActiveObservation.js"; import { createElectronActiveObservationFixtureResult } from "../../../../src/domain/javascript/electronActiveObservation.fixture.js"; import { @@ -48,23 +45,18 @@ const readInput = (input: unknown) => { const reconcileInput = (input: unknown) => reconcileJavaScriptRuntime(readInput(input)); -it.each([ - { - parameter: "include_script_sources", - value: false, - message: /source.*without source-capture selection/i, - }, - { - parameter: "target_id", - value: " target-main ", - message: /target.*disagrees.*captured result/i, - }, -])( - "preserves $parameter input paths for runtime Evidence", - async ({ parameter, value, message }) => { - const root = await applicationFixture(); - const application = await analyzeFixture(root); - const runtime = electronRuntimeEvidence(root, SOURCE); +it("attributes inconsistent runtime Evidence parameters to the exact input paths", async () => { + const root = await applicationFixture(); + const application = await analyzeFixture(root); + const runtime = electronRuntimeEvidence(root, SOURCE); + for (const [parameter, value, message] of [ + [ + "include_script_sources", + false, + /source.*without source-capture selection/i, + ], + ["target_id", " target-main ", /target.*disagrees.*captured result/i], + ] as const) { const invalidRuntime = createEvidence( evidenceSubjectInput(runtime.subject), runtime.provider, @@ -77,15 +69,17 @@ it.each([ authority: runtime.authority, }, ); - - const result = reconcileJavaScriptRuntimeEvidence({ - static_layers: [{ role: "application", analysis: application }], - runtime_observations: [invalidRuntime], - }); - expect(result).toMatchObject({ + expect( + reconcileJavaScriptRuntimeEvidence({ + static_layers: [{ role: "application", analysis: application }], + runtime_observations: [invalidRuntime], + }), + parameter, + ).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError", + operation: "reconcile_javascript_runtime", issues: [ { path: ["runtime_observations", 0, "parameters", parameter], @@ -95,8 +89,8 @@ it.each([ ], }, }); - }, -); + } +}); it("reconciles runtime scripts inside dot-prefixed child directories", async () => { const root = await applicationFixture(); @@ -134,9 +128,9 @@ it("matches renderer, frame, script bytes, and worker without claiming execution runtime_observations: [runtimeEvidence], }); - expect(() => - javascriptRuntimeReconciliationResultSchema.parse(result), - ).not.toThrow(); + expect(javascriptRuntimeReconciliationResultSchema.parse(result)).toEqual( + result, + ); expect(result.source_map_authority).toMatchObject({ used_for_primary_matching: false, static_layer_count: 1, @@ -179,8 +173,16 @@ it("matches renderer, frame, script bytes, and worker without claiming execution javascriptRuntimeReconciliationResultSchema.safeParse({ ...result, reconciliations: [{ ...matched, static_node_id: null }], - }).success, - ).toBe(false); + }).error?.issues, + ).toMatchObject([ + { + path: ["reconciliations", 0], + code: "invalid_union", + errors: expect.arrayContaining([ + [expect.objectContaining({ path: ["static_node_id"] })], + ]), + }, + ]); expect(result.static_load_states).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -259,115 +261,132 @@ it("matches renderer, frame, script bytes, and worker without claiming execution ); }); -it("keeps static load states unknown when one scoped capture omits scripts", async () => { +it("retains observed scripts while incomplete capture sections leave absence unknown", async () => { const fixture = await applicationFixture(); - const staticEvidence = await analyzeFixture(fixture); - const result = reconcileInput({ - static_layers: [{ role: "application", analysis: staticEvidence }], - runtime_observations: [ - electronRuntimeEvidence(fixture, SOURCE, { - includeWorker: false, - targetId: "target-complete", - }), - electronRuntimeEvidence(fixture, SOURCE, { - includeWorker: false, - scriptsUnavailable: true, - targetId: "target-incomplete", - }), - ], - }); - - expect( - result.runtime_captures.map( - ({ scripts_complete_within_scope }) => scripts_complete_within_scope, - ), - ).toEqual(expect.arrayContaining([false, true])); - expect(result.summary.static_not_observed).toBe(0); - expect(result.static_load_states).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - status: "unknown", - reason: "static-or-runtime-coverage-incomplete", - }), - ]), - ); -}); - -it("does not infer absence from an incomplete capture with no mapped entities", async () => { - const fixture = await applicationFixture(); - const outside = await applicationFixture(); + const outside = await createTestTempDirectory("rea-outside-runtime-scope-"); const analysis = await analyzeFixture(fixture); + const { graph } = javascriptApplicationAnalysisResultSchema.parse( + analysis.normalized_result, + ); + const assetId = (path: string) => { + const asset = graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === path), + ); + if (asset === undefined) throw new Error(`Missing fixture asset ${path}`); + return asset.node_id; + }; + const appId = assetId("app.js"); + const workerId = assetId("worker.js"); + const complete = electronRuntimeEvidence(fixture, SOURCE, { includeWorker: false, targetId: "complete", }); - const outsideCapture = (scriptsUnavailable: boolean) => - electronRuntimeEvidence(outside, "export const unrelated = 1;", { + const outsideComplete = electronRuntimeEvidence( + outside, + "export const unrelated = 1;", + { includeWorker: false, - scriptsUnavailable, - targetId: "outside", - }); + targetId: "outside-complete", + }, + ); + const outsideIncomplete = electronRuntimeEvidence( + outside, + "export const unrelated = 1;", + { + includeWorker: false, + scriptsUnavailable: true, + targetId: "outside-incomplete", + }, + ); + const scopedIncomplete = electronRuntimeEvidence(fixture, SOURCE, { + includeWorker: false, + scriptsUnavailable: true, + targetId: "scoped-incomplete", + }); + const noWorkers = electronRuntimeEvidence(fixture, SOURCE, { + includeWorker: false, + workersUnavailable: true, + targetId: "workers-unavailable", + }); const reconcile = ( - runtime_observations: ReturnType[], + ...runtime_observations: ReturnType[] ) => reconcileInput({ static_layers: [{ role: "application", analysis }], runtime_observations, }); - const control = reconcile([complete, outsideCapture(false)]); - expect(control.summary.static_not_observed).toBeGreaterThan(0); - const incomplete = outsideCapture(true); - for (const captures of [ - [complete, incomplete], - [incomplete, complete], - ]) { - const result = reconcile(captures); - expect(result.summary.static_not_observed).toBe(0); - expect(result.static_load_states).toEqual( - expect.arrayContaining([ - expect.objectContaining({ status: "loaded" }), - expect.objectContaining({ - status: "unknown", - reason: "static-or-runtime-coverage-incomplete", - }), - ]), - ); - } -}); - -it("preserves positive script matches without inventing worker absence when workers are unavailable", async () => { - const fixture = await applicationFixture(); - const staticEvidence = await analyzeFixture(fixture); - const reconcile = (workersUnavailable: boolean) => - reconcileInput({ - static_layers: [{ role: "application", analysis: staticEvidence }], - runtime_observations: [ - electronRuntimeEvidence(fixture, SOURCE, { - includeWorker: false, - workersUnavailable, - }), + expect( + reconcile(complete, outsideComplete).static_load_states.find( + ({ static_node_id }) => static_node_id === workerId, + ), + ).toMatchObject({ + status: "not-observed-in-capture", + reason: "not-observed-in-bounded-capture", + }); + for (const { captures, scriptScope } of [ + { + captures: [complete, scopedIncomplete], + scriptScope: [ + ["complete", true], + ["scoped-incomplete", false], ], - }); - const complete = reconcile(false); - const incomplete = reconcile(true); - expect(complete.summary.static_not_observed).toBeGreaterThan(0); - expect(incomplete.summary.static_not_observed).toBe(0); - expect(incomplete.runtime_captures[0]?.scripts_complete_within_scope).toBe( - true, - ); - expect(incomplete.static_load_states).toContainEqual( - expect.objectContaining({ + }, + { + captures: [complete, outsideIncomplete], + scriptScope: [ + ["complete", true], + ["outside-incomplete", false], + ], + }, + { + captures: [outsideIncomplete, complete], + scriptScope: [ + ["complete", true], + ["outside-incomplete", false], + ], + }, + { captures: [noWorkers], scriptScope: [["workers-unavailable", true]] }, + ]) { + const result = reconcile(...captures); + expect( + result.runtime_captures + .map(({ target_key, scripts_complete_within_scope }) => [ + target_key, + scripts_complete_within_scope, + ]) + .sort(), + ).toEqual(scriptScope); + expect(result.summary.static_not_observed).toBe(0); + const script = result.reconciliations.find( + ({ entity_kind, status, static_node_id }) => + entity_kind === "script" && + status === "matched" && + static_node_id === appId, + ); + expect(script).toBeDefined(); + expect( + result.static_load_states.find( + ({ static_node_id }) => static_node_id === appId, + ), + ).toMatchObject({ status: "loaded", reason: "runtime-script-correspondence", - }), - ); - expect(incomplete.static_load_states).toContainEqual( - expect.objectContaining({ + runtime_node_ids: expect.arrayContaining([script?.runtime_node_id]), + }); + expect( + result.static_load_states.find( + ({ static_node_id }) => static_node_id === workerId, + ), + ).toMatchObject({ status: "unknown", reason: "static-or-runtime-coverage-incomplete", - }), - ); + runtime_node_ids: [], + }); + } }); it("preserves unknown Electron frame provenance without linking it to an authorized frame", async () => { @@ -453,124 +472,121 @@ it("preserves unknown Electron frame provenance without linking it to an authori ).toBe(false); }); -it.each(["canonical-paths", "selected-paths"] as const)( - "reconciles %s active Electron as a partial target-only runtime capture", - async (representation) => { - const fixture = await applicationFixture(); - const staticEvidence = await analyzeFixture(fixture); - const applicationPath = join(fixture, "main.js"); - const input = { - ...electronActiveObservationInputSchema.parse({ - executable_path: process.execPath, - application_path: applicationPath, - application_root: fixture, - actions: [], - }), +const activeElectronProvider = { + id: "rea-playwright-electron-active", + name: "REA Playwright active Electron observation provider", + version: "1", +}; + +it("reconciles active Electron targets through admitted paths and rejects inconsistent Evidence", async () => { + const fixture = await applicationFixture(); + const analysis = await analyzeFixture(fixture); + const applicationPath = join(fixture, "main.js"); + const alias = join(fixture, "selected-main.js"); + await symlink(applicationPath, alias); + const executablePath = await realpath(process.execPath); + const resolved = { + ...electronActiveObservationInputSchema.parse({ + executable_path: executablePath, + application_path: applicationPath, + }), + application_root: fixture, + }; + const selected = electronActiveObservationInputSchema.parse({ + executable_path: process.execPath, + application_path: alias, + }); + const capture = createElectronActiveObservationFixtureResult(applicationPath); + capture.application.executable_path = executablePath; + const canonicalEvidence = createElectronActiveEvidence( + resolved, + capture, + activeElectronProvider, + resolved, + ); + const selectedEvidence = createElectronActiveEvidence( + selected, + capture, + activeElectronProvider, + resolved, + ); + expect(selectedEvidence.parameters).toMatchObject({ + application_path: alias, + resolved_paths: { + executable_path: executablePath, + application_path: applicationPath, application_root: fixture, - }; - const capture = - createElectronActiveObservationFixtureResult(applicationPath); - let selectedInput: ElectronActiveObservationInput = input; - let resolvedInput: typeof input = input; - if (representation === "selected-paths") { - const alias = join(fixture, "selected-main.js"); - await symlink(applicationPath, alias); - selectedInput = electronActiveObservationInputSchema.parse({ - executable_path: process.execPath, - application_path: alias, - }); - resolvedInput = { - ...input, - executable_path: await realpath(process.execPath), - }; - capture.application.executable_path = resolvedInput.executable_path; - } - const runtimeEvidence = createElectronActiveEvidence( - selectedInput, - capture, - { - id: "rea-playwright-electron-active", - name: "REA Playwright active Electron observation provider", - version: "1", - }, - resolvedInput, - ); + }, + }); - const result = reconcileInput({ - static_layers: [{ role: "application", analysis: staticEvidence }], - runtime_observations: [runtimeEvidence], - }); - - expect(result.runtime_captures).toMatchObject([ - { + const static_layers = [{ role: "application" as const, analysis }]; + const result = reconcileInput({ + static_layers, + runtime_observations: [canonicalEvidence, selectedEvidence], + }); + expect(result.coverage.status).toBe("partial"); + expect(result.runtime_captures).toMatchObject( + [canonicalEvidence, selectedEvidence] + .map(({ evidence_id }) => ({ + evidence_id, kind: "electron-active", + target_location: applicationPath, scripts: 0, frames: 0, workers: 0, scripts_complete_within_scope: false, - }, - ]); - expect(result.coverage.status).toBe("partial"); - expect(result.limitations.join(" ")).toMatch( - /incomplete|not reported as executed/u, - ); - }, -); - -it("refuses active Electron Evidence without its admitted execution paths", async () => { - const fixture = await applicationFixture(); - const applicationPath = join(fixture, "main.js"); - const input = { - ...electronActiveObservationInputSchema.parse({ - executable_path: process.execPath, - application_path: applicationPath, - application_root: fixture, - actions: [], - }), - application_root: fixture, - }; - const runtime = createElectronActiveEvidence( - input, - createElectronActiveObservationFixtureResult(applicationPath), - { - id: "rea-playwright-electron-active", - name: "REA Playwright active Electron observation provider", - version: "1", - }, - input, + })) + .sort((left, right) => left.evidence_id.localeCompare(right.evidence_id)), ); - const { resolved_paths: _resolved, ...parameters } = runtime.parameters; - const withoutPaths = createEvidence( - evidenceSubjectInput(runtime.subject), - runtime.provider, - { - predicateType: runtime.predicate_type, - operation: runtime.operation, - parameters, - result: runtime.normalized_result, - confidence: runtime.confidence, - authority: runtime.authority, - }, + expect(result.reconciliations.map(({ entity_kind }) => entity_kind)).toEqual([ + "target", + "target", + ]); + expect(result.runtime_captures[0]?.target_node_id).not.toBe( + result.runtime_captures[1]?.target_node_id, ); - expect( - reconcileJavaScriptRuntimeEvidence({ - static_layers: [ - { role: "application", analysis: await analyzeFixture(fixture) }, - ], - runtime_observations: [withoutPaths], - }), - ).toMatchObject({ - ok: false, - error: { - _tag: "AnalysisInputError", - issues: [ - { - path: ["runtime_observations", 0, "parameters", "resolved_paths"], + const { resolved_paths: _paths, ...withoutPaths } = + selectedEvidence.parameters; + for (const [parameters, field] of [ + [withoutPaths, ["parameters", "resolved_paths"]], + [ + { + ...selectedEvidence.parameters, + resolved_paths: { + executable_path: executablePath, + application_path: alias, + application_root: fixture, }, - ], - }, - }); + }, + ["normalized_result", "application", "application_path"], + ], + ] as const) { + const invalid = createEvidence( + evidenceSubjectInput(selectedEvidence.subject), + selectedEvidence.provider, + { + predicateType: selectedEvidence.predicate_type, + operation: selectedEvidence.operation, + parameters, + result: selectedEvidence.normalized_result, + confidence: selectedEvidence.confidence, + authority: selectedEvidence.authority, + }, + ); + expect( + reconcileJavaScriptRuntimeEvidence({ + static_layers, + runtime_observations: [invalid], + }), + ).toMatchObject({ + ok: false, + error: { + _tag: "AnalysisInputError", + issues: [{ path: ["runtime_observations", 0, ...field] }], + }, + }); + } }); it("commits complete Electron actions independently from selected arguments", async () => { @@ -595,30 +611,24 @@ it("commits complete Electron actions independently from selected arguments", as }), application_root: fixture, }); - const provider = { - id: "rea-playwright-electron-active", - name: "REA Playwright active Electron observation provider", - version: "1", - }; const firstInput = makeInput("first-secret", "#first-secret"); const secondInput = makeInput("first-secret", "#second-secret"); const first = createElectronActiveEvidence( firstInput, createElectronActiveObservationFixtureResult(applicationPath), - provider, + activeElectronProvider, firstInput, ); const second = createElectronActiveEvidence( secondInput, createElectronActiveObservationFixtureResult(applicationPath), - provider, + activeElectronProvider, secondInput, ); expect(first.parameters.scenario_sha256).not.toBe( second.parameters.scenario_sha256, ); - expect(JSON.stringify(first)).toContain("first-secret"); expect(first.parameters.actions).toEqual([ { step_id: "click", @@ -637,77 +647,93 @@ it("commits complete Electron actions independently from selected arguments", as expect(first.parameters.args).toEqual(["--token", "first-secret"]); }); -it("imports an operator-provided cache layer through an explicit file mapping", async () => { +it("uses the operator mapping to select one occurrence of a shared cache asset", async () => { const application = await applicationFixture(); const cache = await createTestTempDirectory("rea-runtime-cache-static-"); const runtimeCache = await createTestTempDirectory("rea-runtime-cache-live-"); - const cacheSource = SOURCE; await mkdir(join(cache, "mapped")); await Promise.all([ - writeFile(join(cache, "mapped", "chunk.js"), cacheSource), + writeFile(join(cache, "mapped", "chunk.js"), SOURCE), writeFile(join(cache, "outside.js"), "export const outside = true;\n"), - writeFile(join(runtimeCache, "chunk.js"), cacheSource), - writeFile(join(runtimeCache, "index.html"), ""), ]); - const applicationEvidence = await analyzeFixture(application); - const cacheEvidence = await analyzeFixture(cache); - const runtimeEvidence = electronRuntimeEvidence(runtimeCache, cacheSource, { + const [applicationEvidence, cacheEvidence] = await Promise.all([ + analyzeFixture(application), + analyzeFixture(cache), + ]); + const runtime = electronRuntimeEvidence(runtimeCache, SOURCE, { scriptFile: "chunk.js", includeWorker: false, }); - - const result = reconcileInput({ - static_layers: [ - { role: "application", analysis: applicationEvidence }, - { - role: "cache", - analysis: cacheEvidence, - runtime_mappings: [ - { - kind: "file-root", - root: runtimeCache, - artifact_prefix: "mapped", - }, - ], - }, - ], - runtime_observations: [runtimeEvidence], - }); - const script = result.reconciliations.find( - ({ entity_kind: kind }) => kind === "script", + const reconcile = (withMapping: boolean) => + reconcileInput({ + static_layers: [ + { role: "application", analysis: applicationEvidence }, + { + role: "cache", + analysis: cacheEvidence, + runtime_mappings: withMapping + ? [ + { + kind: "file-root", + root: runtimeCache, + artifact_prefix: "mapped", + }, + ] + : [], + }, + ], + runtime_observations: [runtime], + }); + const mapped = reconcile(true); + const unmapped = reconcile(false); + const cacheLayer = mapped.static_layers.find(({ role }) => role === "cache"); + const shared = mapped.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === "app.js"), ); - const cacheLayer = result.static_layers.find(({ role }) => role === "cache"); - - expect(script).toMatchObject({ + expect( + shared?.observations.map(({ properties }) => properties.path).sort(), + ).toEqual(["app.js", "mapped/chunk.js"]); + expect( + mapped.reconciliations.find(({ entity_kind }) => entity_kind === "script"), + ).toMatchObject({ status: "matched", basis: "content-and-location", static_layer_id: cacheLayer?.layer_id, + static_node_id: shared?.node_id, + candidate_static_count: 1, }); - const outside = result.graph.nodes.find( - (node) => - node.kind === "javascript-asset" && - node.observations.some( - ({ properties }) => properties.path === "outside.js", - ), + const outside = mapped.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === "outside.js"), ); expect( - result.static_load_states.find( - ({ static_node_id: nodeId }) => nodeId === outside?.node_id, + mapped.static_load_states.find( + ({ static_node_id }) => static_node_id === outside?.node_id, ), ).toMatchObject({ + static_layer_id: cacheLayer?.layer_id, status: "unknown", reason: "layer-outside-runtime-scope", }); - const sharedFile = result.graph.nodes.find( - ({ observations }) => - observations.some(({ properties }) => properties.path === "app.js") && - observations.some( - ({ properties }) => properties.path === "mapped/chunk.js", - ), + + const script = unmapped.reconciliations.find( + ({ entity_kind }) => entity_kind === "script", + ); + expect(script).toMatchObject({ + status: "ambiguous", + reason: "ambiguous-static-candidates", + candidate_static_count: 2, + static_node_id: null, + }); + expect(script?.candidate_static_nodes).toEqual( + unmapped.static_layers.map(({ layer_id }) => ({ + static_layer_id: layer_id, + static_node_id: shared?.node_id, + })), ); - expect( - sharedFile?.observations.map(({ properties }) => properties.path), - ).toEqual(expect.arrayContaining(["app.js", "mapped/chunk.js"])); }); const sourceFreeRuntime = (runtimeRoot: string, scriptFile: string) => { @@ -756,43 +782,33 @@ const reconcileScriptLocation = ( }).reconciliations.find(({ entity_kind }) => entity_kind === "script"); }; -it("matches a source-free bundled script to its containing asset without suppressing independent layers", async () => { +it("uses a whole bundle for location-only matches and exact bytes for factory matches", async () => { const root = await createTestTempDirectory("rea-runtime-bundler-location-"); - const source = - '(globalThis["webpackChunkfixture"] = globalThis["webpackChunkfixture"] || []).push([["main"],{1:(module)=>{module.exports=1;}},(__webpack_require__)=>{__webpack_require__(1);}]);\n'; + const factory = "(module)=>{module.exports=1;}"; + const source = `(globalThis["webpackChunkfixture"] = globalThis["webpackChunkfixture"] || []).push([["main"],{1:${factory}},(__webpack_require__)=>{__webpack_require__(1);}]);\n`; await writeFile(join(root, "runtime.js"), source); const application = await analyzeFixture(root); + const { graph } = javascriptApplicationAnalysisResultSchema.parse( + application.normalized_result, + ); + const asset = graph.nodes.find(({ kind }) => kind === "javascript-asset"); + const module = graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-module" && + observations.some(({ properties }) => properties.module_key === "1"), + ); + if (asset === undefined || module === undefined) + throw new Error("Missing fixture bundle or factory"); const secondRoot = await createTestTempDirectory( "rea-runtime-bundler-other-", ); await writeFile(join(secondRoot, "runtime.js"), source); const secondApplication = await analyzeFixture(secondRoot); - const reconcile = (sourceIncluded: boolean, twice = false) => - reconcileInput({ - static_layers: [ - { role: "application", analysis: application }, - ...(twice - ? [ - { - role: "cache", - analysis: secondApplication, - runtime_mappings: [ - { kind: "file-root", root, artifact_prefix: "" }, - ], - }, - ] - : []), - ], - runtime_observations: [ - sourceIncluded - ? electronRuntimeEvidence(root, source, { - scriptFile: "runtime.js", - includeWorker: false, - }) - : sourceFreeRuntime(root, "runtime.js"), - ], - }); - const withoutSource = reconcile(false); + const sourceFree = sourceFreeRuntime(root, "runtime.js"); + const withoutSource = reconcileInput({ + static_layers: [{ role: "application", analysis: application }], + runtime_observations: [sourceFree], + }); const script = withoutSource.reconciliations.find( ({ entity_kind }) => entity_kind === "script", ); @@ -801,105 +817,122 @@ it("matches a source-free bundled script to its containing asset without suppres basis: "artifact-path", confidence: "medium", candidate_static_count: 1, + static_node_id: asset.node_id, }); - expect( - withoutSource.graph.nodes.find( - ({ node_id }) => node_id === script?.static_node_id, - )?.kind, - ).toBe("javascript-asset"); expect(withoutSource.static_load_states).toEqual( expect.arrayContaining([ - expect.objectContaining({ status: "loaded" }), - expect.objectContaining({ status: "resident-in-loaded-asset" }), + expect.objectContaining({ + static_node_id: asset.node_id, + status: "loaded", + runtime_node_ids: [script?.runtime_node_id], + }), + expect.objectContaining({ + static_node_id: module.node_id, + status: "resident-in-loaded-asset", + runtime_node_ids: [], + }), ]), ); - expect( - reconcile(true).reconciliations.find( - ({ entity_kind }) => entity_kind === "script", - ), - ).toMatchObject({ - status: "matched", - basis: "content-and-location", - }); - const moduleCapture = reconcileInput({ - static_layers: [{ role: "application", analysis: application }], - runtime_observations: [ - electronRuntimeEvidence(root, "(module)=>{module.exports=1;}", { - scriptFile: "runtime.js", - includeWorker: false, - }), - ], - }); - const moduleMatch = moduleCapture.reconciliations.find( - ({ entity_kind }) => entity_kind === "script", + + const captures = [source, factory].map((text, index) => + electronRuntimeEvidence(root, text, { + scriptFile: "runtime.js", + includeWorker: false, + targetId: `script-${index}`, + }), ); - expect(moduleMatch).toMatchObject({ - status: "matched", - basis: "content-and-location", + const withSource = reconcileInput({ + static_layers: [{ role: "application", analysis: application }], + runtime_observations: captures, }); - expect( - moduleCapture.graph.nodes.find( - ({ node_id }) => node_id === moduleMatch?.static_node_id, - )?.kind, - ).toBe("javascript-module"); - expect( - reconcile(false, true).reconciliations.find( - ({ entity_kind }) => entity_kind === "script", - ), - ).toMatchObject({ - status: "ambiguous", - reason: "ambiguous-static-candidates", - }); -}); - -it.skipIf(process.platform === "win32")( - "keeps distinct POSIX backslash and slash script locations separate", - async () => { - const root = await createTestTempDirectory("rea-backslash-runtime-"); - const literal = "literal\\name.js"; - await writeFile(join(root, literal), "export const marker = 'literal';\n"); - const application = await analyzeFixture(root); + for (const [index, nodeId] of [asset.node_id, module.node_id].entries()) expect( - reconcileScriptLocation(application, root, "literal/name.js"), - ).toMatchObject({ status: "unmatched" }); - expect(reconcileScriptLocation(application, root, literal)).toMatchObject({ - status: "matched", - basis: "artifact-path", - }); - }, -); - -it.skipIf(process.platform === "win32")( - "does not make POSIX literal and slash paths ambiguous, including Windows runtime mappings", - async () => { - const root = await createTestTempDirectory("rea-backslash-runtime-pair-"); - const literal = "literal\\name.js"; - await mkdir(join(root, "literal")); - await writeFile(join(root, literal), "export const marker = 'literal';\n"); - await writeFile( - join(root, "literal", "name.js"), - "export const marker = 'slash';\n", - ); - const application = await analyzeFixture(root); - const slash = reconcileScriptLocation(application, root, "literal/name.js"); - const backslash = reconcileScriptLocation(application, root, literal); - expect(slash).toMatchObject({ status: "matched", basis: "artifact-path" }); - expect(backslash).toMatchObject({ - status: "matched", - basis: "artifact-path", - }); - expect(slash?.static_node_id).not.toBe(backslash?.static_node_id); - // These recorded Windows paths exercise matching only, not native Windows IO. - expect( - reconcileScriptLocation( - application, - root, - "literal\\name.js", - "C:\\runtime", + withSource.reconciliations.find( + ({ entity_kind, runtime_evidence_id }) => + entity_kind === "script" && + runtime_evidence_id === captures[index]?.evidence_id, ), ).toMatchObject({ status: "matched", - static_node_id: slash?.static_node_id, + basis: "content-and-location", + static_node_id: nodeId, + candidate_static_count: 1, + }); + + const acrossLayers = reconcileInput({ + static_layers: [ + { role: "application", analysis: application }, + { + role: "cache", + analysis: secondApplication, + runtime_mappings: [{ kind: "file-root", root, artifact_prefix: "" }], + }, + ], + runtime_observations: [sourceFree], + }); + const ambiguous = acrossLayers.reconciliations.find( + ({ entity_kind }) => entity_kind === "script", + ); + expect(ambiguous).toMatchObject({ + status: "ambiguous", + reason: "ambiguous-static-candidates", + candidate_static_count: 2, + static_node_id: null, + }); + expect(ambiguous?.candidate_static_nodes).toEqual( + acrossLayers.static_layers.map(({ layer_id }) => ({ + static_layer_id: layer_id, + static_node_id: asset.node_id, + })), + ); +}); + +it.skipIf(process.platform === "win32")( + "matches POSIX literal paths and Windows separators without conflating slash decoys", + async () => { + const root = await createTestTempDirectory("rea-backslash-runtime-pair-"); + const onlyLiteral = "missing\\sibling.js"; + const literal = "literal\\name.js"; + await mkdir(join(root, "literal")); + await Promise.all([ + writeFile(join(root, onlyLiteral), "export const marker = 'unpaired';\n"), + writeFile(join(root, literal), "export const marker = 'literal';\n"), + writeFile( + join(root, "literal", "name.js"), + "export const marker = 'slash';\n", + ), + ]); + const application = await analyzeFixture(root); + const { graph } = javascriptApplicationAnalysisResultSchema.parse( + application.normalized_result, + ); + const assetId = (path: string) => { + const node = graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === path), + ); + if (node === undefined) throw new Error(`Missing fixture asset ${path}`); + return node.node_id; + }; + for (const path of [onlyLiteral, literal, "literal/name.js"]) + expect(reconcileScriptLocation(application, root, path)).toMatchObject({ + status: "matched", + basis: "artifact-path", + static_node_id: assetId(path), + }); + expect( + reconcileScriptLocation(application, root, "missing/sibling.js"), + ).toMatchObject({ + status: "unmatched", + }); + // Recorded Windows paths exercise matching without native Windows IO. + expect( + reconcileScriptLocation(application, root, literal, "C:\\runtime"), + ).toMatchObject({ + status: "matched", + basis: "operator-file-mapping", + static_node_id: assetId("literal/name.js"), }); }, ); @@ -1017,33 +1050,54 @@ it.skipIf(process.platform === "win32")( }, ); -it.skipIf(process.platform === "win32").each(["literal\\dir", "literal%5Cdir"])( - "maps a runtime root to the exact logical artifact prefix %j", - async (prefix) => { +it.skipIf(process.platform === "win32")( + "maps literal and percent-escaped artifact prefixes to different assets", + async () => { const root = await createTestTempDirectory("rea-literal-artifact-prefix-"); - await mkdir(join(root, prefix)); - await writeFile( - join(root, prefix, "app.js"), - "export const marker = 42;\n", - ); + const prefixes = ["literal\\dir", "literal%5Cdir"] as const; + for (const [index, prefix] of prefixes.entries()) { + await mkdir(join(root, prefix)); + await writeFile( + join(root, prefix, "app.js"), + `export const marker = ${index};\n`, + ); + } const analysis = await analyzeFixture(root); - const result = reconcileInput({ - static_layers: [ - { - analysis, - role: "application", - runtime_mappings: [ - { kind: "file-root", root: "/deployed", artifact_prefix: prefix }, - ], - }, - ], - runtime_observations: [sourceFreeRuntime("/deployed", "app.js")], - }); - expect( - result.reconciliations.find( - ({ entity_kind }) => entity_kind === "script", - ), - ).toMatchObject({ status: "matched", basis: "operator-file-mapping" }); + const { graph } = javascriptApplicationAnalysisResultSchema.parse( + analysis.normalized_result, + ); + for (const prefix of prefixes) { + const expected = graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some( + ({ properties }) => properties.path === `${prefix}/app.js`, + ), + ); + if (expected === undefined) + throw new Error(`Missing fixture asset ${prefix}/app.js`); + const result = reconcileInput({ + static_layers: [ + { + analysis, + role: "application", + runtime_mappings: [ + { kind: "file-root", root: "/deployed", artifact_prefix: prefix }, + ], + }, + ], + runtime_observations: [sourceFreeRuntime("/deployed", "app.js")], + }); + expect( + result.reconciliations.find( + ({ entity_kind }) => entity_kind === "script", + ), + ).toMatchObject({ + status: "matched", + basis: "operator-file-mapping", + static_node_id: expected.node_id, + }); + } }, ); @@ -1082,7 +1136,7 @@ it("rejects unsafe logical artifact prefixes without decoding literal names", as const root = await createTestTempDirectory("rea-prefix-admission-"); await writeFile(join(root, "app.js"), "export const marker = 42;\n"); const analysis = await analyzeFixture(root); - const accepts = (artifact_prefix: string) => + const admission = (artifact_prefix: string) => reconcileJavaScriptRuntimeInputSchema.safeParse({ static_layers: [ { @@ -1094,7 +1148,7 @@ it("rejects unsafe logical artifact prefixes without decoding literal names", as }, ], runtime_observations: [sourceFreeRuntime("/deployed", "app.js")], - }).success; + }); for (const prefix of [ "\0", "/absolute", @@ -1105,9 +1159,17 @@ it("rejects unsafe logical artifact prefixes without decoding literal names", as "a/", "./a", ]) - expect(accepts(prefix), prefix).toBe(false); + expect(admission(prefix).error?.issues, prefix).toMatchObject([ + { + path: ["static_layers", 0, "runtime_mappings", 0, "artifact_prefix"], + message: "Artifact prefix must be empty or a safe relative POSIX path", + }, + ]); for (const prefix of ["", "literal\\dir", "literal%5Cdir", "literal%2Fdir"]) - expect(accepts(prefix), prefix).toBe(true); + expect( + admission(prefix).data?.static_layers[0]?.runtime_mappings, + prefix, + ).toMatchObject([{ artifact_prefix: prefix }]); }); it.skipIf(process.platform === "win32")( @@ -1142,142 +1204,201 @@ it.skipIf(process.platform === "win32")( }, ); -it.each([false, true])( - "merges equivalent owned page declarations when target presence is %s", - async (present) => { - const root = await createTestTempDirectory("rea-generated-page-runtime-"); - await mkdir(join(root, "app"), { recursive: true }); - await writeFile( +it("matches owned and raw renderer declarations against their respective application roots", async () => { + const root = await createTestTempDirectory("rea-renderer-ownership-"); + await Promise.all( + ["app/dist", "dist", "sub/dist"].map((directory) => + mkdir(join(root, directory), { recursive: true }), + ), + ); + const renderer = "const { BrowserWindow } = require('electron');"; + await Promise.all([ + writeFile( join(root, "app", "package.json"), JSON.stringify({ main: "main.cjs" }), - ); - await writeFile( + ), + writeFile( join(root, "app", "main.cjs"), - 'const {BrowserWindow} = require("electron"); const w = new BrowserWindow(); w.loadFile("generated.html"); const other = new BrowserWindow(); other.loadFile("./generated.html");', + `${renderer} + new BrowserWindow().loadFile("generated.html"); + new BrowserWindow().loadFile("./generated.html"); + new BrowserWindow().loadFile("materialized.html"); + new BrowserWindow().loadFile("./materialized.html"); + new BrowserWindow().loadFile("dist/owned.html"); + `, + ), + writeFile( + join(root, "sub", "main.cjs"), + `${renderer} + new BrowserWindow().loadFile("dist/raw.html"); + `, + ), + writeFile(join(root, "app", "materialized.html"), "present declared page"), + writeFile(join(root, "app", "dist", "owned.html"), "owned target"), + writeFile(join(root, "dist", "owned.html"), "root decoy"), + writeFile(join(root, "dist", "raw.html"), "raw application target"), + writeFile(join(root, "sub", "dist", "raw.html"), "nested decoy"), + ]); + const analysis = await analyzeFixture(root); + const { graph } = javascriptApplicationAnalysisResultSchema.parse( + analysis.normalized_result, + ); + const roles = graph.nodes.filter(({ kind }) => kind === "electron-renderer"); + expect(roles).toHaveLength(4); + + const cases = [ + { + path: "app/generated.html", + declarations: ["./generated.html", "generated.html"], + }, + { + path: "app/materialized.html", + declarations: ["./materialized.html", "materialized.html"], + }, + { path: "app/dist/owned.html", declarations: ["dist/owned.html"] }, + { path: "dist/raw.html", declarations: ["dist/raw.html"] }, + ]; + const matched = cases.map(({ path, declarations }) => { + const role = roles.find(({ observations }) => + observations.some( + ({ properties }) => properties.declared_path === declarations[0], + ), ); - if (present) - await writeFile(join(root, "app", "generated.html"), "generated page"); - const analysis = await analyzeFixture(root); - const { graph } = javascriptApplicationAnalysisResultSchema.parse( - analysis.normalized_result, - ); - const roles = graph.nodes.filter( - ({ kind }) => kind === "electron-renderer", - ); - expect(roles).toHaveLength(1); + if (role === undefined) + throw new Error(`Missing fixture renderer for ${path}`); expect( - roles[0]?.observations + role.observations .map(({ properties }) => properties.declared_path) .sort(), - ).toEqual(["./generated.html", "generated.html"]); - const reconcileTarget = (path: string) => - reconcileInput({ - static_layers: [{ analysis, role: "application" }], - runtime_observations: [rendererCapture(root, path)], - }).reconciliations.find(({ entity_kind }) => entity_kind === "target"); - expect(reconcileTarget(join(root, "app", "generated.html"))).toMatchObject({ + ).toEqual(declarations); + return { + evidence: rendererCapture(root, join(root, path)), + nodeId: role.node_id, + }; + }); + const decoys = [ + "generated.html", + "materialized.html", + "dist/owned.html", + "sub/dist/raw.html", + ].map((path) => rendererCapture(root, join(root, path))); + const result = reconcileInput({ + static_layers: [{ analysis, role: "application" }], + runtime_observations: [ + ...matched.map(({ evidence }) => evidence), + ...decoys, + ], + }); + const targets = result.reconciliations.filter( + ({ entity_kind }) => entity_kind === "target", + ); + expect(targets).toHaveLength(8); + for (const { evidence, nodeId } of matched) + expect( + targets.find( + ({ runtime_evidence_id }) => + runtime_evidence_id === evidence.evidence_id, + ), + ).toMatchObject({ status: "matched", + basis: "artifact-path", + static_node_id: nodeId, candidate_static_count: 1, }); - expect(reconcileTarget(join(root, "generated.html"))).toMatchObject({ + for (const evidence of decoys) + expect( + targets.find( + ({ runtime_evidence_id }) => + runtime_evidence_id === evidence.evidence_id, + ), + ).toMatchObject({ status: "unmatched", + static_node_id: null, + candidate_static_count: 0, }); - }, -); - -it("matches a resolved nested application path without an operator mapping", async () => { - const root = await createTestTempDirectory("rea-owned-loadfile-runtime-"); - await mkdir(join(root, "app", "dist"), { recursive: true }); - await mkdir(join(root, "dist"), { recursive: true }); - await writeFile( - join(root, "app", "package.json"), - JSON.stringify({ main: "main.cjs" }), - ); - await writeFile( - join(root, "app", "main.cjs"), - "const { BrowserWindow } = require('electron'); const win = new BrowserWindow(); win.loadFile('dist/index.html');", - ); - await writeFile( - join(root, "app", "dist", "index.html"), - "application target", - ); - await writeFile(join(root, "dist", "index.html"), "root decoy"); - const analysis = await analyzeFixture(root); - const reconcileTarget = (path: string) => - reconcileInput({ - static_layers: [{ analysis, role: "application" }], - runtime_observations: [rendererCapture(root, path)], - }).reconciliations.find(({ entity_kind }) => entity_kind === "target"); - expect( - reconcileTarget(join(root, "app", "dist", "index.html")), - ).toMatchObject({ status: "matched" }); - expect(reconcileTarget(join(root, "dist", "index.html"))).toMatchObject({ - status: "unmatched", - }); }); -it("keeps a raw nested loadFile target application-relative when the producer also resolves a nested decoy", async () => { - const root = await createTestTempDirectory("rea-raw-loadfile-ownership-"); - await mkdir(join(root, "dist")); - await mkdir(join(root, "sub", "dist"), { recursive: true }); - await writeFile(join(root, "dist", "index.html"), "application target"); - await writeFile(join(root, "sub", "dist", "index.html"), "nested decoy"); - await writeFile( - join(root, "sub", "main.cjs"), - "const { BrowserWindow } = require('electron'); const win = new BrowserWindow(); win.loadFile('dist/index.html');", - ); - const analysis = await analyzeFixture(root); - const reconcileTarget = (path: string) => - reconcileInput({ - static_layers: [{ analysis, role: "application" }], - runtime_observations: [rendererCapture(root, path)], - }).reconciliations.find(({ entity_kind }) => entity_kind === "target"); - expect(reconcileTarget(join(root, "dist", "index.html"))).toMatchObject({ - status: "matched", - }); - expect( - reconcileTarget(join(root, "sub", "dist", "index.html")), - ).toMatchObject({ status: "unmatched" }); -}); - -it - .skipIf(process.platform === "win32") - .each(["app", "literal\\app", "café", "cafe\u0301"])( - "maps raw renderer declarations through the logical application prefix %j", - async (prefix) => { +it.skipIf(process.platform === "win32")( + "maps raw renderer declarations through the exact logical artifact prefix", + async () => { const root = await createTestTempDirectory("rea-prefixed-native-renderer-"); - await mkdir(join(root, prefix, "dist"), { recursive: true }); - await writeFile( - join(root, prefix, "dist", "index.html"), - "application target", - ); - await writeFile( - join(root, prefix, "main.cjs"), - "const { BrowserWindow } = require('electron'); const win = new BrowserWindow(); win.loadFile('dist/index.html');", - ); + // Separate the Unicode spellings' parents so the fixture also works on + // filesystems that treat canonically equivalent names as the same entry. + const prefixes = ["app", "literal\\app", "nfc/café", "nfd/cafe\u0301"]; + for (const prefix of prefixes) { + await mkdir(join(root, prefix, "dist"), { recursive: true }); + await writeFile( + join(root, prefix, "dist", "index.html"), + `target ${prefix}`, + ); + await writeFile( + join(root, prefix, "main.cjs"), + "const { BrowserWindow } = require('electron'); const win = new BrowserWindow(); win.loadFile('dist/index.html');", + ); + } + const sources = ["app", "literal\\app", "nfc/café", "nfd/café"]; const analysis = await analyzeFixture(root); - const reconcileTarget = (runtimeRoot: string, path: string) => - reconcileInput({ + const unix = rendererCapture("/deployed", "/deployed/dist/index.html"); + const windows = rendererCapture( + "C:\\deployed", + "C:\\deployed\\dist\\index.html", + ); + const decoy = rendererCapture("/deployed", "/deployed/index.html"); + for (const [index, prefix] of prefixes.entries()) { + const result = reconcileInput({ static_layers: [ { analysis, role: "application", runtime_mappings: [ - { kind: "file-root", root: runtimeRoot, artifact_prefix: prefix }, + { kind: "file-root", root: "/deployed", artifact_prefix: prefix }, + { + kind: "file-root", + root: "C:\\deployed", + artifact_prefix: prefix, + }, ], }, ], - runtime_observations: [rendererCapture(runtimeRoot, path)], - }).reconciliations.find(({ entity_kind }) => entity_kind === "target"); - expect( - reconcileTarget("/deployed", "/deployed/dist/index.html"), - ).toMatchObject({ status: "matched", basis: "operator-file-mapping" }); - expect( - reconcileTarget("C:\\deployed", "C:\\deployed\\dist\\index.html"), - ).toMatchObject({ status: "matched", basis: "operator-file-mapping" }); - expect(reconcileTarget("/deployed", "/deployed/index.html")).toMatchObject({ - status: "unmatched", - }); + runtime_observations: [unix, windows, decoy], + }); + const targets = result.reconciliations.filter( + ({ entity_kind }) => entity_kind === "target", + ); + expect(targets).toHaveLength(3); + for (const capture of [unix, windows]) { + const target = targets.find( + ({ runtime_evidence_id }) => + runtime_evidence_id === capture.evidence_id, + ); + expect(target, prefix).toMatchObject({ + status: "matched", + basis: "operator-file-mapping", + candidate_static_count: 1, + }); + const matched = result.graph.nodes.find( + ({ node_id }) => node_id === target?.static_node_id, + ); + expect( + matched?.observations.map(({ evidence }) => evidence.location), + prefix, + ).toMatchObject([ + { + available: true, + value: { + kind: "source-range", + source: `${sources[index]}/main.cjs`, + }, + }, + ]); + } + expect( + targets.find( + ({ runtime_evidence_id }) => + runtime_evidence_id === decoy.evidence_id, + ), + ).toMatchObject({ status: "unmatched" }); + } }, ); diff --git a/tests/boundary/inspector/javascriptRuntimeNodeTarget.test.ts b/tests/boundary/inspector/javascriptRuntimeNodeTarget.test.ts index c56840195b..6f9ae5a67f 100644 --- a/tests/boundary/inspector/javascriptRuntimeNodeTarget.test.ts +++ b/tests/boundary/inspector/javascriptRuntimeNodeTarget.test.ts @@ -3,35 +3,47 @@ import { reconcileJavaScriptRuntimeEvidence } from "../../../src/application/jav import { javascriptRuntimeReconciliationResultSchema } from "../../../src/domain/javascript/javascriptRuntimeReconciliationSchemas.js"; import { nodeReconciliationFixture } from "../../fixtures/nodeRuntimeReconciliation.js"; -test.each([false, true])( - "matches a synthetic Node entry target without inventing script presence: %s", - (scriptObserved) => { - const reconciled = reconcileJavaScriptRuntimeEvidence( - nodeReconciliationFixture(scriptObserved), - ); - if (!reconciled.ok) throw reconciled.error; - const result = javascriptRuntimeReconciliationResultSchema.parse( - reconciled.value.evidence.normalized_result, - ); - const target = result.reconciliations.find( - ({ entity_kind }) => entity_kind === "target", - ); - expect(target).toMatchObject({ - status: "matched", - basis: "artifact-path", - confidence: "medium", - reason: "unique-location-match", - candidate_static_count: 1, - }); - expect( - result.graph.nodes.find( - ({ node_id }) => node_id === target?.static_node_id, - )?.kind, - ).toBe("javascript-asset"); - expect(result.summary).toMatchObject({ - runtime_scripts: scriptObserved ? 1 : 0, - static_loaded: scriptObserved ? 1 : 0, - static_not_observed: 0, - }); - }, -); +test("attributes loaded script state to the reported Node script", () => { + const reconciled = reconcileJavaScriptRuntimeEvidence( + nodeReconciliationFixture(true), + ); + if (!reconciled.ok) throw reconciled.error; + const result = javascriptRuntimeReconciliationResultSchema.parse( + reconciled.value.evidence.normalized_result, + ); + const asset = result.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === "renderer.js"), + ); + if (asset === undefined) throw new Error("Missing fixture renderer asset"); + const target = result.reconciliations.find( + ({ entity_kind }) => entity_kind === "target", + ); + const script = result.reconciliations.find( + ({ entity_kind }) => entity_kind === "script", + ); + expect(target).toMatchObject({ + status: "matched", + static_node_id: asset.node_id, + }); + expect(script).toMatchObject({ + status: "matched", + static_node_id: asset.node_id, + }); + expect(script?.runtime_node_id).not.toBe(target?.runtime_node_id); + expect( + result.static_load_states.find( + ({ static_node_id }) => static_node_id === asset.node_id, + ), + ).toMatchObject({ + status: "loaded", + reason: "runtime-script-correspondence", + runtime_node_ids: [script?.runtime_node_id], + }); + expect(result.summary).toMatchObject({ + runtime_scripts: 1, + static_loaded: 1, + static_not_observed: 0, + }); +}); diff --git a/tests/boundary/inspector/javascriptRuntimeObservation.test.ts b/tests/boundary/inspector/javascriptRuntimeObservation.test.ts index aeda543f51..509672a84f 100644 --- a/tests/boundary/inspector/javascriptRuntimeObservation.test.ts +++ b/tests/boundary/inspector/javascriptRuntimeObservation.test.ts @@ -7,10 +7,7 @@ import { describe, expect, test } from "vitest"; import { createJavaScriptRuntimeObservationEvidence } from "../../../src/application/javascript/JavaScriptRuntimeObservationEvidence.js"; import { createBrowserEvidence } from "../../../src/application/BrowserEvidence.js"; -import { - listJavaScriptRuntimeTargets, - observeJavaScriptRuntime, -} from "../../../src/application/javascript/JavaScriptRuntimeObservationService.js"; +import { observeJavaScriptRuntime } from "../../../src/application/javascript/JavaScriptRuntimeObservationService.js"; import { reconcileJavaScriptRuntimeEvidence } from "../../../src/application/javascript/JavaScriptRuntimeReconciliationService.js"; import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js"; import { V8_INSPECTOR_PROVIDER_IDENTITY } from "../../../src/inspector/providerIdentity.js"; @@ -240,83 +237,71 @@ describe("Inspector execution-context lifecycle metadata", () => { }); }); -describe("Inspector execution-context ID validation", () => { - test.each([ - ["malformed", "2", "1"], - ["valid", 2, 1], - ] as const)( - "accounts for %s context IDs in lifecycle events", - async (mode, createdId, destroyedId) => { - const fixture = await runtimeFixture(); - const fake = await startFakeV8Inspector({ - targetUrl: pathToFileURL(fixture.entry).href, - runtimeEvents: [ - { - method: "Runtime.executionContextCreated", - params: { context: { id: 1, name: "main", origin: "" } }, - }, - { - method: "Runtime.executionContextCreated", - params: { context: { id: createdId, name: "worker", origin: "" } }, - }, - { - method: "Runtime.executionContextDestroyed", - params: { executionContextId: destroyedId }, - }, - ], - scriptEvents: [], - }); - try { - const result = await new V8InspectorProvider().observe( - observeInput(fake.endpoint, fake.targetId, "node"), - ); - if (!result.ok) throw result.error; - const invalidContextUnknowns = result.value.unknowns.filter((text) => - text.includes("malformed execution-context ID"), - ); - expect(result.value.capture).toMatchObject({ - events_observed: 3, - events_retained: 3, - events_dropped: 0, - truncated: false, - }); - if (mode === "malformed") { - expect(result.value.execution_contexts).toEqual([ - { - context_key: "context_0", - execution_context_id: 1, - state: "created", - name: "main", - origin: "", - }, - ]); - expect(invalidContextUnknowns).toEqual([ - "2 execution-context lifecycle events with a missing or malformed execution-context ID were excluded; the affected contexts' lifecycle state is unknown.", - ]); - return; - } - expect(result.value.execution_contexts).toEqual([ - { - context_key: "context_0", - execution_context_id: 1, - state: "destroyed", - name: "main", - origin: "", - }, - { - context_key: "context_1", - execution_context_id: 2, - state: "created", - name: "worker", - origin: "", - }, - ]); - expect(invalidContextUnknowns).toEqual([]); - } finally { - await fake.close(); - } - }, - ); +test("counts malformed lifecycle events without altering valid Inspector contexts", async () => { + const fixture = await runtimeFixture(); + const fake = await startFakeV8Inspector({ + targetUrl: pathToFileURL(fixture.entry).href, + runtimeEvents: [ + { + method: "Runtime.executionContextCreated", + params: { context: { id: 1, name: "main", origin: "" } }, + }, + { + method: "Runtime.executionContextCreated", + params: { context: { id: "2", name: "bad", origin: "" } }, + }, + { + method: "Runtime.executionContextDestroyed", + params: { executionContextId: "1" }, + }, + { + method: "Runtime.executionContextCreated", + params: { context: { id: 2, name: "worker", origin: "" } }, + }, + { + method: "Runtime.executionContextDestroyed", + params: { executionContextId: 2 }, + }, + ], + scriptEvents: [], + }); + try { + const result = await new V8InspectorProvider().observe( + observeInput(fake.endpoint, fake.targetId, "node"), + ); + if (!result.ok) throw result.error; + expect(result.value.capture).toMatchObject({ + events_observed: 5, + events_retained: 5, + events_dropped: 0, + truncated: false, + }); + expect(result.value.execution_contexts).toEqual([ + { + context_key: "context_0", + execution_context_id: 1, + state: "created", + name: "main", + origin: "", + }, + { + context_key: "context_1", + execution_context_id: 2, + state: "destroyed", + name: "worker", + origin: "", + }, + ]); + expect( + result.value.unknowns.filter((text) => + text.includes("malformed execution-context ID"), + ), + ).toEqual([ + "2 execution-context lifecycle events with a missing or malformed execution-context ID were excluded; the affected contexts' lifecycle state is unknown.", + ]); + } finally { + await fake.close(); + } }); test("rejects unknown runtime observation fields", () => { @@ -329,10 +314,12 @@ test("rejects unknown runtime observation fields", () => { ).toBe(false); }); -test("returns every target exposed by the selected Inspector endpoint inline", async () => { +test("lists all connectable targets on the selected endpoint across directories", async () => { const fixture = await runtimeFixture(); + const outside = await temporaryFile("outside.js"); const fake = await startFakeV8Inspector({ targetUrl: pathToFileURL(fixture.entry).href, + additionalTargetUrl: pathToFileURL(outside).href, additionalTargetCount: 205, }); try { @@ -340,8 +327,22 @@ test("returns every target exposed by the selected Inspector endpoint inline", a inspector_endpoint: fake.endpoint, }); if (!listed.ok) throw listed.error; - expect(listed.value.targets).toHaveLength(206); - expect(listed.value.targets[0]?.target_id).toBe(fake.targetId); + const targets = listed.value.targets; + expect(targets).toHaveLength(207); + expect(new Set(targets.map(({ target_id }) => target_id)).size).toBe(207); + expect( + targets.find(({ target_id }) => target_id === fake.targetId)?.location, + ).toMatchObject({ kind: "file", file_path: fixture.entry }); + expect( + targets.find( + ({ target_id }) => target_id === "00000000-0000-4000-8000-000000000002", + )?.location, + ).toMatchObject({ kind: "file", file_path: outside }); + expect( + targets.find( + ({ target_id }) => target_id === "00000000-0000-4000-8000-000000000207", + )?.location, + ).toMatchObject({ kind: "file", file_path: fixture.entry }); } finally { await fake.close(); } @@ -409,27 +410,6 @@ test("rejects a V8 target explicitly marked attached", async () => { } }); -test("includes targets outside the old caller root filter", async () => { - const fixture = await runtimeFixture(); - const outside = await temporaryFile("outside.js"); - const fake = await startFakeV8Inspector({ - targetUrl: pathToFileURL(fixture.entry).href, - additionalTargetUrl: pathToFileURL(outside).href, - }); - try { - const result = await new V8InspectorProvider().listTargets({ - inspector_endpoint: fake.endpoint, - }); - if (!result.ok) throw result.error; - expect(result.value.targets).toHaveLength(2); - expect(result.value.targets.map(({ location }) => location)).toContainEqual( - expect.objectContaining({ file_path: outside }), - ); - } finally { - await fake.close(); - } -}); - test("captures complete metadata with two enable commands", async () => { const fixture = await runtimeFixture(); const outside = await temporaryFile("secret.js"); @@ -499,131 +479,109 @@ test("excludes an Electron main target that reports only bare file://", async () }); test.each([ - ["node", "node"], - ["electron-main", "node"], - ["electron-preload", "page"], - ["electron-renderer", "page"], + { + targetType: "node", + allowed: ["node", "electron-main"], + refused: ["electron-preload", "electron-renderer"], + }, + { + targetType: "page", + allowed: ["electron-preload", "electron-renderer"], + refused: ["node", "electron-main"], + }, ] as const)( - "admits declared %s only on its protocol target family", - async (runtimeKind, targetType) => { + "admits only compatible declared roles for a $targetType Inspector target", + async ({ targetType, allowed, refused }) => { const fixture = await runtimeFixture(); const fake = await startFakeV8Inspector({ targetUrl: pathToFileURL(fixture.entry).href, targetType, }); + const provider = new V8InspectorProvider(); try { - const result = await new V8InspectorProvider().observe( - observeInput(fake.endpoint, fake.targetId, runtimeKind), - ); - expect(result.ok).toBe(true); + for (const kind of refused) { + const result = await provider.observe( + observeInput(fake.endpoint, fake.targetId, kind), + ); + expect(result, kind).toMatchObject({ + ok: false, + error: { reason: "target_not_allowed" }, + }); + } + expect(fake.commands).toEqual([]); + for (const kind of allowed) { + const result = await provider.observe( + observeInput(fake.endpoint, fake.targetId, kind), + ); + if (!result.ok) throw result.error; + expect(result.value.target).toMatchObject({ + protocol_type: targetType, + runtime_kind: kind, + runtime_kind_authority: "caller-declared-unverified", + }); + } + if (targetType === "page") { + const input = observeInput(fake.endpoint, fake.targetId, "node"); + delete input.runtime_kind; + const result = await provider.observe(input); + if (!result.ok) throw result.error; + expect(result.value.target).toMatchObject({ + protocol_type: "page", + runtime_kind: "unknown", + runtime_kind_authority: "not-declared", + }); + } } finally { + await provider.close(); await fake.close(); } }, ); -describe("complete Inspector script hashes", () => { - test("keeps more than one hundred long script hashes distinct", async () => { - const fixture = await runtimeFixture(); - const url = pathToFileURL(fixture.entry).href; - const prefix = "h".repeat(512); - const scriptHashes = Array.from( - { length: 101 }, - (_, index) => `${prefix}${String(index)}`, +test("retains 2,005 authorized scripts with complete URL and hash metadata", async () => { + const shortUrl = "https://example.test/shared.js"; + const longUrl = `https://example.test/${"a".repeat(20_000)}`; + const prefix = "h".repeat(512); + const scriptHashes = Array.from({ length: 2_005 }, (_, index) => + index < 101 ? `${prefix}${index}` : `hash-${index}`, + ); + const scriptUrls = scriptHashes.map((_, index) => + index === 2_004 ? longUrl : shortUrl, + ); + const fake = await startFakeV8Inspector({ + targetUrl: longUrl, + scriptUrls, + scriptHashes, + }); + try { + const result = await new V8InspectorProvider().observe({ + ...observeInput(fake.endpoint, fake.targetId, "node"), + observation_ms: 1_000, + }); + if (!result.ok) throw result.error; + expect(result.value.capture).toMatchObject({ + truncated: false, + truncation_reasons: [], + events_dropped: 0, + }); + expect(result.value.target.location).toMatchObject({ + kind: "url", + sanitized_url: longUrl, + }); + const items = result.value.scripts.items; + expect(items.map(({ cdp_hash }) => cdp_hash).sort()).toEqual( + [...scriptHashes].sort(), ); - const fake = await startFakeV8Inspector({ - targetUrl: url, - scriptUrls: scriptHashes.map(() => url), - scriptHashes, + expect( + items.find(({ cdp_hash }) => cdp_hash === "hash-2004")?.location, + ).toMatchObject({ + kind: "url", + origin: "https://example.test", + sanitized_url: longUrl, }); - try { - const result = await new V8InspectorProvider().observe({ - ...observeInput(fake.endpoint, fake.targetId, "node"), - observation_ms: 1_000, - }); - if (!result.ok) throw result.error; - expect(result.value.scripts.items).toHaveLength(scriptHashes.length); - expect( - new Set(result.value.scripts.items.map(({ cdp_hash }) => cdp_hash)), - ).toEqual(new Set(scriptHashes)); - } finally { - await fake.close(); - } - }); -}); - -describe("undeclared V8 runtime role", () => { - test("observes a target without inventing a runtime role", async () => { - const fixture = await runtimeFixture(); - const fake = await startFakeV8Inspector({ - targetUrl: pathToFileURL(fixture.entry).href, - targetType: "page", - }); - try { - const input = observeInput(fake.endpoint, fake.targetId, "node"); - delete input.runtime_kind; - const result = await new V8InspectorProvider().observe(input); - if (!result.ok) throw result.error; - expect(result.value.target).toMatchObject({ - protocol_type: "page", - runtime_kind: "unknown", - runtime_kind_authority: "not-declared", - }); - } finally { - await fake.close(); - } - }); -}); - -describe("V8 Inspector script retention", () => { - test("retains an authorized script location of more than 20,000 characters", async () => { - const longUrl = `https://example.test/${"a".repeat(20_000)}`; - const fake = await startFakeV8Inspector({ - targetUrl: longUrl, - scriptUrls: [longUrl], - }); - try { - const input = { - ...observeInput(fake.endpoint, fake.targetId, "node"), - }; - const result = await new V8InspectorProvider().observe(input); - if (!result.ok) throw result.error; - expect(result.value.scripts.items).toHaveLength(1); - expect(result.value.scripts.items[0]?.location).toMatchObject({ - kind: "url", - origin: "https://example.test", - sanitized_url: longUrl, - }); - } finally { - await fake.close(); - } - }); - - test("returns every authorized script of a target with 2,005 scripts", async () => { - const fixture = await runtimeFixture(); - const scriptUrls = [ - pathToFileURL(fixture.entry).href, - ...Array.from({ length: 2_004 }, () => pathToFileURL(fixture.entry).href), - ]; - const fake = await startFakeV8Inspector({ - targetUrl: pathToFileURL(fixture.entry).href, - scriptUrls, - }); - try { - const input = { - ...observeInput(fake.endpoint, fake.targetId, "node"), - observation_ms: 1_000, - }; - const result = await new V8InspectorProvider().observe(input); - if (!result.ok) throw result.error; - expect(result.value.capture.truncated).toBe(false); - expect(result.value.capture.truncation_reasons).toEqual([]); - expect(result.value.capture.events_dropped).toBe(0); - expect(result.value.scripts.items).toHaveLength(scriptUrls.length); - } finally { - await fake.close(); - } - }); + } finally { + await fake.close(); + } }); describe("bounded V8 Inspector capture", () => { @@ -657,7 +615,15 @@ describe("bounded V8 Inspector capture", () => { expect(result.value.capture.events_observed).toBeGreaterThan( result.value.capture.events_retained, ); - expect(result.value.execution_contexts).toHaveLength(1); + expect(result.value.execution_contexts).toMatchObject([ + { execution_context_id: 1, state: "destroyed" }, + ]); + const retainedHashes = result.value.scripts.items.map( + ({ cdp_hash }) => cdp_hash, + ); + expect(retainedHashes).toContain(scriptHashes[0]); + expect(retainedHashes).not.toContain(scriptHashes.at(-1)); + expect(result.value.scripts.observed_total).toBe(scriptHashes.length); } finally { await fake.close(); } @@ -702,53 +668,14 @@ describe("passive V8 Inspector evidence", () => { new V8InspectorProvider(), input, ); - expect(first.ok).toBe(true); - expect(second.ok).toBe(true); - if (!first.ok || !second.ok) return; + if (!first.ok) throw first.error; + if (!second.ok) throw second.error; expect(second.value.evidence_id).toBe(first.value.evidence_id); - - const listed = await listJavaScriptRuntimeTargets( - new V8InspectorProvider(), - { - inspector_endpoint: fake.endpoint, - }, - ); - expect(listed.ok).toBe(true); } finally { await fake.close(); } }); - test("reconciles V8 script presence with static Application Graph Evidence", () => { - const root = "/Applications/Example.app/Contents/Resources/app"; - const observation = runtimeObservation( - `${root}/index.html`, - `${root}/renderer.js`, - ); - const runtimeEvidence = createJavaScriptRuntimeObservationEvidence( - "observe_javascript_runtime", - { - inspector_endpoint: "http://127.0.0.1:9229", - target_id: "example-v8-target", - runtime_kind: "electron-main", - observation_ms: 100, - }, - observation, - V8_INSPECTOR_PROVIDER_IDENTITY, - ); - const reconciled = reconcileJavaScriptRuntimeEvidence({ - static_layers: JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE.static_layers, - runtime_observations: [runtimeEvidence], - }); - if (!reconciled.ok) throw reconciled.error; - const result = javascriptRuntimeReconciliationResultSchema.parse( - reconciled.value.evidence.normalized_result, - ); - expect(result.runtime_captures[0]?.kind).toBe("v8-inspector"); - expect(result.summary.runtime_scripts).toBe(1); - expect(result.summary.matched).toBeGreaterThan(0); - }); - test("keeps unverified partial script URLs unresolved during reconciliation", () => { const rawUrl = "file:///tmp/not-verified.js?caller-value=kept"; const resolvedObservation = runtimeObservation( diff --git a/tests/boundary/mcp/javascriptRuntimeObservationMcp.test.ts b/tests/boundary/mcp/javascriptRuntimeObservationMcp.test.ts index bed493de9c..c3ddd0903d 100644 --- a/tests/boundary/mcp/javascriptRuntimeObservationMcp.test.ts +++ b/tests/boundary/mcp/javascriptRuntimeObservationMcp.test.ts @@ -9,7 +9,7 @@ import { Ajv2020 } from "ajv/dist/2020.js"; import { afterEach, describe, expect, test } from "vitest"; import { createTestBinarySession } from "../../fixtures/binarySession.js"; -import { observeJavaScriptRuntime } from "../../../src/application/javascript/JavaScriptRuntimeObservationService.js"; +import { parseEvidence } from "../../../src/domain/evidence.js"; import { V8InspectorProvider } from "../../../src/inspector/V8InspectorProvider.js"; import { javascriptRuntimeObservationSchema, @@ -126,7 +126,6 @@ function registerRuntimeParameterContract( ], }); } - await client.ping(); }); } @@ -162,6 +161,9 @@ function registerRuntimeMetadataContract(): void { const unknowns = repeated(101); const limitations = repeated(101); + const truncationReasons = repeated(21); + const directlyObserved = repeated(21); + const unavailableWithoutInstrumentation = repeated(21); expect( javascriptRuntimeObservationSchema.parse({ runtime: { @@ -184,7 +186,7 @@ function registerRuntimeMetadataContract(): void { events_dropped: 0, metadata_bytes_retained: 0, truncated: false, - truncation_reasons: repeated(21), + truncation_reasons: truncationReasons, }, scripts: { items: [], @@ -196,12 +198,21 @@ function registerRuntimeMetadataContract(): void { }, }, execution_contexts: [], - directly_observed: repeated(21), - unavailable_without_instrumentation: repeated(21), + directly_observed: directlyObserved, + unavailable_without_instrumentation: unavailableWithoutInstrumentation, unknowns, limitations, }), - ).toMatchObject({ unknowns, limitations }); + ).toMatchObject({ + capture: { + observation_ms: 10_001, + truncation_reasons: truncationReasons, + }, + directly_observed: directlyObserved, + unavailable_without_instrumentation: unavailableWithoutInstrumentation, + unknowns, + limitations, + }); }); } @@ -270,28 +281,22 @@ function registerRetainedRuntimeEvidence( }, }, }); - const evidenceId = evidenceIdFrom(observedRuntime.structuredContent); + const evidence = parseEvidence(observedRuntime.structuredContent); + expect(evidence).toMatchObject({ + operation: "observe_javascript_runtime", + parameters: { + inspector_endpoint: inspector.endpoint, + target_id: inspector.targetId, + runtime_kind: "node", + observation_ms: 10, + }, + }); const bundle = await client.callTool({ name: "get_evidence_bundle", arguments: {}, }); expect(bundle.isError).not.toBe(true); - expect(parseMcpEvidenceBundle(bundle).records).toContainEqual( - observedRuntime.structuredContent, - ); - expect(observedRuntime.structuredContent).toMatchObject({ - evidence_id: evidenceId, - operation: "observe_javascript_runtime", - parameters: expect.any(Object), - }); - expect(new Set(inspector.commands.map(({ method }) => method))).toEqual( - new Set(["Runtime.enable", "Debugger.enable"]), - ); - const provider = new V8InspectorProvider(); - resources.push(provider); - const direct = await observeDirectly(inspector, provider); - expect(direct.ok, direct.ok ? undefined : direct.error.message).toBe(true); - if (direct.ok) expect(evidenceId).toBe(direct.value.evidence_id); + expect(parseMcpEvidenceBundle(bundle).records).toContainEqual(evidence); }); } @@ -347,26 +352,3 @@ const createObservationClient = async ( await client.connect(clientTransport); return { entry, inspector, client }; }; - -const observeDirectly = ( - inspector: Awaited>, - provider: V8InspectorProvider, -) => - observeJavaScriptRuntime( - provider, - observeJavaScriptRuntimeInputSchema.parse({ - inspector_endpoint: inspector.endpoint, - target_id: inspector.targetId, - runtime_kind: "node", - observation_ms: 10, - }), - ); - -const evidenceIdFrom = (value: unknown): string => { - if (typeof value !== "object" || value === null) - throw new TypeError("Missing structured result"); - const evidenceId = Reflect.get(value, "evidence_id"); - if (typeof evidenceId !== "string") - throw new TypeError("Missing Evidence ID"); - return evidenceId; -}; diff --git a/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts b/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts index 5600ff9867..4c9bd6eeb4 100644 --- a/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts +++ b/tests/boundary/process/javascriptRuntimeReconciliationAmbiguity.test.ts @@ -40,48 +40,7 @@ afterEach(async () => { ); }); -it("keeps byte-identical cross-layer candidates explicitly ambiguous", async () => { - const application = await applicationFixture(); - const assets = await createTestTempDirectory("rea-runtime-assets-static-"); - const runtime = await createTestTempDirectory("rea-runtime-assets-live-"); - temporary.push(application, assets, runtime); - await Promise.all([ - writeFile(join(assets, "app.js"), SOURCE), - writeFile(join(runtime, "app.js"), SOURCE), - writeFile(join(runtime, "index.html"), ""), - ]); - const result = reconcileInput({ - static_layers: [ - { - role: "application", - analysis: await analyzeFixture(application), - }, - { role: "assets", analysis: await analyzeFixture(assets) }, - ], - runtime_observations: [ - electronRuntimeEvidence(runtime, SOURCE, { includeWorker: false }), - ], - }); - const script = result.reconciliations.find( - ({ entity_kind: kind }) => kind === "script", - ); - - expect(script).toMatchObject({ - status: "ambiguous", - reason: "ambiguous-static-candidates", - candidate_static_count: 2, - }); - expect(script?.candidate_static_nodes).toHaveLength(2); - expect( - new Set( - script?.candidate_static_nodes.map( - ({ static_layer_id: layerId }) => layerId, - ), - ).size, - ).toBe(2); -}); - -it("retains all runtime entities and static load states without projection caps", async () => { +it("retains each capture and both runtime instances of a loaded asset", async () => { const fixture = await applicationFixture(); temporary.push(fixture); const staticEvidence = await analyzeFixture(fixture); @@ -101,10 +60,40 @@ it("retains all runtime entities and static load states without projection caps" runtime_observations: runtimeObservations, }); - expect(result.runtime_captures).toHaveLength(2); expect( - new Set(result.runtime_captures.map(({ target_node_id: id }) => id)).size, - ).toBe(2); + result.runtime_captures + .map(({ evidence_id, target_key }) => [evidence_id, target_key]) + .sort(), + ).toEqual( + [ + [runtimeObservations[0]?.evidence_id, "target-one"], + [runtimeObservations[1]?.evidence_id, "target-two"], + ].sort(), + ); + const app = result.graph.nodes.find( + ({ kind, observations }) => + kind === "javascript-asset" && + observations.some(({ properties }) => properties.path === "app.js"), + ); + if (app === undefined) throw new Error("Missing fixture application asset"); + const scripts = result.reconciliations.filter( + ({ entity_kind }) => entity_kind === "script", + ); + expect(scripts).toMatchObject([ + { status: "matched", static_node_id: app.node_id }, + { status: "matched", static_node_id: app.node_id }, + ]); + expect(scripts[0]?.runtime_node_id).not.toBe(scripts[1]?.runtime_node_id); + expect( + result.static_load_states.find( + ({ static_node_id }) => static_node_id === app.node_id, + ), + ).toMatchObject({ + status: "loaded", + runtime_node_ids: scripts + .map(({ runtime_node_id }) => runtime_node_id) + .sort(), + }); expect(result.summary).toMatchObject({ runtime_targets: 2, runtime_frames: 2, @@ -117,50 +106,6 @@ it("retains all runtime entities and static load states without projection caps" omitted_reconciliation_items: 0, omitted_static_load_states: 0, }); - expect(result.static_load_states).toHaveLength( - result.summary.static_loaded + - result.summary.static_resident + - result.summary.static_not_observed + - result.summary.static_unknown, - ); -}); - -it("rejects runtime source bytes whose Evidence omits source-capture approval", async () => { - const fixture = await applicationFixture(); - temporary.push(fixture); - const staticEvidence = await analyzeFixture(fixture); - const contradictoryRuntime = electronRuntimeEvidence(fixture, SOURCE, { - includeWorker: false, - sourceIncluded: false, - }); - - const result = readRuntimeReconciliationInput( - reconcileJavaScriptRuntimeInputSchema.parse({ - static_layers: [{ role: "application", analysis: staticEvidence }], - runtime_observations: [contradictoryRuntime], - }), - ); - expect(result).toMatchObject({ - ok: false, - error: { - _tag: "AnalysisInputError", - operation: "reconcile_javascript_runtime", - issues: [ - { - path: [ - "runtime_observations", - 0, - "parameters", - "include_script_sources", - ], - reason: "invalid_value", - message: expect.stringMatching( - /source.*without source-capture selection/i, - ), - }, - ], - }, - }); }); it("keeps graph omission counts unknown when a runtime section is unavailable", async () => {