diff --git a/src/config/sessions/sessions.test.ts b/src/config/sessions/sessions.test.ts index 7b6b85a38f6..539c60b9d9d 100644 --- a/src/config/sessions/sessions.test.ts +++ b/src/config/sessions/sessions.test.ts @@ -2,7 +2,7 @@ import fs from "node:fs"; import fsPromises from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; import { upsertAcpSessionMeta } from "../../acp/runtime/session-meta.js"; import * as jsonFiles from "../../infra/json-files.js"; import type { OpenClawConfig } from "../config.js"; @@ -16,30 +16,9 @@ import { import { evaluateSessionFreshness, resolveSessionResetPolicy } from "./reset.js"; import { resolveAndPersistSessionFile } from "./session-file.js"; import { clearSessionStoreCacheForTest, loadSessionStore, updateSessionStore } from "./store.js"; +import { useTempSessionsFixture } from "./test-helpers.js"; import { mergeSessionEntry, type SessionEntry } from "./types.js"; -function useTempSessionsFixture(prefix: string) { - let tempDir = ""; - let storePath = ""; - let sessionsDir = ""; - - beforeEach(() => { - tempDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); - sessionsDir = path.join(tempDir, "agents", "main", "sessions"); - fs.mkdirSync(sessionsDir, { recursive: true }); - storePath = path.join(sessionsDir, "sessions.json"); - }); - - afterEach(() => { - fs.rmSync(tempDir, { recursive: true, force: true }); - }); - - return { - storePath: () => storePath, - sessionsDir: () => sessionsDir, - }; -} - describe("session path safety", () => { it("rejects unsafe session IDs", () => { const unsafeSessionIds = ["../etc/passwd", "a/b", "a\\b", "/abs"]; diff --git a/src/config/sessions/test-helpers.ts b/src/config/sessions/test-helpers.ts new file mode 100644 index 00000000000..46a80dbeffa --- /dev/null +++ b/src/config/sessions/test-helpers.ts @@ -0,0 +1,26 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, beforeEach } from "vitest"; + +export function useTempSessionsFixture(prefix: string) { + let tempDir = ""; + let storePath = ""; + let sessionsDir = ""; + + beforeEach(() => { + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); + sessionsDir = path.join(tempDir, "agents", "main", "sessions"); + fs.mkdirSync(sessionsDir, { recursive: true }); + storePath = path.join(sessionsDir, "sessions.json"); + }); + + afterEach(() => { + fs.rmSync(tempDir, { recursive: true, force: true }); + }); + + return { + storePath: () => storePath, + sessionsDir: () => sessionsDir, + }; +} diff --git a/src/config/sessions/transcript.test.ts b/src/config/sessions/transcript.test.ts index 1cc8d1ef127..bba7f57ae0c 100644 --- a/src/config/sessions/transcript.test.ts +++ b/src/config/sessions/transcript.test.ts @@ -1,33 +1,10 @@ import fs from "node:fs"; -import os from "node:os"; -import path from "node:path"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import * as transcriptEvents from "../../sessions/transcript-events.js"; import { resolveSessionTranscriptPathInDir } from "./paths.js"; +import { useTempSessionsFixture } from "./test-helpers.js"; import { appendAssistantMessageToSessionTranscript } from "./transcript.js"; -function useTempSessionsFixture(prefix: string) { - let tempDir = ""; - let storePath = ""; - let sessionsDir = ""; - - beforeEach(() => { - tempDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); - sessionsDir = path.join(tempDir, "agents", "main", "sessions"); - fs.mkdirSync(sessionsDir, { recursive: true }); - storePath = path.join(sessionsDir, "sessions.json"); - }); - - afterEach(() => { - fs.rmSync(tempDir, { recursive: true, force: true }); - }); - - return { - storePath: () => storePath, - sessionsDir: () => sessionsDir, - }; -} - describe("appendAssistantMessageToSessionTranscript", () => { const fixture = useTempSessionsFixture("transcript-test-"); const sessionId = "test-session-id";