1+ import fs from "node:fs" ;
12import path from "node:path" ;
23import { afterEach , describe , expect , it , vi } from "vitest" ;
34import { loadExactSessionEntry , replaceSessionEntry } from "../config/sessions/session-accessor.js" ;
45import {
56 resolveSqliteScope ,
67 runExclusiveSqliteSessionWrite ,
78} from "../config/sessions/session-accessor.sqlite-scope.js" ;
8- import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js" ;
9+ import {
10+ closeOpenClawAgentDatabasesForTest ,
11+ listOpenClawAgentDatabasesForTest ,
12+ } from "../state/openclaw-agent-db.js" ;
913import { clearCronJobActive , markCronJobActive } from "./active-jobs.js" ;
1014import { CronService } from "./service.js" ;
1115import { createDeferred , setupCronServiceSuite } from "./service.test-harness.js" ;
@@ -19,6 +23,45 @@ afterEach(() => {
1923} ) ;
2024
2125describe ( "CronService.remove session cleanup" , ( ) => {
26+ it ( "does not materialize a session database when the deleted job never ran" , async ( ) => {
27+ const { storePath } = await makeStorePath ( ) ;
28+ const sessionStorePath = path . join ( path . dirname ( storePath ) , "sessions.json" ) ;
29+ const cron = new CronService ( {
30+ storePath,
31+ cronEnabled : true ,
32+ defaultAgentId : "main" ,
33+ resolveSessionStorePath : ( ) => sessionStorePath ,
34+ log : logger ,
35+ enqueueSystemEvent : vi . fn ( ) ,
36+ requestHeartbeat : vi . fn ( ) ,
37+ runIsolatedAgentJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
38+ } ) ;
39+ const job = await cron . add ( {
40+ id : "never-ran" ,
41+ name : "never ran" ,
42+ enabled : false ,
43+ schedule : { kind : "every" , everyMs : 60_000 } ,
44+ sessionTarget : "isolated" ,
45+ wakeMode : "next-heartbeat" ,
46+ payload : { kind : "agentTurn" , message : "work" } ,
47+ } ) ;
48+ const sessionKey = `agent:main:cron:${ job . id } ` ;
49+ const databasePath = resolveSqliteScope ( {
50+ agentId : "main" ,
51+ sessionKey,
52+ storePath : sessionStorePath ,
53+ } ) . path ! ;
54+
55+ expect ( fs . existsSync ( databasePath ) ) . toBe ( false ) ;
56+
57+ await expect ( cron . remove ( job . id ) ) . resolves . toEqual ( { ok : true , removed : true } ) ;
58+
59+ expect ( fs . existsSync ( databasePath ) ) . toBe ( false ) ;
60+ expect (
61+ listOpenClawAgentDatabasesForTest ( ) . some ( ( database ) => database . path === databasePath ) ,
62+ ) . toBe ( false ) ;
63+ } ) ;
64+
2265 it ( "removes only the deleted isolated job's base session" , async ( ) => {
2366 const { storePath } = await makeStorePath ( ) ;
2467 const sessionStorePath = path . join ( path . dirname ( storePath ) , "sessions.json" ) ;
0 commit comments