diff --git a/src/application/javascript/JavaScriptArtifactGraphAccumulator.test.ts b/src/application/javascript/JavaScriptArtifactGraphAccumulator.test.ts index 47a8945f67..f838dc70ea 100644 --- a/src/application/javascript/JavaScriptArtifactGraphAccumulator.test.ts +++ b/src/application/javascript/JavaScriptArtifactGraphAccumulator.test.ts @@ -1,6 +1,5 @@ import { expect, it } from "vitest"; import { completeApplicationCoverage } from "../../domain/javascript/javascriptApplicationEvidenceSchemas.js"; -import { createJavaScriptApplicationNode } from "../../domain/javascript/javascriptApplicationGraph.js"; import { JavaScriptArtifactGraphAccumulator } from "./JavaScriptArtifactGraphAccumulator.js"; import { artifactObservationEvidence } from "./JavaScriptArtifactGraphEvidence.js"; @@ -25,73 +24,45 @@ const nodeInput = (value: string, sha256 = "a".repeat(64)) => ({ ], }); -it("retains live references, exact deduplication and canonical output across later additions", () => { +it("updates held nodes and deduplicates observations across finalization", () => { const accumulator = new JavaScriptArtifactGraphAccumulator(); - const inputs = ["雪😀", "alpha", "beta"].map((value) => nodeInput(value)); - const firstInput = inputs[0]; - if (firstInput === undefined) throw new Error("Missing first input"); - const node = accumulator.addNode(firstInput); - const observations = node.observations; - const firstObservation = observations[0]; - expect(accumulator.addNode(firstInput)).toBe(node); - expect(node.observations).toBe(observations); - expect(observations).toHaveLength(1); - expect(observations[0]).not.toBe(firstObservation); - for (const input of inputs.slice(1)) - expect(accumulator.addNode(input)).toBe(node); - const secondNode = accumulator.addNode(nodeInput("other", "b".repeat(64))); - const firstSnapshot = accumulator.nodes(); - const expectedNode = createJavaScriptApplicationNode({ - ...firstInput, - observations: inputs.flatMap((input) => input.observations), - }); - expect(firstSnapshot).toEqual([expectedNode, secondNode]); - expect(firstSnapshot[0]).toBe(node); - expect(node.observations).toBe(observations); - const finalizedObservations = [...observations]; - expect(accumulator.nodes()).not.toBe(firstSnapshot); - expect(accumulator.nodes()).toEqual(firstSnapshot); - for (const [index, observation] of finalizedObservations.entries()) - expect(node.observations[index]).toBe(observation); - - const later = nodeInput("later"); - expect(accumulator.addNode(later)).toBe(node); - // Updating a duplicate after final sorting must use its new array index. - expect(accumulator.addNode(firstInput)).toBe(node); - expect(accumulator.nodes()).toEqual([ - createJavaScriptApplicationNode({ - ...firstInput, - observations: [...inputs, later].flatMap((input) => input.observations), - }), - secondNode, - ]); - expect(node.observations).toBe(observations); -}); - -it("preserves first-input duplicates and retains the last occurrence when later merged", () => { - const accumulator = new JavaScriptArtifactGraphAccumulator(); - const first = nodeInput("same"); - const distinct = nodeInput("distinct"); - const node = accumulator.addNode({ + const first = nodeInput("雪😀"); + const held = accumulator.addNode({ ...first, observations: [...first.observations, ...first.observations], }); - const observations = node.observations; - const lastInitial = observations[1]; - expect(accumulator.nodes()[0]?.observations).toHaveLength(2); - expect(accumulator.addNode(distinct)).toBe(node); - expect(accumulator.nodes()).toEqual([ - createJavaScriptApplicationNode({ - ...first, - observations: [...first.observations, ...distinct.observations], - }), - ]); - expect(node.observations).toBe(observations); - expect(observations).toContain(lastInitial); - // A repeated observation inside an incoming batch also collapses to one. + const other = accumulator.addNode(nodeInput("other", "b".repeat(64))); + const alpha = nodeInput("alpha"); accumulator.addNode({ - ...distinct, - observations: [...distinct.observations, ...distinct.observations], + ...alpha, + observations: [...alpha.observations, ...alpha.observations], }); - expect(accumulator.nodes()[0]?.observations).toHaveLength(2); + accumulator.addNode(nodeInput("beta")); + + const labels = () => held.observations.map(({ label }) => label).sort(); + const finalized = accumulator.nodes(); + expect(labels()).toEqual(["alpha", "beta", "雪😀"]); + expect(finalized.map(({ node_id }) => node_id).toSorted()).toEqual( + [held.node_id, other.node_id].toSorted(), + ); + const identifiers = held.observations.map( + ({ observation_id }) => observation_id, + ); + expect(identifiers).toEqual(identifiers.toSorted()); + + accumulator.addNode(nodeInput("later")); + accumulator.addNode(first); + expect(labels()).toEqual(["alpha", "beta", "later", "雪😀"]); + const published = accumulator.nodes(); + expect( + published + .find(({ node_id }) => node_id === held.node_id) + ?.observations.map(({ label }) => label) + .sort(), + ).toEqual(["alpha", "beta", "later", "雪😀"]); + expect( + published + .find(({ node_id }) => node_id === other.node_id) + ?.observations.map(({ label }) => label), + ).toEqual(["other"]); }); diff --git a/src/application/javascript/JavaScriptSemanticGraphAssignments.test.ts b/src/application/javascript/JavaScriptSemanticGraphAssignments.test.ts index 98d07b8941..88bf0fd8c1 100644 --- a/src/application/javascript/JavaScriptSemanticGraphAssignments.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphAssignments.test.ts @@ -186,48 +186,36 @@ it.each([ }, ); -it("keeps assignments in computed methods' parameter defaults in the method", () => { +it("keeps assignments with the callable that evaluates them inside computed methods", () => { const graph = graphForJavaScript(` - const accel = 0.001; let speed = 6; - const obj = { ["tick"](value = (speed = accel)) { return speed; } }; + const accel = 0.001; let parameterSpeed = 6; let callbackSpeed = 6; let callback; + const withDefault = { ["tick"](value = (parameterSpeed = accel)) { return parameterSpeed; } }; + const withKeyCallback = { [(() => { callbackSpeed = accel; return "tick"; })()]() {} }; + const withDeferredCallback = { [(callback = () => accel)]() {} }; `); const nodes = new Map(graph.nodes.map((node) => [node.node_id, node])); - const assignment = graph.relations.find( - (edge) => edge.properties.assignment_operator === "=", - ); - expect(assignment).toBeDefined(); - const source = nodes.get(assignment!.source_node_id); - const target = nodes.get(assignment!.target_node_id); - expect(source?.function_node_id).toBeTypeOf("string"); - expect(source?.function_node_id).toBe(target?.function_node_id); -}); - -it("does not treat a callback body nested in a computed key as a key-time assignment input", () => { - const graph = graphForJavaScript(` - const accel = 0.001; let callback; - const obj = { [(callback = () => accel)]() {} }; - `); - expect( - graph.relations.filter( - (edge) => edge.properties.assignment_operator !== undefined, - ), - ).toEqual([]); -}); - -it("retains an assignment owned by a callback inside a computed key", () => { - const graph = graphForJavaScript(` - const accel = 0.001; let speed = 6; - const obj = { [(() => { speed = accel; return "tick"; })()]() {} }; - `); - const nodes = new Map(graph.nodes.map((node) => [node.node_id, node])); - const assignment = graph.relations.find( - (edge) => edge.properties.assignment_operator === "=", - ); - expect(assignment).toBeDefined(); - const source = nodes.get(assignment!.source_node_id); - const target = nodes.get(assignment!.target_node_id); - expect(source?.label).toBe("accel"); - expect(target?.label).toBe("speed"); - expect(source?.function_node_id).toBeTypeOf("string"); - expect(source?.function_node_id).toBe(target?.function_node_id); + const assignments = graph.relations + .filter(({ properties }) => properties.assignment_operator === "=") + .map(({ source_node_id, target_node_id }) => ({ + source: nodes.get(source_node_id), + target: nodes.get(target_node_id), + })); + expect(assignments.map(({ target }) => target?.label).sort()).toEqual([ + "callbackSpeed", + "parameterSpeed", + ]); + for (const { source, target } of assignments) { + expect(source?.label).toBe("accel"); + const owner = source?.function_node_id; + if (owner == null) throw new Error("Expected an assignment owner"); + expect(target?.function_node_id).toBe(owner); + expect(nodes.get(owner)).toMatchObject({ + kind: "function", + identity: { + source_range: { + start: { line: target?.label === "parameterSpeed" ? 3 : 4 }, + }, + }, + }); + } }); diff --git a/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts b/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts index 2dda35da5d..e8ece31a3f 100644 --- a/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphBuilder.test.ts @@ -13,11 +13,6 @@ import { createJavaScriptSemanticGraphProjection, projectJavaScriptSemanticFileSteps, } from "./JavaScriptSemanticGraphBuilder.js"; -import { - addSemanticGraphNode, - constructSemanticGraphNode, - createSemanticGraphProjectionState, -} from "./JavaScriptSemanticGraphConstruction.js"; import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js"; import { queryJavaScriptSemanticGraph } from "../../domain/javascript/javascriptSemanticQuery.js"; import { @@ -25,7 +20,6 @@ import { analyzeParsedJavaScriptSemantics, } from "../../domain/javascript/javascriptSemanticAnalysis.js"; import { parseJavaScriptSource } from "../../domain/javascript/javascriptSourceParser.js"; -import { semanticCoverageResourceLimits } from "../../domain/javascript/javascriptSemanticCoverage.js"; import { getterExpansionSource } from "../../../tests/fixtures/javascriptGetterExpansion.js"; const SHA256 = "a".repeat(64); @@ -56,6 +50,7 @@ it("retains direct call literals and resolves literal queries to their argument" direction: "forward-influence", }), ); + expect(trace.seed_node_ids).toHaveLength(1); expect( trace.nodes.find((node) => node.node_id === trace.seed_node_ids[0])?.label, ).toBe("argument 0"); @@ -64,9 +59,7 @@ it("retains direct call literals and resolves literal queries to their argument" it("reports the primitive byte bound when a direct argument is too large", () => { const source = `rpc(${JSON.stringify("x".repeat(175_000))});`; - const ir = analyzeJavaScriptSemantics(source); - expect(ir.coverage.resourceLimits).toContain("primitive-bytes"); - const graph = graphFor(source, ir); + const graph = graphFor(source); const call = graph.nodes.find(({ kind }) => kind === "call-site"); expect(call?.properties.arguments).toBeUndefined(); expect(graph.coverage.status).not.toBe("complete"); @@ -267,16 +260,6 @@ it.each([ ); }); -it("continues to project an ordinary member access as a property read", () => { - const graph = graphFor(` - const source = { count: 1 }; - const value = source.count; - `); - expect(graph.relations).toContainEqual( - expect.objectContaining({ relation: "reads-property" }), - ); -}); - it("keeps a directly returned or passed property in backward provenance", () => { const source = ` const settings = { index: "products" }; @@ -305,6 +288,9 @@ it("keeps a directly returned or passed property in backward provenance", () => const concatenated = added(); `; const graph = graphFor(source); + expect(graph.relations).toContainEqual( + expect.objectContaining({ relation: "reads-property" }), + ); const provenance = (binding: string, candidates: boolean) => { const node = graph.nodes.find( (item) => item.kind === "binding" && item.label === binding, @@ -324,99 +310,83 @@ it("keeps a directly returned or passed property in backward provenance", () => }; for (const binding of ["a", "b", "direct", "c", "d", "e", "g", "local"]) { expect(provenance(binding, false), binding).toBe(true); - expect(provenance(binding, true), `${binding} candidates`).toBe(true); } expect(provenance("concatenated", false)).toBe(false); expect(provenance("concatenated", true)).toBe(false); }); -it("keeps dynamic calls explicit and returns complete deterministic results", () => { - const graph = graphFor(` - const handlers = { ready() { return 1; } }; - const key = process.argv[2]; - handlers[key](); - `); - expect(graph.unknowns.some(({ reason }) => reason === "dynamic-call")).toBe( - true, +it("retains the unresolved call frontier at the computed call site", () => { + const graph = graphFor( + [ + "const handlers = { ready() { return 1; } };", + "const key = process.argv[2];", + "handlers[key]();", + ].join("\n"), ); - const seedNodeId = graph.relations.find( - ({ relation }) => relation === "defines", - )?.source_node_id; - if (seedNodeId === undefined) throw new Error("Expected a definition seed"); - - const first = queryJavaScriptSemanticGraph( - graph, - javaScriptSemanticQueryInputSchema.parse({ - seed: { kind: "semantic-node", node_id: seedNodeId }, - direction: "forward-influence", - include_ambiguous_dynamic_edges: true, - }), - ); - const repeated = queryJavaScriptSemanticGraph( - graph, - javaScriptSemanticQueryInputSchema.parse({ - seed: { kind: "semantic-node", node_id: seedNodeId }, - direction: "forward-influence", - include_ambiguous_dynamic_edges: true, - }), - ); - expect(repeated).toEqual(first); -}); - -it("preserves primitive candidate budget unknowns in semantic graph coverage", () => { - const source = `const answer = ${primitiveCandidateExpansionExpression()};`; - const ir = analyzeJavaScriptSemantics(source); - const graph = graphFor(source, ir); - - expect(ir.coverage).toMatchObject({ - status: "partial", - resourceLimits: ["primitive-candidates"], - }); + const module = graph.nodes.find(({ kind }) => kind === "module"); + if (module === undefined) throw new Error("Expected the containing module"); expect( - graph.unknowns.filter(({ reason }) => reason === "resource-limit"), - ).toEqual([ - expect.objectContaining({ - family: "data-flow", - reason: "resource-limit", - detail: expect.stringMatching(/primitive candidate budget exceeded/i), - }), + graph.unknowns.filter(({ reason }) => reason === "dynamic-call"), + ).toMatchObject([ + { + node_id: module.node_id, + family: "call-flow", + relation_kinds: ["calls"], + candidate_node_ids: [], + evidence: { + location: { + available: true, + value: { + kind: "source-range", + source: "app.js", + start: { line: 3, column: 0 }, + }, + }, + }, + }, ]); }); it("preserves derived string-byte limit reasons in semantic graph unknowns", () => { - const source = primitiveByteExpansionSource(); - const ir = analyzeJavaScriptSemantics(source); - const graph = graphFor(source, ir); - - expect(semanticCoverageResourceLimits(ir.coverage)).toContain( - "primitive-bytes", + const graph = graphFor(primitiveByteExpansionSource()); + const answer = graph.nodes.find( + ({ kind, label }) => kind === "binding" && label === "answer", ); + if (answer === undefined) + throw new Error("Expected the derived value binding"); expect(graph.unknowns).toContainEqual( expect.objectContaining({ + node_id: answer.node_id, + family: "data-flow", + relation_kinds: ["defines"], reason: "resource-limit", detail: expect.stringMatching(/primitive string-byte budget exceeded/i), }), ); }); -it("retains resource-limit reasons at nested object property slots", () => { +it("retains primitive candidate exhaustion at the nested property slot", () => { const source = `const answer = { nested: { value: ${primitiveCandidateExpansionExpression()} } };`; - const ir = analyzeJavaScriptSemantics(source); - const graph = graphFor(source, ir); - - expect(semanticCoverageResourceLimits(ir.coverage)).toEqual([ - "primitive-candidates", - ]); - expect(graph.unknowns).toContainEqual( - expect.objectContaining({ + const graph = graphFor(source); + const slot = graph.nodes.find( + ({ kind, properties }) => + kind === "property-slot" && + properties.property_pointer === "/nested/value", + ); + if (slot === undefined) throw new Error("Expected the nested property slot"); + expect( + graph.unknowns.filter(({ reason }) => reason === "resource-limit"), + ).toMatchObject([ + { + node_id: slot.node_id, family: "data-flow", relation_kinds: ["defines"], reason: "resource-limit", detail: expect.stringMatching( - /Unknown value at property:\/nested\/value/u, + /primitive candidate budget exceeded.*Unknown value at property:\/nested\/value/iu, ), - }), - ); + }, + ]); }); it("preserves deep expression resource reasons through graph projection", () => { @@ -432,12 +402,8 @@ it("preserves deep expression resource reasons through graph projection", () => for (let index = 0; index < 5_000; index += 1) expression = t.unaryExpression("!", expression, true); declarator.init = expression; - const ir = analyzeParsedJavaScriptSemantics(parsed); - const graph = graphFor(source, ir); + const graph = graphFor(source, analyzeParsedJavaScriptSemantics(parsed)); - expect(semanticCoverageResourceLimits(ir.coverage)).toEqual([ - "expression-depth", - ]); expect( graph.unknowns.filter(({ reason }) => reason === "resource-limit"), ).toEqual([ @@ -769,31 +735,6 @@ it("keeps uncertain parser results as candidates and ignores shadowed coercions" ).toBe(false); }); -const semanticNodeFor = (roleKey: string) => { - const file: JavaScriptArtifactFile = { - path: "budget.js", - container_sha256: SHA256, - sha256: SHA256, - bytes: 0, - inventory_artifact_id: `art_${SHA256}`, - kind: "javascript", - unpacked: false, - text: { included: true, value: "" }, - }; - const state = createSemanticGraphProjectionState(); - return constructSemanticGraphNode( - file, - { - kind: "expression", - roleKey, - location: null, - label: roleKey, - functionNodeId: null, - }, - state, - ); -}; - it("preserves getter expansion limits in semantic graph coverage", () => { const source = getterExpansionSource(); const graph = graphFor(source, analyzeJavaScriptSemantics(source)); @@ -1126,53 +1067,34 @@ it("attributes an adopted file to the ceiling that bounded its offer", () => { expect(limitation).not.toContain("max_semantic_nodes is 10"); }); -it("leaves a small application complete and untruncated", () => { - const graph = graphFor("const answer = 40 + 2;"); - expect(graph.coverage.truncated).toBe(false); - expect(graph.coverage.omitted_nodes).toBe(0); - expect(graph.coverage.omitted_relations).toBe(0); - expect(graph.coverage.limits).toEqual([]); -}); - -it("does not mark a file truncated when it exactly fills its budget", () => { - // Each unbound reference projects one expression, plus the module root. - const graph = graphFor( - "external;\n".repeat( - JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodesPerFile - 1, - ), - ); - expect(graph.nodes).toHaveLength( - JAVASCRIPT_ANALYSIS_RESOURCE_DEFAULTS.semanticNodesPerFile, - ); - expect(graph.coverage).toMatchObject({ +it("reports truncation only when a file exceeds its exact node budget", () => { + const sources = [{ path: "exact.js", text: "first;\nsecond;\nthird;" }]; + const exact = finishSources({ totalNodes: 10, fileNodes: 4 }, sources); + expect(exact.nodes).toHaveLength(4); + expect(exact.coverage).toMatchObject({ + status: "unknown", truncated: false, omitted_nodes: 0, omitted_relations: 0, limits: [], }); -}); - -it("only records node loss when the budget rejects a new identity", () => { - // The truncation signal must record blocked node creation, not a zero - // remaining budget: a file using exactly its share drops nothing. - const state = createSemanticGraphProjectionState(); - state.fileNodeBudget = 2; - state.fileNodesDropped = false; - const first = addSemanticGraphNode(state, semanticNodeFor("first")); - const second = addSemanticGraphNode(state, semanticNodeFor("second")); - expect(first).not.toBeNull(); - expect(second).not.toBeNull(); - expect(state.fileNodeBudget).toBe(0); - expect(state.fileNodesDropped).toBe(false); - - expect(addSemanticGraphNode(state, semanticNodeFor("first"))).toEqual(first); - expect(state.nodes.size).toBe(2); - expect(state.fileNodeBudget).toBe(0); - expect(state.fileNodesDropped).toBe(false); - - const blocked = addSemanticGraphNode(state, semanticNodeFor("third")); - expect(blocked).toBeNull(); - expect(state.fileNodesDropped).toBe(true); + const exceeded = finishSources({ totalNodes: 10, fileNodes: 3 }, sources); + const dropped = exact.nodes.filter( + ({ identity }) => identity.source_range?.start.line === 3, + ); + expect(dropped).toHaveLength(1); + expect(exceeded.nodes.map(({ node_id }) => node_id)).toEqual( + exact.nodes + .filter((node) => node !== dropped[0]) + .map(({ node_id }) => node_id), + ); + expect(exceeded.coverage).toMatchObject({ + status: "partial", + truncated: true, + }); + expect(exceeded.limitations.join("\n")).toContain( + "max_semantic_nodes_per_file is 3", + ); }); it("keeps full static property identities distinct through backward provenance", () => { diff --git a/src/application/javascript/JavaScriptSemanticGraphConstruction.ts b/src/application/javascript/JavaScriptSemanticGraphConstruction.ts index f0979c3858..bcd8af9d1f 100644 --- a/src/application/javascript/JavaScriptSemanticGraphConstruction.ts +++ b/src/application/javascript/JavaScriptSemanticGraphConstruction.ts @@ -196,7 +196,7 @@ export const retainSemanticGraphNode = ( }; /** Retain one canonical semantic node. */ -export const addSemanticGraphNode = ( +const addSemanticGraphNode = ( state: SemanticGraphProjectionState, node: JavaScriptSemanticGraphNode, knownLookupKey?: string, diff --git a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts index a793b686ee..7368c7f7ac 100644 --- a/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphDestructuring.test.ts @@ -1,8 +1,8 @@ import { expect, it } from "vitest"; import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js"; -it.each( - [ +it("links declared and assigned destructuring bindings to their exact source slots", () => { + const cases = [ { pattern: "{ a: selected }", source: "{ a: 1 }", path: ["a"] }, { pattern: "[selected]", source: "[1]", path: ["0"] }, { @@ -16,33 +16,41 @@ it.each( path: ["a", "b"], }, { pattern: "[[selected] = []]", source: "[[1]]", path: ["0", "0"] }, - ].flatMap((pattern) => - [false, true].map((assignment) => ({ ...pattern, assignment })), - ), -)( - "retains source-property flow for $pattern, assignment=$assignment", - ({ pattern, source, path, assignment }) => { - const graph = graphForJavaScript(` - const source = ${source}; - ${assignment ? `let selected; (${pattern} = source);` : `const ${pattern} = source;`} - `); - const target = graph.nodes.find( - (node) => node.kind === "binding" && node.label === "selected", - ); - expect(target).toBeDefined(); - const relations = graph.relations.filter( - (relation) => - relation.relation === "destructures" && - relation.target_node_id === target?.node_id, - ); - expect(relations).toHaveLength(1); - expect(relations[0]?.resolution).toBe("resolved"); - const slot = graph.nodes.find( - (node) => node.node_id === relations[0]?.source_node_id, - ); - expect(slot?.properties.property_path).toEqual(path); - }, -); + ]; + const graph = graphForJavaScript( + cases + .map(({ pattern, source }, index) => + [ + `const source${index} = ${source};`, + `const ${pattern.replace("selected", `declared${index}`)} = source${index};`, + `let assigned${index};`, + `(${pattern.replace("selected", `assigned${index}`)} = source${index});`, + ].join("\n"), + ) + .join("\n"), + ); + const nodes = new Map(graph.nodes.map((node) => [node.node_id, node])); + expect( + graph.relations + .filter(({ relation }) => relation === "destructures") + .map(({ source_node_id, target_node_id, resolution }) => ({ + target: nodes.get(target_node_id)?.label, + path: nodes.get(source_node_id)?.properties.property_path, + resolution, + })) + .sort((left, right) => + String(left.target).localeCompare(String(right.target)), + ), + ).toEqual( + ["assigned", "declared"].flatMap((form) => + cases.map(({ path }, index) => ({ + target: `${form}${index}`, + path, + resolution: "resolved", + })), + ), + ); +}); it.each(["{}", "unknownSource"])( "keeps nested default source flow conservative for %s", diff --git a/src/application/javascript/JavaScriptSemanticGraphElectronProjection.test.ts b/src/application/javascript/JavaScriptSemanticGraphElectronProjection.test.ts index b3c6d40eaf..fb14290564 100644 --- a/src/application/javascript/JavaScriptSemanticGraphElectronProjection.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphElectronProjection.test.ts @@ -2,7 +2,10 @@ import { expect, it } from "vitest"; import { analyzeJavaScriptStaticSource } from "../../../tests/fixtures/javascriptStaticAnalysis.js"; import { analyzeJavaScriptSemantics } from "../../domain/javascript/javascriptSemanticAnalysis.js"; import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js"; -import { projectJavaScriptSemanticFileSteps } from "./JavaScriptSemanticGraphBuilder.js"; +import { + createJavaScriptSemanticGraphProjection, + projectJavaScriptSemanticFileSteps, +} from "./JavaScriptSemanticGraphBuilder.js"; const source = `${Array.from({ length: 40 }, (_, i) => `function library${String(i)}(){return ${String(i)}}`).join("\n")} const e=require("electron"); @@ -20,53 +23,53 @@ const file: JavaScriptArtifactFile = { }; const facts = analyzeJavaScriptStaticSource(source); const ir = analyzeJavaScriptSemantics(source); -const project = (budget: number) => { - const steps = projectJavaScriptSemanticFileSteps( - file, - ir, - { nodes: budget, unknowns: budget }, - facts.electron.call_paths, - ); - let result = steps.next(); - while (!result.done) result = steps.next(); - return result.value; -}; +it("retains Electron boundary evidence until the node budget cuts the path", () => { + for (const budget of [3, 20]) { + const projection = createJavaScriptSemanticGraphProjection({ + totalNodes: budget, + fileNodes: budget, + }); + const steps = projectJavaScriptSemanticFileSteps( + file, + ir, + projection.remainingFileCapacity(), + facts.electron.call_paths, + ); + let result = steps.next(); + while (!result.done) result = steps.next(); + projection.adoptFileProjection(result.value); + const graph = projection.finish(file.container_sha256, { + graph_id: `jag_${"c".repeat(64)}`, + nodes: [], + }); -it("retains Electron calls before library definitions consume the node budget", () => { - const result = project(20); - expect(result.nodes).toHaveLength(20); - expect(result.nodesTruncated).toBe(true); - const terminal = result.nodes.find( - (node) => node.label === "ipc-event.sender.copy", - ); - expect(terminal).toBeDefined(); - expect( - result.relations.some( - (r) => - r.target_node_id === terminal?.node_id && r.resolution === "candidate", - ), - ).toBe(true); - expect( - result.nodes.some( - (node) => - node.properties.entry === "test:copy" && - Array.isArray(node.properties.conditions) && - node.properties.conditions.length === 1, - ), - ).toBe(true); -}); - -it("reports truncation and keeps referential integrity even within a boundary path", () => { - const result = project(3); - expect(result.nodes).toHaveLength(3); - expect(result.nodesTruncated).toBe(true); - expect( - result.nodes.some((node) => node.label === "ipc-event.sender.copy"), - ).toBe(false); - const retained = new Set(result.nodes.map((node) => node.node_id)); - expect( - result.relations.every( - (r) => retained.has(r.source_node_id) && retained.has(r.target_node_id), - ), - ).toBe(true); + expect(graph.nodes).toHaveLength(budget); + expect(graph.coverage).toMatchObject({ + status: "partial", + truncated: true, + }); + const terminal = graph.nodes.find( + ({ label }) => label === "ipc-event.sender.copy", + ); + if (budget === 3) { + expect(terminal).toBeUndefined(); + continue; + } + if (terminal === undefined) + throw new Error("Expected the Electron terminal"); + expect(graph.relations).toContainEqual( + expect.objectContaining({ + target_node_id: terminal.node_id, + resolution: "candidate", + }), + ); + expect(graph.nodes).toContainEqual( + expect.objectContaining({ + properties: expect.objectContaining({ + entry: "test:copy", + conditions: [expect.anything()], + }), + }), + ); + } }); diff --git a/src/application/javascript/JavaScriptSemanticGraphExpressionInfluence.test.ts b/src/application/javascript/JavaScriptSemanticGraphExpressionInfluence.test.ts index 5b57d71daf..5bcdf851ba 100644 --- a/src/application/javascript/JavaScriptSemanticGraphExpressionInfluence.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphExpressionInfluence.test.ts @@ -201,23 +201,6 @@ it("keeps sequence side effects unknown instead of attributing earlier values di ).toBe(true); }); -it("seeds a direct argument at its occurrence and reaches the call", () => { - const graph = graphForJavaScript('consume("only-direct");'); - const result = queryJavaScriptSemanticGraph( - graph, - javaScriptSemanticQueryInputSchema.parse({ - seed: { kind: "literal", value: "only-direct" }, - direction: "forward-influence", - }), - ); - expect(result.summary.total_seed_matches).toBe(1); - expect( - result.nodes.find((node) => node.node_id === result.seed_node_ids[0]) - ?.label, - ).toBe("argument 0"); - expect(result.nodes.some((node) => node.kind === "call-site")).toBe(true); -}); - it("keeps older call-site literal evidence searchable when argument literals are absent", () => { const graph = graphForJavaScript('consume("only-direct");'); const legacy = { diff --git a/src/application/javascript/JavaScriptSemanticGraphResources.test.ts b/src/application/javascript/JavaScriptSemanticGraphResources.test.ts index 43a713b627..c643f58684 100644 --- a/src/application/javascript/JavaScriptSemanticGraphResources.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphResources.test.ts @@ -9,25 +9,21 @@ import { resolveJavaScriptSemanticEvidence } from "../../../tests/support/javasc const SHA256 = "a".repeat(64); const GRAPH_ID = `jag_${"b".repeat(64)}`; -it("projects literal events and exact timer cancellation", () => { +it("keeps literal event links and dynamic event uncertainty", () => { const graph = graphFor(` function run(bus, dynamicName) { - const handle = setInterval(tick, 10); bus.on("ready", handler); bus.once(dynamicName, handler); bus.emit("ready"); bus.removeListener("ready", handler); - clearInterval(handle); } `); const relations = new Set(graph.relations.map(({ relation }) => relation)); for (const expected of [ - "cancels-timer", "dispatches-candidate", "registers-listener", "removes-listener", - "schedules-timer", ] as const) expect(relations.has(expected)).toBe(true); expect( @@ -45,9 +41,6 @@ it("projects literal events and exact timer cancellation", () => { expect( graph.coverage.families.find(({ family }) => family === "event"), ).toMatchObject({ status: "partial" }); - expect( - graph.coverage.families.find(({ family }) => family === "timer"), - ).toMatchObject({ status: "partial" }); }); it("projects child-process creation, I/O, listeners, and signals", () => { @@ -65,32 +58,41 @@ it("projects child-process creation, I/O, listeners, and signals", () => { } `); - const relations = new Set(graph.relations.map(({ relation }) => relation)); - for (const expected of [ - "connects-stdio", - "forwards-signal", - "listens-error", - "listens-exit", - "spawns", - "supplies-argv", - "supplies-env", - ] as const) - expect(relations.has(expected)).toBe(true); + const child = graph.nodes.find(({ kind }) => kind === "child-process"); + if (child === undefined) throw new Error("Expected the spawned child"); + expect(child.properties).toMatchObject({ + method: "spawn", + command: null, + argv_count: 2, + environment_supplied: true, + stdio_mode: "pipe", + }); expect( - graph.nodes.find( - ({ kind, properties }) => - kind === "child-process" && - properties.method === "spawn" && - properties.command === null, - ), - ).toBeDefined(); + graph.relations + .filter(({ target_node_id }) => target_node_id === child.node_id) + .map(({ relation }) => relation), + ).toEqual( + expect.arrayContaining([ + "forwards-signal", + "spawns", + "supplies-argv", + "supplies-env", + ]), + ); expect( - graph.unknowns.some( - ({ family, detail }) => - family === "child-process" && - detail.includes("command remains unresolved"), - ), - ).toBe(true); + graph.relations + .filter(({ source_node_id }) => source_node_id === child.node_id) + .map(({ relation }) => relation), + ).toEqual( + expect.arrayContaining(["connects-stdio", "listens-error", "listens-exit"]), + ); + expect(graph.unknowns).toContainEqual( + expect.objectContaining({ + node_id: child.node_id, + family: "child-process", + detail: expect.stringContaining("command remains unresolved"), + }), + ); expect( graph.coverage.families.find(({ family }) => family === "child-process"), ).toMatchObject({ status: "partial" }); @@ -231,18 +233,48 @@ it("projects built-in resource acquisition and release", () => { ).toMatchObject({ status: "partial" }); }); -it.each(["+= 1", "++"])( - "projects both property dependencies for %s", - (operator) => { - const graph = graphFor(` - const source = { count: 1 }; - source.count ${operator}; - `); - const relations = graph.relations.map(({ relation }) => relation); - expect(relations).toContain("reads-property"); - expect(relations).toContain("writes-property"); - }, -); +it("links the read and write of each update to the same property slot", () => { + const graph = graphFor(` + const source = { count: 1, total: 2 }; + source.count += 1; + source.total++; + `); + for (const path of ["count", "total"]) { + const slot = graph.nodes.find( + ({ kind, properties }) => + kind === "property-slot" && properties.property_pointer === `/${path}`, + ); + if (slot === undefined) throw new Error(`Expected the ${path} slot`); + const updates = graph.nodes.filter( + ({ properties }) => + Array.isArray(properties.property_path) && + properties.property_path[0] === path, + ); + const read = updates.find( + ({ properties }) => properties.operation_kind === "read", + ); + const write = updates.find( + ({ properties }) => properties.operation_kind === "write", + ); + if (read === undefined || write === undefined) + throw new Error(`Expected both operations for ${path}`); + expect(graph.relations).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + relation: "reads-property", + source_node_id: slot.node_id, + target_node_id: read.node_id, + }), + expect.objectContaining({ + relation: "writes-property", + source_node_id: write.node_id, + target_node_id: slot.node_id, + }), + ]), + ); + } +}); + it("does not project a literal property slot for dynamic destructuring", () => { const graph = graphFor(` const source = { token: "TOKEN" }; diff --git a/src/application/javascript/JavaScriptSemanticGraphScope.test.ts b/src/application/javascript/JavaScriptSemanticGraphScope.test.ts index 5fc14c68fc..a4e4c9159a 100644 --- a/src/application/javascript/JavaScriptSemanticGraphScope.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphScope.test.ts @@ -1,105 +1,48 @@ import { expect, it } from "vitest"; -import { graphForJavaScript as graphFor } from "../../../tests/fixtures/javascriptSemanticGraph.js"; -import { analyzeJavaScriptSemantics } from "../../domain/javascript/javascriptSemanticAnalysis.js"; -import { topLevelBinding } from "../../domain/javascript/javascriptSemanticAnalysis.fixture.js"; +import { graphForJavaScript } from "../../../tests/fixtures/javascriptSemanticGraph.js"; -const dynamicScopeUnknowns = (source: string) => - graphFor(source).unknowns.filter(({ reason }) => reason === "dynamic-scope"); - -it("exposes direct eval and with as dynamic-scope unknowns in the public graph", () => { - const unknowns = dynamicScopeUnknowns(` - function inspect(host, source) { - with (host) { consume(target); } - eval(source); - } - `); +it("records dynamic scope only at with and unshadowed direct eval", () => { + const graph = graphForJavaScript( + [ + "function inspect(host, source) {", + " with (host) { consume(target); }", + " eval(source);", + "}", + "function shadowed(eval, source) { eval(source); }", + "function optional(source) { eval?.(source); }", + "function indirect(source) { (0, eval)(source); }", + ].join("\n"), + ); + const inspect = graph.nodes.find( + ({ label, kind }) => kind === "function" && label === "inspect", + ); + if (inspect === undefined) + throw new Error("Expected the containing function"); + const unknowns = graph.unknowns.filter( + ({ reason }) => reason === "dynamic-scope", + ); expect(unknowns).toHaveLength(2); - expect(unknowns.map(({ detail }) => detail).join("\n")).toEqual( - expect.stringContaining("with"), - ); - expect(unknowns.map(({ detail }) => detail).join("\n")).toEqual( - expect.stringContaining("eval"), - ); -}); - -it("does not mark shadowed, optional, or indirect eval as dynamic scope", () => { - const unknowns = dynamicScopeUnknowns(` - function shadowed(eval, source) { eval(source); } - function optional(source) { eval?.(source); } - function indirect(source) { (0, eval)(source); } - `); - expect(unknowns).toEqual([]); -}); - -it("keeps switch discriminants outside the shared case lexical scope", () => { - const ir = analyzeJavaScriptSemantics(` - const selected = choose(); - switch (selected) { - case 1: let selected = 1; let local = 1; break; - case 2: local = 2; break; - } - `); - const selectedBinding = topLevelBinding(ir, "selected"); - const localBindings = ir.bindings.filter(({ name }) => name === "local"); - expect(ir.references.find(({ name }) => name === "selected")?.bindingId).toBe( - selectedBinding.bindingId, - ); - expect(localBindings).toHaveLength(1); - expect(localBindings[0]?.scopeId).not.toBe(selectedBinding?.scopeId); - expect( - ir.references.filter( - ({ name, role }) => name === "selected" && role === "read", - ), - ).toHaveLength(1); - expect( - ir.references.filter( - ({ name, role }) => name === "local" && role === "write", - ), - ).toHaveLength(1); -}); - -it("leaves with-object resolution intact and marks only body name resolution unknown", () => { - const ir = analyzeJavaScriptSemantics(` - const host = getHost(); - const outer = 3; - function inspect() { - with (host) { - const local = 1; - consume(local, outer); - } - } - `); - const host = topLevelBinding(ir, "host"); - const hostRead = ir.references.find( - ({ name, role }) => name === "host" && role === "read", - ); - const outerRead = ir.references.find( - ({ name, role }) => name === "outer" && role === "read", - ); - const localRead = ir.references.find( - ({ name, role }) => name === "local" && role === "read", - ); - expect(hostRead?.bindingId).toBe(host.bindingId); - expect(hostRead?.resolution).toBe("resolved"); - expect(outerRead?.resolution).toBe("unknown"); - expect(localRead?.resolution).toBe("resolved"); - expect(outerRead?.bindingId).toBeNull(); -}); - -it("keeps var declarations in each static block from leaking to another block", () => { - const ir = analyzeJavaScriptSemantics(` - class Example { - static { var first = 1; } - static { var second = 2; consume(first); } - } - `); - const first = ir.bindings.filter(({ name }) => name === "first"); - const second = ir.bindings.filter(({ name }) => name === "second"); - expect(ir.references.find(({ name }) => name === "first")?.resolution).toBe( - "unbound", - ); - expect(first).toHaveLength(1); - expect(second).toHaveLength(1); - expect(first[0]?.scopeId).not.toBe(second[0]?.scopeId); + for (const [statement, line] of [ + ["with", 2], + ["eval", 3], + ] as const) { + expect(unknowns).toContainEqual( + expect.objectContaining({ + node_id: inspect.node_id, + family: "data-flow", + relation_kinds: ["reads", "writes"], + detail: expect.stringContaining(`\`${statement}\``), + evidence: expect.objectContaining({ + location: expect.objectContaining({ + available: true, + value: expect.objectContaining({ + kind: "source-range", + start: { line, column: 2 }, + }), + }), + }), + }), + ); + } }); diff --git a/src/application/javascript/JavaScriptSemanticGraphSeedQueries.test.ts b/src/application/javascript/JavaScriptSemanticGraphSeedQueries.test.ts index 5699a607f8..437095204b 100644 --- a/src/application/javascript/JavaScriptSemanticGraphSeedQueries.test.ts +++ b/src/application/javascript/JavaScriptSemanticGraphSeedQueries.test.ts @@ -64,26 +64,3 @@ it("keeps resolved property seeds on their slot without duplicating access occur graph.nodes.find(({ node_id }) => node_id === trace.seed_node_ids[0])?.kind, ).toBe("property-slot"); }); - -it("queries an empty event name as an exact retained event identity", () => { - const graph = graphForJavaScript( - 'const { EventEmitter } = require("node:events"); const bus = new EventEmitter(); bus.on("", function emptyListener() {}); bus.on("data", function dataListener() {});', - ); - const trace = queryJavaScriptSemanticGraph( - graph, - javaScriptSemanticQueryInputSchema.parse({ - seed: { kind: "event", name: "" }, - direction: "forward-influence", - }), - ); - expect(trace.seed_node_ids.length).toBeGreaterThan(0); - const seeds = trace.nodes.filter(({ node_id }) => - trace.seed_node_ids.includes(node_id), - ); - expect(seeds.map(({ node_id }) => node_id).sort()).toEqual( - [...trace.seed_node_ids].sort(), - ); - expect(seeds.every(({ properties }) => properties.event_name === "")).toBe( - true, - ); -}); diff --git a/src/domain/javascript/javascriptApplicationGraph.test.ts b/src/domain/javascript/javascriptApplicationGraph.test.ts index 13590248e5..9ed171cb0f 100644 --- a/src/domain/javascript/javascriptApplicationGraph.test.ts +++ b/src/domain/javascript/javascriptApplicationGraph.test.ts @@ -6,10 +6,8 @@ import { createJavaScriptApplicationGraph, createJavaScriptApplicationNode, mergeJavaScriptApplicationNodes, - javascriptApplicationGraphSchema, parseJavaScriptApplicationGraph, serializeJavaScriptApplicationGraph, - type JavaScriptApplicationGraph, } from "./javascriptApplicationGraph.js"; import { APPLICATION_GRAPH_DIGESTS, @@ -35,6 +33,10 @@ 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 merged = mergeJavaScriptApplicationNodes([ first, { ...second, identity: reversedIdentity }, @@ -77,13 +79,19 @@ it.each(["kind", "identity"] as const)( }, ); -const createLargeNativeExportGraph = (): JavaScriptApplicationGraph => { - const properties = Object.fromEntries( - Array.from({ length: 1_001 }, (_, index) => [ - `${"export-property-"}${"k".repeat(120)}-${index}`, - "v".repeat(4_097), - ]), - ); +it("preserves deep and wide observation properties in a large native-export graph", () => { + let nested: unknown = "leaf"; + for (let index = 0; index < 12; index += 1) + nested = { [`level-${index}`]: nested }; + const largeKey = `export-property-${"k".repeat(120)}`; + const properties = { + ...Object.fromEntries( + Array.from({ length: 1_000 }, (_, index) => [`symbol-${index}`, index]), + ), + [largeKey]: "v".repeat(4_097), + nested, + }; + const longLabel = "large export graph label ".repeat(300); const nodes = Array.from({ length: 1_001 }, (_, index) => { const path = `native/exports/${index}.symbol`; return createJavaScriptApplicationNode({ @@ -96,7 +104,7 @@ const createLargeNativeExportGraph = (): JavaScriptApplicationGraph => { }, observations: [ { - label: index === 0 ? "large export graph label ".repeat(300) : path, + label: index === 0 ? longLabel : path, properties: index === 0 ? properties : {}, evidence: artifactEvidence( APPLICATION_GRAPH_DIGESTS.nativeAddon, @@ -107,69 +115,27 @@ const createLargeNativeExportGraph = (): JavaScriptApplicationGraph => { ], }); }); - const first = nodes[0]; - if (first === undefined) throw new TypeError("Missing native export"); - - return createJavaScriptApplicationGraph({ + const first = firstOf(nodes, "native export"); + const graph = createJavaScriptApplicationGraph({ schema: "JavaScriptApplicationGraph", root_node_ids: [first.node_id], nodes, edges: [], - coverage: { - status: "complete", - truncated: false, - omitted_count: 0, - limits: [], - }, + coverage: completeCoverage, limitations: [], }); -}; + const loaded = parseJavaScriptApplicationGraph( + JSON.parse(serializeJavaScriptApplicationGraph(graph)), + ); -it("accepts deeply nested canonical application properties", () => { - let nested: unknown = "leaf"; - for (let index = 0; index < 12; index += 1) - nested = { [`level-${String(index)}`]: nested }; - const node = createJavaScriptApplicationNode({ - kind: "native-export", - identity: { - strategy: "canonical-path", - stability: "artifact-version", - artifact_sha256: APPLICATION_GRAPH_DIGESTS.nativeAddon, - path: "native/deep-export", - }, - observations: [ - { - label: "deep export", - properties: { nested }, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.nativeAddon, - "native/deep-export", - "native-analysis-provider", - ), - }, - ], - }); - - const graph = createJavaScriptApplicationGraph({ - schema: "JavaScriptApplicationGraph", - root_node_ids: [node.node_id], - nodes: [node], - edges: [], - coverage: { - status: "complete", - truncated: false, - omitted_count: 0, - limits: [], - }, - limitations: [], - }); - - expect(graph.nodes[0]?.observations[0]?.properties).toEqual({ nested }); - expect( - parseJavaScriptApplicationGraph( - JSON.parse(serializeJavaScriptApplicationGraph(graph)), - ), - ).toEqual(graph); + expect(loaded.nodes.map(({ node_id }) => node_id).toSorted()).toEqual( + nodes.map(({ node_id }) => node_id).toSorted(), + ); + const observation = loaded.nodes.find( + ({ node_id }) => node_id === first.node_id, + )?.observations[0]; + expect(observation?.label).toBe(longLabel); + expect(observation?.properties).toEqual(properties); }); describe("JavaScript Application Graph: vocabulary", () => { @@ -193,20 +159,6 @@ describe("JavaScript Application Graph: vocabulary", () => { }), ).toEqual(graph); }); - - it("keeps large native-export graphs and their complete properties", () => { - const graph = createLargeNativeExportGraph(); - const largeExport = graph.nodes.find(({ observations }) => - observations.some(({ label }) => - label?.startsWith("large export graph label "), - ), - ); - - expect(graph.nodes).toHaveLength(1_001); - expect( - Object.keys(largeExport?.observations[0]?.properties ?? {}), - ).toHaveLength(1_001); - }); }); describe("JavaScript Application Graph: evidence authority", () => { @@ -247,80 +199,51 @@ describe("JavaScript Application Graph: evidence authority", () => { expect(preload.node_id).not.toBe(runtime.node_id); }); - it("retains explicit unknown observations without using them as identity proof", () => { - const node = createJavaScriptApplicationNode({ - kind: "javascript-module", - identity: { - strategy: "content-digest", - stability: "global-exact", - sha256: APPLICATION_GRAPH_DIGESTS.asar, - }, - observations: [ - { - label: "observed module", - properties: {}, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/module.js", - ), - }, - { - label: null, - properties: { question: "runtime correspondence" }, - evidence: unknownEvidence(), - }, - ], - }); - - const graph = graphForNode(node); - expect( - graph.nodes[0]?.observations.find( - ({ evidence }) => evidence.state === "unknown", - )?.evidence, - ).toMatchObject({ authority: "unknown", confidence: "unknown" }); - }); - - it("separates stable entity identity from changing observations", () => { + it("retains unknown observations but requires observed proof for an observation-scoped identity", () => { const identity = { - strategy: "canonical-path" as const, - stability: "artifact-version" as const, - artifact_sha256: APPLICATION_GRAPH_DIGESTS.asar, - path: "dist/chunk.js", + strategy: "observation-fingerprint", + stability: "observation-only", + observation_sha256: "8".repeat(64), + scope: "synthetic static inventory", }; - const first = createJavaScriptApplicationNode({ - kind: "javascript-chunk", + const unknown = { + label: null, + properties: { question: "runtime correspondence" }, + evidence: unknownEvidence(), + }; + const observed = { + label: "unclassified entity", + properties: {}, + evidence: artifactEvidence( + APPLICATION_GRAPH_DIGESTS.asar, + "dist/module.js", + ), + }; + const node = createJavaScriptApplicationNode({ + kind: "unknown", identity, - observations: [ - { - label: "first label", - properties: { modules: 10 }, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/chunk.js", - "ast-static-analysis", - ), - }, - ], - }); - const second = createJavaScriptApplicationNode({ - kind: "javascript-chunk", - identity, - observations: [ - { - label: "renamed chunk", - properties: { modules: 11 }, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/chunk.js", - "ast-static-analysis", - ), - }, - ], + observations: [observed, unknown], }); - expect(first.node_id).toBe(second.node_id); - expect(first.observations[0]?.observation_id).not.toBe( - second.observations[0]?.observation_id, + expect(graphForNode(node).nodes[0]?.observations).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + properties: { question: "runtime correspondence" }, + evidence: expect.objectContaining({ + state: "unknown", + authority: "unknown", + confidence: "unknown", + }), + }), + ]), + ); + const unsupported = createJavaScriptApplicationNode({ + kind: "unknown", + identity, + observations: [unknown], + }); + expect(() => graphForNode(unsupported)).toThrow( + /identity is not supported/u, ); }); }); @@ -444,7 +367,7 @@ describe("JavaScript Application Graph: identity and evidence", () => { }); describe("JavaScript Application Graph: source maps and observations", () => { - it("admits source-map and observation-scoped identities only with supporting observations", () => { + it("requires source-map observations to retain the declaration and map location", () => { const sourceModule = createJavaScriptApplicationNode({ kind: "source-module", identity: { @@ -472,28 +395,7 @@ describe("JavaScript Application Graph: source maps and observations", () => { }, ], }); - const observationScoped = createJavaScriptApplicationNode({ - kind: "unknown", - identity: { - strategy: "observation-fingerprint", - stability: "observation-only", - observation_sha256: "8".repeat(64), - scope: "synthetic static inventory", - }, - observations: [ - { - label: "unclassified entity", - properties: {}, - evidence: artifactEvidence( - APPLICATION_GRAPH_DIGESTS.asar, - "dist/app.js", - ), - }, - ], - }); - expect(graphForNode(sourceModule).nodes).toEqual([sourceModule]); - expect(graphForNode(observationScoped).nodes).toEqual([observationScoped]); const sourceObservation = firstOf( sourceModule.observations, "source observation", @@ -604,9 +506,6 @@ describe("JavaScript Application Graph: source-map digest consistency", () => { expect(() => graphForNode(sourceMapNode(null, null, true))).toThrow( /Source-map observation/u, ); - expect(() => - graphForNode(sourceMapNode("3".repeat(64), "3".repeat(64), undefined)), - ).not.toThrow(); }); }); @@ -643,19 +542,8 @@ describe("JavaScript Application Graph: commitments and schemas", () => { expect(() => parseJavaScriptApplicationGraph(staleEdge)).toThrow( /Edge identifier/u, ); - }); - - it("creates exactly the graph the public schema accepts", () => { - const graph = buildSyntheticJavaScriptApplicationGraph(); - expect(JSON.stringify(graph)).toBe( - JSON.stringify( - javascriptApplicationGraphSchema.parse(structuredClone(graph)), - ), - ); - - const { graph_id: _graphId, ...semantic } = structuredClone(graph); - firstOf(semantic.edges, "edge").edge_id = `jag_edge_${"f".repeat(64)}`; - expect(() => createJavaScriptApplicationGraph(semantic)).toThrow( + const { graph_id: _graphId, ...staleInput } = staleEdge; + expect(() => createJavaScriptApplicationGraph(staleInput)).toThrow( /Edge identifier/u, ); }); @@ -978,7 +866,7 @@ describe("JavaScript Application Graph: identity strategy requirements", () => { }); describe("JavaScript Application Graph: truncation claims", () => { - it("rejects false truncation claims and accepts large properties", () => { + it("rejects false truncation and incomplete-coverage claims", () => { const truncated = artifactEvidence( APPLICATION_GRAPH_DIGESTS.asar, "dist/module.js", @@ -1023,15 +911,5 @@ describe("JavaScript Application Graph: truncation claims", () => { limitations: [], }), ).toThrow(/Non-complete graph coverage requires an explicit limitation/u); - - const properties = Object.fromEntries( - Array.from({ length: 65 }, (_, index) => [`key-${String(index)}`, index]), - ); - expect( - contentNode( - artifactEvidence(APPLICATION_GRAPH_DIGESTS.asar, "dist/module.js"), - properties, - ).observations[0]?.properties, - ).toEqual(properties); }); }); diff --git a/src/domain/javascript/javascriptSemanticAnalysis.structure.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.structure.test.ts index c412ac9413..be81770e3c 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.structure.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.structure.test.ts @@ -765,19 +765,32 @@ describe("JavaScript semantic analysis: write references", () => { }); describe("JavaScript semantic analysis: dynamic and case scopes", () => { - it("marks with and eval as unresolved dynamic scope instead of staying silent", () => { + it("resolves with receivers and local bindings while leaving outer reads unknown", () => { const ir = analyzeJavaScriptSemantics(` - function load(source) { - with (host) { consume(target); } - eval("var injected = 1;"); + const host = getHost(); + const outer = 3; + function inspect() { + with (host) { + const local = 1; + consume(local, outer); + } } `); - const reasons = ir.frontiers - .filter(({ kind }) => kind === "dynamic-scope") - .map(({ reason }) => reason); - expect(reasons).toHaveLength(2); - expect(reasons.join("\n")).toContain("`with`"); - expect(reasons.join("\n")).toContain("`eval`"); + const host = topLevelBinding(ir, "host"); + const hostRead = ir.references.find( + ({ name, role }) => name === "host" && role === "read", + ); + const outerRead = ir.references.find( + ({ name, role }) => name === "outer" && role === "read", + ); + const localRead = ir.references.find( + ({ name, role }) => name === "local" && role === "read", + ); + expect(hostRead?.bindingId).toBe(host.bindingId); + expect(hostRead?.resolution).toBe("resolved"); + expect(outerRead?.resolution).toBe("unknown"); + expect(localRead?.resolution).toBe("resolved"); + expect(outerRead?.bindingId).toBeNull(); }); it("does not assign CommonJS exports to lexically shadowed module or exports", () => { @@ -795,30 +808,53 @@ describe("JavaScript semantic analysis: dynamic and case scopes", () => { ).toEqual([]); }); - it("shares one lexical scope across switch cases and isolates static blocks", () => { - const switchIr = analyzeJavaScriptSemantics(` - const kind = select(); - switch (kind) { - case "a": let shared = 1; break; - case "b": shared = 2; break; + it("resolves a switch discriminant outside the case scope and writes across cases", () => { + const ir = analyzeJavaScriptSemantics(` + const selected = choose(); + switch (selected) { + case 1: let selected = 1; let shared = 1; break; + case 2: shared = 2; break; } `); - // One declaration, one binding — and it belongs to a scope of its own, so - // it cannot leak past the switch statement. - const shared = switchIr.bindings.filter(({ name }) => name === "shared"); + const outer = topLevelBinding(ir, "selected"); + const shared = bindingsNamed(ir, "shared"); expect(shared).toHaveLength(1); - expect(shared[0]?.scopeId).not.toBe( - topLevelBinding(switchIr, "kind").scopeId, + expect(shared[0]?.scopeId).not.toBe(outer.scopeId); + expect( + ir.references.filter(({ name }) => name === "selected"), + ).toMatchObject([ + { role: "read", resolution: "resolved", bindingId: outer.bindingId }, + ]); + expect(ir.references.filter(({ name }) => name === "shared")).toMatchObject( + [ + { + role: "write", + resolution: "resolved", + bindingId: shared[0]?.bindingId, + }, + ], ); - const classIr = analyzeJavaScriptSemantics(` + }); + + it("isolates let and var bindings between static blocks", () => { + const ir = analyzeJavaScriptSemantics(` class Holder { - static { let isolated = 1; } - static { let isolated = 2; } + static { let isolated = 1; var first = 1; } + static { let isolated = 2; var second = 2; consume(first); } } `); - expect( - classIr.bindings.filter(({ name }) => name === "isolated"), - ).toHaveLength(2); + const lexical = bindingsNamed(ir, "isolated"); + const first = bindingsNamed(ir, "first"); + const second = bindingsNamed(ir, "second"); + expect(lexical).toHaveLength(2); + expect(lexical[0]?.scopeId).not.toBe(lexical[1]?.scopeId); + expect(first).toHaveLength(1); + expect(second).toHaveLength(1); + expect(first[0]?.scopeId).not.toBe(second[0]?.scopeId); + expect(ir.references.find(({ name }) => name === "first")).toMatchObject({ + resolution: "unbound", + bindingId: null, + }); }); it("keeps function, class, and method identities separate from bindings", () => { diff --git a/src/domain/javascript/javascriptSemanticGraphQuery.test.ts b/src/domain/javascript/javascriptSemanticGraphQuery.test.ts index 3f1b9c8ec7..63db351866 100644 --- a/src/domain/javascript/javascriptSemanticGraphQuery.test.ts +++ b/src/domain/javascript/javascriptSemanticGraphQuery.test.ts @@ -11,7 +11,6 @@ import { z } from "zod"; import { digestCanonicalValue } from "../canonicalDigest.js"; import type { JsonValue } from "../jsonValue.js"; -import type { ApplicationGraphEvidence } from "./javascriptApplicationEvidenceSchemas.js"; import { JAVASCRIPT_SEMANTIC_RELATION_FAMILIES, JAVASCRIPT_SEMANTIC_RELATION_FAMILY, @@ -38,7 +37,7 @@ import { queryJavaScriptSemanticGraph } from "./javascriptSemanticQuery.js"; const SHA = "a".repeat(64); -it.each(["unknown", "unavailable", "partial"] as const)( +it.each(["unknown", "partial"] as const)( "rejects contradictory truncated %s graph coverage", (status) => { const input = semanticFixtureInput(); @@ -63,24 +62,6 @@ it.each(["unknown", "unavailable", "partial"] as const)( }, ); -it("accepts truncated partial coverage with unknown omission extents", () => { - const input = semanticFixtureInput(); - expect( - createJavaScriptSemanticGraph({ - ...input, - coverage: { - ...input.coverage, - status: "partial", - truncated: true, - omitted_nodes: null, - omitted_relations: null, - limits: [{ name: "nodes", value: 1, unit: "items" }], - }, - limitations: ["Node ceiling left semantic omission extents unknown."], - }).coverage, - ).toMatchObject({ status: "partial", truncated: true }); -}); - it("accepts omitted unknown occurrences as the reason for semantic truncation", () => { const input = semanticFixtureInput(); const family = input.coverage.families[0]!.family; @@ -106,6 +87,9 @@ it("accepts omitted unknown occurrences as the reason for semantic truncation", truncated: true, omitted_nodes: 0, omitted_relations: 0, + omitted_unknown_occurrences: [ + { family, reason: "resource-limit", occurrences: 1 }, + ], }); }); const node = ( @@ -475,17 +459,6 @@ it("counts candidate ownership cycles once and traverses them finitely", () => { ).toBe("found"); }); -it("defines every required relation family and canonicalizes records", () => { - expect(JAVASCRIPT_SEMANTIC_RELATIONS).toContain("argument-to-parameter"); - expect(JAVASCRIPT_SEMANTIC_RELATIONS).toContain("detaches-task"); - expect(JAVASCRIPT_SEMANTIC_RELATIONS).toContain("forwards-signal"); - expect(JAVASCRIPT_SEMANTIC_RELATIONS).toContain("validates"); - const graph = semanticFixtureGraph(); - expect(graph.nodes.map(({ node_id }) => node_id)).toEqual( - graph.nodes.map(({ node_id }) => node_id).toSorted(), - ); -}); - it("rejects stale identities, dangling endpoints, and incomplete coverage claims", () => { const graph = semanticFixtureGraph(); expect(() => @@ -545,29 +518,7 @@ it("rejects stale identities, dangling endpoints, and incomplete coverage claims it("rejects complete family coverage with an unlisted retained frontier", () => { const input = semanticFixtureInput(true); - input.coverage = { - ...input.coverage, - status: "complete", - truncated: false, - omitted_nodes: 0, - omitted_relations: 0, - families: input.coverage.families.map((family) => - family.family === "call-flow" - ? { - ...family, - status: "complete", - omitted_relations: 0, - unknown_ids: [], - } - : family, - ), - }; - input.limitations = []; - expect(() => createJavaScriptSemanticGraph(input)).toThrow( - /Complete family coverage cannot omit or retain unknown facts/u, - ); - - const valid = createJavaScriptSemanticGraph(semanticFixtureInput(true)); + const valid = createJavaScriptSemanticGraph(input); const coverage = { ...valid.coverage, status: "complete" as const, @@ -594,45 +545,26 @@ it("rejects complete family coverage with an unlisted retained frontier", () => "JavaScript semantic graph", )}`, }; - const parsed = javaScriptSemanticGraphSchema.safeParse(invalid); - expect(parsed.success).toBe(false); + const diagnostic = + "Complete family coverage cannot omit or retain unknown facts"; + expect(() => createJavaScriptSemanticGraph(changed)).toThrow(diagnostic); + const parsed = javaScriptSemanticGraphSchema.safeParse( + JSON.parse(JSON.stringify(invalid)), + ); if (parsed.success) throw new Error("Expected inconsistent graph rejection"); expect(parsed.error.issues.map(({ message }) => message)).toContain( - "Complete family coverage cannot omit or retain unknown facts", + diagnostic, ); - expect( - javaScriptSemanticGraphSchema.safeParse(JSON.parse(JSON.stringify(invalid))) - .success, - ).toBe(false); }); it("validates evidence context ownership and references", () => { const graph = semanticFixtureGraph(); - const alteredContext = { - ...graph, - evidence_contexts: graph.evidence_contexts.map((context, index) => - index === 0 - ? { ...context, context_id: `jsrg_evidence_${"f".repeat(64)}` } - : context, - ), - }; - const alteredIssues = javaScriptSemanticGraphSchema.safeParse(alteredContext); - expect(alteredIssues.success).toBe(false); - if (alteredIssues.success) throw new Error("Expected invalid context digest"); - expect(alteredIssues.error.issues.map(({ message }) => message)).toContain( - "Evidence context identifier is stale", - ); - expect(alteredIssues.error.issues.map(({ message }) => message)).toContain( - "Evidence reference names an absent context", - ); - const changedPayload = javaScriptSemanticGraphSchema.safeParse({ ...graph, evidence_contexts: graph.evidence_contexts.map((context, index) => index === 0 ? { ...context, authority: "user-assertion" } : context, ), }); - expect(changedPayload.success).toBe(false); if (changedPayload.success) throw new Error("Expected changed context payload rejection"); expect(changedPayload.error.issues.map(({ message }) => message)).toContain( @@ -654,7 +586,6 @@ it("validates evidence context ownership and references", () => { (left, right) => left.context_id.localeCompare(right.context_id), ), }); - expect(orphanIssues.success).toBe(false); if (orphanIssues.success) throw new Error("Expected orphan context rejection"); expect(orphanIssues.error.issues.map(({ message }) => message)).toContain( @@ -683,7 +614,6 @@ it("validates evidence context ownership and references", () => { ...facts.slice(1), ], }); - expect(dangling.success, fact).toBe(false); if (dangling.success) throw new Error(`Expected dangling ${fact} reference`); expect( @@ -711,7 +641,6 @@ it("validates evidence context ownership and references", () => { ...graph.nodes.slice(1), ], }); - expect(impossibleEvidence.success).toBe(false); if (impossibleEvidence.success) throw new Error("Expected incompatible evidence context and location"); expect( @@ -739,10 +668,47 @@ it("shares provenance contexts while preserving fact-specific locations and unkn }, evidenceContexts, ); - expect(second.evidence.context_id).toBe(first.evidence.context_id); - expect(second.evidence.location).not.toEqual(first.evidence.location); + const { graph_id: _graphId, ...semantic } = graph; + const combined = createJavaScriptSemanticGraph({ + ...semantic, + unknowns: [...graph.unknowns, second], + evidence_contexts: evidenceContexts.contexts, + coverage: { + ...graph.coverage, + families: graph.coverage.families.map((family) => + family.unknown_ids.includes(first.unknown_id) + ? { + ...family, + unknown_ids: [...family.unknown_ids, second.unknown_id], + } + : family, + ), + }, + }); + const stored = javaScriptSemanticGraphSchema.parse( + JSON.parse(JSON.stringify(combined)), + ); + expect(stored.evidence_contexts).toEqual(graph.evidence_contexts); expect(second.unknown_id).not.toBe(first.unknown_id); - expect(evidenceContexts.contexts).toEqual(graph.evidence_contexts); + expect(stored.unknowns).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + unknown_id: first.unknown_id, + evidence: first.evidence, + }), + expect.objectContaining({ + unknown_id: second.unknown_id, + evidence: { + context_id: first.evidence.context_id, + location: { + available: false, + reason: "unresolved", + detail: "Another source location remains unresolved.", + }, + }, + }), + ]), + ); }); const complete = (steps: Iterator): Value => { @@ -772,12 +738,13 @@ it("preserves a transferred graph independent of the caller retained copy", () = input.coverage.families.length = 0; const graph = complete(steps); expect(graph).toEqual(expected); + const nodeIds = graph.nodes.map(({ node_id }) => node_id); + expect(nodeIds).toEqual(nodeIds.toSorted()); expect( javaScriptSemanticGraphSchema.parse(JSON.parse(JSON.stringify(graph))), ).toEqual(expected); expect(isValidatedImmutableJavaScriptSemanticGraph(graph)).toBe(true); expect(isValidatedImmutableJavaScriptSemanticGraph(expected)).toBe(false); - expect(Object.isFrozen(nested)).toBe(false); }); const invalidGraphCases: { @@ -880,22 +847,17 @@ it.each(invalidGraphCases)( }, ); -it("interns each distinct evidence context once under its content digest", () => { +it("interns valid normalized evidence after rejecting malformed provenance", () => { const registry = new JavaScriptSemanticEvidenceContextRegistry(); - const elsewhere: ApplicationGraphEvidence = { - ...semanticGraphTestEvidence(), - location: { - available: true, - value: { - kind: "source-range", - source: "other.js", - start: { line: 7, column: 2 }, - end: { line: 7, column: 9 }, - }, - }, - }; - const first = registry.intern(semanticGraphTestEvidence()); - const repeated = registry.intern(elsewhere); + const observed = semanticGraphTestEvidence(); + expect(() => + registry.intern({ + ...observed, + extractor: { ...observed.extractor, name: "" }, + }), + ).toThrow(z.ZodError); + + const first = registry.intern(observed); const reordered = registry.intern({ ...semanticGraphTestEvidence("inferred"), limitations: [ @@ -904,10 +866,8 @@ it("interns each distinct evidence context once under its content digest", () => ], }); const inferred = registry.intern(semanticGraphTestEvidence("inferred")); - const { location: _location, ...context } = semanticGraphTestEvidence(); + const { location: _location, ...context } = observed; - expect(repeated.context_id).toBe(first.context_id); - expect(repeated.location).toEqual(elsewhere.location); expect(reordered.context_id).toBe(inferred.context_id); expect(inferred.context_id).not.toBe(first.context_id); expect(first.context_id).toBe( @@ -916,19 +876,7 @@ it("interns each distinct evidence context once under its content digest", () => expect(registry.contexts.map(({ context_id }) => context_id)).toEqual( [first.context_id, inferred.context_id].toSorted(), ); - expect(registry.resolve(repeated)).toEqual(elsewhere); -}); - -it("rejects invalid provenance in direct registry.intern calls before sealing", () => { - const registry = new JavaScriptSemanticEvidenceContextRegistry(); - const baseEvidence = semanticGraphTestEvidence(); - expect(() => - registry.intern({ - ...baseEvidence, - extractor: { ...baseEvidence.extractor, name: "" }, - }), - ).toThrow(z.ZodError); - expect(registry.ownedContexts).toEqual([]); + expect(registry.resolve(first)).toEqual(observed); }); it("seals owned builder records while still rejecting tampered spread copies", () => { diff --git a/src/domain/javascript/javascriptSemanticGraphTraversal.test.ts b/src/domain/javascript/javascriptSemanticGraphTraversal.test.ts index 8302895e4e..b7f48e69be 100644 --- a/src/domain/javascript/javascriptSemanticGraphTraversal.test.ts +++ b/src/domain/javascript/javascriptSemanticGraphTraversal.test.ts @@ -115,23 +115,6 @@ const registryForGraph = ( return registry; }; -it("returns every relevant unknown inline", () => { - const graph = semanticFixtureGraph(true); - const callable = graph.nodes.find(({ kind }) => kind === "function"); - if (callable === undefined) throw new Error("Expected callable node"); - const result = queryJavaScriptSemanticGraph( - graph, - javaScriptSemanticQueryInputSchema.parse({ - seed: { kind: "semantic-node", node_id: callable.node_id }, - direction: "forward-influence", - }), - ); - expect(result).toMatchObject({ - summary: { relevant_unknowns: 1 }, - }); - expect(result.unknowns).toHaveLength(1); -}); - it("does not admit a semantic-node seed absent from the graph", () => { const result = queryJavaScriptSemanticGraph( semanticFixtureGraph(), @@ -149,102 +132,67 @@ it("does not admit a semantic-node seed absent from the graph", () => { }); }); -it("returns the complete deterministic forward influence result inline", () => { - const graph = semanticFixtureGraph(); - const input = { - seed: { kind: "literal" as const, value: "TOKEN" }, - direction: "forward-influence" as const, - expected: { role: "sink" as const, classes: ["request" as const] }, - }; - const first = queryJavaScriptSemanticGraph( - graph, - javaScriptSemanticQueryInputSchema.parse(input), - ); - expect(first).toMatchObject({ - status: "found", - summary: { traversed_nodes: 4, traversed_relations: 3 }, - relations: expect.any(Array), - }); - expect(first.expected_match_node_ids).toHaveLength(1); - expect(first.relations).toHaveLength(3); -}); - -it("returns only the referenced evidence contexts with resolvable trace facts", () => { - const graph = semanticFixtureGraph(); - const result = queryJavaScriptSemanticGraph( - graph, - javaScriptSemanticQueryInputSchema.parse({ - seed: { kind: "literal", value: "TOKEN" }, - direction: "forward-influence", - }), - ); - const expectedContextIds = [ - ...new Set([ - ...result.nodes.map(({ evidence }) => evidence.context_id), - ...result.relations.map(({ evidence }) => evidence.context_id), - ...result.unknowns.map(({ evidence }) => evidence.context_id), - ]), - ].sort(); - expect(result.evidence_contexts.map(({ context_id }) => context_id)).toEqual( - expectedContextIds, - ); - for (const node of result.nodes) - expect(resolveJavaScriptSemanticEvidence(result, node.evidence)).toEqual( - resolveJavaScriptSemanticEvidence(graph, node.evidence), - ); -}); - -it("traverses a path of 70 relations", () => { +it("returns reachable influence facts and only their evidence contexts", () => { const original = semanticFixtureGraph(); - const input = constructionInput(original); const evidenceContexts = registryForGraph(original); - const request = original.nodes.find(({ kind }) => kind === "request"); - if (request === undefined) throw new TypeError("Missing request node"); - const chain = Array.from({ length: 70 }, (_, index) => - node("binding", `long-chain-${index}`, evidenceContexts), - ); - const chainRelations = chain.map((target, index) => - createJavaScriptSemanticGraphRelation( - { - source_node_id: chain[index - 1]?.node_id ?? request.node_id, - target_node_id: target.node_id, - relation: "reads", - resolution: "resolved", - properties: {}, - evidence: semanticGraphTestEvidence("inferred"), + const moduleNode = original.nodes.find(({ kind }) => kind === "module"); + if (moduleNode === undefined) + throw new Error("Expected the unreachable module"); + const { node_id: _moduleId, evidence, ...moduleInput } = moduleNode; + const moduleEvidence = resolveJavaScriptSemanticEvidence(original, evidence); + const unrelated = createJavaScriptSemanticGraphNode( + { + ...moduleInput, + evidence: { + ...moduleEvidence, + extractor: { ...moduleEvidence.extractor, name: "unreachable-module" }, }, - evidenceContexts, - ), + }, + evidenceContexts, ); const graph = createJavaScriptSemanticGraph({ - ...input, + ...constructionInput(original), + nodes: original.nodes.map((entry) => + entry.node_id === moduleNode.node_id ? unrelated : entry, + ), evidence_contexts: evidenceContexts.contexts, - nodes: [...input.nodes, ...chain], - relations: [...input.relations, ...chainRelations], - coverage: { - ...original.coverage, - families: original.coverage.families.map((family) => - family.family === "data-flow" - ? { - ...family, - retained_relations: - family.retained_relations + chainRelations.length, - } - : family, - ), - }, }); - const result = queryJavaScriptSemanticGraph( graph, javaScriptSemanticQueryInputSchema.parse({ seed: { kind: "literal", value: "TOKEN" }, direction: "forward-influence", + expected: { role: "sink", classes: ["request"] }, }), ); - expect(result.summary.traversed_nodes).toBe(74); - expect(result.nodes).toHaveLength(74); + expect(result).toMatchObject({ + status: "found", + summary: { traversed_nodes: 4, traversed_relations: 3 }, + }); + expect(result.nodes.map(({ kind }) => kind).sort()).toEqual([ + "binding", + "function", + "literal", + "request", + ]); + expect(result.relations.map(({ relation }) => relation).sort()).toEqual([ + "captures", + "constructs-request", + "defines", + ]); + expect(result.expected_match_node_ids).toEqual([ + original.nodes.find(({ kind }) => kind === "request")?.node_id, + ]); + expect(result.evidence_contexts).toEqual( + graph.evidence_contexts.filter( + ({ context_id }) => context_id !== unrelated.evidence.context_id, + ), + ); + for (const fact of [...result.nodes, ...result.relations]) + expect(resolveJavaScriptSemanticEvidence(result, fact.evidence)).toEqual( + resolveJavaScriptSemanticEvidence(graph, fact.evidence), + ); }); it("accepts deeply nested semantic properties", () => { @@ -263,8 +211,9 @@ it("accepts deeply nested semantic properties", () => { }); it("keeps relevant dynamic frontiers unknown", () => { + const graph = semanticFixtureGraph(true); const result = queryJavaScriptSemanticGraph( - semanticFixtureGraph(true), + graph, javaScriptSemanticQueryInputSchema.parse({ seed: { kind: "literal", value: "TOKEN" }, direction: "forward-influence", @@ -272,12 +221,11 @@ it("keeps relevant dynamic frontiers unknown", () => { ); expect(result.status).toBe("partial"); expect(result.coverage.status).toBe("partial"); - expect(result.unknowns).toMatchObject([ - { reason: "dynamic-call", relation_kinds: ["calls"] }, - ]); + expect(result.summary.relevant_unknowns).toBe(1); + expect(result.unknowns).toEqual(graph.unknowns); }); -it("reports partial status when retained graph facts were truncated", () => { +it("reports a partial trace when the graph has unknown omission counts", () => { const graph = semanticFixtureGraph(); const partial = createJavaScriptSemanticGraph({ ...constructionInput(graph), @@ -291,6 +239,12 @@ it("reports partial status when retained graph facts were truncated", () => { }, limitations: ["The node ceiling left omission extents unknown."], }); + expect(partial.coverage).toMatchObject({ + status: "partial", + truncated: true, + omitted_nodes: null, + omitted_relations: null, + }); const result = queryJavaScriptSemanticGraph( partial, javaScriptSemanticQueryInputSchema.parse({ @@ -594,7 +548,14 @@ it("keeps candidate uncertainty at the end of a long reachable path", () => { expect(result).toMatchObject({ status: "ambiguous", coverage: { status: "partial" }, + summary: { + traversed_nodes: include ? 72 : 71, + traversed_relations: include ? 71 : 70, + }, }); - expect(result.nodes).toHaveLength(include ? 72 : 71); + const reachable = include ? chain : chain.slice(0, -1); + expect(result.nodes.map(({ node_id }) => node_id).toSorted()).toEqual( + reachable.map(({ node_id }) => node_id).toSorted(), + ); } }); diff --git a/tests/acceptance/applications/javascriptGraphQueryCoverage.test.ts b/tests/acceptance/applications/javascriptGraphQueryCoverage.test.ts index ad077dd4a9..32515f5bd1 100644 --- a/tests/acceptance/applications/javascriptGraphQueryCoverage.test.ts +++ b/tests/acceptance/applications/javascriptGraphQueryCoverage.test.ts @@ -14,7 +14,7 @@ cliTest( const root = await createTestTempDirectory("rea-graph-query-coverage-"); await writeFile( join(root, "main.cjs"), - 'const { EventEmitter } = require("node:events"); const bus = new EventEmitter(); bus.on("", function emptyListener() {}); function read() { return this.speed; }', + 'const { EventEmitter } = require("node:events"); const bus = new EventEmitter(); bus.on("", function emptyListener() {}); bus.on("data", function dataListener() {}); function read() { return this.speed; }', ); await writeFile(join(root, "other.js"), "export const answer = 42;"); const response = await cli.run({ @@ -75,6 +75,14 @@ cliTest( }), ]), ); + expect( + seedNodes + .map((node) => [node?.kind, node?.properties.event_name]) + .sort(), + ).toEqual([ + ["event", ""], + ["listener", ""], + ]); } else { expect(trace.summary.relevant_unknowns).toBeGreaterThan(0); expect(seedNodes).toContainEqual( diff --git a/tests/acceptance/applications/javascriptTimerWritesCli.test.ts b/tests/acceptance/applications/javascriptTimerWritesCli.test.ts index 113ec586a8..75c8fa3ec7 100644 --- a/tests/acceptance/applications/javascriptTimerWritesCli.test.ts +++ b/tests/acceptance/applications/javascriptTimerWritesCli.test.ts @@ -17,9 +17,9 @@ cliTest( clearTimeout(handle); } function latest() { - let handle = setTimeout(work, 20); - handle = setTimeout(work, 30); - clearTimeout(handle); + let handle = setInterval(work, 20); + handle = setInterval(work, 30); + clearInterval(handle); } function conditional() { let handle = setTimeout(work, 40); @@ -31,7 +31,6 @@ cliTest( const result = await analyzeJavaScriptDirectory("CLI", cli, root); expect(result.statistics.parse_failures).toBe(0); const graph = result.semantic_graph; - expect(graph).not.toBeNull(); if (graph === null) throw new Error("Expected a semantic graph"); const cancellationTargets = graph.relations .filter(({ relation }) => relation === "cancels-timer") @@ -39,14 +38,22 @@ cliTest( resolution, delay: graph.nodes.find(({ node_id }) => node_id === target_node_id) ?.properties.delay_milliseconds, - })); - expect(cancellationTargets).toEqual( - expect.arrayContaining([ - { resolution: "resolved", delay: 30 }, - { resolution: "candidate", delay: 40 }, - ]), - ); - expect(cancellationTargets).toHaveLength(2); + })) + .sort((left, right) => Number(left.delay) - Number(right.delay)); + expect(cancellationTargets).toEqual([ + { resolution: "resolved", delay: 30 }, + { resolution: "candidate", delay: 40 }, + ]); + const scheduled = graph.relations + .filter(({ relation }) => relation === "schedules-timer") + .map( + ({ target_node_id }) => + graph.nodes.find(({ node_id }) => node_id === target_node_id) + ?.properties.delay_milliseconds, + ); + expect(scheduled.map(Number).sort((left, right) => left - right)).toEqual([ + 10, 20, 30, 40, + ]); expect( graph.unknowns.filter(({ family }) => family === "timer"), ).toHaveLength(2); diff --git a/tests/boundary/filesystem/javascriptArtifactGraphObservationLookup.test.ts b/tests/boundary/filesystem/javascriptArtifactGraphObservationLookup.test.ts index 58d16d2454..22d05b2d89 100644 --- a/tests/boundary/filesystem/javascriptArtifactGraphObservationLookup.test.ts +++ b/tests/boundary/filesystem/javascriptArtifactGraphObservationLookup.test.ts @@ -45,9 +45,6 @@ it("uses the first structured observation when paths repeat", async () => { const sourceMapObservation = analysis.source_maps.find( ({ path }) => path === "app.js.map", ); - expect(packageObservation).toBeDefined(); - expect(jsonObservation).toBeDefined(); - expect(sourceMapObservation).toBeDefined(); if ( packageObservation === undefined || jsonObservation === undefined || @@ -126,129 +123,53 @@ it("uses the first structured observation when paths repeat", async () => { } }); -it("preserves unknown omitted-key count for unavailable JSON", async () => { - const root = await createTestTempDirectory("rea-unavailable-json-"); +it("distinguishes unavailable and unselected JSON while preserving unknown key counts", async () => { + const root = await createTestTempDirectory("rea-json-observation-coverage-"); + const unavailable = "unavailable.json"; + const unselected = ["unselected.json", "upper.JSON"]; await Promise.all([ writeFile(join(root, "package.json"), JSON.stringify({ name: "fixture" })), - writeFile(join(root, "config.json"), Buffer.from([0xff])), + writeFile(join(root, unavailable), Buffer.from([0xff])), + ...unselected.map((path) => + writeFile(join(root, path), JSON.stringify({ setting: true })), + ), ]); const snapshot = await scanCanonicalArtifactInventory(root, {}); const reader = createJavaScriptArtifactReader(root, "directory"); - try { - const files = await readJavaScriptArtifactFiles(reader, snapshot); + const files = await readJavaScriptArtifactFiles( + reader, + snapshot, + undefined, + ["package.json", unavailable], + ); const { analysis } = await analyzeArtifactFiles(files); const graph = await immutableArtifactGraph(snapshot, files, analysis); - - const configFile = graph.nodes.find(({ observations }) => - observations.some( + const observations = graph.nodes.flatMap( + ({ observations }) => observations, + ); + expect( + observations.find( ({ properties }) => - properties.path === "config.json" && properties.file_kind === "json", - ), - ); - - const configObservation = configFile?.observations.find( - ({ properties }) => - properties.path === "config.json" && properties.file_kind === "json", - ); - - expect(configObservation?.properties).toMatchObject({ - path: "config.json", + properties.path === unavailable && properties.file_kind === "json", + )?.properties, + ).toMatchObject({ text_status: "invalid-utf8", json_parse_status: "unavailable", omitted_json_top_level_keys: null, }); - } finally { - await reader.close(); - } -}); - -it("preserves unknown omitted-key count for unselected JSON", async () => { - const root = await createTestTempDirectory("rea-unselected-json-"); - - await Promise.all([ - writeFile(join(root, "package.json"), JSON.stringify({ name: "fixture" })), - writeFile(join(root, "config.json"), JSON.stringify({ setting: true })), - ]); - - const snapshot = await scanCanonicalArtifactInventory(root, {}); - const reader = createJavaScriptArtifactReader(root, "directory"); - - try { - const files = await readJavaScriptArtifactFiles( - reader, - snapshot, - undefined, - ["package.json"], - ); - - const { analysis } = await analyzeArtifactFiles(files); - const graph = await immutableArtifactGraph(snapshot, files, analysis); - - const configFile = graph.nodes.find(({ observations }) => - observations.some( - ({ properties }) => - properties.path === "config.json" && properties.file_kind === "json", - ), - ); - - const configObservation = configFile?.observations.find( - ({ properties }) => - properties.path === "config.json" && properties.file_kind === "json", - ); - - expect(configObservation?.properties).toMatchObject({ - path: "config.json", - text_status: "outside-analysis-scope", - json_parse_status: null, - omitted_json_top_level_keys: null, - }); - } finally { - await reader.close(); - } -}); - -it("preserves unknown omitted-key count for unselected uppercase JSON", async () => { - const root = await createTestTempDirectory("rea-unselected-uppercase-json-"); - - await Promise.all([ - writeFile(join(root, "package.json"), JSON.stringify({ name: "fixture" })), - writeFile(join(root, "config.JSON"), JSON.stringify({ setting: true })), - ]); - - const snapshot = await scanCanonicalArtifactInventory(root, {}); - const reader = createJavaScriptArtifactReader(root, "directory"); - - try { - const files = await readJavaScriptArtifactFiles( - reader, - snapshot, - undefined, - ["package.json"], - ); - - const { analysis } = await analyzeArtifactFiles(files); - const graph = await immutableArtifactGraph(snapshot, files, analysis); - - const configFile = graph.nodes.find(({ observations }) => - observations.some( - ({ properties }) => - properties.path === "config.JSON" && properties.file_kind === "json", - ), - ); - - const configObservation = configFile?.observations.find( - ({ properties }) => - properties.path === "config.JSON" && properties.file_kind === "json", - ); - - expect(configObservation?.properties).toMatchObject({ - path: "config.JSON", - text_status: "outside-analysis-scope", - json_parse_status: null, - omitted_json_top_level_keys: null, - }); + for (const path of unselected) + expect( + observations.find( + ({ properties }) => + properties.path === path && properties.file_kind === "json", + )?.properties, + ).toMatchObject({ + text_status: "outside-analysis-scope", + json_parse_status: null, + omitted_json_top_level_keys: null, + }); } finally { await reader.close(); }