Skip to content

Commit e9250be

Browse files
authored
test(sessions): close SQLite fixtures before cleanup (#113074)
1 parent 6680b0f commit e9250be

5 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎src/config/sessions/ambient-transcript-watermark.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import fs from "node:fs";
2-
import os from "node:os";
32
import path from "node:path";
43
import { afterEach, beforeEach, describe, expect, it } from "vitest";
4+
import { cleanupTempDirs, makeTempDir } from "../../../test/helpers/temp-dir.js";
5+
import { closeOpenClawAgentDatabasesForTest } from "../../state/openclaw-agent-db.js";
6+
import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js";
57
import {
68
readAmbientTranscriptWatermark,
79
resolveAmbientTranscriptWatermarkKey,
810
updateAmbientTranscriptWatermark,
911
} from "./ambient-transcript-watermark.js";
1012
import { loadSessionEntry, replaceSessionEntry } from "./session-accessor.js";
1113

14+
const tempDirs: string[] = [];
15+
1216
describe("ambient transcript watermark", () => {
1317
let tempDir: string;
1418
let storePath: string;
@@ -20,12 +24,14 @@ describe("ambient transcript watermark", () => {
2024
});
2125

2226
beforeEach(() => {
23-
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-ambient-watermark-"));
27+
tempDir = makeTempDir(tempDirs, "openclaw-ambient-watermark-");
2428
storePath = path.join(tempDir, "sessions.json");
2529
});
2630

2731
afterEach(() => {
28-
fs.rmSync(tempDir, { recursive: true, force: true });
32+
closeOpenClawAgentDatabasesForTest();
33+
closeOpenClawStateDatabaseForTest();
34+
cleanupTempDirs(tempDirs);
2935
});
3036

3137
it("stamps and resolves the watermark for the current session id only", async () => {

‎src/config/sessions/entry-freshness.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from "node:path";
22
import { afterEach, beforeEach, describe, expect, it } from "vitest";
33
import { cleanupTempDirs, makeTempDir } from "../../../test/helpers/temp-dir.js";
4+
import { closeOpenClawAgentDatabasesForTest } from "../../state/openclaw-agent-db.js";
5+
import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js";
46
import { resolveSessionEntryResetFreshness } from "./entry-freshness.js";
57
import { appendTranscriptEvent, upsertSessionEntry } from "./session-accessor.js";
68

@@ -18,6 +20,8 @@ describe("resolveSessionEntryResetFreshness", () => {
1820
});
1921

2022
afterEach(() => {
23+
closeOpenClawAgentDatabasesForTest();
24+
closeOpenClawStateDatabaseForTest();
2125
cleanupTempDirs(tempDirs);
2226
});
2327

‎src/config/sessions/session-accessor.test.ts‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from "node:fs";
2-
import os from "node:os";
32
import path from "node:path";
43
import { DatabaseSync } from "node:sqlite";
54
import { expectDefined } from "@openclaw/normalization-core";
65
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
76
import { withTestTimeout } from "../../../test/helpers/promise.js";
7+
import { cleanupTempDirs, makeTempDir } from "../../../test/helpers/temp-dir.js";
88
import type { MsgContext } from "../../auto-reply/templating.js";
99
import {
1010
onInternalSessionTranscriptUpdate,
@@ -17,6 +17,10 @@ import {
1717
openOpenClawAgentDatabase,
1818
resolveOpenClawAgentSqlitePath,
1919
} from "../../state/openclaw-agent-db.js";
20+
import {
21+
closeOpenClawStateDatabaseForTest,
22+
isOpenClawStateDatabaseOpen,
23+
} from "../../state/openclaw-state-db.js";
2024
import { appendSqliteTrajectoryRuntimeEvents } from "../../trajectory/runtime-store.sqlite.js";
2125
import type { TrajectoryEvent } from "../../trajectory/types.js";
2226
import {
@@ -88,6 +92,7 @@ import { withOwnedSessionTranscriptWrites } from "./transcript-write-context.js"
8892
import type { InternalSessionEntry, SessionEntry } from "./types.js";
8993

9094
const cleanupArchivedSessionTranscriptsMock = vi.hoisted(() => vi.fn(async () => {}));
95+
const tempDirs: string[] = [];
9196

9297
vi.mock("../../gateway/session-archive.runtime.js", async (importOriginal) => {
9398
const actual = await importOriginal<typeof import("../../gateway/session-archive.runtime.js")>();
@@ -127,20 +132,47 @@ describe("session accessor seam", () => {
127132
let tempDir: string;
128133
let storePath: string;
129134
let transcriptPath: string;
135+
let cleanupProbeDatabasePath = "";
136+
let cleanupProbeRoot = "";
130137

131138
function loadMainInitializationSnapshot(sessionKey: string) {
132139
return loadReplySessionInitializationSnapshot({ agentId: "main", sessionKey, storePath });
133140
}
134141

135142
beforeEach(() => {
136143
cleanupArchivedSessionTranscriptsMock.mockReset();
137-
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-session-accessor-"));
144+
tempDir = makeTempDir(tempDirs, "openclaw-session-accessor-");
138145
storePath = path.join(tempDir, "sessions.json");
139146
transcriptPath = path.join(tempDir, "session.jsonl");
140147
});
141148

142149
afterEach(() => {
143-
fs.rmSync(tempDir, { recursive: true, force: true });
150+
closeOpenClawAgentDatabasesForTest();
151+
closeOpenClawStateDatabaseForTest();
152+
cleanupTempDirs(tempDirs);
153+
});
154+
155+
describe.sequential("session database teardown boundary", () => {
156+
it("opens cached agent and shared-state handles", async () => {
157+
await replaceSessionEntry(
158+
{ agentId: "main", sessionKey: "agent:main:cleanup-probe", storePath },
159+
{ sessionId: "cleanup-probe", updatedAt: 1 },
160+
);
161+
cleanupProbeDatabasePath = expectDefined(
162+
resolveSqliteTargetFromSessionStorePath(storePath, { agentId: "main" }).path,
163+
"cleanup probe database path",
164+
);
165+
cleanupProbeRoot = tempDir;
166+
167+
expect(isOpenClawAgentDatabaseOpen(cleanupProbeDatabasePath)).toBe(true);
168+
expect(isOpenClawStateDatabaseOpen()).toBe(true);
169+
});
170+
171+
it("releases both cache owners before the next test", () => {
172+
expect(isOpenClawAgentDatabaseOpen(cleanupProbeDatabasePath)).toBe(false);
173+
expect(isOpenClawStateDatabaseOpen()).toBe(false);
174+
expect(fs.existsSync(cleanupProbeRoot)).toBe(false);
175+
});
144176
});
145177

146178
it("exposes the canonical SQLite session lifecycle owners", () => {

‎src/config/sessions/session-registry-maintenance.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import fs from "node:fs/promises";
33
import path from "node:path";
44
import { afterAll, beforeAll, describe, expect, it } from "vitest";
55
import { beginSessionWorkAdmission } from "../../sessions/session-lifecycle-admission.js";
6+
import { closeOpenClawAgentDatabasesForTest } from "../../state/openclaw-agent-db.js";
7+
import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js";
68
import { createFixtureSuite } from "../../test-utils/fixture-suite.js";
79
import { readSessionArchiveContentSync } from "./archive-compression.js";
810
import { isRetainedSessionTranscriptArchiveName } from "./artifacts.js";
@@ -24,6 +26,8 @@ beforeAll(async () => {
2426
});
2527

2628
afterAll(async () => {
29+
closeOpenClawAgentDatabasesForTest();
30+
closeOpenClawStateDatabaseForTest();
2731
await fixtureSuite.cleanup();
2832
});
2933

‎src/test-utils/fixture-suite.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
// Loads fixture suites from disk for parametrized tests.
22
import fs from "node:fs/promises";
3-
import os from "node:os";
43
import path from "node:path";
4+
import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js";
55

66
/** Creates a temp fixture root with deterministic per-case subdirectories. */
77
export function createFixtureSuite(rootPrefix: string) {
88
let fixtureRoot = "";
99
let fixtureCount = 0;
10+
const fixtureRoots = new Set<string>();
1011

1112
return {
1213
async setup(): Promise<void> {
13-
// Canonicalize: macOS tmpdir sits behind a symlink (/var -> /private/var)
14-
// and production realpaths state/session paths, so symlinked roots break
15-
// path-equality assertions.
16-
fixtureRoot = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), rootPrefix)));
14+
fixtureRoot = makeTempDir(fixtureRoots, rootPrefix);
1715
},
1816
async cleanup(): Promise<void> {
1917
if (!fixtureRoot) {
2018
return;
2119
}
22-
await fs.rm(fixtureRoot, { recursive: true, force: true });
20+
cleanupTempDirs(fixtureRoots);
2321
fixtureRoot = "";
2422
},
2523
async createCaseDir(prefix: string): Promise<string> {

0 commit comments

Comments
 (0)