@@ -204,4 +204,89 @@ it.layer(NodeServices.layer)("secret publication", (it) => {
204204 if ( ( yield * HostProcessPlatform ) !== "win32" ) assert . equal ( stat . mode & 0o777 , 0o600 ) ;
205205 } ) . pipe ( Effect . provide ( configLayer ( ) ) ) ,
206206 ) ;
207+
208+ it . effect ( "flushes the published directory entry before reporting success" , ( ) =>
209+ Effect . gen ( function * ( ) {
210+ const fs = yield * FileSystem . FileSystem ;
211+ const config = yield * ServerConfig . ServerConfig ;
212+ const flushing = yield * Deferred . make < void > ( ) ;
213+ const resume = yield * Deferred . make < void > ( ) ;
214+ let complete = false ;
215+ const store = yield * ServerSecretStore . make . pipe (
216+ Effect . provideService ( FileSystem . FileSystem , {
217+ ...fs ,
218+ open : ( path , options ) =>
219+ fs . open ( path , options ) . pipe (
220+ Effect . map ( ( file ) =>
221+ path === config . secretsDir
222+ ? {
223+ ...file ,
224+ sync : Deferred . succeed ( flushing , undefined ) . pipe (
225+ Effect . andThen ( Deferred . await ( resume ) ) ,
226+ ) ,
227+ }
228+ : file ,
229+ ) ,
230+ ) ,
231+ } ) ,
232+ ) ;
233+ const writer = yield * store . create ( "signing-key" , new Uint8Array ( 32 ) ) . pipe (
234+ Effect . tap ( ( ) =>
235+ Effect . sync ( ( ) => {
236+ complete = true ;
237+ } ) ,
238+ ) ,
239+ Effect . forkChild ,
240+ ) ;
241+ yield * Deferred . await ( flushing ) ;
242+ assert . isFalse ( complete ) ;
243+ assert . lengthOf ( Option . getOrThrow ( yield * store . get ( "signing-key" ) ) , 32 ) ;
244+ yield * Deferred . succeed ( resume , undefined ) ;
245+ yield * Fiber . join ( writer ) ;
246+ assert . isTrue ( complete ) ;
247+ } ) . pipe ( Effect . provide ( configLayer ( ) ) ) ,
248+ ) ;
249+
250+ for ( const [ platform , code , succeeds ] of [
251+ [ "win32" , "EPERM" , true ] ,
252+ [ "linux" , "EPERM" , false ] ,
253+ [ "win32" , "EIO" , false ] ,
254+ [ "linux" , "EIO" , false ] ,
255+ ] as const ) {
256+ it . effect ( `handles directory flush ${ code } on ${ platform } without losing published bytes` , ( ) =>
257+ Effect . gen ( function * ( ) {
258+ const fs = yield * FileSystem . FileSystem ;
259+ const config = yield * ServerConfig . ServerConfig ;
260+ const failure = PlatformError . systemError ( {
261+ _tag : code === "EPERM" ? "PermissionDenied" : "Unknown" ,
262+ module : "FileSystem" ,
263+ method : "sync" ,
264+ pathOrDescriptor : config . secretsDir ,
265+ cause : Object . assign ( new Error ( "Injected directory sync failure" ) , { code } ) ,
266+ } ) ;
267+ const store = yield * ServerSecretStore . make . pipe (
268+ Effect . provideService ( HostProcessPlatform , platform ) ,
269+ Effect . provideService ( FileSystem . FileSystem , {
270+ ...fs ,
271+ open : ( path , options ) =>
272+ fs
273+ . open ( path , options )
274+ . pipe (
275+ Effect . map ( ( file ) =>
276+ path === config . secretsDir ? { ...file , sync : Effect . fail ( failure ) } : file ,
277+ ) ,
278+ ) ,
279+ } ) ,
280+ ) ;
281+ const result = yield * store . create ( "signing-key" , new Uint8Array ( 32 ) ) . pipe ( Effect . result ) ;
282+ assert . equal ( result . _tag , succeeds ? "Success" : "Failure" ) ;
283+ if ( result . _tag === "Failure" ) {
284+ assert . instanceOf ( result . failure , ServerSecretStore . SecretStorePersistError ) ;
285+ assert . equal ( result . failure . cause , failure ) ;
286+ }
287+ assert . lengthOf ( Option . getOrThrow ( yield * store . get ( "signing-key" ) ) , 32 ) ;
288+ assert . deepEqual ( yield * fs . readDirectory ( config . secretsDir ) , [ "signing-key.bin" ] ) ;
289+ } ) . pipe ( Effect . provide ( configLayer ( ) ) ) ,
290+ ) ;
291+ }
207292} ) ;
0 commit comments