@@ -49,6 +49,19 @@ function runCredentialWriter(env: Record<string, string>): Promise<ChildResult>
4949 } ) ;
5050}
5151
52+ async function waitForFiles ( paths : string [ ] , timeoutMs = 5_000 ) : Promise < void > {
53+ const deadline = Date . now ( ) + timeoutMs ;
54+
55+ while ( true ) {
56+ const missing = paths . filter ( path => ! existsSync ( path ) ) ;
57+ if ( missing . length === 0 ) return ;
58+ if ( Date . now ( ) >= deadline ) {
59+ throw new Error ( `Timed out waiting for files: ${ missing . join ( ', ' ) } ` ) ;
60+ }
61+ await new Promise ( resolveDelay => setTimeout ( resolveDelay , 5 ) ) ;
62+ }
63+ }
64+
5265describe ( 'credentials cross-process writes' , ( ) => {
5366 beforeAll ( ( ) => {
5467 execNpm ( [ 'run' , 'build' ] , { cwd : REPO_ROOT , stdio : 'pipe' } ) ;
@@ -81,7 +94,9 @@ describe('credentials cross-process writes', () => {
8194 CRED_API_KEY : 'sk-child-timeout' ,
8295 CRED_PATH : join ( tmpRoot , 'credentials' ) ,
8396 CRED_PROFILE : 'child-timeout' ,
97+ CRED_POLL_MS : '1' ,
8498 CRED_START_PATH : startPath ,
99+ CRED_START_TIMEOUT_MS : '100' ,
85100 } ) ;
86101
87102 expect ( result ) . toMatchObject ( {
@@ -130,16 +145,18 @@ describe('credentials cross-process writes', () => {
130145 apiKey : `sk-child-${ index } ` ,
131146 profile : `child-${ index } ` ,
132147 } ) ) ;
133- const children = profiles . map ( ( { apiKey, profile } ) =>
148+ const readyPaths = profiles . map ( ( { profile } ) => join ( tmpRoot , `${ profile } .ready` ) ) ;
149+ const children = profiles . map ( ( { apiKey, profile } , index ) =>
134150 runCredentialWriter ( {
135151 CRED_API_KEY : apiKey ,
136152 CRED_PATH : credentialsPath ,
137153 CRED_PROFILE : profile ,
154+ CRED_READY_PATH : readyPaths [ index ] ! ,
138155 CRED_START_PATH : startPath ,
139156 } ) ,
140157 ) ;
141158
142- await new Promise ( resolveDelay => setTimeout ( resolveDelay , 50 ) ) ;
159+ await waitForFiles ( readyPaths ) ;
143160 writeFileSync ( startPath , 'go' ) ;
144161
145162 const results = await Promise . all ( children ) ;
0 commit comments