@@ -9,12 +9,15 @@ import * as FileSystem from "effect/FileSystem";
99import * as Path from "effect/Path" ;
1010import { vi } from "vite-plus/test" ;
1111
12+ import { renderBootServicePlist , renderBootServiceUnit } from "./cloud/bootService.ts" ;
1213import {
1314 databaseBackupBlocker ,
1415 Launcher ,
1516 PREPARED_TIMEOUT_MS ,
1617 type BackupSpaceProbe ,
1718 readServiceState ,
19+ repointServiceUnit ,
20+ type ServiceUnit ,
1821 writeServiceState ,
1922} from "./serviceLauncher.ts" ;
2023import {
@@ -107,6 +110,35 @@ it("rejects contradictory service state", () => {
107110 ) ;
108111} ) ;
109112
113+ const unitPlan = ( root : string , version : string ) => ( {
114+ program : [ `${ root } /runtime/versions/${ version } /t3` , "__service-launcher" ] ,
115+ baseDir : root ,
116+ logPath : `${ root } /logs/boot-service.log` ,
117+ unitPath : `${ root } /unit/t3code.service` ,
118+ } ) ;
119+
120+ it ( "points a unit or launch agent at another runtime and leaves the rest alone" , ( ) => {
121+ for ( const root of [ "/home/user/.t3" , "/home/user name/.t3" , "/home/100%/.t3" ] ) {
122+ assert . equal (
123+ repointServiceUnit ( renderBootServiceUnit ( unitPlan ( root , "1.0.0" ) ) , "1.1.0" ) ,
124+ renderBootServiceUnit ( unitPlan ( root , "1.1.0" ) ) ,
125+ ) ;
126+ }
127+ const root = "/Users/user/.t3 & co" ;
128+ const plistOptions = {
129+ homeDir : "/Users/user" ,
130+ environmentPath : `/usr/bin:${ root } /runtime/versions/1.0.0` ,
131+ } ;
132+ assert . equal (
133+ repointServiceUnit ( renderBootServicePlist ( unitPlan ( root , "1.0.0" ) , plistOptions ) , "1.1.0" ) ,
134+ renderBootServicePlist ( unitPlan ( root , "1.1.0" ) , plistOptions ) ,
135+ ) ;
136+ for ( const version of [ "1.1.0" , "1.2.0" ] ) {
137+ const unit = renderBootServiceUnit ( unitPlan ( root , version ) ) ;
138+ assert . equal ( repointServiceUnit ( unit , "1.1.0" ) , unit ) ;
139+ }
140+ } ) ;
141+
110142// A pinned runtime is an executable at <versionDir>/t3. The tests stand one up
111143// as a Node shebang script so the launcher spawns it the way it spawns the
112144// real single-executable, IPC channel included.
@@ -128,6 +160,28 @@ const writeFakeRuntime = (
128160 return entryPath ;
129161 } ) ;
130162
163+ // The unit the launcher repoints after a commit, written where its plan says.
164+ const fakeServiceUnit = (
165+ fs : FileSystem . FileSystem ,
166+ path : Path . Path ,
167+ root : string ,
168+ version : string ,
169+ ) =>
170+ Effect . gen ( function * ( ) {
171+ const plan = unitPlan ( root , version ) ;
172+ yield * fs . makeDirectory ( path . dirname ( plan . unitPath ) , { recursive : true } ) ;
173+ yield * fs . writeFileString ( plan . unitPath , renderBootServiceUnit ( plan ) ) ;
174+ const unit : ServiceUnit & { reloads : number } = {
175+ path : plan . unitPath ,
176+ reloads : 0 ,
177+ reload : ( ) => {
178+ unit . reloads += 1 ;
179+ return Promise . resolve ( ) ;
180+ } ,
181+ } ;
182+ return unit ;
183+ } ) ;
184+
131185it . layer ( NodeServices . layer ) ( "service state persistence" , ( it ) => {
132186 it . effect ( "durably replaces and strictly reads one state document" , ( ) =>
133187 Effect . gen ( function * ( ) {
@@ -298,8 +352,15 @@ if (context.update?.status === "pending") {
298352 activeVersion : "1.0.0" ,
299353 } ) ,
300354 ) ;
355+ const unit = yield * fakeServiceUnit ( fs , path , root , "1.0.0" ) ;
301356
302- const launcher = new Launcher ( root , yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ) ;
357+ const launcher = new Launcher (
358+ root ,
359+ yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ,
360+ {
361+ serviceUnit : unit ,
362+ } ,
363+ ) ;
303364 yield * Effect . promise ( ( ) =>
304365 launcher . run ( ) . then (
305366 ( ) => Promise . reject ( new Error ( "launcher unexpectedly completed" ) ) ,
@@ -310,6 +371,77 @@ if (context.update?.status === "pending") {
310371 const state = yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ;
311372 assert . equal ( state . activeVersion , "1.1.0" ) ;
312373 assert . equal ( state . update ?. status , "committed" ) ;
374+ assert . equal (
375+ yield * fs . readFileString ( unit . path ) ,
376+ renderBootServiceUnit ( unitPlan ( root , "1.1.0" ) ) ,
377+ ) ;
378+ assert . equal ( unit . reloads , 1 ) ;
379+ } ) ,
380+ ) ;
381+
382+ it . effect ( "commits even when the unit cannot be repointed" , ( ) =>
383+ Effect . gen ( function * ( ) {
384+ const fs = yield * FileSystem . FileSystem ;
385+ const path = yield * Path . Path ;
386+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-service-launcher-unit-" } ) ;
387+ const statePath = path . join ( root , "runtime" , "service-state.json" ) ;
388+ const databasePath = path . join ( root , "userdata" , "state.sqlite" ) ;
389+ yield * fs . makeDirectory ( path . dirname ( databasePath ) , { recursive : true } ) ;
390+ yield * fs . writeFileString ( databasePath , "before trial" ) ;
391+ const encodedDatabasePath = JSON . stringify ( databasePath ) ;
392+ const childSource = `
393+ const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);
394+ if (context.update?.status === "pending") {
395+ process.send({ type: "prepared", updateId: context.update.id });
396+ process.on("message", (message) => {
397+ if (message.type === "committed") process.exit(0);
398+ });
399+ } else if (context.update === undefined) {
400+ process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${ encodedDatabasePath } });
401+ setInterval(() => {}, 1_000);
402+ } else {
403+ process.exit(0);
404+ }
405+ ` ;
406+ for ( const version of [ "1.0.0" , "1.1.0" ] ) {
407+ yield * writeFakeRuntime (
408+ fs ,
409+ path ,
410+ path . join ( root , "runtime" , "versions" , version ) ,
411+ childSource ,
412+ ) ;
413+ }
414+ yield * Effect . promise ( ( ) =>
415+ writeServiceState ( statePath , {
416+ protocol : SERVICE_LAUNCHER_PROTOCOL ,
417+ activeVersion : "1.0.0" ,
418+ } ) ,
419+ ) ;
420+ let reloads = 0 ;
421+ const launcher = new Launcher (
422+ root ,
423+ yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ,
424+ {
425+ serviceUnit : {
426+ path : path . join ( root , "unit" , "missing.service" ) ,
427+ reload : ( ) => {
428+ reloads += 1 ;
429+ return Promise . resolve ( ) ;
430+ } ,
431+ } ,
432+ } ,
433+ ) ;
434+ yield * Effect . promise ( ( ) =>
435+ launcher . run ( ) . then (
436+ ( ) => Promise . reject ( new Error ( "launcher unexpectedly completed" ) ) ,
437+ ( ) => Promise . resolve ( ) ,
438+ ) ,
439+ ) ;
440+
441+ const state = yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ;
442+ assert . equal ( state . activeVersion , "1.1.0" ) ;
443+ assert . equal ( state . update ?. status , "committed" ) ;
444+ assert . equal ( reloads , 0 ) ;
313445 } ) ,
314446 ) ;
315447
@@ -348,8 +480,15 @@ if (context.update?.status === "pending") {
348480 activeVersion : "1.0.0" ,
349481 } ) ,
350482 ) ;
483+ const unit = yield * fakeServiceUnit ( fs , path , root , "1.0.0" ) ;
351484
352- const launcher = new Launcher ( root , yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ) ;
485+ const launcher = new Launcher (
486+ root ,
487+ yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ,
488+ {
489+ serviceUnit : unit ,
490+ } ,
491+ ) ;
353492 yield * Effect . promise ( ( ) =>
354493 launcher . run ( ) . then (
355494 ( ) => Promise . reject ( new Error ( "launcher unexpectedly completed" ) ) ,
@@ -360,6 +499,11 @@ if (context.update?.status === "pending") {
360499 const state = yield * Effect . promise ( ( ) => readServiceState ( statePath ) ) ;
361500 assert . equal ( state . activeVersion , "1.0.0" ) ;
362501 assert . equal ( state . update ?. status , "rolled-back" ) ;
502+ assert . equal (
503+ yield * fs . readFileString ( unit . path ) ,
504+ renderBootServiceUnit ( unitPlan ( root , "1.0.0" ) ) ,
505+ ) ;
506+ assert . equal ( unit . reloads , 0 ) ;
363507 assert . equal (
364508 state . update ?. status === "rolled-back" ? state . update . reason : undefined ,
365509 "invalid-prepared" ,
0 commit comments