Files
rea/tests/acceptance/arArchiveExtraction.test.ts

228 lines
7.6 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,
artifactInventoryResultSchema,
} from "../../src/domain/artifactGraph.js";
import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.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(
"preserves signed ar member timestamps through CLI and MCP inventories",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-ar-timestamps-");
const source = join(root, "library.a");
const timestamps = [-86_400, -1, 0, 1_700_000_000];
const members = timestamps.map((timestamp, index) => {
const member = arMember(`member${String(index)}.o/`, "stored bytes");
member.write(String(timestamp).padEnd(12), 16, "ascii");
return member;
});
await writeFile(source, Buffer.concat([AR_MAGIC, ...members]));
const inspected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(inspected.exitCode, inspected.stdout + inspected.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 reply = await call("inspect_artifact", { detail: "complete" });
expect(reply.isError, JSON.stringify(reply)).not.toBe(true);
for (const evidence of [inspected.json, reply.structuredContent]) {
const inspection = artifactInspectionResultSchema.parse(
parseEvidence(evidence).normalized_result,
);
const inventoryEvidence = inspection.substeps[0]?.evidence;
if (inventoryEvidence === undefined)
throw new Error("Inspection did not retain ar inventory Evidence");
const inventory = artifactInventoryResultSchema.parse(
inventoryEvidence.normalized_result,
);
expect(inventory.coverage.status).toBe("complete");
expect(
inventory.occurrences
.filter(({ format_metadata }) => format_metadata?.kind === "ar-entry")
.map(({ logical_path, format_metadata }) => [
logical_path,
format_metadata?.kind === "ar-entry" ? format_metadata.mtime : null,
]),
).toEqual(
timestamps.map((timestamp, index) => [
`member${String(index)}.o`,
timestamp,
]),
);
}
},
);
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" });
},
);
cliTest(
"inventories and extracts MSVC members recorded under absolute paths",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-msvc-lib-");
const source = join(root, "absolute-paths.lib");
// lib.exe records the absolute object path it was given in the
// NUL-terminated "//" table that follows its two linker members.
await writeFile(
source,
Buffer.concat([
AR_MAGIC,
arMember("/", "first-linker"),
arMember("/", "second-linker"),
arMember(
"//",
"C:\\build\\Release\\decode.obj\0C:\\build\\Release\\reflect.obj\0",
),
arMember("/0", "decode bytes"),
arMember("/28", "reflect bytes"),
]),
);
const inspected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
const graph = artifactInventoryResultSchema.parse(
artifactInspectionResultSchema.parse(
parseEvidence(inspected.json).normalized_result,
).substeps[0]?.evidence.normalized_result,
);
expect(graph.coverage.status).toBe("complete");
expect(
graph.occurrences
.filter((row) => row.logical_path !== ".")
.map((row) => [
row.logical_path,
row.format_metadata?.kind === "ar-entry"
? row.format_metadata.source_path
: undefined,
]),
).toEqual([
["decode.obj", "C:\\build\\Release\\decode.obj"],
["reflect.obj", "C:\\build\\Release\\reflect.obj"],
]);
const output = join(root, "output");
const extracted = await cli.run({
arguments: [
"extract-artifact",
source,
"--output-root",
output,
"--json",
],
});
expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0);
expect((await readdir(output)).sort()).toEqual([
"decode.obj",
"reflect.obj",
]);
expect(await readFile(join(output, "decode.obj"), "utf8")).toBe(
"decode bytes",
);
expect(await readFile(join(output, "reflect.obj"), "utf8")).toBe(
"reflect bytes",
);
},
);