11import fs from "node:fs" ;
2- import os from "node:os" ;
32import path from "node:path" ;
43import { DatabaseSync } from "node:sqlite" ;
54import { expectDefined } from "@openclaw/normalization-core" ;
65import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
76import { withTestTimeout } from "../../../test/helpers/promise.js" ;
7+ import { cleanupTempDirs , makeTempDir } from "../../../test/helpers/temp-dir.js" ;
88import type { MsgContext } from "../../auto-reply/templating.js" ;
99import {
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" ;
2024import { appendSqliteTrajectoryRuntimeEvents } from "../../trajectory/runtime-store.sqlite.js" ;
2125import type { TrajectoryEvent } from "../../trajectory/types.js" ;
2226import {
@@ -88,6 +92,7 @@ import { withOwnedSessionTranscriptWrites } from "./transcript-write-context.js"
8892import type { InternalSessionEntry , SessionEntry } from "./types.js" ;
8993
9094const cleanupArchivedSessionTranscriptsMock = vi . hoisted ( ( ) => vi . fn ( async ( ) => { } ) ) ;
95+ const tempDirs : string [ ] = [ ] ;
9196
9297vi . 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" , ( ) => {
0 commit comments