Skip to content

Commit 4f439e9

Browse files
committed
fix(web): retain the cached primary during failed discovery
1 parent 1607f44 commit 4f439e9

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

‎apps/web/src/connection/platform.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
// @vitest-environment jsdom
12
import {
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";
910
import * as Effect from "effect/Effect";
11+
import * as Stream from "effect/Stream";
12+
import { PlatformConnectionSource } from "@t3tools/client-runtime/platform";
1013
import {
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+
});

‎apps/web/src/connection/platform.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ export function secondaryRegistrationsToRetainAfterTopologyRead(
569569
);
570570
}
571571

572-
const layerPlatformConnectionSource = Layer.effect(
572+
export const layerPlatformConnectionSource = Layer.effect(
573573
PlatformConnectionSource.PlatformConnectionSource,
574574
Effect.gen(function* () {
575575
if (isHostedStaticApp() || isLocalEnvironmentDisabled()) {
@@ -589,7 +589,19 @@ const layerPlatformConnectionSource = Layer.effect(
589589
cachedTarget.value.wsBaseUrl === primary.target.target.wsBaseUrl
590590
? [new PrimaryConnectionRegistration({ target: cachedTarget.value })]
591591
: [];
592-
const cacheRef = yield* Ref.make(new Map<string, CachedPlatformRegistration>());
592+
const cacheRef = yield* Ref.make(
593+
new Map<string, CachedPlatformRegistration>(
594+
cachedRegistrations.map((registration) => [
595+
PRIMARY_LOCAL_ENVIRONMENT_ID,
596+
{
597+
signature: `primary|${registration.target.httpBaseUrl}|${registration.target.wsBaseUrl}`,
598+
registration,
599+
// Cached identity is provisional: discover it on every poll until one succeeds.
600+
refreshAtEpochMs: 0,
601+
},
602+
]),
603+
),
604+
);
593605
const rejectedRef = yield* Ref.make(new Map<string, RejectedSecondaryBootstrap>());
594606

595607
const buildPlatformRegistrations = Effect.gen(function* () {
@@ -633,6 +645,10 @@ const layerPlatformConnectionSource = Layer.effect(
633645
const cacheEntry = { signature, registration: built.value };
634646
next.set(PRIMARY_LOCAL_ENVIRONMENT_ID, cacheEntry);
635647
registrations.push(built.value);
648+
} else if (cached?.signature === signature) {
649+
// Discovery failure must not remove a published environment and its drafts.
650+
next.set(PRIMARY_LOCAL_ENVIRONMENT_ID, cached);
651+
registrations.push(cached.registration);
636652
}
637653
}
638654
}

0 commit comments

Comments
 (0)