Skip to content

Commit 7693d35

Browse files
committed
fix(test): isolate non-git workspace fixtures
1 parent 429a099 commit 7693d35

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

‎src/gateway/server.sessions.create.test.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from "node:path";
77
import { promisify } from "node:util";
88
import { afterEach, expect, test, vi } from "vitest";
99
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
10+
import { findGitCheckoutRoot } from "../agents/worktrees/git.js";
1011
import {
1112
findLiveRegistryWorktreeByOwner,
1213
listRegistryWorktrees,
@@ -61,6 +62,21 @@ const { createSessionStoreDir, createSelectedGlobalSessionStore, openClient } =
6162
const execFileAsync = promisify(execFile);
6263
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
6364

65+
async function makeNonGitTempDir(prefix: string): Promise<string> {
66+
let root = await fs.realpath(os.tmpdir());
67+
for (;;) {
68+
const checkoutRoot = findGitCheckoutRoot(root);
69+
if (!checkoutRoot) {
70+
return tempDirs.make(prefix, root);
71+
}
72+
const parent = path.dirname(checkoutRoot);
73+
if (parent === checkoutRoot) {
74+
throw new Error("could not find a temp root outside a git checkout");
75+
}
76+
root = parent;
77+
}
78+
}
79+
6480
test("sessions.create and sessions.delete preserve every concurrent session lifecycle", async () => {
6581
const { storePath } = await createSessionStoreDir();
6682
const sessionCount = 24;
@@ -1147,7 +1163,7 @@ test("sessions.create persists a Gateway cwd without a managed worktree", async
11471163
});
11481164

11491165
test("sessions.create uses a non-git Gateway cwd directly but not as a worktree source", async () => {
1150-
const cwd = tempDirs.make("openclaw-session-direct-cwd-", await fs.realpath(os.tmpdir()));
1166+
const cwd = await makeNonGitTempDir("openclaw-session-direct-cwd-");
11511167
const client = { client: { connect: { scopes: ["operator.admin"] } } as never };
11521168
const direct = await directSessionReq("sessions.create", { cwd }, client);
11531169
expect(direct.ok).toBe(true);
@@ -1369,9 +1385,7 @@ test("sessions.create reset-in-place persists the returned worktree cwd", async
13691385
});
13701386

13711387
test("sessions.create rejects worktrees for non-git agent workspaces", async () => {
1372-
const workspace = await fs.mkdtemp(
1373-
path.join(await fs.realpath(os.tmpdir()), "openclaw-session-plain-workspace-"),
1374-
);
1388+
const workspace = await makeNonGitTempDir("openclaw-session-plain-workspace-");
13751389
testState.agentConfig = { workspace };
13761390
await createSessionStoreDir();
13771391
try {
@@ -1388,7 +1402,6 @@ test("sessions.create rejects worktrees for non-git agent workspaces", async ()
13881402
});
13891403
} finally {
13901404
testState.agentConfig = undefined;
1391-
await fs.rm(workspace, { recursive: true, force: true });
13921405
}
13931406
});
13941407

0 commit comments

Comments
 (0)