@@ -4,6 +4,8 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
44import * as ConfigProvider from "effect/ConfigProvider" ;
55import * as Duration from "effect/Duration" ;
66import * as Effect from "effect/Effect" ;
7+ import * as Exit from "effect/Exit" ;
8+ import * as Fiber from "effect/Fiber" ;
79import * as FileSystem from "effect/FileSystem" ;
810import * as Layer from "effect/Layer" ;
911import * as Path from "effect/Path" ;
@@ -473,7 +475,14 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
473475 protocol : SERVICE_LAUNCHER_PROTOCOL ,
474476 activeVersion : "1.2.4" ,
475477 } ) ;
476- expect ( yield * fs . readFileString ( plan . unitPath ) ) . toContain ( "versions/1.2.4/t3" ) ;
478+ expect ( BootService . bootServiceProgramOf ( yield * fs . readFileString ( plan . unitPath ) ) ) . toBe (
479+ path . join (
480+ baseDir ,
481+ "runtime" ,
482+ ...( platform === "darwin" ? [ "launcher" ] : [ "versions" , "1.2.4" ] ) ,
483+ "t3" ,
484+ ) ,
485+ ) ;
477486 expect (
478487 commands . filter (
479488 ( command ) =>
@@ -855,6 +864,103 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
855864 } ) ,
856865 ) ;
857866
867+ it . effect ( "finishes publishing the launcher before honoring interruption" , ( ) =>
868+ Effect . gen ( function * ( ) {
869+ const { service, makeService, fs, baseDir } = yield * makeHarness ( "darwin" ) ;
870+ const path = yield * Path . Path ;
871+ const launcherDir = path . join ( baseDir , "runtime" , "launcher" ) ;
872+ yield * service . install ( ) ;
873+ const interruptingFs = FileSystem . FileSystem . of ( {
874+ ...fs ,
875+ rename : ( from , to ) =>
876+ fs
877+ . rename ( from , to )
878+ . pipe (
879+ Effect . andThen (
880+ to === `${ launcherDir } .prev`
881+ ? Effect . withFiber ( ( fiber ) => Effect . sync ( ( ) => fiber . interruptUnsafe ( ) ) )
882+ : Effect . void ,
883+ ) ,
884+ ) ,
885+ } ) ;
886+ const updated = yield * makeService ( undefined , "1.2.4" ) . pipe (
887+ Effect . provideService ( FileSystem . FileSystem , interruptingFs ) ,
888+ ) ;
889+ const installing = yield * Effect . forkChild ( updated . install ( { start : false } ) ) ;
890+ expect ( Exit . isFailure ( yield * Fiber . await ( installing ) ) ) . toBe ( true ) ;
891+ expect ( yield * fs . readFileString ( path . join ( launcherDir , ".install-complete" ) ) ) . toBe ( "1.2.4\n" ) ;
892+ expect ( yield * fs . exists ( path . join ( launcherDir , "t3" ) ) ) . toBe ( true ) ;
893+ expect ( yield * fs . exists ( `${ launcherDir } .next` ) ) . toBe ( false ) ;
894+ } ) ,
895+ ) ;
896+
897+ it . effect ( "recovers the previous launcher before staging after an interrupted swap" , ( ) =>
898+ Effect . gen ( function * ( ) {
899+ const { service, makeService, fs, baseDir } = yield * makeHarness ( "darwin" ) ;
900+ const path = yield * Path . Path ;
901+ const launcherDir = path . join ( baseDir , "runtime" , "launcher" ) ;
902+ yield * service . install ( ) ;
903+ yield * fs . rename ( launcherDir , `${ launcherDir } .prev` ) ;
904+ const failingFs = FileSystem . FileSystem . of ( {
905+ ...fs ,
906+ copy : ( from , to , options ) =>
907+ to === `${ launcherDir } .next`
908+ ? Effect . fail (
909+ PlatformError . systemError ( {
910+ _tag : "PermissionDenied" ,
911+ module : "FileSystem" ,
912+ method : "copy" ,
913+ pathOrDescriptor : to ,
914+ } ) ,
915+ )
916+ : fs . copy ( from , to , options ) ,
917+ } ) ;
918+ const updated = yield * makeService ( undefined , "1.2.4" ) . pipe (
919+ Effect . provideService ( FileSystem . FileSystem , failingFs ) ,
920+ ) ;
921+ expect ( ( yield * updated . install ( ) . pipe ( Effect . flip ) ) . _tag ) . toBe ( "BootServiceInstallError" ) ;
922+ expect ( yield * fs . readFileString ( path . join ( launcherDir , ".install-complete" ) ) ) . toBe ( "1.2.3\n" ) ;
923+ expect ( ( yield * service . status ) . current ) . toBe ( true ) ;
924+ yield * ( yield * makeService ( undefined , "1.2.4" ) ) . install ( ) ;
925+ expect ( yield * fs . readFileString ( path . join ( launcherDir , ".install-complete" ) ) ) . toBe ( "1.2.4\n" ) ;
926+ } ) ,
927+ ) ;
928+
929+ it . effect ( "retries launcher cleanup after uninstall removed the plist" , ( ) =>
930+ Effect . gen ( function * ( ) {
931+ const { service, makeService, fs, baseDir } = yield * makeHarness ( "darwin" ) ;
932+ const path = yield * Path . Path ;
933+ const launcherDir = path . join ( baseDir , "runtime" , "launcher" ) ;
934+ yield * service . install ( ) ;
935+ yield * fs . makeDirectory ( `${ launcherDir } .next` ) ;
936+ yield * fs . makeDirectory ( `${ launcherDir } .prev` ) ;
937+ const failingFs = FileSystem . FileSystem . of ( {
938+ ...fs ,
939+ remove : ( target , options ) =>
940+ target === launcherDir
941+ ? Effect . fail (
942+ PlatformError . systemError ( {
943+ _tag : "PermissionDenied" ,
944+ module : "FileSystem" ,
945+ method : "remove" ,
946+ pathOrDescriptor : target ,
947+ } ) ,
948+ )
949+ : fs . remove ( target , options ) ,
950+ } ) ;
951+ const failing = yield * makeService ( ) . pipe (
952+ Effect . provideService ( FileSystem . FileSystem , failingFs ) ,
953+ ) ;
954+ expect ( ( yield * failing . uninstall . pipe ( Effect . flip ) ) . _tag ) . toBe ( "BootServiceInstallError" ) ;
955+ expect ( ( yield * service . status ) . installed ) . toBe ( false ) ;
956+ expect ( yield * service . uninstall ) . toBe ( true ) ;
957+ for ( const directory of [ launcherDir , `${ launcherDir } .next` , `${ launcherDir } .prev` ] ) {
958+ expect ( yield * fs . exists ( directory ) ) . toBe ( false ) ;
959+ }
960+ expect ( yield * service . uninstall ) . toBe ( false ) ;
961+ } ) ,
962+ ) ;
963+
858964 it . effect ( "puts the previous launcher copy back when the swap fails on macOS" , ( ) =>
859965 Effect . gen ( function * ( ) {
860966 const { service, makeService, fs, baseDir } = yield * makeHarness ( "darwin" ) ;
@@ -888,6 +994,42 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
888994 } ) ,
889995 ) ;
890996
997+ it . effect ( "retains recovery when publication and rollback both fail" , ( ) =>
998+ Effect . gen ( function * ( ) {
999+ const { service, makeService, fs, baseDir } = yield * makeHarness ( "darwin" ) ;
1000+ const path = yield * Path . Path ;
1001+ const launcherDir = path . join ( baseDir , "runtime" , "launcher" ) ;
1002+ yield * service . install ( ) ;
1003+ const failingFs = FileSystem . FileSystem . of ( {
1004+ ...fs ,
1005+ rename : ( from , to ) =>
1006+ to === launcherDir
1007+ ? Effect . fail (
1008+ PlatformError . systemError ( {
1009+ _tag : "PermissionDenied" ,
1010+ module : "FileSystem" ,
1011+ method : "rename" ,
1012+ pathOrDescriptor : from ,
1013+ } ) ,
1014+ )
1015+ : fs . rename ( from , to ) ,
1016+ } ) ;
1017+ const updated = yield * makeService ( undefined , "1.2.4" ) . pipe (
1018+ Effect . provideService ( FileSystem . FileSystem , failingFs ) ,
1019+ ) ;
1020+ expect ( ( yield * updated . install ( ) . pipe ( Effect . flip ) ) . _tag ) . toBe ( "BootServiceInstallError" ) ;
1021+ expect ( yield * fs . exists ( launcherDir ) ) . toBe ( false ) ;
1022+ expect ( yield * fs . readFileString ( path . join ( `${ launcherDir } .prev` , ".install-complete" ) ) ) . toBe (
1023+ "1.2.3\n" ,
1024+ ) ;
1025+ expect ( yield * fs . exists ( `${ launcherDir } .next` ) ) . toBe ( false ) ;
1026+ const retry = yield * makeService ( undefined , "1.2.4" ) ;
1027+ yield * retry . install ( ) ;
1028+ expect ( ( yield * retry . status ) . current ) . toBe ( true ) ;
1029+ expect ( yield * fs . exists ( `${ launcherDir } .prev` ) ) . toBe ( false ) ;
1030+ } ) ,
1031+ ) ;
1032+
8911033 it . effect ( "finishes the install when the retired launcher copy cannot be removed" , ( ) =>
8921034 Effect . gen ( function * ( ) {
8931035 const { service, makeService, fs, baseDir } = yield * makeHarness ( "darwin" ) ;
0 commit comments