Files
reverse-engineering-anything/src/domain/analysisView/javascriptFactBucketMerge.test.ts
T
ShuaixinHuangandVincent Koc f7bfa770f3 perf(analysis-view): stream indexed JavaScript fact matches in graph order (#15267)
* perf(analysis-view): stream indexed JavaScript fact matches in graph order

* test(analysis-view): cover interleaved bucket selections

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-12-01 01:44:51 +08:00

101 lines
3.4 KiB
TypeScript

import { expect, it } from "vitest";
import { freezeOwnedJsonTreeSnapshot } from "../immutableJson.js";
import { buildSyntheticJavaScriptApplicationGraph } from "../javascript/javascriptApplicationGraph.fixture.js";
import {
javascriptFactGroupsViewRequestSchema,
javascriptFactsViewRequestSchema,
projectJavaScriptFactGroups,
projectJavaScriptFacts,
} from "./javascriptFactsView.js";
it("matches linear filtering and grouping across interleaved indexed buckets", () => {
const graph = buildSyntheticJavaScriptApplicationGraph();
const node = graph.nodes.find(({ observations }) => observations.length > 0);
const observation = node?.observations[0];
if (node === undefined || observation === undefined)
throw new Error("Expected a fixture observation");
const paths = ["first.js", "second.js", "third.js"];
const nodes = freezeOwnedJsonTreeSnapshot(
Array.from({ length: 257 }, (_, index) => ({
...node,
node_id: `jag_node_${index.toString(16).padStart(64, "0")}`,
observations: [0, 1].map((position) => ({
...observation,
evidence: {
...observation.evidence,
location: {
available: true as const,
value: {
kind: "artifact-path" as const,
path: paths[(index + position) % 3]!,
},
},
},
properties: {
operation: index % 4 === 0 ? "send" : "invoke",
channel: String(index % 5),
},
})),
})),
);
const where = {
paths: [...paths].reverse().concat(paths[0]!, "absent.js"),
kinds: [node.kind, node.kind],
properties: [{ key: "operation", values: ["send"] }],
};
const facts = javascriptFactsViewRequestSchema.parse({
kind: "javascript-facts",
where,
property_keys: ["channel"],
offset: 50,
limit: 5,
});
const indexed = projectJavaScriptFacts(nodes, facts, nodes);
expect(indexed.total).toBe(130);
expect(indexed.rows).toHaveLength(5);
expect(indexed).toEqual(projectJavaScriptFacts(nodes, facts));
const latePage = { ...facts, where: { paths }, offset: 235 };
expect(projectJavaScriptFacts(nodes, latePage, nodes)).toEqual(
projectJavaScriptFacts(nodes, latePage),
);
const groups = javascriptFactGroupsViewRequestSchema.parse({
kind: "javascript-fact-groups",
where,
group_by: ["channel"],
offset: 1,
limit: 3,
});
const indexedGroups = projectJavaScriptFactGroups(nodes, groups, nodes);
expect(indexedGroups.total).toBe(5);
expect(indexedGroups.rows).toEqual(
["1", "2", "3"].map((channel) => ({
properties: { channel },
observation_count: 26,
node_count: 13,
})),
);
expect(indexedGroups).toEqual(projectJavaScriptFactGroups(nodes, groups));
for (const selectedPaths of [paths, [paths[0]!], [], ["absent.js"]]) {
for (const offset of [0, 50, 1000]) {
const selection = {
...facts,
where: { ...where, paths: selectedPaths },
offset,
};
expect(projectJavaScriptFacts(nodes, selection, nodes)).toEqual(
projectJavaScriptFacts(nodes, selection),
);
}
}
const unmatchedKind = node.kind === "ipc-channel" ? "package" : "ipc-channel";
const noKind = javascriptFactsViewRequestSchema.parse({
...facts,
where: { ...where, kinds: [unmatchedKind] },
});
expect(projectJavaScriptFacts(nodes, noKind, nodes)).toEqual({
rows: [],
total: 0,
});
});