11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import { describe , expect , it } from "@effect/vitest" ;
3+ import * as Duration from "effect/Duration" ;
34import * as Effect from "effect/Effect" ;
45import * as FileSystem from "effect/FileSystem" ;
56import * as Layer from "effect/Layer" ;
67import * as Option from "effect/Option" ;
78import * as Path from "effect/Path" ;
89import * as PlatformError from "effect/PlatformError" ;
10+ import * as TestClock from "effect/testing/TestClock" ;
911import * as Tracer from "effect/Tracer" ;
1012import {
1113 HttpClient ,
@@ -14,7 +16,7 @@ import {
1416 type HttpClientRequest ,
1517} from "effect/unstable/http" ;
1618
17- import { EnvironmentId } from "@t3tools/contracts" ;
19+ import { DESKTOP_UPDATE_RESTART_MARKER_FILE , EnvironmentId } from "@t3tools/contracts" ;
1820import { RelayClientTracer } from "@t3tools/shared/relayTracing" ;
1921import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts" ;
2022import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
@@ -285,6 +287,20 @@ describe("releaseManagedTunnelOnShutdown", () => {
285287 ) ;
286288 } ) ;
287289
290+ // Writes the marker the desktop app leaves just before it stops its backend
291+ // to install an update, and returns when it was written.
292+ const writeDesktopUpdateRestartMarker = Effect . gen ( function * ( ) {
293+ const fs = yield * FileSystem . FileSystem ;
294+ const path = yield * Path . Path ;
295+ const config = yield * ServerConfigModule . ServerConfig ;
296+ const runtimeDir = path . join ( config . baseDir , "runtime" ) ;
297+ const markerPath = path . join ( runtimeDir , DESKTOP_UPDATE_RESTART_MARKER_FILE ) ;
298+ yield * fs . makeDirectory ( runtimeDir , { recursive : true } ) ;
299+ yield * fs . writeFileString ( markerPath , "" ) ;
300+ const { mtime } = yield * fs . stat ( markerPath ) ;
301+ return Option . getOrThrow ( mtime ) . getTime ( ) ;
302+ } ) ;
303+
288304 const provideReleaseHarness =
289305 ( harness : ReleaseHarness ) =>
290306 < A , E , R > ( effect : Effect . Effect < A , E , R > ) =>
@@ -454,6 +470,38 @@ describe("releaseManagedTunnelOnShutdown", () => {
454470 } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
455471 } ) ;
456472
473+ it . effect ( "keeps the tunnel once when the desktop app restarts it for an update" , ( ) => {
474+ const { store, values } = makeMemorySecretStore ( managedLinkSecrets ) ;
475+ const applyConfigCalls : Array < unknown > = [ ] ;
476+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
477+
478+ return Effect . gen ( function * ( ) {
479+ yield * TestClock . setTime ( yield * writeDesktopUpdateRestartMarker ) ;
480+
481+ expect ( yield * releaseManagedTunnelOnShutdown ( ) ) . toBe ( false ) ;
482+ expect ( requests ) . toEqual ( [ ] ) ;
483+ expect ( values . has ( CLOUD_ENDPOINT_RUNTIME_CONFIG ) ) . toBe ( true ) ;
484+
485+ // The shutdown consumed the marker, so a later quit releases the tunnel.
486+ expect ( yield * releaseManagedTunnelOnShutdown ( ) ) . toBe ( true ) ;
487+ expect ( requests ) . toHaveLength ( 1 ) ;
488+ } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
489+ } ) ;
490+
491+ it . effect ( "releases the tunnel when the desktop update marker is stale" , ( ) => {
492+ const { store } = makeMemorySecretStore ( managedLinkSecrets ) ;
493+ const applyConfigCalls : Array < unknown > = [ ] ;
494+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
495+
496+ return Effect . gen ( function * ( ) {
497+ const writtenAt = yield * writeDesktopUpdateRestartMarker ;
498+ yield * TestClock . setTime ( writtenAt + Duration . toMillis ( Duration . minutes ( 2 ) ) ) ;
499+
500+ expect ( yield * releaseManagedTunnelOnShutdown ( ) ) . toBe ( true ) ;
501+ expect ( requests ) . toHaveLength ( 1 ) ;
502+ } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
503+ } ) ;
504+
457505 it . effect ( "still releases a pending update when the launcher is stopping" , ( ) => {
458506 // `t3 service uninstall` or `systemctl stop` during the pending window:
459507 // the launcher writes its stop marker before signalling the child, so no
0 commit comments