Skip to content
Open
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
13 changes: 7 additions & 6 deletions integrations/claude-code-plugin/tests/hook-contract.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { hostEnv } from "./host-env.mjs";
import { removeSandbox } from "./hook-sandbox.mjs";

const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(here, "..");
Expand Down Expand Up @@ -285,7 +286,7 @@ test("malformed, empty and non-object payloads: every hook exits 0 silently", as
assertContract(await runAll(cases), 8000);
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -305,7 +306,7 @@ test("no token anywhere: every hook exits 0 silently and sends no request", asyn
assert.deepEqual(loom.ops, [], "a hook without a token reached the server");
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -330,7 +331,7 @@ test("server unreachable, answering 401, or hanging: every hook stays within its
} finally {
await unauthorized.close();
await hang.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -348,7 +349,7 @@ test("a host that never closes stdin does not hold a hook open", async () => {
assertContract(await runAll(cases), 8000);
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -366,7 +367,7 @@ test("a well-formed session against a healthy server: every hook exits 0 silentl
assert.ok(loom.ops.length > 0, "no hook reached a healthy server");
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});

Expand All @@ -385,6 +386,6 @@ test("a payload over the stdin cap is dropped, not parsed: every hook exits 0 si
assertContract(await runAll(cases, 4), 15000);
} finally {
await loom.close();
fs.rmSync(box.dir, { recursive: true, force: true });
await removeSandbox(box);
}
});
12 changes: 12 additions & 0 deletions integrations/claude-code-plugin/tests/hook-sandbox.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from "node:fs";
import { setTimeout as delay } from "node:timers/promises";

export async function removeSandbox(box, { timeoutMs = 45000 } = {}) {
const deadline = performance.now() + timeoutMs;
// The flusher removes its job only after its last cursor/recording write.
while (fs.readdirSync(box.tmp).some((name) => /^loom-flush-\d+-\d+\.json$/.test(name))) {
if (performance.now() >= deadline) throw new Error("unfinished detached jobs in hook sandbox");
await delay(10);
}
fs.rmSync(box.dir, { recursive: true, force: true });
}
182 changes: 182 additions & 0 deletions integrations/claude-code-plugin/tests/hook-sandbox.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import "./temp-home.mjs";
import { test } from "node:test";
import assert from "node:assert/strict";
import { fork, spawn } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import http from "node:http";
import path from "node:path";
import { once } from "node:events";
import { setImmediate as immediate } from "node:timers/promises";
import { removeSandbox } from "./hook-sandbox.mjs";
import { hostEnv } from "./host-env.mjs";
import { dispatchFlush } from "../lib/flush-job.mjs";

function sandbox() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "loom-hook-drain-"));
const tmp = path.join(dir, "tmp");
fs.mkdirSync(tmp);
return { dir, tmp };
}

test("sandbox removal waits for its detached job's final write", async () => {
const box = sandbox();
const script = path.join(box.dir, "writer.cjs");
fs.writeFileSync(
script,
`
const fs = require("node:fs");
const path = require("node:path");
const root = process.argv[2];
const job = path.join(root, "tmp", "loom-flush-1-" + process.pid + ".json");
fs.writeFileSync(job, "{}");
process.send("ready");
process.once("message", () => {
fs.writeFileSync(path.join(root, "cursor"), "done");
fs.unlinkSync(job);
process.send("written", () => process.disconnect());
});
`,
);
const child = fork(script, [box.dir], {
env: hostEnv({}),
stdio: ["ignore", "ignore", "pipe", "ipc"],
detached: true,
});
const closed = once(child, "close");
let removing;
let released = false;
let stderr = "";
child.stderr.on("data", (part) => {
stderr += part;
});
try {
const [ready] = await once(child, "message");
assert.equal(ready, "ready");
let removed = false;
removing = removeSandbox(box).then(() => {
removed = true;
});
await immediate();
assert.equal(removed, false, "the owned writer still has a pending job");
assert.equal(fs.existsSync(box.dir), true);
const written = once(child, "message");
released = true;
child.send("release");
assert.equal((await written)[0], "written");
await removing;
assert.deepEqual(await closed, [0, null], stderr);
assert.equal(fs.existsSync(box.dir), false);
} finally {
if (!released && child.connected) child.send("release");
await closed;
if (removing) await removing;
fs.rmSync(box.dir, { recursive: true, force: true });
}
});

test("an unfinished job refuses removal at the cleanup deadline", async () => {
const box = sandbox();
const job = path.join(box.tmp, "loom-flush-2-3.json");
fs.writeFileSync(job, "{}");
try {
await assert.rejects(removeSandbox(box, { timeoutMs: 0 }), /unfinished detached jobs/);
assert.equal(fs.existsSync(job), true);
} finally {
fs.rmSync(box.dir, { recursive: true, force: true });
}
});

test("an empty job census permits removal with other sandbox files", async () => {
const box = sandbox();
fs.writeFileSync(path.join(box.tmp, "cursor"), "42");
await removeSandbox(box);
assert.equal(fs.existsSync(box.dir), false);
});

test("sandbox removal waits for the real flusher's completion marker", async () => {
const box = sandbox();
let arrived;
const request = new Promise((resolve) => {
arrived = resolve;
});
let respond;
const server = http.createServer((req, res) => {
req.resume();
req.on("end", () => {
respond = () => {
if (!res.writableEnded) {
res.writeHead(200, { "content-type": "application/json" });
res.end("{}");
}
};
arrived();
});
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
let child;
let closed;
let removing;
const token = [
"e30",
Buffer.from(JSON.stringify({ org: "test", namespaces: ["scope"] })).toString("base64url"),
"test",
].join(".");
try {
assert.equal(
dispatchFlush(
{
appends: [{ org: "test", namespace: "scope", agent: "test", session_id: "test" }],
cursorPath: path.join(box.tmp, "cursor"),
advanceTo: 42,
},
{
tmp: box.tmp,
timeoutMs: 1000,
spawnImpl: (command, args, options) => {
child = spawn(command, args, {
...options,
env: hostEnv({
HOME: box.dir,
TMPDIR: box.tmp,
LOOM_BASE_URL: `http://127.0.0.1:${server.address().port}`,
LOOM_API_KEY: token,
LOOM_HOOK_TIMEOUT_MS: "1000",
LOOM_AUTO_SIGNUP: "off",
}),
});
closed = once(child, "close");
return child;
},
},
),
true,
);
await Promise.race([
request,
closed.then(() => {
throw new Error("flusher exited before its request");
}),
]);
let removed = false;
removing = removeSandbox(box).then(() => {
removed = true;
});
await immediate();
assert.equal(removed, false);
assert.equal(fs.existsSync(box.dir), true);
respond();
assert.deepEqual(await closed, [0, null]);
await removing;
assert.equal(fs.existsSync(box.dir), false);
} finally {
respond?.();
try {
if (closed) await closed;
if (removing) await removing;
} finally {
await new Promise((resolve) => server.close(resolve));
fs.rmSync(box.dir, { recursive: true, force: true });
}
}
});
Loading