@@ -2,6 +2,7 @@ import { NodeHttpServer } from "@effect/platform-node";
22import { type ExecutionEnvironmentDescriptor , OrchestratorMcpFailure } from "@t3tools/contracts" ;
33import { expect , it } from "@effect/vitest" ;
44import * as Context from "effect/Context" ;
5+ import * as Deferred from "effect/Deferred" ;
56import * as Duration from "effect/Duration" ;
67import * as Effect from "effect/Effect" ;
78import * as Layer from "effect/Layer" ;
@@ -279,6 +280,53 @@ const serveRaw = <E, R>(
279280 ) ,
280281 ) ;
281282
283+ it . effect ( "lists stored outcomes without waiting for an unreachable peer" , ( ) =>
284+ Effect . scoped (
285+ Effect . gen ( function * ( ) {
286+ const b = yield * serveB ( box ) ;
287+ const a = yield * makeA ( laptop ) ;
288+ const pairing = yield * b . auth . issuePairingCredential ( ) ;
289+ const linked = yield * a . links . link ( {
290+ url : b . url ,
291+ pairingCode : pairing . credential ,
292+ access : "auto" ,
293+ } ) ;
294+ const probed = yield * Deferred . make < void > ( ) ;
295+ const release = yield * Deferred . make < void > ( ) ;
296+ const asleep = yield * serveRaw ( ( ) =>
297+ Deferred . succeed ( probed , undefined ) . pipe (
298+ Effect . andThen ( Deferred . await ( release ) ) ,
299+ Effect . as ( HttpServerResponse . empty ( { status : 503 } ) ) ,
300+ ) ,
301+ ) ;
302+ yield * a . sql `UPDATE peer_environment_links SET urls_json = ${ JSON . stringify ( [ asleep ] ) } ` ;
303+
304+ const listed = yield * Effect . raceFirst (
305+ a . links . list ,
306+ Deferred . await ( probed ) . pipe ( Effect . as ( "blocked on peer" ) ) ,
307+ ) ;
308+ yield * Deferred . succeed ( release , undefined ) ;
309+ expect ( listed ) . toEqual ( [ { ...linked , urls : [ asleep ] , status : "reachable" } ] ) ;
310+
311+ yield * a . links . recordOutcome ( box . environmentId , { error : "Peer did not answer" } ) ;
312+ expect ( yield * a . links . list ) . toMatchObject ( [
313+ {
314+ status : "unreachable" ,
315+ lastError : "Peer did not answer" ,
316+ lastReachedAt : linked . lastReachedAt ,
317+ } ,
318+ ] ) ;
319+ yield * a . links . recordOutcome ( box . environmentId , { error : null } ) ;
320+ expect ( yield * a . links . list ) . toMatchObject ( [ { status : "reachable" , lastError : null } ] ) ;
321+ yield * a . sql `UPDATE peer_environment_links SET last_reached_at = NULL` ;
322+ expect ( yield * a . links . list ) . toMatchObject ( [ { status : "unreachable" } ] ) ;
323+ yield * TestClock . adjust ( Duration . days ( 31 ) ) ;
324+ expect ( yield * a . links . list ) . toMatchObject ( [ { status : "expired" } ] ) ;
325+ expect ( yield * Deferred . isDone ( probed ) ) . toBe ( false ) ;
326+ } ) ,
327+ ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
328+ ) ;
329+
282330it . effect ( "never follows a peer's redirect with the pairing code" , ( ) =>
283331 Effect . scoped (
284332 Effect . gen ( function * ( ) {
@@ -335,6 +383,10 @@ it.effect("never sends a link's token to an address that stops answering as that
335383 expect ( failed . message ) . toContain ( "Box did not answer" ) ;
336384 expect ( yield * Ref . get ( b . bearers ) ) . toEqual ( [ ] ) ;
337385 expect ( ( yield * a . links . list ) . map ( ( link ) => link . status ) ) . toEqual ( [ "unreachable" ] ) ;
386+
387+ yield * Ref . set ( b . descriptor , box ) ;
388+ yield * probe ( a ) ;
389+ expect ( ( yield * a . links . list ) . map ( ( link ) => link . status ) ) . toEqual ( [ "reachable" ] ) ;
338390 } ) ,
339391 ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
340392) ;
0 commit comments