Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/control_plane_ts/legacy_writer_fence_read_error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import { syncBuiltinESMExports } from "node:module";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";

import {
legacyCoordinationWriterFencePath,
loadLegacyCoordinationWriterFence,
} from "../../loopx/control_plane/coordination/legacy_writer_fence.ts";

test("fence reader removes the filesystem error path without dropping the diagnostic", async t => {
const root = await fs.mkdtemp(join(tmpdir(), "loopx-fence-read-"));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const path = legacyCoordinationWriterFencePath(root, "synthetic-goal");
const reason = "EACCES: permission denied, open";

for (const withPath of [true, false]) {
await t.test(withPath ? "path-bearing error" : "error without path", async () => {
// ENOENT returns missing before this branch, so it cannot test cleanup.
const error = Object.assign(new Error(withPath ? `${reason} '${path}'` : reason), {
code: "EACCES",
...(withPath ? { path } : {}),
});
const read = t.mock.method(fs, "readFile", async () => { throw error; });
syncBuiltinESMExports();
try {
assert.deepEqual(await loadLegacyCoordinationWriterFence(root, "synthetic-goal"), {
status: "failed",
reason_code: "legacy_writer_fence_read_failed",
reason,
});
assert.equal(read.mock.callCount(), 1);
assert.deepEqual(read.mock.calls[0].arguments, [path, "utf8"]);
} finally {
read.mock.restore();
syncBuiltinESMExports();
}
});
}
});

test("fence reader distinguishes a missing file from a real directory read failure", async t => {
const root = await fs.mkdtemp(join(tmpdir(), "loopx-fence-read-"));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const path = legacyCoordinationWriterFencePath(root, "synthetic-goal");
assert.deepEqual(await loadLegacyCoordinationWriterFence(root, "synthetic-goal"), { status: "missing" });

await fs.mkdir(path, { recursive: true });
const result = await loadLegacyCoordinationWriterFence(root, "synthetic-goal");
assert.equal(result.status, "failed");
if (result.status !== "failed") assert.fail("directory read must fail closed");
assert.equal(result.reason_code, "legacy_writer_fence_read_failed");
assert.match(result.reason, /EISDIR/);
assert.equal(result.reason.includes(path), false);
assert.equal((await fs.stat(path)).isDirectory(), true);
});
1 change: 1 addition & 0 deletions tsconfig.control-plane.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"tests/control_plane_ts/task_lease_acquire_cli.test.ts",
"tests/control_plane_ts/legacy_writer_fence_caller_parity_support.ts",
"tests/control_plane_ts/legacy_writer_fence_caller_parity.test.ts",
"tests/control_plane_ts/legacy_writer_fence_read_error.test.ts",
"tests/control_plane_ts/turn_journal.test.ts",
"tests/control_plane_ts/turn_journal_effects.test.ts",
"tests/control_plane_ts/vision_checkpoint.test.ts",
Expand Down