test(analysis): share item view request fixtures (#17441)

This commit is contained in:
Vincent Koc
2026-12-06 15:25:31 +08:00
committed by GitHub
parent 0cb7fef560
commit 235371230c
+66 -95
View File
@@ -83,6 +83,22 @@ const nativePage = (
limit: number,
) => ({ kind: "native", facet, offset, limit }) as const;
type LayoutItemRequest = Extract<
AnalysisViewRequest,
{ kind: "item"; collection: "sections" | "symbols" }
>;
const layoutItem = (
collection: LayoutItemRequest["collection"],
selector: LayoutItemRequest["selector"],
) => ({ kind: "item", collection, selector }) as const;
type ModuleItemRequest = Extract<
AnalysisViewRequest,
{ kind: "item"; collection: "modules" }
>;
const moduleItem = (selector: ModuleItemRequest["selector"]) =>
({ kind: "item", collection: "modules", selector }) as const;
it("returns input errors for malformed parent Evidence identifiers before projection", () => {
for (const evidenceId of [`xx_${"a".repeat(64)}`, "ev_bad"])
expect(
@@ -562,22 +578,20 @@ it("projects layout summary, facet, item, and stable pages", () => {
if (mitigations.value.kind !== "facet")
throw new Error("expected facet view");
expect(mitigations.value.facet).toEqual(layout.mitigations);
const data = projectAnalysisView(parent, {
kind: "item",
collection: "sections",
selector: { name: ".data" },
});
const data = projectAnalysisView(
parent,
layoutItem("sections", { name: ".data" }),
);
if (!data.ok) throw data.error;
if (data.value.kind !== "item") throw new Error("expected item view");
expect(data.value.item).toMatchObject({
index: 2,
name: { display: ".data" },
});
const indexed = projectAnalysisView(parent, {
kind: "item",
collection: "sections",
selector: { index: 0 },
});
const indexed = projectAnalysisView(
parent,
layoutItem("sections", { index: 0 }),
);
if (!indexed.ok) throw indexed.error;
expect(indexed.value.view_digest).not.toBe(data.value.view_digest);
expect(indexed.value.view_digest).not.toBe(parent.evidenceId.slice(3));
@@ -645,32 +659,25 @@ it.each(["sections", "symbols"] as const)(
[collection]: [opaque, valid],
},
};
const selected = projectAnalysisView(parent, {
kind: "item",
collection,
selector: { name: "�name" },
});
const selected = projectAnalysisView(
parent,
layoutItem(collection, { name: "�name" }),
);
expect(selected).toMatchObject({ ok: true, value: { item: valid } });
expect(
projectAnalysisView(parent, {
kind: "item",
collection,
selector: { name: "\ud800name" },
}).ok,
projectAnalysisView(
parent,
layoutItem(collection, { name: "\ud800name" }),
).ok,
).toBe(false);
expect(
projectAnalysisView(parent, {
kind: "item",
collection,
selector: { name: "provider-formatted-name" },
}).ok,
projectAnalysisView(
parent,
layoutItem(collection, { name: "provider-formatted-name" }),
).ok,
).toBe(false);
expect(
projectAnalysisView(parent, {
kind: "item",
collection,
selector: { index: 0 },
}),
projectAnalysisView(parent, layoutItem(collection, { index: 0 })),
).toMatchObject({ ok: true, value: { item: opaque } });
expect(parent.normalizedResult[collection]).toEqual([opaque, valid]);
},
@@ -690,11 +697,7 @@ it.each([
};
expect(isImmutableJsonSnapshot(layout[collection])).toBe(false);
const select = (name: string) =>
projectAnalysisView(parent, {
kind: "item",
collection,
selector: { name },
});
projectAnalysisView(parent, layoutItem(collection, { name }));
const first = select(name);
expect(first).toMatchObject({
@@ -712,11 +715,10 @@ it.each([
it("rejects ambiguous layout names with candidate indexes", () => {
const parent = layoutParent();
const result = projectAnalysisView(parent, {
kind: "item",
collection: "sections",
selector: { name: ".text" },
});
const result = projectAnalysisView(
parent,
layoutItem("sections", { name: ".text" }),
);
expect(result).toMatchObject({
ok: false,
error: {
@@ -791,11 +793,7 @@ it("projects JavaScript summary and preserves selected module observations", ()
});
expect(JSON.stringify(summary.value)).not.toMatch(/"graph"/);
expect(summary.value.summary).not.toHaveProperty("semantic_graph");
const item = projectAnalysisView(parent, {
kind: "item",
collection: "modules",
selector: { path: "renderer.js" },
});
const item = projectAnalysisView(parent, moduleItem({ path: "renderer.js" }));
if (!item.ok) throw item.error;
if (item.value.kind !== "item") throw new Error("expected item view");
expect(item.value.item).toMatchObject({
@@ -955,11 +953,10 @@ it("counts retained range observations including repeats and skips unavailable l
parent,
moduleReferencesPage({}, 0, 1),
);
const item = projectAnalysisView(parent, {
kind: "item",
collection: "modules",
selector: { node_id: node.node_id },
});
const item = projectAnalysisView(
parent,
moduleItem({ node_id: node.node_id }),
);
if (!references.ok) throw references.error;
if (!item.ok) throw item.error;
expect(references.value).toMatchObject({
@@ -1098,11 +1095,10 @@ it("rejects ambiguous JavaScript module paths with candidate node ids", () => {
const parent = javascriptParent(
analysisViewBindJavaScriptGraphs(analysis, graph),
);
const result = projectAnalysisView(parent, {
kind: "item",
collection: "modules",
selector: { path: "renderer.js" },
});
const result = projectAnalysisView(
parent,
moduleItem({ path: "renderer.js" }),
);
if (result.ok) throw new Error("expected ambiguous path");
expect(result.error).toMatchObject({
_tag: "AnalysisInputError",
@@ -1198,11 +1194,7 @@ it("keeps export bindings out of module pages and path selection", () => {
examined: 1,
exhausted: true,
});
const item = projectAnalysisView(parent, {
kind: "item",
collection: "modules",
selector: { path: "renderer.js" },
});
const item = projectAnalysisView(parent, moduleItem({ path: "renderer.js" }));
if (!item.ok) throw item.error;
if (item.value.kind !== "item") throw new Error("expected item view");
expect(item.value.item).toMatchObject({ node_id: original.node_id });
@@ -1313,11 +1305,7 @@ it("pages actual modules, including unknown and empty paths, without unrelated r
expect.objectContaining({ kind: "source-module", path: "" }),
]),
);
const emptyPath = projectAnalysisView(parent, {
kind: "item",
collection: "modules",
selector: { path: "" },
});
const emptyPath = projectAnalysisView(parent, moduleItem({ path: "" }));
for (const [path, kind] of [
[null, "javascript-module"],
["", "source-module"],
@@ -1336,11 +1324,7 @@ it("pages actual modules, including unknown and empty paths, without unrelated r
const role = nodes.find((node) => node.kind === "electron-renderer");
if (role === undefined) throw new Error("missing role");
expect(
projectAnalysisView(parent, {
kind: "item",
collection: "modules",
selector: { node_id: role.node_id },
}).ok,
projectAnalysisView(parent, moduleItem({ node_id: role.node_id })).ok,
).toBe(false);
});
@@ -1573,11 +1557,7 @@ it.each(["partial", "unknown"] as const)(
{ kind: "page", collection: "modules", offset: 0, limit: 10 },
moduleReferencesPage({}, 0, 10),
moduleReferencesPage({ path: "not-retained.js" }, 0, 10),
{
kind: "item",
collection: "modules",
selector: { path: "renderer.js" },
},
moduleItem({ path: "renderer.js" }),
] as const) {
const result = projectAnalysisView(parent, view);
if (!result.ok) throw result.error;
@@ -1625,11 +1605,7 @@ it("does not match unavailable layout names using a display placeholder", () =>
],
},
},
{
kind: "item",
collection: "sections",
selector: { name: "<unavailable>" },
},
layoutItem("sections", { name: "<unavailable>" }),
);
expect(result.ok).toBe(false);
});
@@ -1667,11 +1643,10 @@ it("rejects unsupported parent operations and incompatible collections", () => {
ok: false,
error: { _tag: "AnalysisInputError" },
});
const sectionsOnJs = projectAnalysisView(javascriptParent(), {
kind: "item",
collection: "sections",
selector: { name: ".text" },
});
const sectionsOnJs = projectAnalysisView(
javascriptParent(),
layoutItem("sections", { name: ".text" }),
);
expect(sectionsOnJs).toMatchObject({
ok: false,
error: { _tag: "AnalysisInputError" },
@@ -1759,11 +1734,7 @@ it("selects a unique content-deduplicated module by any retained path", () => {
analysisViewBindJavaScriptGraphs(source, graph),
);
for (const path of ["renderer.js", "renderer-alias.js"]) {
const selected = projectAnalysisView(parent, {
kind: "item",
collection: "modules",
selector: { path },
});
const selected = projectAnalysisView(parent, moduleItem({ path }));
if (!selected.ok) throw selected.error;
expect(selected.value).toMatchObject({
kind: "item",
@@ -2010,11 +1981,11 @@ it("preserves exact parity and WeakMap reuse for indexed JavaScript module and f
try {
const views: AnalysisViewRequest[] = [
{ kind: "summary" },
{ kind: "item", collection: "modules", selector: { node_id: ids.modB } },
{ kind: "item", collection: "modules", selector: { node_id: "missing" } },
{ kind: "item", collection: "modules", selector: { path: "c.js" } },
{ kind: "item", collection: "modules", selector: { path: "a.js" } },
{ kind: "item", collection: "modules", selector: { path: "none.js" } },
moduleItem({ node_id: ids.modB }),
moduleItem({ node_id: "missing" }),
moduleItem({ path: "c.js" }),
moduleItem({ path: "a.js" }),
moduleItem({ path: "none.js" }),
{ kind: "page", collection: "modules", offset: 1, limit: 2 },
moduleReferencesPage({}, 1, 2),
moduleReferencesPage({ path: "b.js" }, 0, 10),