test(javascript): consolidate semantic graph coverage

This commit is contained in:
morluto committed 2026-11-17 21:02:58 -05:00
1 parent e2e3881c0e
commit c77d4261a1
17 files changed
+684 -1098

No files matched your search

@@ -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", () => {