mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-13 10:11:20 +00:00
test(config): share session test fixture helper
This commit is contained in:
@@ -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"];
|
||||
|
||||
26
src/config/sessions/test-helpers.ts
Normal file
26
src/config/sessions/test-helpers.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user