test(il2cpp): consolidate retained metadata projection coverage (#12870)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 15:51:59 +00:00
1 parent 75908cc1fa
commit cfc1644238
3 files changed
+72 -167

No files matched your search

@@ -1,29 +1,22 @@
import { createEmptyIl2cppMetadataFixture } from "../il2cpp/il2cppMetadata.fixture.js";
import { expect, it } from "vitest";
import { z } from "zod";
import {
il2cppMetadataParentSchema,
type Il2cppMetadata,
} from "../il2cpp/il2cppMetadata.js";
import type { z } from "zod";
import type { Il2cppMetadata } from "../il2cpp/il2cppMetadata.js";
import { createEvidence } from "../evidence.js";
import { freezeJsonSnapshot } from "../immutableJson.js";
import { jsonObjectSchema } from "../jsonValue.js";
import type { AdmittedView, SummaryAnalysisView } from "./analysisViewShape.js";
import type { SummaryAnalysisView } from "./analysisViewShape.js";
import {
il2cppMetadataViewRequestSchema,
projectIl2cppMetadataView as projectAdmittedIl2cppMetadataView,
} from "./il2cppMetadataView.js";
type Il2cppMetadataViewInput =
| SummaryAnalysisView
| z.input<typeof il2cppMetadataViewRequestSchema>;
type Il2cppMetadataView = z.output<typeof il2cppMetadataViewRequestSchema>;
/** Check a caller selection against the closed request, then pass the admitted view. */
const projectIl2cppMetadataView = (
parent: Parameters<typeof projectAdmittedIl2cppMetadataView>[0],
view: Il2cppMetadataViewInput,
): ReturnType<typeof projectAdmittedIl2cppMetadataView> =>
projectAdmittedIl2cppMetadataView(parent, view satisfies AdmittedView);
view: Il2cppMetadataView | SummaryAnalysisView,
) => projectAdmittedIl2cppMetadataView(parent, view);
const report: Il2cppMetadata = {
...createEmptyIl2cppMetadataFixture(),
@@ -111,40 +104,6 @@ it("keeps collection counts exact across sealed metadata pages", () => {
}
});
it("preserves historical omission as unknown while keeping existing metadata queries usable", () => {
const historical = jsonObjectSchema.parse(report);
for (const key of [
"fields",
"properties",
"events",
"parameters",
"string_literals",
])
delete historical[key];
const source = parent(freezeJsonSnapshot(historical), true);
const summary = summarize(source);
expect(summary).toMatchObject({
summary: {
counts: { types: 2, methods: 1, fields: null, parameters: null },
},
unknowns: expect.arrayContaining([
expect.stringContaining("did not collect fields"),
]),
});
const existing = projectIl2cppMetadataView(source, page);
if (!existing.ok) throw existing.error;
expect(existing.value).toMatchObject({
items: [{ index: 0, name: "Outer" }],
});
const unavailable = projectIl2cppMetadataView(source, {
kind: "il2cpp-metadata",
collection: "parameters",
offset: 0,
limit: 1,
});
expect(unavailable.ok).toBe(false);
});
it("treats Evidence retained before binary linking as unknown, not unlinked", () => {
const historical = jsonObjectSchema.parse(report);
for (const key of [
@@ -219,54 +178,25 @@ const linkedBinary = {
types_count: 4,
};
it("selects parameter types by owning method through the parameters collection", () => {
const linked = {
...report,
parameters: [
{ name: "a", method: 0 },
{ name: "b", method: 0 },
],
binary: { ...linkedBinary },
method_addresses: report.methods.map(() => ({
state: "no-body" as const,
address: null,
rva: null,
})),
return_types: report.methods.map(() => ({ type: -1, name: null })),
field_types: report.fields.map(() => ({ type: 1, name: "int" })),
parameter_types: [
{ type: 2, name: "string" },
{ type: 3, name: "bool" },
],
};
const select = (
source: ReturnType<typeof parent>,
method_indices: number[],
) =>
projectIl2cppMetadataView(source, {
it("refuses linked parameter types when the parent omitted their owners", () => {
const historical = jsonObjectSchema.parse(linkedReport());
delete historical.parameters;
const result = projectIl2cppMetadataView(
parent(freezeJsonSnapshot(historical), true),
{
kind: "il2cpp-metadata",
collection: "parameter_types",
method_indices,
method_indices: [0],
offset: 0,
limit: 8,
});
const owned = select(parent(linked, false), [0]);
if (!owned.ok) throw owned.error;
expect(owned.value).toMatchObject({
items: [
{ index: 0, type: 2, name: "string" },
{ index: 1, type: 3, name: "bool" },
],
});
const none = select(parent(linked, false), [1]);
if (!none.ok) throw none.error;
expect(none.value).toMatchObject({ items: [] });
// Without retained parameters the owners are unknown, so the filter is refused.
const historical = jsonObjectSchema.parse(linked);
delete historical.parameters;
expect(select(parent(freezeJsonSnapshot(historical), true), [0]).ok).toBe(
false,
},
);
expect(result).toMatchObject({
ok: false,
error: {
issues: [{ path: ["source", "normalized_result", "parameter_types"] }],
},
});
});
it.each([{ method_addresses: undefined }, { binary: undefined }])(
@@ -281,10 +211,6 @@ it.each([{ method_addresses: undefined }, { binary: undefined }])(
);
it.each([
{ types: [{ ...report.types[0]!, declaring_type: 0 }, report.types[1]!] },
{ types: [{ ...report.types[0]!, declaring_type: 1 }, report.types[1]!] },
{ types: [report.types[0]!, { ...report.types[1]!, declaring_type: 999 }] },
{ methods: [{ name: "Run", declaring_type: 999 }] },
{ fields: [{ name: "bad", declaring_type: 2 }] },
{ parameters: [{ name: "bad", method: 1 }] },
{ string_literals: [{ value: null, value_base64: null }] },
@@ -468,34 +394,6 @@ const linkedReport = (): Il2cppMetadata => ({
parameter_types: [{ type: 0, name: "int" }],
});
it.each([-1, 1, 99])(
"rejects imported code-generation module image reference %i",
(image) => {
const value = linkedReport();
value.binary!.code_gen_modules[0]!.image = image;
const parsed = il2cppMetadataParentSchema.safeParse(value);
expect(parsed.success).toBe(false);
if (!parsed.success)
expect(parsed.error.issues).toEqual(
expect.arrayContaining([
expect.objectContaining({
path: ["binary", "code_gen_modules", 0, "image"],
}),
]),
);
for (const selection of [{}, { image_indices: [image] }]) {
const view = projectIl2cppMetadataView(parent(value, false), {
kind: "il2cpp-metadata",
collection: "code_gen_modules",
...selection,
offset: 0,
limit: 64,
});
expect(view.ok).toBe(false);
}
},
);
it("selects every folded method by numeric address with stable indices and both source identities", () => {
const source = parent(freezeJsonSnapshot(linkedReport()), true);
const view = projectIl2cppMetadataView(source, {
@@ -669,15 +567,11 @@ const genericLinked = (): Il2cppMetadata => {
it("pages generic method instances by definition, state, address, name and field", () => {
const source = parent(freezeJsonSnapshot(genericLinked()), true);
const view = (
filters: Omit<
Extract<
Il2cppMetadataViewInput,
{ collection: "generic_method_instances" }
>,
"kind" | "collection" | "offset" | "limit"
>,
) => {
type Filters = Omit<
Extract<Il2cppMetadataView, { collection: "generic_method_instances" }>,
"kind" | "collection" | "offset" | "limit"
>;
const view = (filters: Filters) => {
const result = projectIl2cppMetadataView(source, {
kind: "il2cpp-metadata",
collection: "generic_method_instances",
@@ -688,15 +582,7 @@ it("pages generic method instances by definition, state, address, name and field
if (!result.ok) throw result.error;
return result.value;
};
const items = (
filters: Omit<
Extract<
Il2cppMetadataViewInput,
{ collection: "generic_method_instances" }
>,
"kind" | "collection" | "offset" | "limit"
>,
) => {
const items = (filters: Filters) => {
const value = view(filters);
return "items" in value ? value.items : undefined;
};
@@ -775,7 +661,6 @@ it("keeps generic method issues out of summaries and pages them", () => {
it("retains an unreadable generic definition identity without matching a method selector", () => {
const linked = genericLinked();
linked.generic_method_instances![2]!.method = null;
expect(il2cppMetadataParentSchema.safeParse(linked).success).toBe(true);
const source = parent(freezeJsonSnapshot(linked), true);
const page = projectIl2cppMetadataView(source, {
kind: "il2cpp-metadata",
@@ -799,12 +684,6 @@ it("retains an unreadable generic definition identity without matching a method
).toEqual([]);
});
it("rejects a named generic instance whose method definition is unreadable", () => {
const linked = genericLinked();
linked.generic_method_instances![0]!.method = null;
expect(il2cppMetadataParentSchema.safeParse(linked).success).toBe(false);
});
it("keeps unrequested generic instances unknown and refuses their page", () => {
const source = parent(freezeJsonSnapshot(linkedReport()), true);
expect(summarize(source)).toMatchObject({
@@ -888,6 +767,7 @@ it("refuses generic rows that break the address or definition invariants", () =>
expect(variant({ state: "unknown", address: null, rva: null })).toBe(false);
// A named row needs a retained definition; an unnamed one may not have one,
// but is then unknown.
expect(variant({ method: null })).toBe(false);
expect(variant({ method: 9 })).toBe(false);
expect(variant({ method: 9, name: null })).toBe(false);
// A method spec issue on row 0: its row is unknown and keeps no address.
@@ -698,21 +698,6 @@ export const projectIl2cppMetadataView = (
},
]),
);
if (
view.collection === "parameter_types" &&
view.method_indices !== undefined &&
report.parameters === undefined
)
return err(
analysisViewInputError([
{
path: ["view", "method_indices"],
reason: "invalid_value",
message:
"Retained Evidence did not collect parameters, so parameter owners are unknown; inspect the metadata again to select parameter types by method.",
},
]),
);
const matches = selection(view, report);
const selectedFields =
view.fields === undefined ? undefined : new Set<string>(view.fields);
+45 -5
View File
@@ -148,7 +148,6 @@ cliTest(
original.images.length - 1,
-1,
original.images.length,
99,
]) {
const report = structuredClone(original);
report.binary!.code_gen_modules[0]!.image = image;
@@ -175,9 +174,27 @@ cliTest(
expect(result.exitCode, result.stderr).toBe(0);
expect(mcp.isError, JSON.stringify(mcp)).not.toBe(true);
} else {
const error = {
code: "invalid_request",
details: {
issues: [
{
path: [
"source",
"normalized_result",
"binary",
"code_gen_modules",
0,
"image",
],
reason: image < 0 ? "out_of_range" : "invalid_value",
},
],
},
};
expect(result.exitCode, `image ${image}`).toBe(1);
expect(result.json).toMatchObject({ code: "invalid_request" });
expect(parseMcpToolError(mcp).error.code).toBe("invalid_request");
expect(result.json).toMatchObject(error);
expect(parseMcpToolError(mcp).error).toMatchObject(error);
}
}
},
@@ -236,9 +253,31 @@ cliTest(
expect(cliResult.exitCode, cliResult.stderr).toBe(0);
expect(mcp.isError, JSON.stringify(mcp)).not.toBe(true);
} else {
const owner = {
self: ["types", 0],
cycle: ["types", expect.any(Number)],
"type-range": ["types", 1],
"method-range": ["methods", 0],
}[scenario];
const error = {
code: "invalid_request",
details: {
issues: [
{
path: [
"source",
"normalized_result",
...owner,
"declaring_type",
],
reason: "invalid_value",
},
],
},
};
expect(cliResult.exitCode, scenario).toBe(1);
expect(cliResult.json).toMatchObject({ code: "invalid_request" });
expect(parseMcpToolError(mcp).error.code).toBe("invalid_request");
expect(cliResult.json).toMatchObject(error);
expect(parseMcpToolError(mcp).error).toMatchObject(error);
}
}
},
@@ -801,6 +840,7 @@ it("imports historical metadata Evidence with unavailable member collections wit
summary: { counts: { methods: 5, fields: null, parameters: null } },
unknowns: expect.arrayContaining([
expect.stringContaining("did not collect parameters"),
expect.stringContaining("did not collect fields"),
]),
});
const types = await call("inspect_analysis_view", {