test(config): share session test fixture helper

This commit is contained in:
Vincent Koc
2026-04-06 06:33:38 +01:00
parent c13352a7ef
commit fa9b3fb13a
3 changed files with 30 additions and 48 deletions

View File

@@ -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"];

View File

@@ -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,
};
}

View File

@@ -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";