The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
fix(web): refresh stale desktop endpoints when Connections reopens (#16026)
Co-authored-by: Arthur Katcher <192321283+arthurkatcher@users.noreply.github.com>
Arthur Katcher committed
Oct 18, 2026 at 21:23 UTC
7686c57311323291b8c462db9990e23ef120c940
2 files changed
+155
-49
apps/web/src/state/desktopNetworkAccess.test.ts
+153
-48
@@ -1,8 +1,10 @@
1
+import { it } from "@effect/vitest";
2
import type { AdvertisedEndpoint, DesktopServerExposureState } from "@t3tools/contracts";
3
import * as Cause from "effect/Cause";
4
+import * as Effect from "effect/Effect";
5
import * as AsyncResult from "effect/reactivity/AsyncResult";
6
import { AtomRegistry } from "effect/reactivity";
5
-import { describe, expect, it, vi } from "vite-plus/test";
7
+import { describe, expect, vi } from "vite-plus/test";
8
9
import { createDesktopNetworkAccessStateAtom } from "./desktopNetworkAccess";
10
@@ -15,43 +17,144 @@ const serverExposureState: DesktopServerExposureState = {
17
};
18
19
const advertisedEndpoints: ReadonlyArray<AdvertisedEndpoint> = [];
20
+const tailscaleEndpoint: AdvertisedEndpoint = {
21
+ id: "tailscale-ip:100.74.126.34",
22
+ httpBaseUrl: "http://100.74.126.34:37737/",
23
+ wsBaseUrl: "ws://100.74.126.34:37737/",
24
+ label: "Tailscale IP",
25
+ provider: { id: "tailscale", kind: "private-network", label: "Tailscale", isAddon: true },
26
+ reachability: "private-network",
27
+ status: "available",
28
+ source: "desktop-addon",
29
+ compatibility: { hostedHttpsApp: "mixed-content-blocked", desktopApp: "compatible" },
30
+};
31
const serverExposureLoadCause = new Error("exposure failed");
32
const advertisedEndpointsLoadCause = new Error("endpoints failed");
33
34
describe("desktopNetworkAccessState", () => {
22
- it("retains the loaded snapshot when the settings screen remounts", async () => {
23
- const getServerExposureState = vi.fn(async () => serverExposureState);
24
- const getAdvertisedEndpoints = vi.fn(async () => advertisedEndpoints);
25
- const atom = createDesktopNetworkAccessStateAtom(() => ({
26
- getAdvertisedEndpoints,
27
- getServerExposureState,
28
- }));
29
- const registry = AtomRegistry.make();
35
+ it.effect("retains a fresh snapshot after the settings screen unmounts and remounts", () =>
36
+ Effect.gen(function* () {
37
+ const now = vi.spyOn(Date, "now").mockReturnValue(1_000_000);
38
+ const getServerExposureState = vi.fn(async () => serverExposureState);
39
+ const getAdvertisedEndpoints = vi.fn(async () => advertisedEndpoints);
40
+ const atom = createDesktopNetworkAccessStateAtom(() => ({
41
+ getAdvertisedEndpoints,
42
+ getServerExposureState,
43
+ }));
44
+ const registry = AtomRegistry.make();
45
31
- const unmount = registry.mount(atom);
32
- await vi.waitFor(() => {
33
- expect(AsyncResult.value(registry.get(atom))).toEqual(
34
- expect.objectContaining({ _tag: "Some" }),
35
- );
36
- });
37
- unmount();
46
+ let unmount = registry.mount(atom);
47
+ try {
48
+ yield* AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true });
49
+ unmount();
50
+ yield* Effect.yieldNow;
51
+ now.mockReturnValue(1_029_999);
52
+
53
+ unmount = registry.mount(atom);
54
+ expect(AsyncResult.value(registry.get(atom))).toEqual(
55
+ expect.objectContaining({
56
+ _tag: "Some",
57
+ value: { advertisedEndpoints, serverExposureState },
58
+ }),
59
+ );
60
+ expect(getServerExposureState).toHaveBeenCalledTimes(1);
61
+ expect(getAdvertisedEndpoints).toHaveBeenCalledTimes(1);
62
+ } finally {
63
+ unmount();
64
+ registry.dispose();
65
+ now.mockRestore();
66
+ }
67
+ }),
68
+ );
69
+
70
+ it.effect("keeps the previous snapshot while an explicit refresh loads current endpoints", () =>
71
+ Effect.gen(function* () {
72
+ let resolveEndpoints!: (value: ReadonlyArray<AdvertisedEndpoint>) => void;
73
+ const nextEndpoints = new Promise<ReadonlyArray<AdvertisedEndpoint>>((resolve) => {
74
+ resolveEndpoints = resolve;
75
+ });
76
+ const getAdvertisedEndpoints = vi
77
+ .fn(async () => advertisedEndpoints)
78
+ .mockResolvedValueOnce(advertisedEndpoints)
79
+ .mockReturnValueOnce(nextEndpoints);
80
+ const atom = createDesktopNetworkAccessStateAtom(() => ({
81
+ getAdvertisedEndpoints,
82
+ getServerExposureState: async () => serverExposureState,
83
+ }));
84
+ const registry = AtomRegistry.make();
85
+ let unmount = registry.mount(atom);
86
39
- const remount = registry.mount(atom);
40
- const result = registry.get(atom);
41
- expect(AsyncResult.value(result)).toEqual(
42
- expect.objectContaining({
43
- _tag: "Some",
44
- value: { advertisedEndpoints, serverExposureState },
45
- }),
46
- );
47
- expect(getServerExposureState).toHaveBeenCalledTimes(1);
48
- expect(getAdvertisedEndpoints).toHaveBeenCalledTimes(1);
87
+ try {
88
+ yield* AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true });
89
+ registry.refresh(atom);
90
+ unmount();
91
+ yield* Effect.yieldNow;
92
+ unmount = registry.mount(atom);
93
50
- remount();
51
- registry.dispose();
52
- });
94
+ expect(AsyncResult.value(registry.get(atom))).toEqual(
95
+ expect.objectContaining({
96
+ _tag: "Some",
97
+ value: { advertisedEndpoints, serverExposureState },
98
+ }),
99
+ );
100
+ resolveEndpoints([tailscaleEndpoint]);
101
+ const refreshed = yield* AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true });
102
+ expect(refreshed.advertisedEndpoints).toEqual([tailscaleEndpoint]);
103
+ expect(getAdvertisedEndpoints).toHaveBeenCalledTimes(2);
104
+ } finally {
105
+ resolveEndpoints([]);
106
+ unmount();
107
+ registry.dispose();
108
+ }
109
+ }),
110
+ );
111
54
- it.each([
112
+ it.effect.each([
113
+ { change: "appears", before: [], after: [tailscaleEndpoint] },
114
+ { change: "disappears", before: [tailscaleEndpoint], after: [] },
115
+ ])("revalidates a stale snapshot when Tailscale $change between visits", ({ before, after }) =>
116
+ Effect.gen(function* () {
117
+ const now = vi.spyOn(Date, "now").mockReturnValue(1_000_000);
118
+ const getServerExposureState = vi.fn(async () => serverExposureState);
119
+ const getAdvertisedEndpoints = vi.fn(async () => before);
120
+ const atom = createDesktopNetworkAccessStateAtom(() => ({
121
+ getAdvertisedEndpoints,
122
+ getServerExposureState,
123
+ }));
124
+ const registry = AtomRegistry.make();
125
+ let unmount = registry.mount(atom);
126
+
127
+ try {
128
+ const initial = yield* AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true });
129
+ expect(initial.advertisedEndpoints).toEqual(before);
130
+ unmount();
131
+ yield* Effect.yieldNow;
132
+ let markRefreshStarted!: () => void;
133
+ const refreshStarted = new Promise<void>((resolve) => {
134
+ markRefreshStarted = resolve;
135
+ });
136
+ getAdvertisedEndpoints.mockImplementation(async () => {
137
+ markRefreshStarted();
138
+ return after;
139
+ });
140
+ now.mockReturnValue(1_030_000);
141
+
142
+ unmount = registry.mount(atom);
143
+ // SWR schedules stale revalidation after the initial cached read.
144
+ yield* Effect.promise(() => refreshStarted);
145
+ const refreshed = yield* AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true });
146
+ expect(refreshed.advertisedEndpoints).toEqual(after);
147
+ expect(getServerExposureState).toHaveBeenCalledTimes(2);
148
+ expect(getAdvertisedEndpoints).toHaveBeenCalledTimes(2);
149
+ } finally {
150
+ unmount();
151
+ registry.dispose();
152
+ now.mockRestore();
153
+ }
154
+ }),
155
+ );
156
+
157
+ it.effect.each([
158
{
159
cause: serverExposureLoadCause,
160
expectedTag: "DesktopServerExposureStateLoadError",
@@ -64,24 +167,26 @@ describe("desktopNetworkAccessState", () => {
167
getAdvertisedEndpoints: async () => Promise.reject(advertisedEndpointsLoadCause),
168
getServerExposureState: async () => serverExposureState,
169
},
67
- ])("retains the $expectedTag cause", async (testCase) => {
68
- const atom = createDesktopNetworkAccessStateAtom(() => ({
69
- getAdvertisedEndpoints: testCase.getAdvertisedEndpoints,
70
- getServerExposureState: testCase.getServerExposureState,
71
- }));
72
- const registry = AtomRegistry.make();
73
- registry.mount(atom);
170
+ ])("retains the $expectedTag cause", (testCase) =>
171
+ Effect.gen(function* () {
172
+ const atom = createDesktopNetworkAccessStateAtom(() => ({
173
+ getAdvertisedEndpoints: testCase.getAdvertisedEndpoints,
174
+ getServerExposureState: testCase.getServerExposureState,
175
+ }));
176
+ const registry = AtomRegistry.make();
177
+ registry.mount(atom);
178
75
- await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
76
- const result = registry.get(atom);
77
- if (!AsyncResult.isFailure(result)) throw new Error("Expected network access load to fail.");
179
+ yield* Effect.exit(AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true }));
180
+ const result = registry.get(atom);
181
+ if (!AsyncResult.isFailure(result)) throw new Error("Expected network access load to fail.");
182
79
- expect(Cause.squash(result.cause)).toEqual(
80
- expect.objectContaining({
81
- _tag: testCase.expectedTag,
82
- cause: testCase.cause,
83
- }),
84
- );
85
- registry.dispose();
86
- });
183
+ expect(Cause.squash(result.cause)).toEqual(
184
+ expect.objectContaining({
185
+ _tag: testCase.expectedTag,
186
+ cause: testCase.cause,
187
+ }),
188
+ );
189
+ registry.dispose();
190
+ }),
191
+ );
192
});
apps/web/src/state/desktopNetworkAccess.ts
+2
-1
@@ -80,11 +80,12 @@ export function createDesktopNetworkAccessStateAtom(
80
});
81
82
return Atom.make(loadDesktopNetworkAccess()).pipe(
83
+ // Retain the snapshot, but let the SWR wrapper revalidate when Settings remounts.
84
+ Atom.keepAlive,
85
Atom.swr({
86
staleTime: DESKTOP_NETWORK_ACCESS_STALE_TIME_MS,
87
revalidateOnMount: true,
88
}),
87
- Atom.keepAlive,
89
Atom.withLabel("desktop:network-access"),
90
);
91
}