11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import { assert , describe , it } from "@effect/vitest" ;
3+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
4+ import * as Cause from "effect/Cause" ;
35import * as Deferred from "effect/Deferred" ;
46import * as Effect from "effect/Effect" ;
7+ import * as Exit from "effect/Exit" ;
58import * as Fiber from "effect/Fiber" ;
69import * as FileSystem from "effect/FileSystem" ;
710import * as Layer from "effect/Layer" ;
@@ -11,6 +14,7 @@ import * as PlatformError from "effect/PlatformError";
1114import * as Sink from "effect/Sink" ;
1215import * as Stream from "effect/Stream" ;
1316import * as TestClock from "effect/testing/TestClock" ;
17+ import * as ChildProcess from "effect/process/ChildProcess" ;
1418import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1519
1620import * as DesktopEnvironment from "./DesktopEnvironment.ts" ;
@@ -57,6 +61,7 @@ const layerHandler = (
5761 recorded : RecordedRegistration ,
5862 input : {
5963 readonly environment ?: Record < string , unknown > ;
64+ readonly processSpawner ?: ChildProcessSpawner . ChildProcessSpawner [ "Service" ] ;
6065 readonly updateDesktopDatabaseExitCode ?: number ;
6166 readonly updateDesktopDatabaseStalled ?: boolean ;
6267 readonly updateDesktopDatabaseStarted ?: Deferred . Deferred < void > ;
@@ -107,6 +112,7 @@ const layerHandler = (
107112 Layer . succeed (
108113 ChildProcessSpawner . ChildProcessSpawner ,
109114 ChildProcessSpawner . make ( ( command ) => {
115+ if ( input . processSpawner ) return input . processSpawner . spawn ( command ) ;
110116 const childProcess = command as unknown as {
111117 readonly command : string ;
112118 readonly args : ReadonlyArray < string > ;
@@ -370,6 +376,69 @@ describe("DesktopLinuxUrlHandler", () => {
370376 } ) ;
371377 } ) ;
372378
379+ it . effect . skipIf ( HostProcess . Platform . defaultValue ( ) !== "linux" ) (
380+ "continues registration after a timed-out cache helper ignores SIGTERM" ,
381+ ( ) =>
382+ Effect . gen ( function * ( ) {
383+ const realSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
384+ const nodeExecutable = yield * HostProcess . ExecutablePath ;
385+ const recorded = emptyRecording ( ) ;
386+ const spawned = yield * Deferred . make < ChildProcessSpawner . ChildProcessHandle > ( ) ;
387+ const ready = yield * Deferred . make < void > ( ) ;
388+ const processSpawner = ChildProcessSpawner . make ( ( command ) =>
389+ Effect . gen ( function * ( ) {
390+ if ( ! ChildProcess . isStandardCommand ( command ) ) {
391+ return yield * Effect . die ( "expected a single registration command" ) ;
392+ }
393+ recorded . commands . push ( { command : command . command , args : command . args } ) ;
394+ if ( command . command !== "update-desktop-database" ) return mockProcess ( 0 ) ;
395+ const child = yield * realSpawner . spawn (
396+ ChildProcess . make (
397+ nodeExecutable ,
398+ [
399+ "-e" ,
400+ // The watchdog makes the unfixed case exit with a distinguishable code.
401+ "process.on('SIGTERM', () => setTimeout(() => process.exit(99), 5000));" +
402+ "process.stdout.write('ready\\n'); setInterval(() => {}, 2147483647);" ,
403+ ] ,
404+ { ...command . options , stdout : "pipe" } ,
405+ ) ,
406+ ) ;
407+ yield * Deferred . succeed ( spawned , child ) ;
408+ yield * Stream . runHead ( child . stdout ) ;
409+ yield * Deferred . succeed ( ready , undefined ) ;
410+ return child ;
411+ } ) ,
412+ ) ;
413+ const registration = yield * runRegister ( recorded , { processSpawner } ) . pipe (
414+ Effect . forkScoped ,
415+ ) ;
416+ const child = yield * Deferred . await ( spawned ) ;
417+ yield * Effect . addFinalizer ( ( ) =>
418+ child . isRunning . pipe (
419+ Effect . flatMap ( ( running ) =>
420+ running ? child . kill ( { killSignal : "SIGKILL" } ) : Effect . void ,
421+ ) ,
422+ Effect . ignore ,
423+ ) ,
424+ ) ;
425+ yield * Deferred . await ( ready ) ;
426+ yield * TestClock . adjust ( "5 seconds" ) ;
427+ yield * Fiber . join ( registration ) ;
428+
429+ const exit = yield * Effect . exit ( child . exitCode ) ;
430+ if ( Exit . isFailure ( exit ) ) {
431+ assert . include ( Cause . pretty ( exit . cause ) , "SIGKILL" ) ;
432+ } else {
433+ assert . fail ( `cache helper exited with ${ exit . value } instead of being force-killed` ) ;
434+ }
435+ assert . deepEqual (
436+ recorded . commands . map ( ( { command } ) => command ) ,
437+ [ "update-desktop-database" , "xdg-mime" ] ,
438+ ) ;
439+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
440+ ) ;
441+
373442 it . effect ( "continues to xdg-mime when the desktop MIME cache refresh stalls" , ( ) =>
374443 Effect . gen ( function * ( ) {
375444 const recorded = emptyRecording ( ) ;
0 commit comments