@@ -4,7 +4,16 @@ import fsSync from "node:fs";
44import fs from "node:fs/promises" ;
55import path from "node:path" ;
66import { resetLogger , setLoggerOverride } from "openclaw/plugin-sdk/runtime-env" ;
7- import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
7+ import {
8+ afterEach ,
9+ beforeAll ,
10+ beforeEach ,
11+ describe ,
12+ expect ,
13+ it ,
14+ type MockInstance ,
15+ vi ,
16+ } from "vitest" ;
817import { logWebSelfId } from "./auth-store.js" ;
918import { enqueueCredsSave } from "./creds-persistence.js" ;
1019import { baileys , getLastSocket , resetBaileysMocks , resetLoadConfigMock } from "./test-helpers.js" ;
@@ -80,56 +89,55 @@ function createTempCaFile(contents: string): string {
8089function mockFsOpenForCredsWrites ( params ?: {
8190 onTempWrite ?: ( filePath : string ) => Promise < void > | void ;
8291} ) {
92+ const open = fs . open . bind ( fs ) ;
8393 const writeFile = fs . writeFile . bind ( fs ) ;
84- const tempHandles : Array < {
94+ type FileHandle = Awaited < ReturnType < typeof fs . open > > ;
95+ const handles : Array < {
8596 filePath : string ;
86- sync : ReturnType < typeof vi . fn > ;
87- close : ReturnType < typeof vi . fn > ;
97+ flags : string | number | undefined ;
98+ mode : number | undefined ;
99+ handle : FileHandle ;
100+ chmod : MockInstance < FileHandle [ "chmod" ] > ;
101+ sync : MockInstance < FileHandle [ "sync" ] > ;
102+ close : MockInstance < FileHandle [ "close" ] > ;
88103 } > = [ ] ;
89- const dirHandles : Array < {
90- filePath : string ;
91- sync : ReturnType < typeof vi . fn > ;
92- close : ReturnType < typeof vi . fn > ;
93- } > = [ ] ;
94- const tempWrites : string [ ] = [ ] ;
104+ const writes : Array < { filePath : string ; data : unknown } > = [ ] ;
95105 const writeFileSpy = vi
96106 . spyOn ( fs , "writeFile" )
97- . mockImplementation ( async ( filePath , data , opts ) => {
98- if ( typeof filePath === "string" && filePath . includes ( ".creds." ) ) {
99- tempWrites . push ( filePath ) ;
100- await params ?. onTempWrite ?.( filePath ) ;
107+ . mockImplementation ( async ( target , data , options ) => {
108+ const observed = handles . find ( ( { handle } ) => handle === target ) ;
109+ if ( observed && path . basename ( observed . filePath ) . startsWith ( ".creds." ) ) {
110+ writes . push ( { filePath : observed . filePath , data } ) ;
111+ await params ?. onTempWrite ?.( observed . filePath ) ;
101112 }
102- return await writeFile ( filePath as never , data as never , opts as never ) ;
113+ return await writeFile ( target , data , options ) ;
103114 } ) ;
104115 const openSpy = vi . spyOn ( fs , "open" ) . mockImplementation ( async ( filePath , flags , mode ) => {
105- if ( typeof filePath === "string" && flags === "r+" && filePath . includes ( ".creds." ) ) {
106- const handle = {
107- filePath,
108- sync : vi . fn ( async ( ) => { } ) ,
109- close : vi . fn ( async ( ) => { } ) ,
110- } ;
111- tempHandles . push ( handle ) ;
112- return handle as never ;
113- }
114- if ( typeof filePath === "string" && flags === "r" ) {
115- const handle = {
116+ const handle = await open ( filePath , flags , mode ) ;
117+ if ( typeof filePath === "string" ) {
118+ handles . push ( {
116119 filePath,
117- sync : vi . fn ( async ( ) => { } ) ,
118- close : vi . fn ( async ( ) => { } ) ,
119- } ;
120- dirHandles . push ( handle ) ;
121- return handle as never ;
120+ flags,
121+ mode : typeof mode === "number" ? mode : undefined ,
122+ handle,
123+ chmod : vi . spyOn ( handle , "chmod" ) ,
124+ sync : vi . spyOn ( handle , "sync" ) ,
125+ close : vi . spyOn ( handle , "close" ) ,
126+ } ) ;
122127 }
123- throw new Error (
124- `unexpected fs.open call: ${ String ( filePath ) } ${ String ( flags ) } ${ String ( mode ) } ` ,
125- ) ;
128+ return handle ;
126129 } ) ;
127130 return {
128- openSpy,
129- writeFileSpy,
130- tempWrites,
131- tempHandles,
132- dirHandles,
131+ handles,
132+ writes,
133+ get tempHandles ( ) {
134+ return handles . filter (
135+ ( { filePath, flags } ) => flags === "wx" && path . basename ( filePath ) . startsWith ( ".creds." ) ,
136+ ) ;
137+ } ,
138+ get dirHandles ( ) {
139+ return handles . filter ( ( { flags } ) => flags === "r" ) ;
140+ } ,
133141 restore ( ) {
134142 writeFileSpy . mockRestore ( ) ;
135143 openSpy . mockRestore ( ) ;
@@ -186,27 +194,6 @@ function requireValue<T>(value: T | undefined, label: string): T {
186194 return value ;
187195}
188196
189- function requireString ( value : unknown , label : string ) : string {
190- if ( typeof value !== "string" ) {
191- throw new Error ( `expected ${ label } ` ) ;
192- }
193- return value ;
194- }
195-
196- function firstWriteFileCall ( writeFileSpy : ReturnType < typeof vi . fn > ) : {
197- data : unknown ;
198- options : { flag ?: string ; mode ?: number } ;
199- path : string ;
200- } {
201- const [ filePath , data , options ] = firstMockCall ( writeFileSpy , "fs.writeFile" ) ;
202- expect ( typeof filePath ) . toBe ( "string" ) ;
203- return {
204- data,
205- options : ( options ?? { } ) as { flag ?: string ; mode ?: number } ,
206- path : filePath as string ,
207- } ;
208- }
209-
210197function expectRuntimeLogContaining (
211198 runtime : { log : ReturnType < typeof vi . fn > } ,
212199 text : string ,
@@ -275,11 +262,12 @@ describe("web session", () => {
275262 passedLogger . trace ( "ignored" ) ;
276263 await emitCredsUpdate ( authDir ) ;
277264
278- const write = firstWriteFileCall ( openMock . writeFileSpy ) ;
279- expect ( write . path ) . toContain ( path . join ( authDir , ".creds." ) ) ;
265+ const write = requireValue ( openMock . writes [ 0 ] , "WhatsApp credential write" ) ;
266+ const tempHandle = requireValue ( openMock . tempHandles [ 0 ] , "WhatsApp credential handle" ) ;
267+ expect ( write . filePath ) . toContain ( path . join ( authDir , ".creds." ) ) ;
280268 expect ( typeof write . data ) . toBe ( "string" ) ;
281- expect ( write . options . mode ) . toBe ( 0o600 ) ;
282- expect ( write . options . flag ) . toBe ( "wx" ) ;
269+ expect ( tempHandle . mode ) . toBe ( 0o600 ) ;
270+ expect ( tempHandle . flags ) . toBe ( "wx" ) ;
283271 openMock . restore ( ) ;
284272 } ) ;
285273
@@ -976,46 +964,55 @@ describe("web session", () => {
976964 ) ;
977965
978966 it ( "writes creds.json atomically via temp file and rename" , async ( ) => {
967+ const authDir = createTempAuthDir ( "openclaw-wa-creds-atomic-write" ) ;
968+ const credsPath = path . join ( authDir , "creds.json" ) ;
979969 const openMock = mockFsOpenForCredsWrites ( ) ;
980- const renameSpy = vi . spyOn ( fs , "rename" ) . mockResolvedValue ( undefined ) ;
981- const rmSpy = vi . spyOn ( fs , "rm" ) . mockResolvedValue ( undefined ) ;
982- const chmodSpy = vi . spyOn ( fs , "chmod" ) . mockResolvedValue ( undefined ) ;
970+ const renameSpy = vi . spyOn ( fs , "rename" ) ;
971+ const rmSpy = vi . spyOn ( fs , "rm" ) ;
983972
984973 try {
985- await writeCredsJsonAtomically ( "/tmp/openclaw-oauth/whatsapp/default" , {
986- 987- } ) ;
974+ await writeCredsJsonAtomically ( authDir , { me :
{ id :
"[email protected] " } } ) ; 988975
989- const write = firstWriteFileCall ( openMock . writeFileSpy ) ;
990- expect ( write . path ) . toContain (
991- path . join ( "/tmp" , "openclaw-oauth" , "whatsapp" , "default" , ".creds." ) ,
992- ) ;
976+ const write = requireValue ( openMock . writes [ 0 ] , "WhatsApp credential write" ) ;
977+ const tempHandle = requireValue ( openMock . tempHandles [ 0 ] , "WhatsApp credential handle" ) ;
978+ expect ( write . filePath ) . toContain ( path . join ( authDir , ".creds." ) ) ;
993979 expect ( typeof write . data ) . toBe ( "string" ) ;
994- expect ( write . options . mode ) . toBe ( 0o600 ) ;
995- expect ( write . options . flag ) . toBe ( "wx" ) ;
980+ expect ( tempHandle . mode ) . toBe ( 0o600 ) ;
981+ expect ( tempHandle . flags ) . toBe ( "wx" ) ;
996982 expect ( openMock . tempHandles ) . toHaveLength ( 1 ) ;
997- expect ( openMock . tempHandles [ 0 ] ?. sync ) . toHaveBeenCalledTimes ( 1 ) ;
998- expect ( openMock . tempHandles [ 0 ] ?. close ) . toHaveBeenCalledTimes ( 1 ) ;
999- expect ( renameSpy ) . toHaveBeenCalledTimes ( 1 ) ;
983+ expect ( tempHandle . chmod ) . toHaveBeenCalledWith ( 0o600 ) ;
984+ expect ( tempHandle . sync ) . toHaveBeenCalledTimes ( 1 ) ;
985+ expect ( tempHandle . close ) . toHaveBeenCalledTimes ( 1 ) ;
986+ expect ( renameSpy ) . toHaveBeenCalledExactlyOnceWith ( tempHandle . filePath , credsPath ) ;
1000987 expect ( rmSpy ) . not . toHaveBeenCalled ( ) ;
1001- expect ( chmodSpy ) . toHaveBeenCalledWith (
1002- path . join ( "/tmp" , "openclaw-oauth" , "whatsapp" , "default" , "creds.json" ) ,
1003- 0o600 ,
1004- ) ;
1005988 expect ( openMock . dirHandles ) . toHaveLength ( 1 ) ;
1006989 expect ( openMock . dirHandles [ 0 ] ?. sync ) . toHaveBeenCalledTimes ( 1 ) ;
1007- const writePath = openMock . tempHandles [ 0 ] ?. filePath ;
1008- const [ , renameTarget ] = firstMockCall ( renameSpy , "creds atomic rename" ) ;
1009- expect ( typeof writePath ) . toBe ( "string" ) ;
1010- expect ( writePath ) . toContain ( ".creds." ) ;
1011- expect ( requireString ( renameTarget , "creds rename target path" ) ) . toContain (
1012- path . join ( "/tmp" , "openclaw-oauth" , "whatsapp" , "default" , "creds.json" ) ,
1013- ) ;
990+ expect ( openMock . dirHandles [ 0 ] ?. close ) . toHaveBeenCalledTimes ( 1 ) ;
991+ expect ( JSON . parse ( fsSync . readFileSync ( credsPath , "utf8" ) ) ) . toEqual ( {
992+ 993+ } ) ;
994+ expect ( fsSync . statSync ( credsPath ) . mode & 0o777 ) . toBe ( 0o600 ) ;
995+ if ( process . platform !== "win32" ) {
996+ const parentHandle = requireValue (
997+ openMock . handles . find (
998+ ( { filePath, flags } ) => filePath === authDir && typeof flags === "number" ,
999+ ) ,
1000+ "pinned WhatsApp credential directory" ,
1001+ ) ;
1002+ expect ( parentHandle . flags ) . toBe (
1003+ fsSync . constants . O_RDONLY |
1004+ fsSync . constants . O_DIRECTORY |
1005+ fsSync . constants . O_NOFOLLOW |
1006+ fsSync . constants . O_NONBLOCK ,
1007+ ) ;
1008+ expect ( parentHandle . chmod ) . toHaveBeenCalledWith ( 0o700 ) ;
1009+ expect ( parentHandle . close ) . toHaveBeenCalledTimes ( 1 ) ;
1010+ expect ( fsSync . statSync ( authDir ) . mode & 0o777 ) . toBe ( 0o700 ) ;
1011+ }
10141012 } finally {
10151013 openMock . restore ( ) ;
10161014 renameSpy . mockRestore ( ) ;
10171015 rmSpy . mockRestore ( ) ;
1018- chmodSpy . mockRestore ( ) ;
10191016 }
10201017 } ) ;
10211018
0 commit comments