mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 13:07:39 +00:00
refactor(core): dedupe gateway runtime and config tests
This commit is contained in:
@@ -19,6 +19,28 @@ import { resolveSessionResetPolicy } from "./reset.js";
|
||||
import { appendAssistantMessageToSessionTranscript } from "./transcript.js";
|
||||
import 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"];
|
||||
@@ -148,20 +170,7 @@ describe("session store lock (Promise chain mutex)", () => {
|
||||
});
|
||||
|
||||
describe("appendAssistantMessageToSessionTranscript", () => {
|
||||
let tempDir: string;
|
||||
let storePath: string;
|
||||
let sessionsDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "transcript-test-"));
|
||||
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 });
|
||||
});
|
||||
const fixture = useTempSessionsFixture("transcript-test-");
|
||||
|
||||
it("creates transcript file and appends message for valid session", async () => {
|
||||
const sessionId = "test-session-id";
|
||||
@@ -173,12 +182,12 @@ describe("appendAssistantMessageToSessionTranscript", () => {
|
||||
channel: "discord",
|
||||
},
|
||||
};
|
||||
fs.writeFileSync(storePath, JSON.stringify(store), "utf-8");
|
||||
fs.writeFileSync(fixture.storePath(), JSON.stringify(store), "utf-8");
|
||||
|
||||
const result = await appendAssistantMessageToSessionTranscript({
|
||||
sessionKey,
|
||||
text: "Hello from delivery mirror!",
|
||||
storePath,
|
||||
storePath: fixture.storePath(),
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -206,20 +215,7 @@ describe("appendAssistantMessageToSessionTranscript", () => {
|
||||
});
|
||||
|
||||
describe("resolveAndPersistSessionFile", () => {
|
||||
let tempDir: string;
|
||||
let storePath: string;
|
||||
let sessionsDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "session-file-test-"));
|
||||
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 });
|
||||
});
|
||||
const fixture = useTempSessionsFixture("session-file-test-");
|
||||
|
||||
it("persists fallback topic transcript paths for sessions without sessionFile", async () => {
|
||||
const sessionId = "topic-session-id";
|
||||
@@ -230,22 +226,47 @@ describe("resolveAndPersistSessionFile", () => {
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
};
|
||||
fs.writeFileSync(storePath, JSON.stringify(store), "utf-8");
|
||||
const sessionStore = loadSessionStore(storePath, { skipCache: true });
|
||||
const fallbackSessionFile = resolveSessionTranscriptPathInDir(sessionId, sessionsDir, 456);
|
||||
fs.writeFileSync(fixture.storePath(), JSON.stringify(store), "utf-8");
|
||||
const sessionStore = loadSessionStore(fixture.storePath(), { skipCache: true });
|
||||
const fallbackSessionFile = resolveSessionTranscriptPathInDir(
|
||||
sessionId,
|
||||
fixture.sessionsDir(),
|
||||
456,
|
||||
);
|
||||
|
||||
const result = await resolveAndPersistSessionFile({
|
||||
sessionId,
|
||||
sessionKey,
|
||||
sessionStore,
|
||||
storePath,
|
||||
storePath: fixture.storePath(),
|
||||
sessionEntry: sessionStore[sessionKey],
|
||||
fallbackSessionFile,
|
||||
});
|
||||
|
||||
expect(result.sessionFile).toBe(fallbackSessionFile);
|
||||
|
||||
const saved = loadSessionStore(storePath, { skipCache: true });
|
||||
const saved = loadSessionStore(fixture.storePath(), { skipCache: true });
|
||||
expect(saved[sessionKey]?.sessionFile).toBe(fallbackSessionFile);
|
||||
});
|
||||
|
||||
it("creates and persists entry when session is not yet present", async () => {
|
||||
const sessionId = "new-session-id";
|
||||
const sessionKey = "agent:main:telegram:group:123";
|
||||
fs.writeFileSync(fixture.storePath(), JSON.stringify({}), "utf-8");
|
||||
const sessionStore = loadSessionStore(fixture.storePath(), { skipCache: true });
|
||||
const fallbackSessionFile = resolveSessionTranscriptPathInDir(sessionId, fixture.sessionsDir());
|
||||
|
||||
const result = await resolveAndPersistSessionFile({
|
||||
sessionId,
|
||||
sessionKey,
|
||||
sessionStore,
|
||||
storePath: fixture.storePath(),
|
||||
fallbackSessionFile,
|
||||
});
|
||||
|
||||
expect(result.sessionFile).toBe(fallbackSessionFile);
|
||||
expect(result.sessionEntry.sessionId).toBe(sessionId);
|
||||
const saved = loadSessionStore(fixture.storePath(), { skipCache: true });
|
||||
expect(saved[sessionKey]?.sessionFile).toBe(fallbackSessionFile);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user