diff --git a/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts b/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts index a6b89df361..e156f71b0c 100644 --- a/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts +++ b/src/domain/javascript/javascriptApplicationAnalysisSteps.test.ts @@ -84,7 +84,6 @@ describe("immutable JavaScript result reuse", () => { }); else Object.defineProperty(input, "input_path", { enumerable: false }); freezeJsonSnapshot(input); - const expected = javascriptApplicationAnalysisResultSchema.parse(input); for (let repeat = 0; repeat < 2; repeat += 1) { const parsed = complete( parseOwnedJavaScriptApplicationAnalysisSteps(input), @@ -93,9 +92,8 @@ describe("immutable JavaScript result reuse", () => { expect(Object.hasOwn(parsed, "not_in_parsed_result")).toBe(false); expect( Object.getOwnPropertyDescriptor(parsed, "input_path")?.enumerable, - ).toBe( - Object.getOwnPropertyDescriptor(expected, "input_path")?.enumerable, - ); + ).toBe(true); + expect(parsed.input_path).toBe(input.input_path); } }, ); @@ -141,38 +139,49 @@ describe("immutable JavaScript result reuse", () => { complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), ).not.toBe(input); input.statistics.findings = -1; - expect(() => - complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), - ).toThrow(z.ZodError); + expect( + issuesFrom(() => + complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), + ), + ).toMatchObject([{ path: ["statistics", "findings"] }]); }, ); }); describe("owned JavaScript graph validation", () => { - it("reuses exact sealed graphs and preserves all public result fields", () => { + it("reuses exact sealed graphs and copies mutable imported graphs", () => { const input = ownedExample(); const result = complete( parseOwnedJavaScriptApplicationAnalysisSteps(input), ); - expect(result).toEqual( - javascriptApplicationAnalysisResultSchema.parse(input), - ); expect(result.graph).toBe(input.graph); expect(result.semantic_graph).toBe(input.semantic_graph); expect(result.summary).not.toBe(input.summary); expect(result.statistics).not.toBe(input.statistics); + expect( + issuesFrom(() => + complete( + parseOwnedJavaScriptApplicationAnalysisSteps({ + ...input, + extra: true, + }), + ), + ), + ).toMatchObject([{ code: "unrecognized_keys", path: [], keys: ["extra"] }]); expect(Object.isFrozen(result.graph.nodes[0]?.observations)).toBe(true); expect(Object.isFrozen(result.semantic_graph.nodes[0]?.properties)).toBe( true, ); - expect(isValidatedImmutableJavaScriptApplicationGraph(result.graph)).toBe( - true, + + const imported = structuredClone(input); + const parsed = complete( + parseOwnedJavaScriptApplicationAnalysisSteps(imported), ); + expect(parsed).toEqual(result); + expect(parsed.graph).not.toBe(imported.graph); + expect(parsed.semantic_graph).not.toBe(imported.semantic_graph); expect( - isValidatedImmutableJavaScriptSemanticGraph(result.semantic_graph), - ).toBe(true); - expect( - isValidatedImmutableJavaScriptApplicationGraph(result.graph.nodes), + isValidatedImmutableJavaScriptSemanticGraph(parsed.semantic_graph), ).toBe(false); }); @@ -201,41 +210,15 @@ describe("owned JavaScript graph validation", () => { ); }); - it.each(["mutable", "shallow-frozen", "json-sealed"])( - "fully validates %s imported graphs instead of trusting their IDs", - (representation) => { - const input = example(); - input.graph.graph_id = `jag_${"f".repeat(64)}`; - if (representation === "shallow-frozen") Object.freeze(input.graph); - if (representation === "json-sealed") freezeJsonSnapshot(input.graph); - expect(isValidatedImmutableJavaScriptApplicationGraph(input.graph)).toBe( - false, - ); - const issues = issuesFrom(() => - complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), - ); - expect(issues).toEqual( - issuesFrom(() => - javascriptApplicationAnalysisResultSchema.parse(input), - ), - ); - expect( - issues.some(({ path }) => path.join("/") === "graph/graph_id"), - ).toBe(true); - }, - ); - - it("fully parses valid imported graphs without conferring an owned proof", () => { + it("does not trust a deeply frozen imported graph with a stale commitment", () => { const input = example(); - const parsed = complete( - parseOwnedJavaScriptApplicationAnalysisSteps(input), - ); - expect(parsed).toEqual(input); - expect(parsed.graph).not.toBe(input.graph); - expect(parsed.semantic_graph).not.toBe(input.semantic_graph); + input.graph.limitations.push("A limitation added after commitment."); + freezeJsonSnapshot(input); expect( - isValidatedImmutableJavaScriptSemanticGraph(parsed.semantic_graph), - ).toBe(false); + issuesFrom(() => + complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), + ), + ).toMatchObject([{ path: ["graph", "graph_id"] }]); }); it("rejects a forged semantic graph paired with an authenticated application graph", () => { @@ -244,16 +227,11 @@ describe("owned JavaScript graph validation", () => { ...input.semantic_graph, graph_id: `jsrg_${"f".repeat(64)}`, }); - expect( - isValidatedImmutableJavaScriptSemanticGraph(input.semantic_graph), - ).toBe(false); expect( issuesFrom(() => complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), ), - ).toEqual( - issuesFrom(() => javascriptApplicationAnalysisResultSchema.parse(input)), - ); + ).toMatchObject([{ path: ["semantic_graph", "graph_id"] }]); }); it("does not authenticate an abandoned sealing operation", () => { @@ -307,15 +285,17 @@ describe("owned JavaScript result bindings", () => { sealTransferredJavaScriptSemanticGraphSteps(semantic), ); } - expect( - issuesFrom(() => - complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), - ), - ).toEqual( - issuesFrom(() => - javascriptApplicationAnalysisResultSchema.parse(input), - ), + const bindingPath = { + application: ["semantic_graph", "application_graph_id"], + root: ["semantic_graph", "root_artifact_sha256"], + node: ["semantic_graph", "nodes"], + }[binding]!; + const issues = issuesFrom(() => + complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), ); + expect( + issues.map(({ path }) => path.slice(0, bindingPath.length)), + ).toContainEqual(bindingPath); }, ); @@ -350,21 +330,6 @@ describe("owned JavaScript result bindings", () => { issuesFrom(() => javascriptApplicationAnalysisResultSchema.parse(input)), ); }); - - it("retains strict metadata validation alongside authenticated graphs", () => { - const input = { - ...ownedExample(), - statistics: { ...example().statistics, findings: -1 }, - extra: true, - }; - expect( - issuesFrom(() => - complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)), - ), - ).toEqual( - issuesFrom(() => javascriptApplicationAnalysisResultSchema.parse(input)), - ); - }); }); describe("graphs transferred to their sealing steps", () => { @@ -424,39 +389,22 @@ describe("graphs transferred to their sealing steps", () => { }); }); -describe("skipped_javascript_files", () => { - it("refuses a result without the skipped counter", () => { - const withoutCounter = structuredClone( - JAVASCRIPT_APPLICATION_EVIDENCE_EXAMPLE.normalized_result, - ) as Record; - const statistics = withoutCounter.statistics as Record; - delete statistics.skipped_javascript_files; - expect( - javascriptApplicationAnalysisResultSchema.safeParse(withoutCounter).error - ?.issues[0]?.path, - ).toEqual(["statistics", "skipped_javascript_files"]); - }); - - it("keeps the producer-written skipped count in current evidence", () => { - const current = structuredClone( - JAVASCRIPT_APPLICATION_EVIDENCE_EXAMPLE.normalized_result, - ) as Record; - const statistics = current.statistics as Record; - statistics.skipped_javascript_files = 3; - const result = javascriptApplicationAnalysisResultSchema.parse(current); - expect(result.statistics.skipped_javascript_files).toBe(3); - }); -}); - -describe("Electron boundary summary", () => { - it("refuses a summary without the protocol registration count", () => { - const current = example(); - const { protocol_registrations: _count, ...summary } = current.summary; - expect( - javascriptApplicationAnalysisResultSchema.safeParse({ +it("requires the published skipped-file and protocol registration counts", () => { + const current = example(); + const { skipped_javascript_files: _skipped, ...statistics } = + current.statistics; + const { protocol_registrations: _protocols, ...summary } = current.summary; + expect( + javascriptApplicationAnalysisResultSchema + .safeParse({ ...current, + statistics, summary, - }).error?.issues[0]?.path, - ).toEqual(["summary", "protocol_registrations"]); - }); + }) + .error?.issues.map(({ path }) => path.join("/")) + .sort(), + ).toEqual([ + "statistics/skipped_javascript_files", + "summary/protocol_registrations", + ]); }); diff --git a/src/domain/javascript/javascriptApplicationGraph.test.ts b/src/domain/javascript/javascriptApplicationGraph.test.ts index 9ed171cb0f..16de095e37 100644 --- a/src/domain/javascript/javascriptApplicationGraph.test.ts +++ b/src/domain/javascript/javascriptApplicationGraph.test.ts @@ -5,10 +5,12 @@ import { createJavaScriptApplicationEdge, createJavaScriptApplicationGraph, createJavaScriptApplicationNode, + javascriptApplicationGraphSchema, mergeJavaScriptApplicationNodes, parseJavaScriptApplicationGraph, serializeJavaScriptApplicationGraph, } from "./javascriptApplicationGraph.js"; +import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js"; import { APPLICATION_GRAPH_DIGESTS, artifactEvidence, @@ -33,51 +35,47 @@ it("merges observations by entity identity without changing source nodes", () => stability: "global-exact" as const, strategy: "content-digest" as const, }; - expect(second.node_id).toBe(first.node_id); - expect(second.observations[0]?.observation_id).not.toBe( - first.observations[0]?.observation_id, - ); + const sourceSnapshot = structuredClone([first, second]); const merged = mergeJavaScriptApplicationNodes([ first, { ...second, identity: reversedIdentity }, first, ]); - expect(merged).toHaveLength(1); - const node = firstOf(merged, "merged node"); - expect(node.node_id).toBe(first.node_id); - expect(node.observations.map(({ properties }) => properties)).toEqual( - expect.arrayContaining([{ selected: "first" }, { selected: "second" }]), - ); - expect(node.observations).toHaveLength(2); - expect(first.observations).toHaveLength(1); - expect(second.observations).toHaveLength(1); - const graph = graphForNode(node); - expect(parseJavaScriptApplicationGraph(graph)).toEqual(graph); + expect(merged).toMatchObject([ + { node_id: first.node_id, identity: first.identity }, + ]); + expect( + merged[0]?.observations + .map(({ properties }) => properties) + .sort((left, right) => + String(left.selected).localeCompare(String(right.selected)), + ), + ).toEqual([{ selected: "first" }, { selected: "second" }]); + expect([first, second]).toEqual(sourceSnapshot); }); -it.each(["kind", "identity"] as const)( - "rejects colliding entity IDs with a different %s before merging observations", - (field) => { - const node = contentNode( - artifactEvidence(APPLICATION_GRAPH_DIGESTS.asar, "main.js"), - ); - const conflicting = - field === "kind" - ? { ...node, kind: "javascript-asset" as const } - : { - ...node, - identity: { - strategy: "content-digest" as const, - stability: "global-exact" as const, - sha256: "f".repeat(64), - }, - }; - expect(() => mergeJavaScriptApplicationNodes([node, conflicting])).toThrow( +it("rejects a colliding entity ID when the kind or identity disagrees", () => { + const node = contentNode( + artifactEvidence(APPLICATION_GRAPH_DIGESTS.asar, "main.js"), + ); + const original = structuredClone(node); + const conflicts = [ + { ...node, kind: "javascript-asset" as const }, + { + ...node, + identity: { + strategy: "content-digest" as const, + stability: "global-exact" as const, + sha256: "f".repeat(64), + }, + }, + ]; + for (const conflict of conflicts) + expect(() => mergeJavaScriptApplicationNodes([node, conflict])).toThrow( "Colliding JAG node identifiers disagree", ); - expect(node.observations).toHaveLength(1); - }, -); + expect(node).toEqual(original); +}); it("preserves deep and wide observation properties in a large native-export graph", () => { let nested: unknown = "leaf"; @@ -185,14 +183,6 @@ describe("JavaScript Application Graph: evidence authority", () => { state: "observed", }); expect(observedAs?.evidence.authority).toBe("passive-cdp-runtime"); - expect(observedAs?.identifier_strategy).toEqual({ - strategy: "semantic-content-sha256", - stability: "relationship-exact", - }); - expect(runtime.observations[0]?.identifier_strategy).toEqual({ - strategy: "semantic-content-sha256", - stability: "observation-exact", - }); expect(nativeExport.observations[0]?.evidence.authority).toBe( "native-analysis-provider", ); @@ -291,84 +281,53 @@ describe("JavaScript Application Graph: identity and evidence", () => { expect(graphForNode(node).nodes).toEqual([node]); }); - it("scopes runtime identities to a capture and artifact paths to a version", () => { - const runtime = nodeByLabel( - buildSyntheticJavaScriptApplicationGraph(), - "preload runtime script", - ); - const differentCapture = createJavaScriptApplicationNode({ - kind: "runtime-script-instance", - identity: { - strategy: "runtime-instance", - stability: "capture-only", - capture_evidence_id: `ev_${"c".repeat(64)}`, - capture_sha256: "6".repeat(64), - runtime_key: "script-preload", - }, - observations: [ - { - label: "other capture", - properties: {}, - evidence: { - ...runtimeEvidence("script-preload"), - location: { - available: true, - value: { - kind: "runtime", - capture_sha256: "6".repeat(64), - target_key: "target-main", - frame_key: "frame-main", - script_key: "script-preload", - }, - }, + it("scopes the same artifact path to its version", () => { + const path = "dist/app.js"; + const asset = (digest: string) => + createJavaScriptApplicationNode({ + kind: "javascript-asset", + identity: { + strategy: "canonical-path", + stability: "artifact-version", + artifact_sha256: digest, + path, + }, + observations: [ + { + label: null, + properties: {}, + evidence: artifactEvidence(digest, path), }, - }, - ], - }); - expect(differentCapture.node_id).not.toBe(runtime.node_id); - - const firstArtifact = createJavaScriptApplicationNode({ - kind: "javascript-asset", - identity: { - strategy: "canonical-path", - stability: "artifact-version", - artifact_sha256: APPLICATION_GRAPH_DIGESTS.asar, - path: "dist/app.js", - }, - observations: [ - { - label: null, - properties: {}, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/app.js", - ), - }, - ], - }); - const secondArtifact = createJavaScriptApplicationNode({ - kind: "javascript-asset", - identity: { - strategy: "canonical-path", - stability: "artifact-version", - artifact_sha256: "7".repeat(64), - path: "dist/app.js", - }, - observations: [ - { - label: null, - properties: {}, - evidence: artifactEvidence("7".repeat(64), "dist/app.js"), - }, - ], - }); - expect(firstArtifact.node_id).not.toBe(secondArtifact.node_id); + ], + }); + const earlier = asset(APPLICATION_GRAPH_DIGESTS.asar); + const later = asset("7".repeat(64)); + expect(graphForNode(earlier).nodes[0]?.node_id).not.toBe( + graphForNode(later).nodes[0]?.node_id, + ); }); }); -describe("JavaScript Application Graph: source maps and observations", () => { - it("requires source-map observations to retain the declaration and map location", () => { - const sourceModule = createJavaScriptApplicationNode({ +it("requires every source-map observation to agree with the original source identity", () => { + const sourceDigest = "3".repeat(64); + const reference = sourceMapReference( + "webpack:///src/editor.ts", + null, + "dist/app.js.map", + ); + const observed = artifactEvidence( + APPLICATION_GRAPH_DIGESTS.asar, + "dist/app.js.map", + "ast-static-analysis", + ); + const mappedNode = ( + digest: string | null, + observations: readonly Omit< + ApplicationNode["observations"][number], + "observation_id" | "identifier_strategy" + >[], + ) => + createJavaScriptApplicationNode({ kind: "source-module", identity: { strategy: "source-map-original", @@ -376,177 +335,125 @@ describe("JavaScript Application Graph: source maps and observations", () => { stability: "source-map-exact", source_map_sha256: APPLICATION_GRAPH_DIGESTS.asar, original_source: "webpack:///src/editor.ts", - source_sha256: null, + source_sha256: digest, }, - observations: [ - { - label: "src/editor.ts", - source_map_reference: sourceMapReference( - "webpack:///src/editor.ts", - null, - "dist/app.js.map", - ), - properties: { recovered_from: "source-map" }, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/app.js.map", - "ast-static-analysis", - ), - }, - ], + observations, }); - expect(graphForNode(sourceModule).nodes).toEqual([sourceModule]); - const sourceObservation = firstOf( - sourceModule.observations, - "source observation", - ); - const { - source_map_reference, - observation_id, - identifier_strategy, - ...withoutReference - } = sourceObservation; - void observation_id; - void identifier_strategy; - expect(() => - graphForNode( - createJavaScriptApplicationNode({ - kind: sourceModule.kind, - identity: sourceModule.identity, - observations: [withoutReference], - }), - ), - ).toThrow(/Source-map observation/u); - expect(() => - graphForNode( - createJavaScriptApplicationNode({ - kind: sourceModule.kind, - identity: sourceModule.identity, - observations: [ - { - ...withoutReference, - source_map_reference: { - ...source_map_reference, - map_path: "different/map.js.map", - }, - }, - ], - }), - ), - ).toThrow(/Source-map observation/u); - }); -}); - -describe("JavaScript Application Graph: source-map digest consistency", () => { - it("rejects source-map observations that contradict a known original digest", () => { - const sourceMapNode = ( - identityDigest: string | null, - observedDigest: unknown, - contentAvailable?: boolean, - ) => - createJavaScriptApplicationNode({ - kind: "source-module", - identity: { - strategy: "source-map-original", - source_root: null, - stability: "source-map-exact", - source_map_sha256: APPLICATION_GRAPH_DIGESTS.asar, - original_source: "src/editor.ts", - source_sha256: identityDigest, - }, - observations: [ - { - label: null, - source_map_reference: sourceMapReference( - "src/editor.ts", - null, - "dist/app.js.map", - ), - properties: { - ...(contentAvailable === undefined - ? {} - : { content_available: contentAvailable }), - ...(observedDigest === undefined - ? {} - : { content_sha256: observedDigest }), - }, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/app.js.map", - "ast-static-analysis", - ), + const known = { + label: "known source", + source_map_reference: reference, + properties: { content_sha256: sourceDigest, content_available: true }, + evidence: observed, + }; + const unknown = { + ...known, + label: "unknown source", + properties: { content_sha256: null, content_available: false }, + }; + const knownGraph = graphForNode(mappedNode(sourceDigest, [known])); + const unknownGraph = graphForNode(mappedNode(null, [unknown])); + const rows = [ + { + graph: knownGraph, + node: mappedNode(sourceDigest, [ + known, + { ...known, label: "missing declaration", source_map_reference: null }, + { + ...known, + label: "wrong map path", + source_map_reference: { + ...reference, + map_path: "different/map.js.map", }, - ], - }); - - expect(() => - graphForNode(sourceMapNode("2".repeat(64), "3".repeat(64))), - ).toThrow(/Source-map observation/u); - expect(() => graphForNode(sourceMapNode(null, "3".repeat(64)))).toThrow( - /Source-map observation/u, - ); - expect(() => graphForNode(sourceMapNode(null, 7))).toThrow( - /Source-map observation/u, - ); - expect(() => - graphForNode(sourceMapNode("3".repeat(64), "3".repeat(64))), - ).not.toThrow(); - expect(() => - graphForNode(sourceMapNode("3".repeat(64), "3".repeat(64), false)), - ).toThrow(/Source-map observation/u); - expect(() => - graphForNode(sourceMapNode("3".repeat(64), null, false)), - ).toThrow(/Source-map observation/u); - expect(() => - graphForNode(sourceMapNode("3".repeat(64), null, true)), - ).toThrow(/Source-map observation/u); - expect(() => - graphForNode(sourceMapNode("3".repeat(64), undefined)), - ).toThrow(/Source-map observation/u); - expect(() => graphForNode(sourceMapNode(null, null, true))).toThrow( - /Source-map observation/u, - ); - }); + }, + { + ...known, + label: "different digest", + properties: { content_sha256: "2".repeat(64) }, + }, + { ...known, label: "absent digest", properties: {} }, + { + ...known, + label: "discarded content", + properties: { + content_sha256: sourceDigest, + content_available: false, + }, + }, + ]), + rejected: [ + "absent digest", + "different digest", + "discarded content", + "missing declaration", + "wrong map path", + ], + }, + { + graph: unknownGraph, + node: mappedNode(null, [ + unknown, + { + ...unknown, + label: "unproven digest", + properties: { content_sha256: sourceDigest }, + }, + { + ...unknown, + label: "non-digest content", + properties: { content_sha256: 7 }, + }, + { + ...unknown, + label: "unproven content", + properties: { content_sha256: null, content_available: true }, + }, + ]), + rejected: ["non-digest content", "unproven content", "unproven digest"], + }, + ]; + for (const { graph, node, rejected } of rows) { + const parsed = javascriptApplicationGraphSchema.safeParse({ + ...graph, + nodes: [node], + }); + const badLabels = parsed.error?.issues + .filter(({ path }) => path.at(-1) === "source_map_reference") + .map(({ path }) => node.observations[Number(path[3])]?.label) + .sort(); + expect(badLabels).toEqual(rejected); + } }); -describe("JavaScript Application Graph: commitments and schemas", () => { - it("rejects unknown fields and stale commitments", () => { - const graph = buildSyntheticJavaScriptApplicationGraph(); - expect(() => - parseJavaScriptApplicationGraph({ ...graph, surprise: true }), - ).toThrow(); +it("rejects unknown fields and stale nested commitments at their owning fields", () => { + const graph = buildSyntheticJavaScriptApplicationGraph(); + expect( + javascriptApplicationGraphSchema.safeParse({ ...graph, surprise: true }) + .error?.issues, + ).toMatchObject([ + { path: [], code: "unrecognized_keys", keys: ["surprise"] }, + ]); - const staleGraph = structuredClone(graph); - staleGraph.graph_id = `jag_${"f".repeat(64)}`; - expect(() => parseJavaScriptApplicationGraph(staleGraph)).toThrow( - /Graph identifier/u, - ); - - const staleNode = structuredClone(graph); - firstOf(staleNode.nodes, "node").node_id = `jag_node_${"f".repeat(64)}`; - expect(() => parseJavaScriptApplicationGraph(staleNode)).toThrow( - /Node identifier/u, - ); - - const staleObservation = structuredClone(graph); - firstOf( - firstOf(staleObservation.nodes, "node").observations, - "observation", - ).observation_id = `jag_observation_${"f".repeat(64)}`; - expect(() => parseJavaScriptApplicationGraph(staleObservation)).toThrow( - /Observation identifier/u, - ); - - const staleEdge = structuredClone(graph); - firstOf(staleEdge.edges, "edge").edge_id = `jag_edge_${"f".repeat(64)}`; - expect(() => parseJavaScriptApplicationGraph(staleEdge)).toThrow( - /Edge identifier/u, - ); - const { graph_id: _graphId, ...staleInput } = staleEdge; - expect(() => createJavaScriptApplicationGraph(staleInput)).toThrow( - /Edge identifier/u, - ); - }); + const staleNode = structuredClone(graph); + firstOf(staleNode.nodes, "node").node_id = `jag_node_${"f".repeat(64)}`; + const staleObservation = structuredClone(graph); + firstOf( + firstOf(staleObservation.nodes, "node").observations, + "observation", + ).observation_id = `jag_observation_${"f".repeat(64)}`; + const staleEdge = structuredClone(graph); + firstOf(staleEdge.edges, "edge").edge_id = `jag_edge_${"f".repeat(64)}`; + for (const { input, path } of [ + { input: staleNode, path: ["nodes", 0, "node_id"] }, + { + input: staleObservation, + path: ["nodes", 0, "observations", 0, "observation_id"], + }, + { input: staleEdge, path: ["edges", 0, "edge_id"] }, + ]) + expect( + javascriptApplicationGraphSchema.safeParse(input).error?.issues, + ).toContainEqual(expect.objectContaining({ path, code: "custom" })); }); describe("JavaScript Application Graph: duplicate and orphan rejection", () => { @@ -667,48 +574,67 @@ describe("JavaScript Application Graph: duplicate and orphan rejection", () => { }); }); -it("rejects runtime identities without an explicit capture Evidence identifier", () => { - expect(() => +it("requires capture provenance for runtime script identities", () => { + const captureEvidenceId = `ev_${"c".repeat(64)}`; + const identity = { + strategy: "runtime-instance", + stability: "capture-only", + capture_sha256: APPLICATION_GRAPH_DIGESTS.capture, + runtime_key: "script-preload", + }; + const node = ( + candidate: unknown, + evidence = runtimeEvidence("script-preload"), + ) => createJavaScriptApplicationNode({ kind: "runtime-script-instance", - identity: { - strategy: "runtime-instance", - stability: "capture-only", - capture_sha256: APPLICATION_GRAPH_DIGESTS.capture, - runtime_key: "script-preload", - }, - observations: [ - { - label: null, - properties: {}, - evidence: runtimeEvidence("script-preload"), - }, - ], - }), - ).toThrow(/capture_evidence_id/u); -}); + identity: candidate, + observations: [{ label: "preload", properties: {}, evidence }], + }); + expect(() => node(identity)).toThrow(/capture_evidence_id/u); + const matching = { ...identity, capture_evidence_id: captureEvidenceId }; + const runtime = node(matching); + expect(graphForNode(runtime).nodes).toEqual([runtime]); -it("requires runtime identities to retain their capture Evidence provenance", () => { - const runtimeWithUnrelatedEvidence = createJavaScriptApplicationNode({ - kind: "runtime-script-instance", - identity: { - strategy: "runtime-instance", - stability: "capture-only", - capture_evidence_id: `ev_${"d".repeat(64)}`, - capture_sha256: APPLICATION_GRAPH_DIGESTS.capture, - runtime_key: "script-preload", - }, - observations: [ - { - label: null, - properties: {}, - evidence: runtimeEvidence("script-preload"), - }, - ], - }); - expect(() => graphForNode(runtimeWithUnrelatedEvidence)).toThrow( + const otherEvidenceId = `ev_${"d".repeat(64)}`; + const foreign = { ...identity, capture_evidence_id: otherEvidenceId }; + expect(() => graphForNode(node(foreign))).toThrow( /identity is not supported/u, ); + expect(() => + graphForNode( + node( + matching, + artifactEvidence( + APPLICATION_GRAPH_DIGESTS.asar, + "dist/preload.js", + "ast-static-analysis", + ), + ), + ), + ).toThrow(/identity is not supported/u); + const otherEvidence = runtimeEvidence("script-preload"); + otherEvidence.evidence_ids = [otherEvidenceId]; + const otherCapture = node(foreign, otherEvidence); + expect(graphForNode(otherCapture).nodes[0]?.node_id).not.toBe( + runtime.node_id, + ); + + const otherDigest = "6".repeat(64); + const otherLocation = runtimeEvidence("script-preload"); + if ( + !otherLocation.location.available || + otherLocation.location.value.kind !== "runtime" + ) + throw new Error("Missing fixture runtime location"); + otherLocation.location.value.capture_sha256 = otherDigest; + const relocatedCapture = node( + { ...matching, capture_sha256: otherDigest }, + otherLocation, + ); + expect(graphForNode(relocatedCapture).nodes[0]?.node_id).not.toBe( + runtime.node_id, + ); }); describe("JavaScript Application Graph: identity strategy requirements", () => { @@ -736,31 +662,6 @@ describe("JavaScript Application Graph: identity strategy requirements", () => { /identity is not supported/u, ); - const runtimeWithStaticEvidence = createJavaScriptApplicationNode({ - kind: "runtime-script-instance", - identity: { - strategy: "runtime-instance", - stability: "capture-only", - capture_evidence_id: `ev_${"c".repeat(64)}`, - capture_sha256: APPLICATION_GRAPH_DIGESTS.capture, - runtime_key: "script-preload", - }, - observations: [ - { - label: null, - properties: {}, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/preload.js", - "ast-static-analysis", - ), - }, - ], - }); - expect(() => graphForNode(runtimeWithStaticEvidence)).toThrow( - /identity is not supported/u, - ); - const mismatchedDigest = contentNode( artifactEvidence("7".repeat(64), "dist/module.js"), ); @@ -787,20 +688,49 @@ describe("JavaScript Application Graph: identity strategy requirements", () => { ); }); - it.each(["ast-static-analysis", "managed-static-analysis"] as const)( - "prevents %s from claiming runtime locations", - (authority) => { - const evidence = artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/module.js", - authority, - ); - evidence.location = runtimeEvidence("script-module").location; - expect(() => graphForNode(contentNode(evidence))).toThrow( - /cannot claim a runtime location/u, - ); - }, - ); + it("requires static and native locations to agree with their observation authority", () => { + const path = "dist/module.js"; + const base = contentNode( + artifactEvidence(APPLICATION_GRAPH_DIGESTS.asar, path), + ); + const graph = graphForNode(base); + const badAuthority = [ + "ast-static-analysis", + "managed-static-analysis", + "native-analysis-provider", + ] as const; + const mixed = firstOf( + mergeJavaScriptApplicationNodes([ + base, + ...badAuthority.map((authority) => { + const evidence = artifactEvidence( + APPLICATION_GRAPH_DIGESTS.asar, + path, + authority, + ); + evidence.location = runtimeEvidence("script-module").location; + return contentNode(evidence); + }), + ]), + "mixed authority node", + ); + const rejected = javascriptApplicationGraphSchema + .safeParse({ + ...graph, + nodes: [mixed], + }) + .error?.issues.filter( + ({ message, path }) => + path.at(-1) === "location" && + message === + "Static and native authorities cannot claim a runtime location", + ) + .map( + ({ path }) => mixed.observations[Number(path[3])]?.evidence.authority, + ) + .sort(); + expect(rejected).toEqual(badAuthority); + }); it("enforces epistemic truthfulness and content-addressed artifacts", () => { const inferredAsObserved = inferredArtifactEvidence( diff --git a/src/domain/javascript/javascriptApplicationVersionComparison.test.ts b/src/domain/javascript/javascriptApplicationVersionComparison.test.ts index 71715b88f4..6284ea1e5b 100644 --- a/src/domain/javascript/javascriptApplicationVersionComparison.test.ts +++ b/src/domain/javascript/javascriptApplicationVersionComparison.test.ts @@ -7,8 +7,6 @@ import { createJavaScriptApplicationEdge, createJavaScriptApplicationGraph, createJavaScriptApplicationNode, - isValidatedImmutableJavaScriptApplicationGraph, - parseJavaScriptApplicationGraph, type JavaScriptApplicationGraph, } from "./javascriptApplicationGraph.js"; import { @@ -99,8 +97,8 @@ const compare = () => rightNativeEvidence: [], }); -const unrelatedGraph = (artifactSha: string): JavaScriptApplicationGraph => { - const node = createJavaScriptApplicationNode({ +const unrelatedNode = (artifactSha: string) => + createJavaScriptApplicationNode({ kind: "javascript-chunk", identity: { strategy: "canonical-path", @@ -116,15 +114,6 @@ const unrelatedGraph = (artifactSha: string): JavaScriptApplicationGraph => { }, ], }); - return createJavaScriptApplicationGraph({ - schema: "JavaScriptApplicationGraph", - root_node_ids: [node.node_id], - nodes: [node], - edges: [], - coverage: completeCoverage, - limitations: [], - }); -}; const nativeEvidence = (sha256: string | undefined, ordinal: number) => createEvidence( @@ -260,108 +249,93 @@ it.each(occurrenceDifferences)( }, ); -describe("application version comparison Evidence links", () => { - it("links every exact digest observation only to its paired or unmatched item", () => { - const left = buildGraph(LEFT_SHA, 1); - const right = buildGraph(RIGHT_SHA, 2); - const leftEvidenceId = `ev_${"1".repeat(64)}`; - const rightEvidenceId = `ev_${"2".repeat(64)}`; - const leftBDigest = `${"0".repeat(63)}1`; - const leftNative = [ - nativeEvidence(leftBDigest, 1), - nativeEvidence(leftBDigest, 2), - nativeEvidence("9".repeat(64), 3), - ]; - const rightNative = [ - nativeEvidence("0".repeat(64), 4), +it("attributes native Evidence to exact observations on the owning side", () => { + const left = buildGraph(LEFT_SHA, 1); + const fullRight = buildGraph(RIGHT_SHA, 2); + const byLabel = (graph: JavaScriptApplicationGraph, label: string) => { + const node = graph.nodes.find(({ observations }) => + observations.some((value) => value.label === label), + ); + if (node === undefined) throw new Error(`Missing fixture node ${label}`); + return node; + }; + const leftA = byLabel(left, "a.js"); + const leftB = byLabel(left, "b.js"); + const rightA = byLabel(fullRight, "a.js"); + const rightB = byLabel(fullRight, "b.js"); + const unrelated = unrelatedNode(RIGHT_SHA); + const { graph_id: _graphId, ...rightInput } = fullRight; + const right = createJavaScriptApplicationGraph({ + ...rightInput, + nodes: [ + ...fullRight.nodes.filter(({ node_id }) => node_id !== rightB.node_id), + unrelated, + ], + edges: fullRight.edges.filter( + ({ source_node_id, target_node_id }) => + source_node_id !== rightB.node_id && target_node_id !== rightB.node_id, + ), + }); + const leftEvidenceId = `ev_${"1".repeat(64)}`; + const rightEvidenceId = `ev_${"2".repeat(64)}`; + const parents = [leftEvidenceId, rightEvidenceId]; + const bDigest = `${"0".repeat(63)}1`; + const leftNativeA = nativeEvidence("0".repeat(64), 1); + const leftNativeB = nativeEvidence(bDigest, 2); + const leftNativeB2 = nativeEvidence(bDigest, 3); + const rightNativeA = nativeEvidence("0".repeat(64), 4); + const result = compareJavaScriptApplicationVersions({ + left: { + evidenceId: leftEvidenceId, + rootArtifactSha256: LEFT_SHA, + graph: left, + }, + right: { + evidenceId: rightEvidenceId, + rootArtifactSha256: RIGHT_SHA, + graph: right, + }, + leftNativeEvidence: [leftNativeA, leftNativeB, leftNativeB2], + rightNativeEvidence: [ + rightNativeA, + nativeEvidence(bDigest, 5), nativeEvidence(undefined, 6), - ]; - const unrelatedRight = unrelatedGraph(RIGHT_SHA); - const unmatched = compareJavaScriptApplicationVersions({ - left: { - evidenceId: leftEvidenceId, - rootArtifactSha256: LEFT_SHA, - graph: left, - }, - right: { - evidenceId: rightEvidenceId, - rootArtifactSha256: RIGHT_SHA, - graph: unrelatedRight, - }, - leftNativeEvidence: leftNative, - rightNativeEvidence: rightNative, - }); - const leftB = left.nodes.find(({ observations }) => - observations.some(({ label }) => label === "b.js"), - ); - if (leftB === undefined) throw new Error("Missing left b.js node"); - const unmatchedB = unmatched.items.find( - ({ left_node_id: id }) => id === leftB.node_id, - ); - expect(unmatchedB?.evidence_links).toEqual( - [ - leftEvidenceId, - rightEvidenceId, - leftNative[0]!.evidence_id, - leftNative[1]!.evidence_id, - ].sort(), - ); - const unrelatedItem = unmatched.items.find( - ({ right_node_id: id }) => id === unrelatedRight.nodes[0]?.node_id, - ); - expect(unrelatedItem?.evidence_links).toEqual( - [leftEvidenceId, rightEvidenceId].sort(), - ); - - const paired = compareJavaScriptApplicationVersions({ - left: { - evidenceId: leftEvidenceId, - rootArtifactSha256: LEFT_SHA, - graph: left, - }, - right: { - evidenceId: rightEvidenceId, - rootArtifactSha256: RIGHT_SHA, - graph: right, - }, - leftNativeEvidence: [nativeEvidence("0".repeat(64), 5)], - rightNativeEvidence: rightNative, - }); - const leftA = left.nodes.find(({ observations }) => - observations.some(({ label }) => label === "a.js"), - ); - if (leftA === undefined) throw new Error("Missing left a.js node"); - const pairedA = paired.items.find( - ({ left_node_id: id }) => id === leftA.node_id, - ); - expect(pairedA?.match.status).toBe("matched"); - expect(pairedA?.evidence_links).toEqual( - [ - leftEvidenceId, - rightEvidenceId, - nativeEvidence("0".repeat(64), 5).evidence_id, - rightNative[0]!.evidence_id, - ].sort(), - ); + ], + }); + expect( + result.items.find(({ left_node_id }) => left_node_id === leftA.node_id), + ).toMatchObject({ + right_node_id: rightA.node_id, + match: { status: "matched" }, + evidence_links: [ + ...parents, + leftNativeA.evidence_id, + rightNativeA.evidence_id, + ].sort(), + }); + expect( + result.items.find(({ left_node_id }) => left_node_id === leftB.node_id), + ).toMatchObject({ + right_node_id: null, + status: "removed", + evidence_links: [ + ...parents, + leftNativeB.evidence_id, + leftNativeB2.evidence_id, + ].sort(), + }); + expect( + result.items.find( + ({ right_node_id }) => right_node_id === unrelated.node_id, + ), + ).toMatchObject({ + left_node_id: null, + status: "added", + evidence_links: parents.sort(), }); }); describe("application version comparison graph ownership", () => { - it("returns a sealed change graph that passes complete validation", () => { - const result = compare(); - expect( - result.graph.edges.some(({ relation }) => relation === "changed_from"), - ).toBe(true); - expect(isValidatedImmutableJavaScriptApplicationGraph(result.graph)).toBe( - true, - ); - expect(Object.isFrozen(result.graph.edges)).toBe(true); - expect(parseJavaScriptApplicationGraph(result.graph)).toEqual(result.graph); - expect(applicationVersionComparisonResultSchema.parse(result)).toEqual( - result, - ); - }); - it("retains deduplicated complete input limits on the change graph and inferred edge", () => { const leftLimits = [ { name: "modules", value: 100, unit: "items" as const }, @@ -414,30 +388,46 @@ describe("application version comparison graph ownership", () => { }); }); - it("does not reuse a change graph without an owned proof", () => { + it("reuses only the owned change graph and validates its enclosing result", () => { const result = compare(); - const copy = structuredClone(result.graph); - expect(isValidatedImmutableJavaScriptApplicationGraph(copy)).toBe(false); - expect(() => - ownedApplicationVersionComparisonResultSchema.parse({ - ...result, - graph: copy, - }), - ).toThrow(); expect( - applicationVersionComparisonResultSchema.parse({ ...result, graph: copy }) - .graph, - ).toEqual(result.graph); - }); + ownedApplicationVersionComparisonResultSchema.parse(result).graph, + ).toBe(result.graph); - it("keeps coverage validation for owned results", () => { - const result = compare(); - expect(() => - ownedApplicationVersionComparisonResultSchema.parse({ + const copied = { ...result, graph: structuredClone(result.graph) }; + expect( + ownedApplicationVersionComparisonResultSchema.safeParse(copied).error + ?.issues, + ).toMatchObject([{ path: ["graph"] }]); + expect(applicationVersionComparisonResultSchema.parse(copied)).toEqual( + result, + ); + + for (const status of ["partial", "unknown"] as const) { + const checked = ownedApplicationVersionComparisonResultSchema.safeParse({ ...result, - coverage: { ...result.coverage, status: "partial" }, - }), - ).toThrow("Comparison coverage must match source graph completeness"); + coverage: { ...result.coverage, status }, + }); + const messages = checked.error?.issues.map(({ path, message }) => ({ + path, + message, + })); + expect(messages).toEqual([ + { + path: ["coverage", "status"], + message: "Comparison coverage must match source graph completeness", + }, + ...(status === "unknown" + ? [ + { + path: ["coverage", "status"], + message: + "Comparison coverage is unknown exactly when a source graph's coverage is unknown or unavailable", + }, + ] + : []), + ]); + } }); }); @@ -485,45 +475,54 @@ describe("version comparison coverage", () => { limits: [{ name: "modules", value: 3, unit: "items" }], }; - it.each(["unknown", "unavailable"] as const)( - "reports unknown, not a known gap, when a source graph is %s", - (status) => { - const result = compareWith(completeCoverage, unknown(status)); - expect(result.coverage).toMatchObject({ - right_graph_status: status, - status: "unknown", - }); - // The change graph and each changed_from edge make the same claim. - expect(result.graph.coverage).toMatchObject({ + it.each([ + { + name: "unknown source", + left: unknown("unknown"), + right: completeCoverage, + status: "unknown", + graphCoverage: { status: "unknown", + truncated: false, omitted_count: null, + }, + }, + { + name: "unavailable source with truncation", + left: truncated, + right: unknown("unavailable"), + status: "unknown", + graphCoverage: { + status: "unknown", + truncated: false, + omitted_count: null, + }, + }, + { + name: "reported truncation", + left: truncated, + right: completeCoverage, + status: "truncated", + graphCoverage: { status: "partial", truncated: true, omitted_count: 3 }, + }, + ])( + "publishes supported coverage for $name", + ({ left, right, status, graphCoverage }) => { + const result = compareWith(left, right); + expect(result.coverage).toEqual({ + status, + left_graph_status: left.status, + right_graph_status: right.status, + left_graph_omitted_count: left.omitted_count, + right_graph_omitted_count: right.omitted_count, }); + expect(result.graph.coverage).toMatchObject(graphCoverage); const changed = result.graph.edges.filter( ({ relation }) => relation === "changed_from", ); expect(changed.length).toBeGreaterThan(0); for (const edge of changed) - expect(edge.evidence.coverage.status).toBe("unknown"); - // Unknown wins over a known truncation on the other side. - expect(compareWith(truncated, unknown(status)).coverage.status).toBe( - "unknown", - ); + expect(edge.evidence.coverage).toMatchObject(graphCoverage); }, ); - - it("keeps a known truncation when both graphs report their coverage", () => { - expect(compareWith(truncated, completeCoverage).coverage.status).toBe( - "truncated", - ); - }); - - it("refuses an unknown status that the source graphs do not support", () => { - const result = compare(); - expect(() => - ownedApplicationVersionComparisonResultSchema.parse({ - ...result, - coverage: { ...result.coverage, status: "unknown" }, - }), - ).toThrow(/unknown exactly when/u); - }); }); diff --git a/src/domain/javascript/javascriptApplicationVersionMatching.test.ts b/src/domain/javascript/javascriptApplicationVersionMatching.test.ts index 247f44f00a..54bb81a601 100644 --- a/src/domain/javascript/javascriptApplicationVersionMatching.test.ts +++ b/src/domain/javascript/javascriptApplicationVersionMatching.test.ts @@ -70,28 +70,53 @@ const compare = ( rightNativeEvidence: [], }); -describe("application version matching", () => { - it.each([ - ["babel-ast-v1", "babel-ast-v1"], - ["babel-ast-v2", "babel-ast-v2"], - ["babel-ast-v3", "babel-ast-v3"], - ["babel-ast-v1", "babel-ast-v2"], - ["babel-ast-v1", "babel-ast-v3"], - ["babel-ast-v2", "babel-ast-v3"], - ])( - "compares %s with %s without mixing fingerprint algorithms", - (leftAlgorithm, rightAlgorithm) => { - const left = moduleNode("before", "1", "a", leftAlgorithm); - const right = moduleNode("after", "2", "a", rightAlgorithm); - const result = compare(graph([left]), graph([right])); - const sameAlgorithm = leftAlgorithm === rightAlgorithm; - expect(result.matching.structural_fingerprint).toBe( - sameAlgorithm ? 1 : 0, - ); - expect(result.matching.unmatched).toBe(sameAlgorithm ? 0 : 2); - }, - ); +it("matches equal digests only within the same archived fingerprint algorithm", () => { + const left = [ + moduleNode("left-v1", "1", "a", "babel-ast-v1"), + moduleNode("left-v2", "2", "a", "babel-ast-v2"), + moduleNode("left-v3", "3", "a", "babel-ast-v3"), + moduleNode("left-incompatible", "4", "b", "babel-ast-v1"), + ] as const; + const right = [ + moduleNode("right-v3", "5", "a", "babel-ast-v3"), + moduleNode("right-v2", "6", "a", "babel-ast-v2"), + moduleNode("right-v1", "7", "a", "babel-ast-v1"), + moduleNode("right-incompatible", "8", "b", "babel-ast-v3"), + ] as const; + const result = compare(graph([...left]), graph([...right])); + expect(result.items).toHaveLength(5); + for (const [before, after] of [ + [left[0], right[2]], + [left[1], right[1]], + [left[2], right[0]], + ] as const) { + expect( + result.items.find(({ left_node_id }) => left_node_id === before.node_id), + ).toMatchObject({ + right_node_id: after.node_id, + status: "changed", + match: { status: "matched", basis: "structural-fingerprint" }, + }); + } + expect( + result.items.find(({ left_node_id }) => left_node_id === left[3].node_id), + ).toMatchObject({ + right_node_id: null, + status: "removed", + match: { status: "unmatched" }, + }); + expect( + result.items.find( + ({ right_node_id }) => right_node_id === right[3].node_id, + ), + ).toMatchObject({ + left_node_id: null, + status: "added", + match: { status: "unmatched" }, + }); +}); +describe("application version matching", () => { it("does not revisit a source group that becomes unique after identity matching", () => { const shared = moduleNode("shared", "1"); const leftOnly = moduleNode("left", "1"); diff --git a/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts b/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts index 951d073434..dcd27b0af8 100644 --- a/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts +++ b/src/domain/javascript/javascriptApplicationVersionRelationshipUncertainty.test.ts @@ -12,10 +12,9 @@ import { compareJavaScriptApplicationVersions } from "./javascriptApplicationVer const graph = ( sha: string, - peers: "ambiguous" | "unique" | "unmatched", + peers: "ambiguous" | "unmatched", options: { relation?: "imports" | "invokes"; - target?: "peer-one" | "peer-two"; direction?: "in" | "out"; content?: string; partial?: true; @@ -39,9 +38,7 @@ const graph = ( ? (options.content ?? "d".repeat(64)) : peers === "ambiguous" ? "c".repeat(64) - : peers === "unique" - ? (key === "peer-one" ? "e" : "f").repeat(64) - : (key === "peer-one" ? "1" : "2").repeat(64), + : (key === "peer-one" ? "1" : "2").repeat(64), ...(key === "main" ? { structural_fingerprint_sha256: "9".repeat(64), @@ -52,8 +49,7 @@ const graph = ( evidence, ), ); - const main = nodes[0], - target = nodes[options.target === "peer-two" ? 2 : 1]; + const [main, target] = nodes; if (main === undefined || target === undefined) throw new Error("Missing fixture nodes"); return { @@ -147,15 +143,6 @@ it.each(["ambiguous", "unmatched"] as const)( }, ); -it("keeps equal relationships to uniquely paired peers unchanged", () => { - const { item } = compare( - graph("a".repeat(64), "unique"), - graph("b".repeat(64), "unique"), - ); - expect(item).toMatchObject({ status: "unchanged", dimensions: [] }); - expect(item.limitations).toEqual([]); -}); - it.each([{ relation: "invokes" as const }, { direction: "in" as const }])( "retains positive relation/direction differences beside unknown peer identity %j", (options) => { @@ -173,17 +160,6 @@ it.each([{ relation: "invokes" as const }, { direction: "in" as const }])( }, ); -it("reports a resolved target change", () => { - const { item } = compare( - graph("a".repeat(64), "unique"), - graph("b".repeat(64), "unique", { target: "peer-two" }), - ); - expect(item).toMatchObject({ - status: "changed", - dimensions: ["relationships"], - }); -}); - it("preserves definite content change alongside unresolved relationships", () => { const { item } = compare( graph("a".repeat(64), "ambiguous"), diff --git a/src/domain/javascript/javascriptApplicationVersionRelationships.test.ts b/src/domain/javascript/javascriptApplicationVersionRelationships.test.ts index 40b0dc0780..e7a74fe6cf 100644 --- a/src/domain/javascript/javascriptApplicationVersionRelationships.test.ts +++ b/src/domain/javascript/javascriptApplicationVersionRelationships.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { expect, it } from "vitest"; import { createJavaScriptApplicationEdge, @@ -25,10 +25,8 @@ const sourceDigest = (name: string): string => const buildGraph = ( artifactSha: string, edges: readonly EdgeSpec[], - order: "forward" | "reverse" = "forward", ): JavaScriptApplicationGraph => { - const names = order === "forward" ? [...NAMES] : [...NAMES].reverse(); - const nodes = names.map((name) => + const nodes = NAMES.map((name) => createJavaScriptApplicationNode({ kind: "javascript-module", identity: { @@ -55,12 +53,11 @@ const buildGraph = ( if (node === undefined) throw new Error(`Missing fixture node ${name}`); return node.node_id; }; - const ordered = order === "forward" ? [...edges] : [...edges].reverse(); return createJavaScriptApplicationGraph({ schema: "JavaScriptApplicationGraph", root_node_ids: [idOf("a")], nodes, - edges: ordered.map(([source, target, relation]) => + edges: edges.map(([source, target, relation]) => createJavaScriptApplicationEdge({ source_node_id: idOf(source), target_node_id: idOf(target), @@ -74,11 +71,11 @@ const buildGraph = ( }); }; -const relationshipChanges = ( +const compare = ( left: JavaScriptApplicationGraph, right: JavaScriptApplicationGraph, -): string[] => { - const result = compareJavaScriptApplicationVersions({ +) => { + return compareJavaScriptApplicationVersions({ left: { evidenceId: `ev_${"1".repeat(64)}`, rootArtifactSha256: LEFT_SHA, @@ -92,17 +89,6 @@ const relationshipChanges = ( leftNativeEvidence: [], rightNativeEvidence: [], }); - expect(result.summary.added + result.summary.removed).toBe(0); - return result.items - .filter(({ dimensions }) => dimensions.includes("relationships")) - .map(({ left_node_id: id }) => { - const node = left.nodes.find(({ node_id: nodeId }) => nodeId === id); - const label = node?.observations[0]?.label; - if (label === undefined || label === null) - throw new Error("Changed item has no node"); - return label; - }) - .sort(); }; const BASE_EDGES: readonly EdgeSpec[] = [ @@ -112,64 +98,93 @@ const BASE_EDGES: readonly EdgeSpec[] = [ ["c", "b", "imports"], ]; -describe("application version relationship comparison", () => { - it("reports no relationship change for identical topology in any node or edge order", () => { - expect( - relationshipChanges( - buildGraph(LEFT_SHA, BASE_EDGES), - buildGraph(RIGHT_SHA, BASE_EDGES, "reverse"), - ), - ).toEqual([]); - }); +it("keeps paired relationships unchanged regardless of observation order", () => { + const left = buildGraph(LEFT_SHA, BASE_EDGES); + const right = buildGraph(RIGHT_SHA, BASE_EDGES); + const result = compare(left, right); + expect(result.items).toHaveLength(NAMES.length); + expect(result.items).toMatchObject( + Array.from(NAMES, () => ({ + status: "unchanged", + dimensions: [], + limitations: [], + match: { basis: "exact-module-source-digest" }, + })), + ); + expect( + compare(left, { + ...right, + nodes: [...right.nodes].reverse(), + edges: [...right.edges].reverse(), + }), + ).toEqual(result); +}); - it("flags both endpoints when an edge relation changes", () => { - const changed: readonly EdgeSpec[] = [ +it.each<{ + name: string; + before: readonly EdgeSpec[]; + after: readonly EdgeSpec[]; + changed: readonly ModuleName[]; +}>([ + { + name: "relation kind", + before: BASE_EDGES, + after: [ ["a", "b", "imports"], ["b", "c", "contains"], ["a", "c", "contains"], ["c", "b", "imports"], - ]; - expect( - relationshipChanges( - buildGraph(LEFT_SHA, BASE_EDGES), - buildGraph(RIGHT_SHA, changed), - ), - ).toEqual(["b.js", "c.js"]); - }); - - it("flags a node whose only change is an incoming edge", () => { - const left: readonly EdgeSpec[] = [["a", "b", "imports"]]; - const right: readonly EdgeSpec[] = [ + ], + changed: ["b", "c"], + }, + { + name: "new inbound edge", + before: [["a", "b", "imports"]], + after: [ ["a", "b", "imports"], ["c", "b", "imports"], - ]; - expect( - relationshipChanges( - buildGraph(LEFT_SHA, left), - buildGraph(RIGHT_SHA, right), - ), - ).toEqual(["b.js", "c.js"]); - }); - - it("flags a node when an edge is re-pointed at a different matched neighbour", () => { - const left: readonly EdgeSpec[] = [["a", "b", "imports"]]; - const right: readonly EdgeSpec[] = [["a", "c", "imports"]]; - expect( - relationshipChanges( - buildGraph(LEFT_SHA, left), - buildGraph(RIGHT_SHA, right), - ), - ).toEqual(["a.js", "b.js", "c.js"]); - }); - - it("distinguishes edge direction between the same pair", () => { - const left: readonly EdgeSpec[] = [["a", "b", "imports"]]; - const right: readonly EdgeSpec[] = [["b", "a", "imports"]]; - expect( - relationshipChanges( - buildGraph(LEFT_SHA, left), - buildGraph(RIGHT_SHA, right), - ), - ).toEqual(["a.js", "b.js"]); - }); -}); + ], + changed: ["b", "c"], + }, + { + name: "new matched target", + before: [["a", "b", "imports"]], + after: [["a", "c", "imports"]], + changed: ["a", "b", "c"], + }, + { + name: "reversed edge", + before: [["a", "b", "imports"]], + after: [["b", "a", "imports"]], + changed: ["a", "b"], + }, +])( + "reports exact endpoint changes for a $name", + ({ before, after, changed }) => { + const left = buildGraph(LEFT_SHA, before); + const right = buildGraph(RIGHT_SHA, after); + const result = compare(left, right); + expect(result.items).toHaveLength(NAMES.length); + for (const name of NAMES) { + const path = `${name}.js`; + const oldNode = left.nodes.find( + ({ observations }) => observations[0]?.label === path, + ); + const newNode = right.nodes.find( + ({ observations }) => observations[0]?.label === path, + ); + const affected = changed.includes(name); + expect( + result.items.find( + ({ left_node_id }) => left_node_id === oldNode?.node_id, + ), + path, + ).toMatchObject({ + right_node_id: newNode?.node_id, + status: affected ? "changed" : "unchanged", + dimensions: affected ? ["relationships"] : [], + match: { basis: "exact-module-source-digest" }, + }); + } + }, +); diff --git a/src/domain/javascript/javascriptApplicationVersionRelocation.test.ts b/src/domain/javascript/javascriptApplicationVersionRelocation.test.ts index 76af18f2dd..f35541d670 100644 --- a/src/domain/javascript/javascriptApplicationVersionRelocation.test.ts +++ b/src/domain/javascript/javascriptApplicationVersionRelocation.test.ts @@ -135,40 +135,35 @@ const partialCoverage: JavaScriptApplicationGraph["coverage"] = { omitted_count: null, }; -it("does not report a candidate-only IPC edge as a definite relationship change", () => { - const result = compare( - graph("a".repeat(64), 10, partialCoverage, { candidate: true }), - graph("b".repeat(64), 50, partialCoverage, { - noEdge: true, - permission: "changed", - }), - ); - const channel = result.items.find((item) => item.node_kind === "ipc-channel"); - expect(channel?.status).toBe("changed"); - expect(channel?.dimensions).toContain("properties"); - expect(channel?.dimensions).not.toContain("relationships"); - expect(channel?.limitations.length).toBeGreaterThan(0); -}); - -it.each([false, true])( - "keeps a candidate and a definite equivalent uncertain when reversed: %s", - (reverse) => { - const candidate = graph("a".repeat(64), 10, partialCoverage, { - candidate: true, - }); - const definite = graph("b".repeat(64), 50, partialCoverage, { - permission: "changed", - }); - const result = reverse - ? compare(definite, candidate) - : compare(candidate, definite); +it("keeps candidate edges uncertain against missing and definite counterparts", () => { + const candidate = graph("a".repeat(64), 10, partialCoverage, { + candidate: true, + }); + const absent = graph("b".repeat(64), 50, partialCoverage, { + noEdge: true, + permission: "changed", + }); + const definite = graph("b".repeat(64), 50, partialCoverage, { + permission: "changed", + }); + for (const [left, right] of [ + [candidate, absent], + [candidate, definite], + [definite, candidate], + ] as const) { + const result = compare(left, right); const channel = result.items.find( - (item) => item.node_kind === "ipc-channel", + ({ node_kind }) => node_kind === "ipc-channel", ); - expect(channel?.status).toBe("changed"); - expect(channel?.dimensions).not.toContain("relationships"); - }, -); + expect(channel).toMatchObject({ + status: "changed", + dimensions: ["location", "properties"], + limitations: expect.arrayContaining([ + "Incomplete source coverage leaves some candidate relationships unconfirmed.", + ]), + }); + } +}); it("retains an independent definite relationship change beside candidate edges", () => { const result = compare( @@ -180,8 +175,10 @@ it("retains an independent definite relationship change beside candidate edges", }), ); const channel = result.items.find((item) => item.node_kind === "ipc-channel"); - expect(channel?.status).toBe("changed"); - expect(channel?.dimensions).toContain("relationships"); + expect(channel).toMatchObject({ + status: "changed", + dimensions: ["location", "properties", "relationships"], + }); }); it("keeps relocated exact modules and inferred channels unchanged while retaining both coordinates", () => { @@ -257,34 +254,31 @@ it("still classifies complete relationship changes beside relocation as changed" expect(item.dimensions).toEqual(["location", "relationships"]); }); -it("retains definite content changes under partial coverage alongside relocation", () => { +it("retains independent content and property changes beside relocation under partial coverage", () => { const result = compare( graph("a".repeat(64), 10, completeCoverage), - graph("b".repeat(64), 50, partialCoverage, { content: "e".repeat(64) }), + graph("b".repeat(64), 50, partialCoverage, { + content: "e".repeat(64), + permission: "changed", + }), ); + expect(result.summary).toEqual({ + unchanged: 0, + added: 0, + removed: 0, + changed: 2, + unknown: 0, + }); expect( - result.items.find((item) => item.node_kind === "javascript-module"), + result.items.find(({ node_kind }) => node_kind === "javascript-module"), ).toMatchObject({ status: "changed", dimensions: ["content", "location", "properties"], }); expect( - result.items.find((item) => item.node_kind === "ipc-channel")?.status, - ).toBe("unknown"); -}); - -it("retains definite property changes beside relocation", () => { - const result = compare( - graph("a".repeat(64), 10, completeCoverage), - graph("b".repeat(64), 50, partialCoverage, { permission: "changed" }), - ); - expect( - result.items.find((item) => item.node_kind === "ipc-channel"), + result.items.find(({ node_kind }) => node_kind === "ipc-channel"), ).toMatchObject({ status: "changed", dimensions: ["location", "properties"], }); - expect( - result.items.find((item) => item.node_kind === "javascript-module")?.status, - ).toBe("unknown"); }); diff --git a/src/domain/javascript/javascriptApplicationVersionUnavailableScopes.test.ts b/src/domain/javascript/javascriptApplicationVersionUnavailableScopes.test.ts index 13d415e334..64a0cf4e9c 100644 --- a/src/domain/javascript/javascriptApplicationVersionUnavailableScopes.test.ts +++ b/src/domain/javascript/javascriptApplicationVersionUnavailableScopes.test.ts @@ -109,37 +109,6 @@ const compare = ( rightNativeEvidence: [], }); -it("keeps unmatched unavailable scopes unknown in either direction", () => { - const leftRoot = node("d".repeat(64), "main.js"); - const rightRoot = node("d".repeat(64), "main.js"); - const leftScope = unavailableScope(LEFT_SHA); - const rightScope = unavailableScope(RIGHT_SHA); - const forward = compare([leftRoot], [rightRoot, rightScope]); - const reverse = compare([leftRoot, leftScope], [rightRoot]); - - expect( - forward.items.find( - ({ right_node_id }) => right_node_id === rightScope.node_id, - ), - ).toMatchObject({ - status: "unknown", - dimensions: ["availability"], - limitations: [LIMITATION], - evidence_links: [evidenceId("1"), evidenceId("2")], - }); - expect( - reverse.items.find( - ({ left_node_id }) => left_node_id === leftScope.node_id, - ), - ).toMatchObject({ - status: "unknown", - dimensions: ["availability"], - limitations: [LIMITATION], - }); - expect(forward.summary).toMatchObject({ added: 0, removed: 0, unknown: 1 }); - expect(reverse.summary).toMatchObject({ added: 0, removed: 0, unknown: 1 }); -}); - it("keeps matched unavailable scopes unknown with their source limitation", () => { const leftRoot = node("d".repeat(64), "main.js"); const rightRoot = node("d".repeat(64), "main.js"); @@ -162,48 +131,52 @@ it("retains unknown scope facts and reports genuine entity changes", () => { const leftRoot = node("d".repeat(64), "main.js"); const rightRoot = node("d".repeat(64), "main.js"); const unknown = unavailableScope(RIGHT_SHA, "unknown"); - const added = node("e".repeat(64), "new.js"); + const unavailable = unavailableScope(LEFT_SHA); const removed = node("f".repeat(64), "removed.js"); - const mixedEvidenceAddition = observedAssetWithUnavailableFact(); - const unknownResult = compare([leftRoot], [rightRoot, unknown]); - const addedResult = compare([leftRoot], [rightRoot, added]); - const removedResult = compare([leftRoot, removed], [rightRoot]); - const mixedResult = compare([leftRoot], [rightRoot, mixedEvidenceAddition]); + const added = observedAssetWithUnavailableFact(); + const result = compare( + [leftRoot, removed, unavailable], + [rightRoot, added, unknown], + ); - expect( - unknownResult.items.find( - ({ right_node_id }) => right_node_id === unknown.node_id, - ), - ).toMatchObject({ status: "unknown", limitations: [LIMITATION] }); - expect(unknownResult.summary).toMatchObject({ added: 0, unknown: 1 }); - expect( - addedResult.items.find( - ({ right_node_id }) => right_node_id === added.node_id, - ), - ).toMatchObject({ status: "added", dimensions: ["availability"] }); - expect(addedResult.summary.added).toBe(1); - expect( - mixedResult.items.find( - ({ right_node_id }) => right_node_id === mixedEvidenceAddition.node_id, - ), - ).toMatchObject({ status: "added", limitations: [LIMITATION] }); - expect(mixedResult.summary.added).toBe(1); - expect( - removedResult.items.find( - ({ left_node_id }) => left_node_id === removed.node_id, - ), - ).toMatchObject({ status: "removed", dimensions: ["availability"] }); - expect(removedResult.summary.removed).toBe(1); - expect( - compare( - [leftRoot, node("c".repeat(64), "same.js")], - [rightRoot, node("c".repeat(64), "same.js")], - ).summary, - ).toMatchObject({ - unchanged: 2, - added: 0, - removed: 0, + expect(result.summary).toEqual({ + unchanged: 1, + added: 1, + removed: 1, + changed: 0, + unknown: 2, }); + expect(result.items).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + left_node_id: unavailable.node_id, + right_node_id: null, + status: "unknown", + dimensions: ["availability"], + limitations: [LIMITATION], + }), + expect.objectContaining({ + left_node_id: null, + right_node_id: unknown.node_id, + status: "unknown", + dimensions: ["availability"], + limitations: [LIMITATION], + }), + expect.objectContaining({ + left_node_id: removed.node_id, + right_node_id: null, + status: "removed", + dimensions: ["availability"], + }), + expect.objectContaining({ + left_node_id: null, + right_node_id: added.node_id, + status: "added", + dimensions: ["availability"], + limitations: [LIMITATION], + }), + ]), + ); }); it("does not treat unexamined source metadata as a file change", () => { diff --git a/tests/acceptance/applications/applicationVersionMatchingCli.test.ts b/tests/acceptance/applications/applicationVersionMatchingCli.test.ts index 5197b8d00d..895e81c264 100644 --- a/tests/acceptance/applications/applicationVersionMatchingCli.test.ts +++ b/tests/acceptance/applications/applicationVersionMatchingCli.test.ts @@ -14,24 +14,20 @@ cliTest( async ({ cli }) => { const root = await createTestTempDirectory("rea-version-scope-"); const application = join(root, "application"); - const copy = join(root, "copy"); - await Promise.all([mkdir(application), mkdir(copy)]); + await mkdir(application); const files = [ ["lib.js", "export function shared() { return 1; }\n"], ["main.js", 'export const name = "one";\n'], ] as const; for (const [name, contents] of files) { - await Promise.all([ - writeFile(join(application, name), contents), - writeFile(join(copy, name), contents), - ]); + await writeFile(join(application, name), contents); } - const analyze = async (input: string, selected: boolean) => { + const analyze = async (selected: boolean) => { const response = await cli.run({ arguments: [ "analyze-javascript-application", - input, + application, ...(selected ? ["--source-path", "main.js"] : []), "--json", ], @@ -40,9 +36,8 @@ cliTest( expect(response.exitCode, response.stderr).toBe(0); return parseEvidence(response.json); }; - const full = await analyze(application, false); - const selected = await analyze(application, true); - const fullCopy = await analyze(copy, false); + const full = await analyze(false); + const selected = await analyze(true); const selectedAnalysis = javascriptApplicationAnalysisResultSchema.parse( selected.normalized_result, ); @@ -83,7 +78,6 @@ cliTest( }; const forward = await compare(full, selected, "forward"); const reverse = await compare(selected, full, "reverse"); - const unchangedCopy = await compare(full, fullCopy, "full-copy"); expect( forward.items.find( @@ -111,7 +105,6 @@ cliTest( expect(forward.summary.removed).toBe(0); expect(reverse.summary.added).toBe(0); expect(reverse.summary.removed).toBe(0); - expect(unchangedCopy.summary).toMatchObject({ added: 0, removed: 0 }); }, ); @@ -119,12 +112,17 @@ cliTest( "uses syntax-aware bundle fingerprints for changed syntax and renamed bindings", async ({ cli }) => { const root = await createTestTempDirectory("rea-fingerprint-version-"); + const literalPrefix = "x".repeat(1_024); const pairs = [ ["bigint", "exports.v = 1n;", "exports.v = 2n;"], ["bigint-key", "exports.v = { 1n: 1 };", "exports.v = { 2n: 1 };"], ["directive", '"use strict";', '"use asm";'], ["assignment", "exports.v = 1;", "exports.v += 1;"], - ["string", 'exports.v = "left";', 'exports.v = "right";'], + [ + "long-string", + `exports.v = "${literalPrefix}left";`, + `exports.v = "${literalPrefix}right";`, + ], ["class-method", "class A { foo() {} }", "class A { bar() {} }"], ["class-field", "class A { foo = 1; }", "class A { bar = 1; }"], [