@@ -10,6 +10,31 @@ import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest
1010import { deriveConceptTags } from "./concept-vocabulary.js" ;
1111import { isPromotionOriginBlocked } from "./dreaming-consolidation-candidates.js" ;
1212
13+ const fsSafeTestState = vi . hoisted ( ( ) => ( {
14+ beforeRename : undefined as undefined | ( ( params : { tempPath : string } ) => Promise < void > | void ) ,
15+ } ) ) ;
16+
17+ vi . mock ( "openclaw/plugin-sdk/security-runtime" , async ( importOriginal ) => {
18+ const actual = await importOriginal < typeof import ( "openclaw/plugin-sdk/security-runtime" ) > ( ) ;
19+ return {
20+ ...actual ,
21+ replaceFileAtomic : (
22+ params : Parameters < typeof actual . replaceFileAtomic > [ 0 ] ,
23+ ) : ReturnType < typeof actual . replaceFileAtomic > => {
24+ const originalBeforeRename = params . beforeRename ;
25+ return actual . replaceFileAtomic ( {
26+ ...params ,
27+ beforeRename : fsSafeTestState . beforeRename
28+ ? async ( context ) => {
29+ await originalBeforeRename ?.( context ) ;
30+ await fsSafeTestState . beforeRename ?.( context ) ;
31+ }
32+ : originalBeforeRename ,
33+ } ) ;
34+ } ,
35+ } ;
36+ } ) ;
37+
1338vi . mock ( "openclaw/plugin-sdk/memory-host-events" , ( ) => ( {
1439 appendMemoryHostEvent : vi . fn ( async ( ) => { } ) ,
1540} ) ) ;
@@ -59,6 +84,7 @@ describe("short-term promotion", () => {
5984 } ) ;
6085
6186 afterEach ( ( ) => {
87+ fsSafeTestState . beforeRename = undefined ;
6288 vi . restoreAllMocks ( ) ;
6389 } ) ;
6490
@@ -4211,24 +4237,21 @@ describe("short-term promotion", () => {
42114237 } ) ;
42124238
42134239 const truncateAt = 51_200 ;
4214- const originalWriteFile = fs . writeFile . bind ( fs ) ;
4215- vi . spyOn ( fs , "writeFile" ) . mockImplementation ( ( async (
4216- target : Parameters < typeof fs . writeFile > [ 0 ] ,
4217- data : Parameters < typeof fs . writeFile > [ 1 ] ,
4218- options ?: Parameters < typeof fs . writeFile > [ 2 ] ,
4219- ) => {
4220- const targetPath =
4221- typeof target === "string" ? target : target instanceof URL ? target . pathname : "" ;
4222- if ( targetPath && path . basename ( targetPath ) . startsWith ( "MEMORY.md" ) ) {
4223- const text =
4224- typeof data === "string" ? data : Buffer . from ( data as Uint8Array ) . toString ( ) ;
4225- await originalWriteFile ( target , text . slice ( 0 , truncateAt ) , options ) ;
4226- throw Object . assign ( new Error ( "EFBIG: file too large, write" ) , {
4227- code : "EFBIG" ,
4228- } ) ;
4240+ fsSafeTestState . beforeRename = async ( { tempPath } ) => {
4241+ if ( ! path . basename ( tempPath ) . startsWith ( "MEMORY.md.promotion" ) ) {
4242+ return ;
42294243 }
4230- return originalWriteFile ( target , data , options ) ;
4231- } ) as typeof fs . writeFile ) ;
4244+ const handle = await fs . open ( tempPath , "r+" ) ;
4245+ try {
4246+ await handle . truncate ( truncateAt ) ;
4247+ await handle . sync ( ) ;
4248+ } finally {
4249+ await handle . close ( ) ;
4250+ }
4251+ throw Object . assign ( new Error ( "EFBIG: file too large, write" ) , {
4252+ code : "EFBIG" ,
4253+ } ) ;
4254+ } ;
42324255
42334256 await expect (
42344257 applyShortTermPromotions ( {
@@ -4253,9 +4276,7 @@ describe("short-term promotion", () => {
42534276 } ) ,
42544277 ) . resolves . toHaveLength ( 1 ) ;
42554278 expect (
4256- ( await fs . readdir ( workspaceDir ) ) . filter ( ( entry ) =>
4257- entry . startsWith ( "MEMORY.md.promotion" ) ,
4258- ) ,
4279+ ( await fs . readdir ( workspaceDir ) ) . filter ( ( entry ) => entry . includes ( "MEMORY.md.promotion" ) ) ,
42594280 ) . toEqual ( [ ] ) ;
42604281 } ) ;
42614282 } ) ;
0 commit comments