refactor(test): dedupe temp session path setup in file repair e2e

This commit is contained in:
Peter Steinberger
2026-02-21 18:57:33 +00:00
parent 70fdab6e95
commit 9ead79937e

View File

@@ -1,7 +1,7 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import { repairSessionFileIfNeeded } from "./session-file-repair.js"; import { repairSessionFileIfNeeded } from "./session-file-repair.js";
function buildSessionHeaderAndMessage() { function buildSessionHeaderAndMessage() {
@@ -22,10 +22,21 @@ function buildSessionHeaderAndMessage() {
return { header, message }; return { header, message };
} }
const tempDirs: string[] = [];
async function createTempSessionPath() {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-repair-"));
tempDirs.push(dir);
return { dir, file: path.join(dir, "session.jsonl") };
}
afterEach(async () => {
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
});
describe("repairSessionFileIfNeeded", () => { describe("repairSessionFileIfNeeded", () => {
it("rewrites session files that contain malformed lines", async () => { it("rewrites session files that contain malformed lines", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-repair-")); const { file } = await createTempSessionPath();
const file = path.join(dir, "session.jsonl");
const { header, message } = buildSessionHeaderAndMessage(); const { header, message } = buildSessionHeaderAndMessage();
const content = `${JSON.stringify(header)}\n${JSON.stringify(message)}\n{"type":"message"`; const content = `${JSON.stringify(header)}\n${JSON.stringify(message)}\n{"type":"message"`;
@@ -46,8 +57,7 @@ describe("repairSessionFileIfNeeded", () => {
}); });
it("does not drop CRLF-terminated JSONL lines", async () => { it("does not drop CRLF-terminated JSONL lines", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-repair-")); const { file } = await createTempSessionPath();
const file = path.join(dir, "session.jsonl");
const { header, message } = buildSessionHeaderAndMessage(); const { header, message } = buildSessionHeaderAndMessage();
const content = `${JSON.stringify(header)}\r\n${JSON.stringify(message)}\r\n`; const content = `${JSON.stringify(header)}\r\n${JSON.stringify(message)}\r\n`;
await fs.writeFile(file, content, "utf-8"); await fs.writeFile(file, content, "utf-8");
@@ -58,8 +68,7 @@ describe("repairSessionFileIfNeeded", () => {
}); });
it("warns and skips repair when the session header is invalid", async () => { it("warns and skips repair when the session header is invalid", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-repair-")); const { file } = await createTempSessionPath();
const file = path.join(dir, "session.jsonl");
const badHeader = { const badHeader = {
type: "message", type: "message",
id: "msg-1", id: "msg-1",
@@ -79,7 +88,7 @@ describe("repairSessionFileIfNeeded", () => {
}); });
it("returns a detailed reason when read errors are not ENOENT", async () => { it("returns a detailed reason when read errors are not ENOENT", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-repair-")); const { dir } = await createTempSessionPath();
const warn = vi.fn(); const warn = vi.fn();
const result = await repairSessionFileIfNeeded({ sessionFile: dir, warn }); const result = await repairSessionFileIfNeeded({ sessionFile: dir, warn });