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
12 changes: 12 additions & 0 deletions tests/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import path from "node:path";
import process from "node:process";
import { spawnSync } from "node:child_process";

// Keep the suite hermetic when it runs from inside a live Claude Code
// session. Two inherited variables otherwise change behavior:
// - CLAUDE_PLUGIN_DATA points at the user's real plugin data directory, so
// in-process state writes and spawned companions (which inherit it through
// buildEnv) leak fixture workspaces and job records into that real
// directory. Pin it to a fresh temp root for the whole test process.
// - CODEX_COMPANION_SESSION_ID makes status/result session-filter jobs that
// fixtures seed without a sessionId, hiding them. Session-scoped tests set
// the variable explicitly in the env they compose.
process.env.CLAUDE_PLUGIN_DATA = fs.mkdtempSync(path.join(os.tmpdir(), "codex-plugin-cc-tests-"));
delete process.env.CODEX_COMPANION_SESSION_ID;

export function makeTempDir(prefix = "codex-plugin-test-") {
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
}
Expand Down
19 changes: 15 additions & 4 deletions tests/state.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ import { resolveJobFile, resolveJobLogFile, resolveStateDir, resolveStateFile, s

test("resolveStateDir uses a temp-backed per-workspace directory", () => {
const workspace = makeTempDir();
const stateDir = resolveStateDir(workspace);
const previousPluginDataDir = process.env.CLAUDE_PLUGIN_DATA;
delete process.env.CLAUDE_PLUGIN_DATA;

try {
const stateDir = resolveStateDir(workspace);

assert.equal(stateDir.startsWith(os.tmpdir()), true);
assert.match(path.basename(stateDir), /.+-[a-f0-9]{16}$/);
assert.match(stateDir, new RegExp(`^${os.tmpdir().replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`));
assert.equal(stateDir.startsWith(os.tmpdir()), true);
assert.match(path.basename(stateDir), /.+-[a-f0-9]{16}$/);
assert.match(stateDir, new RegExp(`^${os.tmpdir().replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`));
} finally {
if (previousPluginDataDir === undefined) {
delete process.env.CLAUDE_PLUGIN_DATA;
} else {
process.env.CLAUDE_PLUGIN_DATA = previousPluginDataDir;
}
}
});

test("resolveStateDir uses CLAUDE_PLUGIN_DATA when it is provided", () => {
Expand Down