@@ -10,8 +10,13 @@ import * as Layer from "effect/Layer";
1010import * as Path from "effect/Path" ;
1111import * as Result from "effect/Result" ;
1212import * as Schema from "effect/Schema" ;
13+ import * as Sink from "effect/Sink" ;
1314import * as Stream from "effect/Stream" ;
15+ import * as Deferred from "effect/Deferred" ;
16+ import * as Fiber from "effect/Fiber" ;
1417import * as Ndjson from "effect/encoding/Ndjson" ;
18+ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
19+ import * as TestClock from "effect/testing/TestClock" ;
1520import * as AcpErrors from "effect-acp/errors" ;
1621import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
1722
@@ -639,6 +644,90 @@ it.layer(NodeServices.layer)("Antigravity profile preparation", (it) => {
639644 } ) ,
640645 ) ;
641646
647+ // Stands in for the helper process: it prints the expected marker, then exits after
648+ // `exitDelay`. `started` completes once the exit wait begins, so the TestClock is only
649+ // advanced after the preflight timer and the delay are both registered.
650+ const preflightHelper = ( exitDelay : Effect . Effect < void > ) => {
651+ const spawns = { count : 0 } ;
652+ const started = Deferred . makeUnsafe < void > ( ) ;
653+ const spawner = ChildProcessSpawner . make ( ( ) =>
654+ Effect . sync ( ( ) => {
655+ spawns . count += 1 ;
656+ return ChildProcessSpawner . makeHandle ( {
657+ pid : ChildProcessSpawner . ProcessId ( 1 ) ,
658+ exitCode : Deferred . succeed ( started , undefined ) . pipe (
659+ Effect . andThen ( exitDelay ) ,
660+ Effect . as ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
661+ ) ,
662+ isRunning : Effect . succeed ( false ) ,
663+ kill : ( ) => Effect . void ,
664+ unref : Effect . succeed ( Effect . void ) ,
665+ stdin : Sink . drain ,
666+ stdout : Stream . empty ,
667+ stderr : Stream . encodeText (
668+ Stream . make (
669+ `${ ANTIGRAVITY_AUTH_BROWSER_MARKER } "https://example.invalid/t3-antigravity-browser-preflight"\n` ,
670+ ) ,
671+ ) ,
672+ all : Stream . empty ,
673+ getInputFd : ( ) => Sink . drain ,
674+ getOutputFd : ( ) => Stream . empty ,
675+ } ) ;
676+ } ) ,
677+ ) ;
678+ return { spawner, spawns, started } ;
679+ } ;
680+
681+ const prepareWithHelper = Effect . fn ( function * (
682+ runtimeExecutablePath : string ,
683+ spawner : ChildProcessSpawner . ChildProcessSpawner [ "Service" ] ,
684+ ) {
685+ const fs = yield * FileSystem . FileSystem ;
686+ const path = yield * Path . Path ;
687+ const directory = yield * fs . makeTempDirectoryScoped ( ) ;
688+ return yield * prepareAntigravityProfile ( {
689+ profileDirectory : path . join ( directory , "profile" ) ,
690+ userHome : path . join ( directory , "home" ) ,
691+ runtimeExecutablePath,
692+ platform : "linux" ,
693+ baseEnv : { } ,
694+ } ) . pipe ( Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ) ;
695+ } ) ;
696+
697+ it . effect ( "waits out a slow preflight and skips it once the helper was verified" , ( ) =>
698+ Effect . gen ( function * ( ) {
699+ const helper = preflightHelper ( Effect . sleep ( "20 seconds" ) ) ;
700+ const fiber = yield * prepareWithHelper ( "/fake/slow-node" , helper . spawner ) . pipe (
701+ Effect . forkChild ,
702+ ) ;
703+ yield * Deferred . await ( helper . started ) ;
704+ yield * TestClock . adjust ( "20 seconds" ) ;
705+ yield * Fiber . join ( fiber ) ;
706+
707+ yield * prepareWithHelper ( "/fake/slow-node" , helper . spawner ) ;
708+ expect ( helper . spawns . count ) . toBe ( 1 ) ;
709+ } ) ,
710+ ) ;
711+
712+ it . effect ( "fails a hung preflight after 30 seconds and verifies again on the next open" , ( ) =>
713+ Effect . gen ( function * ( ) {
714+ const hung = preflightHelper ( Effect . never ) ;
715+ const fiber = yield * prepareWithHelper ( "/fake/hung-node" , hung . spawner ) . pipe (
716+ Effect . result ,
717+ Effect . forkChild ,
718+ ) ;
719+ yield * Deferred . await ( hung . started ) ;
720+ yield * TestClock . adjust ( "29 seconds" ) ;
721+ expect ( fiber . pollUnsafe ( ) ) . toBeUndefined ( ) ;
722+ yield * TestClock . adjust ( "1 second" ) ;
723+ expect ( Result . isFailure ( yield * Fiber . join ( fiber ) ) ) . toBe ( true ) ;
724+
725+ const recovered = preflightHelper ( Effect . void ) ;
726+ yield * prepareWithHelper ( "/fake/hung-node" , recovered . spawner ) ;
727+ expect ( recovered . spawns . count ) . toBe ( 1 ) ;
728+ } ) ,
729+ ) ;
730+
642731 it . effect ( "rejects Python BROWSER delimiter collisions before starting a helper" , ( ) =>
643732 Effect . gen ( function * ( ) {
644733 const fs = yield * FileSystem . FileSystem ;
0 commit comments