11import * as NodePath from "@effect/platform-node/NodePath" ;
22import * as TestClock from "effect/testing/TestClock" ;
33import * as NodeEvents from "node:events" ;
4+ import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem" ;
5+ import * as FileSystem from "effect/FileSystem" ;
46import * as HostProcess from "@t3tools/shared/HostProcess" ;
57import { assert , describe , it } from "@effect/vitest" ;
68import * as Effect from "effect/Effect" ;
@@ -36,11 +38,13 @@ vi.mock("electron-updater", async () => {
3638
3739const installer = "/opt/T3 scripts/install-macos-update.sh" ;
3840const archive = "/cache/T3 update $(untouched).zip" ;
41+ const helperPid = 42424 ;
3942
4043beforeEach ( ( ) => {
4144 vi . resetAllMocks ( ) ;
4245 mocks . spawn . mockImplementation ( ( ) => {
4346 const child = Object . assign ( new NodeEvents . EventEmitter ( ) , {
47+ pid : helperPid ,
4448 unref : vi . fn ( ) ,
4549 kill : vi . fn ( ) ,
4650 stdout : Object . assign ( new NodeEvents . EventEmitter ( ) , { destroy : vi . fn ( ) } ) ,
@@ -114,11 +118,20 @@ describe("ElectronUpdater install handoff", () => {
114118 "stops the unready helper on %s without quitting T3" ,
115119 ( failure ) =>
116120 Effect . gen ( function * ( ) {
121+ let markSignalled ! : ( ) => void ;
122+ const signalled = new Promise < void > ( ( resolve ) => {
123+ markSignalled = resolve ;
124+ } ) ;
117125 const child = Object . assign ( new NodeEvents . EventEmitter ( ) , {
118- kill : vi . fn ( ) ,
126+ pid : helperPid ,
127+ kill : vi . fn ( markSignalled ) ,
119128 stdout : Object . assign ( new NodeEvents . EventEmitter ( ) , { destroy : vi . fn ( ) } ) ,
120129 stderr : Object . assign ( new NodeEvents . EventEmitter ( ) , { destroy : vi . fn ( ) } ) ,
121130 } ) ;
131+ using signals = vi . spyOn ( process , "kill" ) . mockImplementation ( ( ) => {
132+ markSignalled ( ) ;
133+ return true ;
134+ } ) ;
122135 const started = new Promise < void > ( ( resolveStarted ) => {
123136 mocks . spawn . mockImplementation ( ( ) => {
124137 queueMicrotask ( resolveStarted ) ;
@@ -127,16 +140,27 @@ describe("ElectronUpdater install handoff", () => {
127140 } ) ;
128141 const fiber = yield * install ( true ) . pipe ( Effect . forkChild ) ;
129142 yield * Effect . promise ( ( ) => started ) ;
130- if ( failure === "interrupt" ) yield * Fiber . interrupt ( fiber ) ;
131- else {
132- yield * TestClock . adjust ( "120 seconds" ) ;
133- assert . equal ( ( yield * Fiber . join ( fiber ) ) . _tag , "Failure" ) ;
143+ const stopping = yield * (
144+ failure === "interrupt" ? Fiber . interrupt ( fiber ) : TestClock . adjust ( "120 seconds" )
145+ ) . pipe ( Effect . forkChild ) ;
146+ yield * Effect . promise ( ( ) => signalled ) ;
147+ try {
148+ assert . deepEqual ( signals . mock . calls , [ [ - helperPid , "SIGTERM" ] ] ) ;
149+ assert . isUndefined ( fiber . pollUnsafe ( ) , "installation waits for its helper to close" ) ;
150+ child . emit ( "exit" , null , "SIGTERM" ) ;
151+ yield * Effect . promise ( ( ) => Promise . resolve ( ) ) ;
152+ assert . isUndefined ( fiber . pollUnsafe ( ) , "exit alone does not finish cleanup" ) ;
153+ } finally {
154+ child . emit ( "close" , null , "SIGTERM" ) ;
134155 }
135- assert . equal ( child . kill . mock . calls . length , 1 ) ;
156+ yield * Fiber . join ( stopping ) ;
157+ if ( failure === "timeout" ) assert . equal ( ( yield * Fiber . join ( fiber ) ) . _tag , "Failure" ) ;
158+ assert . equal ( child . kill . mock . calls . length , 0 ) ;
136159 assert . equal ( mocks . quit . mock . calls . length , 0 ) ;
137160 assert . equal ( mocks . nativeInstall . mock . calls . length , 0 ) ;
138161 } ) ,
139162 ) ;
163+
140164 it . effect . each ( [ "linux" , "win32" ] as const ) (
141165 "keeps the native installer on %s even when an external macOS installer is configured" ,
142166 ( platform ) =>
@@ -179,22 +203,38 @@ describe("ElectronUpdater install handoff", () => {
179203 "helper-rejection" ,
180204 "invalid-readiness" ,
181205 "quit-failure" ,
206+ "before-quit-failure" ,
182207 ] ) ( "keeps T3 open without native fallback after %s" , ( failure ) =>
183208 Effect . gen ( function * ( ) {
209+ using signals = vi . spyOn ( process , "kill" ) . mockImplementation ( ( ) => {
210+ queueMicrotask ( ( ) => mocks . spawn . mock . results [ 0 ] ?. value . emit ( "close" , null , "SIGTERM" ) ) ;
211+ return true ;
212+ } ) ;
213+ if ( failure === "before-quit-failure" )
214+ mocks . beforeQuit . mockImplementation ( ( ) => {
215+ throw new Error ( "quit gate rejected" ) ;
216+ } ) ;
184217 if ( failure === "quit-failure" )
185218 mocks . quit . mockImplementation ( ( ) => {
186219 throw new Error ( "quit rejected" ) ;
187220 } ) ;
188- if ( failure !== "missing-download" && failure !== "quit-failure" ) {
221+ if (
222+ failure !== "missing-download" &&
223+ failure !== "quit-failure" &&
224+ failure !== "before-quit-failure"
225+ ) {
189226 mocks . spawn . mockImplementation ( ( ) => {
190227 const child = Object . assign ( new NodeEvents . EventEmitter ( ) , {
228+ pid : failure === "spawn-failure" ? undefined : helperPid ,
191229 kill : vi . fn ( ) ,
192230 stdout : Object . assign ( new NodeEvents . EventEmitter ( ) , { destroy : vi . fn ( ) } ) ,
193231 stderr : Object . assign ( new NodeEvents . EventEmitter ( ) , { destroy : vi . fn ( ) } ) ,
194232 } ) ;
195233 queueMicrotask ( ( ) => {
196- if ( failure === "spawn-failure" )
234+ if ( failure === "spawn-failure" ) {
197235 child . emit ( "error" , new Error ( "cannot spawn installer" ) ) ;
236+ child . emit ( "close" , - 1 ) ;
237+ }
198238 if ( failure === "helper-rejection" ) {
199239 child . emit ( "exit" , 1 ) ;
200240 child . stderr . emit ( "data" , Buffer . from ( "invalid " ) ) ;
@@ -215,16 +255,99 @@ describe("ElectronUpdater install handoff", () => {
215255 result . failure . stage ,
216256 failure === "missing-download"
217257 ? "validation"
218- : failure === "quit-failure"
258+ : failure === "quit-failure" || failure === "before-quit-failure"
219259 ? "shutdown"
220260 : "handoff" ,
221261 ) ;
222262 if ( failure === "helper-rejection" )
223263 assert . equal ( ( result . failure . cause as Error ) . message , "invalid signature" ) ;
224264 }
225265 assert . equal ( mocks . quit . mock . calls . length , failure === "quit-failure" ? 1 : 0 ) ;
226- assert . equal ( mocks . beforeQuit . mock . calls . length , failure === "quit-failure" ? 1 : 0 ) ;
266+ assert . equal (
267+ mocks . beforeQuit . mock . calls . length ,
268+ failure === "quit-failure" || failure === "before-quit-failure" ? 1 : 0 ,
269+ ) ;
270+ assert . deepEqual (
271+ signals . mock . calls ,
272+ [ "invalid-readiness" , "quit-failure" , "before-quit-failure" ] . includes ( failure )
273+ ? [ [ - helperPid , "SIGTERM" ] ]
274+ : [ ] ,
275+ ) ;
227276 assert . equal ( mocks . nativeInstall . mock . calls . length , 0 ) ;
228277 } ) ,
229278 ) ;
230279} ) ;
280+
281+ describe . runIf ( HostProcess . Platform . defaultValue ( ) !== "win32" ) ( "POSIX installer ownership" , ( ) => {
282+ it . live ( "reaps a foreground tool and runs shell cleanup before cancellation returns" , ( ) =>
283+ Effect . gen ( function * ( ) {
284+ const { spawn } = yield * Effect . promise ( ( ) =>
285+ vi . importActual < typeof import ( "node:child_process" ) > ( "node:child_process" ) ,
286+ ) ;
287+ const fs = yield * FileSystem . FileSystem ;
288+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "installer-group-" } ) ;
289+ yield * fs . writeFileString ( `${ root } /candidate` , "staged payload" ) ;
290+ yield * fs . writeFileString ( `${ root } /untouched` , "unrelated" ) ;
291+ let child : import ( "node:child_process" ) . ChildProcess | undefined ;
292+ let closed : Promise < void > | undefined ;
293+ let didClose = false ;
294+ const started = new Promise < number > ( ( resolve , reject ) => {
295+ mocks . spawn . mockImplementation ( ( ) => {
296+ child = spawn (
297+ "/bin/sh" ,
298+ [
299+ "-c" ,
300+ `set -eu
301+ trap 'rm -f "$FIXTURE_ROOT/candidate"' EXIT
302+ trap 'exit 1' TERM
303+ /bin/sh -c 'printf "tool:%s\\n" "$$" >&2; exec /bin/sleep 60'
304+ exit 0` ,
305+ ] ,
306+ {
307+ detached : true ,
308+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
309+ env : { ...process . env , FIXTURE_ROOT : root } ,
310+ } ,
311+ ) ;
312+ closed = new Promise < void > ( ( resolveClosed ) =>
313+ child ! . once ( "close" , ( ) => {
314+ didClose = true ;
315+ resolveClosed ( ) ;
316+ } ) ,
317+ ) ;
318+ let output = "" ;
319+ child . stderr ! . on ( "data" , ( chunk : Buffer ) => {
320+ output += chunk . toString ( ) ;
321+ const pid = / t o o l : ( \d + ) / . exec ( output ) ?. [ 1 ] ;
322+ if ( pid ) resolve ( Number ( pid ) ) ;
323+ } ) ;
324+ child . once ( "error" , reject ) ;
325+ return child ;
326+ } ) ;
327+ } ) ;
328+ try {
329+ const fiber = yield * install ( true ) . pipe ( Effect . forkChild ) ;
330+ const toolPid = yield * Effect . promise ( ( ) => started ) ;
331+ assert . notEqual ( toolPid , child ! . pid , "fixture has a distinct foreground child" ) ;
332+ yield * Fiber . interrupt ( fiber ) ;
333+ assert . isTrue ( didClose , "cancellation awaits its helper and foreground tool" ) ;
334+ assert . throws ( ( ) => process . kill ( toolPid , 0 ) , / E S R C H / ) ;
335+ assert . isFalse ( yield * fs . exists ( `${ root } /candidate` ) ) ;
336+ assert . equal ( yield * fs . readFileString ( `${ root } /untouched` ) , "unrelated" ) ;
337+ assert . equal ( mocks . quit . mock . calls . length , 0 ) ;
338+ } finally {
339+ if ( ! didClose && child ?. pid !== undefined ) {
340+ yield * Effect . sync ( ( ) => {
341+ try {
342+ process . kill ( - child ! . pid ! , "SIGTERM" ) ;
343+ } catch ( cause ) {
344+ if ( ! ( cause instanceof Error && "code" in cause && cause . code === "ESRCH" ) )
345+ throw cause ;
346+ }
347+ } ) ;
348+ }
349+ if ( closed ) yield * Effect . promise ( ( ) => closed ! ) ;
350+ }
351+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeFileSystem . layer ) ) ,
352+ ) ;
353+ } ) ;
0 commit comments