1+ // @vitest -environment jsdom
12import {
23 AuthStandardClientScopes ,
34 EnvironmentId ,
45 PRIMARY_LOCAL_ENVIRONMENT_ID ,
56 type DesktopBridge ,
67 type DesktopSshEnvironmentTarget ,
78} from "@t3tools/contracts" ;
8- import { describe , expect , it } from "@effect/vitest" ;
9+ import { afterEach , describe , expect , it , vi } from "@effect/vitest" ;
910import * as Effect from "effect/Effect" ;
11+ import * as Stream from "effect/Stream" ;
12+ import { PlatformConnectionSource } from "@t3tools/client-runtime/platform" ;
1013import {
1114 ConnectionBlockedError ,
1215 ConnectionTransientError ,
@@ -18,6 +21,7 @@ import {
1821 isRejectedBootstrapCredentialError ,
1922 isRejectedSecondaryBootstrap ,
2023 nextRejectedSecondaryBootstrap ,
24+ layerPlatformConnectionSource ,
2125 primaryRegistrationToRetainAfterTopologyRead ,
2226 provisionDesktopSshEnvironment ,
2327 readPrimaryEnvironmentTargetResult ,
@@ -305,3 +309,45 @@ describe("primary topology cache", () => {
305309 ) . toBeUndefined ( ) ;
306310 } ) ;
307311} ) ;
312+
313+ describe ( "cached primary discovery" , ( ) => {
314+ afterEach ( ( ) => {
315+ localStorage . clear ( ) ;
316+ vi . unstubAllGlobals ( ) ;
317+ } ) ;
318+
319+ it . live ( "retains the early primary through failed discovery and retries discovery" , ( ) => {
320+ const target = {
321+ _tag : "PrimaryConnectionTarget" ,
322+ environmentId : EnvironmentId . make ( "cached-primary" ) ,
323+ label : "Cached primary" ,
324+ httpBaseUrl : `${ window . location . origin } /` ,
325+ wsBaseUrl : `${ window . location . origin . replace ( "http" , "ws" ) } /` ,
326+ } ;
327+ localStorage . setItem ( "t3code:desktop-primary-target:v1" , JSON . stringify ( target ) ) ;
328+ const fetchMock = vi
329+ . fn ( )
330+ . mockResolvedValueOnce ( new Response ( null , { status : 503 } ) )
331+ . mockImplementation ( ( ) =>
332+ Promise . resolve (
333+ Response . json ( {
334+ environmentId : target . environmentId ,
335+ label : "Discovered primary" ,
336+ platform : { os : "linux" , arch : "x64" } ,
337+ serverVersion : "0.0.0-test" ,
338+ capabilities : { repositoryIdentity : true } ,
339+ } ) ,
340+ ) ,
341+ ) ;
342+ vi . stubGlobal ( "fetch" , fetchMock ) ;
343+
344+ return Effect . gen ( function * ( ) {
345+ const source = yield * PlatformConnectionSource . PlatformConnectionSource ;
346+ const emissions = yield * source . registrations . pipe ( Stream . take ( 3 ) , Stream . runCollect ) ;
347+ expect (
348+ emissions . map ( ( registrations ) => registrations . map ( ( entry ) => entry . target . label ) ) ,
349+ ) . toEqual ( [ [ "Cached primary" ] , [ "Cached primary" ] , [ "Discovered primary" ] ] ) ;
350+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
351+ } ) . pipe ( Effect . provide ( layerPlatformConnectionSource ) ) ;
352+ } ) ;
353+ } ) ;
0 commit comments