Files
reverse-engineering-anything/tests/acceptance/arArchiveExtraction.test.ts
ShuaixinHuangandVincent Koc dd52af8c92 feat(artifacts): extract stored Unix ar members (#15248)
* feat(artifacts): extract stored Unix ar members

* test(artifacts): assert structured thin ar extraction refusal

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-11-30 23:53:45 +08:00

101 lines
3.2 KiB
TypeScript

import { readdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { arMember } from "../../src/artifacts/ar.fixture.js";
import { AR_MAGIC } from "../../src/domain/arFormat.js";
import { artifactExtractionResultSchema } from "../../src/domain/artifactGraph.js";
import { parseEvidence } from "../../src/domain/evidence.js";
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
cliTest(
"extracts only stored Unix ar members through CLI and MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-ar-extract-");
const source = join(root, "library.a");
await writeFile(
source,
Buffer.concat([
AR_MAGIC,
arMember("/", "symbol table"),
arMember("//", "very-long-object-name.o/\n"),
arMember("/0", "stored bytes"),
]),
);
const cliOutput = join(root, "cli-output");
const cliResult = await cli.run({
arguments: [
"extract-artifact",
source,
"--output-root",
cliOutput,
"--json",
],
});
expect(cliResult.exitCode, cliResult.stdout + cliResult.stderr).toBe(0);
const { call } = await connectLocalToolsMcp({
REA_MCP_TOOL_FAMILIES: "artifact,session",
});
const opened = await call("open_binary", { path: source });
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
const mcpOutput = join(root, "mcp-output");
const reply = await call("extract_artifact", { output_root: mcpOutput });
expect(reply.isError, JSON.stringify(reply)).not.toBe(true);
for (const [evidence, output] of [
[cliResult.json, cliOutput],
[reply.structuredContent, mcpOutput],
] as const) {
const result = artifactExtractionResultSchema.parse(
parseEvidence(evidence).normalized_result,
);
expect(result.containment_verified).toBe(true);
expect(await readdir(output)).toEqual(["very-long-object-name.o"]);
expect(
await readFile(join(output, "very-long-object-name.o"), "utf8"),
).toBe("stored bytes");
}
},
);
cliTest(
"refuses extraction of thin Unix ar archives without an output tree",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-thin-ar-extract-");
const source = join(root, "library.a");
await writeFile(join(root, "external.o"), "external content");
await writeFile(
source,
Buffer.concat([
Buffer.from("!<thin>\n"),
arMember("//", "external.o/\n"),
arMember("/0", "", 16),
]),
);
const output = join(root, "output");
const result = await cli.run({
arguments: [
"extract-artifact",
source,
"--output-root",
output,
"--json",
],
});
expect(result.exitCode).not.toBe(0);
expect(result.json).toMatchObject({
code: "unsupported_target",
details: {
failure_kind: "unsupported",
operation: "extract_artifact",
format: "ar",
},
});
await expect(readdir(output)).rejects.toMatchObject({ code: "ENOENT" });
},
);