test: reuse identical JavaScript analysis fixtures

This commit is contained in:
morluto
2026-10-11 17:43:28 +08:00
parent aaeadf2d4c
commit 42951bbbbe
2 changed files with 90 additions and 79 deletions
@@ -113,18 +113,20 @@ describe("JavaScript export return-shape comparison", () => {
}); });
describe("JavaScript export return-shape pairing limits", () => { describe("JavaScript export return-shape pairing limits", () => {
it.each([ it("does not pair an inline discriminant with absent or unknown presence", async () => {
{ presence: "absent", side: "left" }, const rows = [
{ presence: "unknown-coverage", side: "left" }, { presence: "absent", side: "left" },
{ presence: "absent", side: "right" }, { presence: "unknown-coverage", side: "left" },
{ presence: "unknown-coverage", side: "right" }, { presence: "absent", side: "right" },
] as const)( { presence: "unknown-coverage", side: "right" },
"does not pair an inline $side discriminant whose presence is $presence", ] as const;
async ({ presence, side }) => { const [left, right] = await analyzeSources({
const [left, right] = await analyzeSources({ left: 'export default () => ({ kind: "result", count: 1 });',
left: 'export default () => ({ kind: "result", count: 1 });', right: 'export default () => ({ kind: "result", total: 2 });',
right: 'export default () => ({ kind: "result", total: 2 });', });
});
for (const { presence, side } of rows) {
const label = `${side} discriminant presence ${presence}`;
const selected = side === "left" ? left : right; const selected = side === "left" ? left : right;
const graph = createJavaScriptApplicationGraph({ const graph = createJavaScriptApplicationGraph({
schema: "JavaScriptApplicationGraph", schema: "JavaScriptApplicationGraph",
@@ -165,26 +167,28 @@ describe("JavaScript export return-shape pairing limits", () => {
side === "left" side === "left"
? compare({ ...left, graph }, right) ? compare({ ...left, graph }, right)
: compare(left, { ...right, graph }); : compare(left, { ...right, graph });
expect(result.coverage).toMatchObject({ expect(result.coverage, label).toMatchObject({
status: "partial", status: "partial",
paired_variants: 0, paired_variants: 0,
unpaired_left_variants: 1, unpaired_left_variants: 1,
unpaired_right_variants: 1, unpaired_right_variants: 1,
}); });
expect(result.changes).toHaveLength(2); expect(result.changes, label).toHaveLength(2);
expect( expect(
result.changes.every( result.changes.every(
({ status, discriminant }) => ({ status, discriminant }) =>
status === "unknown" && discriminant === null, status === "unknown" && discriminant === null,
), ),
label,
).toBe(true); ).toBe(true);
expect( expect(
result.property_inventories.every( result.property_inventories.every(
({ paired, discriminant }) => !paired && discriminant === null, ({ paired, discriminant }) => !paired && discriminant === null,
), ),
label,
).toBe(true); ).toBe(true);
}, }
); });
it("does not pair discriminants that an unknown trailing spread can overwrite", async () => { it("does not pair discriminants that an unknown trailing spread can overwrite", async () => {
const [left, right] = await analyzeSources({ const [left, right] = await analyzeSources({
@@ -169,27 +169,13 @@ const nativeResultSchema = z.object({
code: z.string().nullable(), code: z.string().nullable(),
}); });
it.each( it.each(cases)(
(["import", "require"] as const).flatMap((moduleKind) => "matches Node import and require target selection for $name",
cases.map((testCase) => ({ ...testCase, moduleKind })), async ({ name, exports, expected, nodeError, rejectedTarget }) => {
),
)(
"matches Node $moduleKind target selection for $name",
async ({
moduleKind,
name,
exports,
expected,
nodeError,
rejectedTarget,
}) => {
const root = await createTestTempDirectory("rea-package-target-"); const root = await createTestTempDirectory("rea-package-target-");
const mainPath = moduleKind === "import" ? "main.mjs" : "main.cjs";
const files = { const files = {
[mainPath]: "main.mjs": 'import value from "fixture"; export default value;',
moduleKind === "import" "main.cjs": 'module.exports = require("fixture");',
? 'import value from "fixture"; export default value;'
: 'module.exports = require("fixture");',
"node_modules/fixture/package.json": JSON.stringify({ exports }), "node_modules/fixture/package.json": JSON.stringify({ exports }),
...Object.fromEntries( ...Object.fromEntries(
[ [
@@ -209,57 +195,78 @@ it.each(
await writeFile(join(root, path), text); await writeFile(join(root, path), text);
}), }),
); );
const runner = for (const moduleKind of ["import", "require"] as const) {
moduleKind === "import" const runner =
? 'const load = async () => (await import("fixture")).default;' moduleKind === "import"
: 'const load = async () => require("fixture");'; ? 'const load = async () => (await import("fixture")).default;'
const native = await promisify(execFile)( : 'const load = async () => require("fixture");';
process.execPath, const native = await promisify(execFile)(
[ process.execPath,
"--input-type", [
moduleKind === "import" ? "module" : "commonjs", "--input-type",
"--eval", moduleKind === "import" ? "module" : "commonjs",
`${runner} load().then(value => console.log(JSON.stringify({value,code:null})), error => console.log(JSON.stringify({value:null,code:error.code ?? error.name})));`, "--eval",
], `${runner} load().then(value => console.log(JSON.stringify({value,code:null})), error => console.log(JSON.stringify({value:null,code:error.code ?? error.name})));`,
{ cwd: root, timeout: 5_000 }, ],
); { cwd: root, timeout: 5_000 },
const oracle = nativeResultSchema.parse(JSON.parse(native.stdout));
expect(oracle.value, name).toBe(expected);
if (nodeError !== undefined)
expect(oracle.code).toBe(
moduleKind === "import" && nodeError === "MODULE_NOT_FOUND"
? "ERR_MODULE_NOT_FOUND"
: nodeError,
); );
const oracle = nativeResultSchema.parse(JSON.parse(native.stdout));
expect(oracle.value, `${name} (${moduleKind})`).toBe(expected);
if (nodeError !== undefined)
expect(oracle.code, `${name} (${moduleKind})`).toBe(
moduleKind === "import" && nodeError === "MODULE_NOT_FOUND"
? "ERR_MODULE_NOT_FOUND"
: nodeError,
);
}
const result = await reconstructJavaScriptArtifact({ const result = await reconstructJavaScriptArtifact({
input_path: root, input_path: root,
format: "directory", format: "directory",
}); });
const edge = result.graph.edges.find( for (const mainPath of ["main.mjs", "main.cjs"] as const) {
({ properties }) => const edge = result.graph.edges.find(
properties.specifier === "fixture" && ({ evidence, properties }) =>
properties.resolution_status !== undefined && properties.specifier === "fixture" &&
properties.resolution_status !== null, properties.resolution_status !== undefined &&
); properties.resolution_status !== null &&
expect(edge).toBeDefined(); evidence.location.available &&
expect(edge?.properties.resolved_path).toBe(expected); evidence.location.value.kind === "source-range" &&
if (expected !== null) { evidence.location.value.source === mainPath,
expect(edge?.properties.resolution_status).toBe("resolved");
} else {
expect(edge?.properties.resolution_status).not.toBe("resolved");
}
if (rejectedTarget !== undefined) {
expect(edge?.properties.resolution_status).toBe("rejected");
expect(edge?.evidence.limitations.join(" ")).toContain(
JSON.stringify(rejectedTarget),
); );
expect(edge?.evidence.limitations.join(" ")).toContain("exports target"); expect(edge, `${name} (${mainPath})`).toBeDefined();
expect(edge?.evidence.limitations.join(" ")).not.toContain( expect(edge?.properties.resolved_path, `${name} (${mainPath})`).toBe(
"not valid package JSON", expected,
); );
expect(edge?.evidence.location).toMatchObject({ if (expected !== null) {
value: { kind: "source-range", source: mainPath }, expect(
}); edge?.properties.resolution_status,
`${name} (${mainPath})`,
).toBe("resolved");
} else {
expect(
edge?.properties.resolution_status,
`${name} (${mainPath})`,
).not.toBe("resolved");
}
if (rejectedTarget !== undefined) {
expect(
edge?.properties.resolution_status,
`${name} (${mainPath})`,
).toBe("rejected");
expect(edge?.evidence.limitations.join(" ")).toContain(
JSON.stringify(rejectedTarget),
);
expect(edge?.evidence.limitations.join(" ")).toContain(
"exports target",
);
expect(edge?.evidence.limitations.join(" ")).not.toContain(
"not valid package JSON",
);
expect(edge?.evidence.location).toMatchObject({
value: { kind: "source-range", source: mainPath },
});
}
} }
}, },
); );