1+ import { AsyncLocalStorage } from "node:async_hooks" ;
2+ import nativeFs from "node:fs" ;
13import fs from "node:fs/promises" ;
4+ import { syncBuiltinESMExports } from "node:module" ;
25import path from "node:path" ;
36import { DatabaseSync } from "node:sqlite" ;
47import type { OpenClawConfig } from "openclaw/plugin-sdk/memory-core-host-engine-foundation" ;
58import { MEMORY_INDEX_CHUNKS_TABLE } from "openclaw/plugin-sdk/memory-core-host-engine-storage" ;
69import { createOpenClawTestState } from "openclaw/plugin-sdk/test-state" ;
7- import { describe , expect , it } from "vitest" ;
10+ import { describe , expect , it , vi } from "vitest" ;
811import {
912 configureMemoryCoreDreamingStateForTests ,
1013 resetMemoryCoreDreamingStateForTests ,
@@ -21,6 +24,30 @@ describe("memory watchers on the real filesystem", () => {
2124 async ( operation ) => {
2225 const state = await createOpenClawTestState ( { label : "memory-watch-filesystem" } ) ;
2326 const initialWatchers = activeFilesystemWatchers ( ) ;
27+ const turnContext = new AsyncLocalStorage < string > ( ) ;
28+ const pendingInputContext = new AsyncLocalStorage < string > ( ) ;
29+ const watcherContexts : Array < { turn ?: string ; pendingInput ?: string } > = [ ] ;
30+ const timerContexts : typeof watcherContexts = [ ] ;
31+ const originalWatch = nativeFs . watch ;
32+ const watchObserver = vi . spyOn ( nativeFs , "watch" ) . mockImplementation ( ( ...args ) => {
33+ watcherContexts . push ( {
34+ turn : turnContext . getStore ( ) ,
35+ pendingInput : pendingInputContext . getStore ( ) ,
36+ } ) ;
37+ return originalWatch ( ...args ) ;
38+ } ) ;
39+ syncBuiltinESMExports ( ) ;
40+ const originalSetTimeout = globalThis . setTimeout ;
41+ const timerObserver = vi . spyOn ( globalThis , "setTimeout" ) . mockImplementation ( ( ...args ) => {
42+ // Observe the real startup pressure check and filesystem debounce timers.
43+ if ( args [ 1 ] === 10_000 || args [ 1 ] === 1500 ) {
44+ timerContexts . push ( {
45+ turn : turnContext . getStore ( ) ,
46+ pendingInput : pendingInputContext . getStore ( ) ,
47+ } ) ;
48+ }
49+ return originalSetTimeout ( ...args ) ;
50+ } ) ;
2451 let manager : MemoryIndexManager | null = null ;
2552 let index : DatabaseSync | undefined ;
2653 try {
@@ -42,7 +69,14 @@ describe("memory watchers on the real filesystem", () => {
4269 } ,
4370 } ,
4471 } ;
45- manager = await MemoryIndexManager . get ( { cfg, agentId : "main" } ) ;
72+ manager = await turnContext . run ( "opening turn" , ( ) =>
73+ pendingInputContext . run ( "accepted input" , async ( ) => {
74+ const opened = await MemoryIndexManager . get ( { cfg, agentId : "main" } ) ;
75+ expect ( turnContext . getStore ( ) ) . toBe ( "opening turn" ) ;
76+ expect ( pendingInputContext . getStore ( ) ) . toBe ( "accepted input" ) ;
77+ return opened ;
78+ } ) ,
79+ ) ;
4680 if ( ! manager ) {
4781 throw new Error ( "memory manager unavailable" ) ;
4882 }
@@ -93,6 +127,11 @@ describe("memory watchers on the real filesystem", () => {
93127 ] ) ;
94128 expect ( await activeManager . search ( "Amethyst" ) ) . toEqual ( [ ] ) ;
95129 expect ( await activeManager . search ( "Cobalt" ) ) . toEqual ( [ ] ) ;
130+ expect ( watcherContexts . length ) . toBeGreaterThan ( 0 ) ;
131+ expect ( timerContexts . length ) . toBeGreaterThan ( 0 ) ;
132+ for ( const context of [ ...watcherContexts , ...timerContexts ] ) {
133+ expect ( context ) . toEqual ( { turn : undefined , pendingInput : undefined } ) ;
134+ }
96135
97136 index . close ( ) ;
98137 index = undefined ;
@@ -101,6 +140,9 @@ describe("memory watchers on the real filesystem", () => {
101140 } finally {
102141 index ?. close ( ) ;
103142 await manager ?. close ( ) ;
143+ timerObserver . mockRestore ( ) ;
144+ watchObserver . mockRestore ( ) ;
145+ syncBuiltinESMExports ( ) ;
104146 resetMemoryCoreDreamingStateForTests ( ) ;
105147 await state . cleanup ( ) ;
106148 }
0 commit comments