22import fs from "node:fs" ;
33import os from "node:os" ;
44import path from "node:path" ;
5- import {
6- loadSessionStore ,
7- saveSessionStore ,
8- type SessionEntry ,
9- } from "openclaw/plugin-sdk/session-store-runtime" ;
5+ import { loadSessionStore } from "openclaw/plugin-sdk/session-store-runtime" ;
106import { afterEach , beforeEach , describe , expect , it } from "vitest" ;
117import type { OpenClawConfig } from "../runtime-api.js" ;
128import { isFeishuSessionStoreKey , runFeishuDoctorSequence } from "./doctor.js" ;
@@ -63,13 +59,10 @@ function storePath(agentId = "main"): string {
6359 return path . join ( sessionsDir ( agentId ) , "sessions.json" ) ;
6460}
6561
66- async function writeStore (
67- entries : Record < string , SessionEntry > ,
68- agentId = "main" ,
69- ) : Promise < string > {
62+ function writeStore ( entries : Record < string , unknown > , agentId = "main" ) : string {
7063 const target = storePath ( agentId ) ;
7164 fs . mkdirSync ( path . dirname ( target ) , { recursive : true } ) ;
72- await saveSessionStore ( target , entries , { skipMaintenance : true } ) ;
65+ fs . writeFileSync ( target , JSON . stringify ( entries , null , 2 ) ) ;
7366 return target ;
7467}
7568
@@ -138,7 +131,7 @@ describe("Feishu doctor state repair", () => {
138131 fs . writeFileSync ( path . join ( feishuDedupDir , "default.json" ) , JSON . stringify ( { msg1 : 1 } ) ) ;
139132
140133 writeTranscript ( "sess-ok" , [ sessionHeader ( "sess-ok" ) , userMessage ( "hello" ) ] ) ;
141- await writeStore ( {
134+ writeStore ( {
142135 "agent:main:feishu:direct:ou_user" : {
143136 sessionId : "sess-ok" ,
144137 sessionFile : "sess-ok.jsonl" ,
@@ -162,16 +155,15 @@ describe("Feishu doctor state repair", () => {
162155 ] ) ;
163156 const customStorePath = path . join ( stateDir ( ) , "custom-sessions" , "sessions.json" ) ;
164157 fs . mkdirSync ( path . dirname ( customStorePath ) , { recursive : true } ) ;
165- await saveSessionStore (
158+ fs . writeFileSync (
166159 customStorePath ,
167- {
160+ JSON . stringify ( {
168161 "agent:main:feishu:direct:ou_user" : {
169162 sessionId : "sess-abs" ,
170163 sessionFile : transcriptPath ,
171164 updatedAt : Date . now ( ) ,
172165 } ,
173- } ,
174- { skipMaintenance : true } ,
166+ } ) ,
175167 ) ;
176168
177169 const result = await runFeishuDoctorSequence ( {
@@ -195,7 +187,7 @@ describe("Feishu doctor state repair", () => {
195187 userMessage ( "world" ) ,
196188 userMessage ( "" ) ,
197189 ] ) ;
198- await writeStore ( {
190+ writeStore ( {
199191 "agent:main:feishu:direct:ou_user" : {
200192 sessionId : "sess-separated-blanks" ,
201193 sessionFile : "sess-separated-blanks.jsonl" ,
@@ -238,7 +230,7 @@ describe("Feishu doctor state repair", () => {
238230 sessionHeader ( "sess-ok" ) ,
239231 userMessage ( "hello" ) ,
240232 ] ) ;
241- const targetStorePath = await writeStore ( {
233+ const targetStorePath = writeStore ( {
242234 "agent:main:feishu:direct:ou_user" : {
243235 sessionId : "sess-ok" ,
244236 sessionFile : "sess-ok.jsonl" ,
@@ -294,7 +286,7 @@ describe("Feishu doctor state repair", () => {
294286 userMessage ( "" ) ,
295287 ] ) ;
296288
297- const targetStorePath = await writeStore ( {
289+ const targetStorePath = writeStore ( {
298290 "agent:main:feishu:direct:ou_user" : {
299291 sessionId : "sess-bad" ,
300292 sessionFile : "sess-bad.jsonl" ,
@@ -353,46 +345,14 @@ describe("Feishu doctor state repair", () => {
353345 ) . toBe ( true ) ;
354346 } ) ;
355347
356- it ( "archives unhealthy Feishu sessions from SQLite-only retired agent stores" , async ( ) => {
357- const retiredAgent = "retired" ;
358- const transcriptPath = writeTranscript (
359- "sess-retired-bad" ,
360- [ sessionHeader ( "sess-retired-bad" ) , userMessage ( "" ) , userMessage ( "" ) , userMessage ( "" ) ] ,
361- retiredAgent ,
362- ) ;
363- const targetStorePath = storePath ( retiredAgent ) ;
364- const entries : Record < string , SessionEntry > = {
365- "agent:retired:feishu:direct:ou_user" : {
366- sessionId : "sess-retired-bad" ,
367- sessionFile : "sess-retired-bad.jsonl" ,
368- updatedAt : Date . now ( ) ,
369- } ,
370- } ;
371- await saveSessionStore ( targetStorePath , entries , { skipMaintenance : true } ) ;
372- expect ( fs . existsSync ( targetStorePath ) ) . toBe ( false ) ;
373-
374- const result = await runFeishuDoctorSequence ( {
375- cfg : feishuConfig ( ) ,
376- env : process . env ,
377- shouldRepair : true ,
378- } ) ;
379-
380- expect ( result . warningNotes ) . toEqual ( [ ] ) ;
381- expect ( result . changeNotes . join ( "\n" ) ) . toContain ( "Removed 1 Feishu-scoped session entry" ) ;
382-
383- const store = loadSessionStore ( targetStorePath , { skipCache : true } ) ;
384- expect ( store [ "agent:retired:feishu:direct:ou_user" ] ) . toBeUndefined ( ) ;
385- expect ( fs . existsSync ( transcriptPath ) ) . toBe ( false ) ;
386- } ) ;
387-
388348 it ( "archives unhealthy default-scope sessions when metadata identifies Feishu" , async ( ) => {
389349 const transcriptPath = writeTranscript ( "sess-default-feishu-bad" , [
390350 sessionHeader ( "sess-default-feishu-bad" ) ,
391351 userMessage ( "" ) ,
392352 userMessage ( "" ) ,
393353 userMessage ( "" ) ,
394354 ] ) ;
395- const targetStorePath = await writeStore ( {
355+ const targetStorePath = writeStore ( {
396356 "agent:main:main" : {
397357 sessionId : "sess-default-feishu-bad" ,
398358 sessionFile : "sess-default-feishu-bad.jsonl" ,
0 commit comments