@@ -10,12 +10,14 @@ import * as Layer from "effect/Layer";
1010import * as Option from "effect/Option" ;
1111import * as Path from "effect/Path" ;
1212import * as TestClock from "effect/testing/TestClock" ;
13+ import * as TestConsole from "effect/testing/TestConsole" ;
1314import { HttpClient , HttpClientResponse } from "effect/http" ;
1415import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1516import { afterEach , vi } from "vite-plus/test" ;
1617
18+ import packageJson from "../../package.json" with { type : "json" } ;
1719import * as BootService from "../cloud/bootService.ts" ;
18- import { prunePinnedRuntimes } from "../cloud/pinnedRuntime.ts" ;
20+ import { pinnedRuntimePaths , prunePinnedRuntimes } from "../cloud/pinnedRuntime.ts" ;
1921import * as ManagedPolicy from "../policy/ManagedPolicy.ts" ;
2022import * as ProcessRunner from "../processRunner.ts" ;
2123import {
@@ -434,3 +436,96 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
434436 } ) . pipe ( Effect . scoped , Effect . provideService ( HostProcess . Platform , "linux" ) ) ,
435437 ) ;
436438} ) ;
439+
440+ // Without terminals, update skips its restart prompt whatever runs the test.
441+ const withoutTerminals = Effect . acquireRelease (
442+ Effect . sync ( ( ) =>
443+ [ process . stdin , process . stdout ] . map ( ( stream ) => {
444+ const descriptor = Object . getOwnPropertyDescriptor ( stream , "isTTY" ) ;
445+ Object . defineProperty ( stream , "isTTY" , { configurable : true , value : false } ) ;
446+ return { stream, descriptor } ;
447+ } ) ,
448+ ) ,
449+ ( saved ) =>
450+ Effect . sync ( ( ) => {
451+ for ( const { stream, descriptor } of saved ) {
452+ if ( descriptor === undefined ) Reflect . deleteProperty ( stream , "isTTY" ) ;
453+ else Object . defineProperty ( stream , "isTTY" , descriptor ) ;
454+ }
455+ } ) ,
456+ ) ;
457+
458+ it . layer ( NodeServices . layer ) ( "t3 update with a background service" , ( it ) => {
459+ it . effect ( "names the version a service with a pending restart still runs" , ( ) =>
460+ Effect . gen ( function * ( ) {
461+ yield * withoutTerminals ;
462+ const fs = yield * FileSystem . FileSystem ;
463+ const path = yield * Path . Path ;
464+ const baseDir = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-update-" } ) ;
465+ const version = packageJson . version ;
466+ const runtime = pinnedRuntimePaths ( path , baseDir , version , "linux" ) ;
467+ yield * fs . makeDirectory ( runtime . versionDir , { recursive : true } ) ;
468+ yield * fs . writeFileString ( runtime . entryPath , "" ) ;
469+ yield * fs . writeFileString ( runtime . sentinelPath , `${ version } \n` ) ;
470+ const unitPath = path . join ( baseDir , "t3code.service" ) ;
471+ const logPath = path . join ( baseDir , "boot-service.log" ) ;
472+ // An earlier `t3 update` pointed the service at this version without restarting it.
473+ const service = Layer . mock ( BootService . BootService , {
474+ status : Effect . succeed < BootService . BootServiceStatus > ( {
475+ supported : true ,
476+ installed : true ,
477+ current : false ,
478+ installedVersion : version ,
479+ installedBaseDir : baseDir ,
480+ problems : [ "restart-pending" ] ,
481+ unitPath,
482+ logPath,
483+ } ) ,
484+ install : ( ) => Effect . succeed ( { program : [ runtime . entryPath ] , baseDir, unitPath, logPath } ) ,
485+ } ) ;
486+ vi . spyOn ( BootService , "layer" ) . mockReturnValue ( service ) ;
487+ const runner = ProcessRunner . ProcessRunner . of ( {
488+ run : ( input ) =>
489+ input . args ?. includes ( "--version" ) === true
490+ ? unrelatedLsof
491+ . run ( input )
492+ . pipe ( Effect . map ( ( result ) => ( { ...result , stdout : `t3 v${ version } \n` } ) ) )
493+ : unrelatedLsof . run ( input ) ,
494+ } ) ;
495+
496+ const output = yield * runUpdate ( {
497+ baseDir,
498+ logsDir : baseDir ,
499+ serverRuntimeStatePath : path . join ( baseDir , "server-runtime.json" ) ,
500+ channel : undefined ,
501+ requestedVersion : version ,
502+ allowDowngrade : false ,
503+ assumeYes : false ,
504+ } ) . pipe (
505+ Effect . andThen ( TestConsole . logLines ) ,
506+ Effect . map ( ( lines ) => lines . join ( "\n" ) ) ,
507+ Effect . provide (
508+ Layer . mergeAll (
509+ service ,
510+ Layer . succeed ( ProcessRunner . ProcessRunner , runner ) ,
511+ ManagedPolicy . layerTest ( ) ,
512+ Layer . succeed (
513+ HttpClient . HttpClient ,
514+ HttpClient . make ( ( ) => Effect . die ( "unexpected release lookup" ) ) ,
515+ ) ,
516+ Layer . fresh ( TestConsole . layer ) ,
517+ ) ,
518+ ) ,
519+ ) ;
520+
521+ assert . include (
522+ output ,
523+ `Background service still running the version before ${ version } . Run \`t3 service restart\` when you are ready to switch it to ${ version } .` ,
524+ ) ;
525+ } ) . pipe (
526+ Effect . scoped ,
527+ Effect . provideService ( HostProcess . Platform , "linux" ) ,
528+ Effect . provideService ( HostProcess . Environment , { PATH : "" } ) ,
529+ ) ,
530+ ) ;
531+ } ) ;
0 commit comments