@@ -319,6 +319,97 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
319319 } ) ,
320320 ) ;
321321
322+ it . effect ( "keeps a pending update and its backup while owned, then resumes the trial" , ( ) =>
323+ Effect . gen ( function * ( ) {
324+ const fs = yield * FileSystem . FileSystem ;
325+ const path = yield * Path . Path ;
326+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-service-reclaim-update-" } ) ;
327+ const statePath = path . join ( root , "runtime" , "service-state.json" ) ;
328+ const ownerPath = path . join ( root , "userdata" , "server-owner.sqlite" ) ;
329+ const databasePath = path . join ( root , "userdata" , "state.sqlite" ) ;
330+ const startsPath = path . join ( root , "starts.log" ) ;
331+ yield * fs . makeDirectory ( path . dirname ( ownerPath ) , { recursive : true } ) ;
332+ yield * fs . writeFileString ( databasePath , "before update" ) ;
333+ for ( const version of [ "1.0.0" , "1.1.0" ] ) {
334+ yield * writeFakeRuntime (
335+ fs ,
336+ path ,
337+ path . join ( root , "runtime" , "versions" , version ) ,
338+ [
339+ `const db = new (require("node:sqlite").DatabaseSync)(${ JSON . stringify ( ownerPath ) } );` ,
340+ `try { db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;"); }` ,
341+ `catch { process.exit(${ SERVER_EXIT_CODE_STATE_DIR_OWNED } ); }` ,
342+ "const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);" ,
343+ `require("node:fs").appendFileSync(${ JSON . stringify ( startsPath ) } , context.childVersion + "\\n");` ,
344+ 'process.on("message", (message) => message.type === "committed" && process.send("committed"));' ,
345+ 'if (context.update?.status === "pending") process.send({ type: "prepared", updateId: context.update.id });' ,
346+ "setInterval(() => {}, 1_000);" ,
347+ ] . join ( "\n" ) ,
348+ ) ;
349+ }
350+ yield * Effect . promise ( ( ) =>
351+ writeServiceState ( statePath , {
352+ protocol : SERVICE_LAUNCHER_PROTOCOL ,
353+ activeVersion : "1.0.0" ,
354+ update : {
355+ id : "update-1" ,
356+ fromVersion : "1.0.0" ,
357+ targetVersion : "1.1.0" ,
358+ dbPath : databasePath ,
359+ status : "pending" ,
360+ } ,
361+ } ) ,
362+ ) ;
363+ using otherOwner = new NodeSqlite . DatabaseSync ( ownerPath ) ;
364+ otherOwner . exec ( "BEGIN EXCLUSIVE;" ) ;
365+ const refused = Promise . withResolvers < void > ( ) ;
366+ const committed = Promise . withResolvers < void > ( ) ;
367+ yield * Effect . acquireRelease (
368+ Effect . sync ( ( ) =>
369+ vi . spyOn ( process . stderr , "write" ) . mockImplementation ( ( chunk ) => {
370+ if ( String ( chunk ) . includes ( "owns this T3 home" ) ) refused . resolve ( ) ;
371+ return true ;
372+ } ) ,
373+ ) ,
374+ ( spy ) => Effect . sync ( ( ) => spy . mockRestore ( ) ) ,
375+ ) ;
376+ yield * Effect . acquireRelease (
377+ Effect . sync ( ( ) => {
378+ spawnHook . current = ( child ) =>
379+ child . on ( "message" , ( message ) => message === "committed" && committed . resolve ( ) ) ;
380+ } ) ,
381+ ( ) =>
382+ Effect . sync ( ( ) => {
383+ spawnHook . current = undefined ;
384+ } ) ,
385+ ) ;
386+ const launcher = new Launcher (
387+ root ,
388+ yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ,
389+ { stateDirOwnerPollMs : 10 } ,
390+ ) ;
391+ const running = launcher . run ( ) ;
392+ yield * Effect . promise ( ( ) => refused . promise ) ;
393+ const idle = yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ;
394+ assert . strictEqual ( idle . activeVersion , "1.0.0" ) ;
395+ assert . strictEqual ( idle . update ?. status , "pending" ) ;
396+ assert . isTrue ( yield * fs . exists ( path . join ( root , "runtime" , "db-backup" , "update-1" ) ) ) ;
397+
398+ // The other owner runs the target version and migrates before it releases the home.
399+ yield * fs . writeFileString ( databasePath , "migrated by 1.1.0" ) ;
400+ otherOwner . close ( ) ;
401+ yield * Effect . promise ( ( ) => committed . promise ) ;
402+ yield * Effect . promise ( ( ) => launcher . stop ( "SIGTERM" ) ) ;
403+ yield * Effect . promise ( ( ) => running ) ;
404+
405+ const state = yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ;
406+ assert . strictEqual ( state . activeVersion , "1.1.0" ) ;
407+ assert . strictEqual ( state . update ?. status , "committed" ) ;
408+ assert . strictEqual ( yield * fs . readFileString ( startsPath ) , "1.1.0\n" ) ;
409+ assert . strictEqual ( yield * fs . readFileString ( databasePath ) , "migrated by 1.1.0" ) ;
410+ } ) ,
411+ ) ;
412+
322413 it . effect ( "treats the T3 home as owned while the lock or a pre-lock server holds it" , ( ) =>
323414 Effect . gen ( function * ( ) {
324415 const fs = yield * FileSystem . FileSystem ;
0 commit comments