Files
rea/tests/boundary/filesystem/stableArtifact.test.ts
T
morluto b955b825fc fix(artifacts): retain read cleanup ownership through CLI and MCP shutdown
Keep failed stable-read handles and WABT runtime roots under retryable resource scopes. Preserve cancellation causes and primary errors, close the same service instances bound to MCP tools, and consolidate CLI operation/cleanup projection.

Validation: fast typecheck and lint, cached runtime build, docs checks, SDK schema validation, focused service and real-file cleanup tests, CLI cleanup regressions, and MCP lifecycle/shutdown workflows. Provider process fixtures simulate engine output; real WABT and Linux EVM execution are not established by this pass.
2026-10-13 16:08:41 +08:00

81 lines
2.7 KiB
TypeScript

import {
lstat,
open,
rename,
symlink,
unlink,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { readStableArtifact } from "../../../src/artifacts/readStableArtifact.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
it("reads exact binary and empty bytes and rejects symlink/size/cancellation boundaries", async () => {
const root = await createTestTempDirectory("rea-stable-artifact-");
const path = join(root, "source.bin");
await writeFile(path, Buffer.from([0, 255, 254]));
expect((await readStableArtifact(path, 3)).bytes).toEqual(
Buffer.from([0, 255, 254]),
);
await expect(readStableArtifact(path, 2)).rejects.toMatchObject({
reason: "limit",
});
await writeFile(path, "");
expect((await readStableArtifact(path, 0)).bytes.length).toBe(0);
const real = join(root, "original.bin");
await rename(path, real);
await symlink(real, path);
await expect(readStableArtifact(path, 3)).rejects.toMatchObject({
reason: "path",
});
const controller = new AbortController();
controller.abort();
await expect(
readStableArtifact(real, 3, controller.signal),
).rejects.toMatchObject({
reason: "cancelled",
cause: controller.signal.reason,
});
});
const checkChange = async (
stage: "before-open" | "after-read" | "symlink-replacement",
): Promise<void> => {
const root = await createTestTempDirectory("rea-stable-read-");
const path = join(root, "selected");
await writeFile(path, "original evidence");
let inspected = 0;
const reading = readStableArtifact(path, 1024, undefined, {
async lstat(selected) {
inspected++;
if (stage === "after-read" && inspected === 2) await unlink(selected);
const metadata = await lstat(selected, { bigint: true });
if (inspected === 1 && stage !== "after-read") {
await unlink(selected);
if (stage === "symlink-replacement") await symlink("missing", selected);
}
return metadata;
},
open,
});
await expect(reading).rejects.toMatchObject({ reason: "integrity" });
};
it.each(["before-open", "after-read"] as const)(
"retains an actual post-validation %s change as integrity failure",
checkChange,
);
it.skipIf(process.platform === "win32")(
"retains a post-validation symlink replacement as an integrity failure",
() => checkChange("symlink-replacement"),
);
it("keeps initially missing inputs distinguishable from later changes", async () => {
const root = await createTestTempDirectory("rea-stable-missing-");
await expect(
readStableArtifact(join(root, "missing"), 1024),
).rejects.toMatchObject({ code: "ENOENT" });
});