Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

desktopNetworkAccess.test.ts

192 lines7.3 KB

1import { it } from "@effect/vitest";
2import type { AdvertisedEndpoint, DesktopServerExposureState } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as AsyncResult from "effect/reactivity/AsyncResult";
6import { AtomRegistry } from "effect/reactivity";
7import { describe, expect, vi } from "vite-plus/test";
8​
9import { createDesktopNetworkAccessStateAtom } from "./desktopNetworkAccess";
10​
11const serverExposureState: DesktopServerExposureState = {
12 advertisedHost: "192.168.1.10",
13 endpointUrl: "http://192.168.1.10:37737",
14 mode: "network-accessible",
15 tailscaleServeEnabled: false,
16 tailscaleServePort: 443,
17};
18​
19const advertisedEndpoints: ReadonlyArray<AdvertisedEndpoint> = [];
20const 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};
31const serverExposureLoadCause = new Error("exposure failed");
32const advertisedEndpointsLoadCause = new Error("endpoints failed");
33​
34describe("desktopNetworkAccessState", () => {
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​
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​
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​
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​
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",
161 getAdvertisedEndpoints: async () => advertisedEndpoints,
162 getServerExposureState: async () => Promise.reject(serverExposureLoadCause),
163 },
164 {
165 cause: advertisedEndpointsLoadCause,
166 expectedTag: "DesktopAdvertisedEndpointsLoadError",
167 getAdvertisedEndpoints: async () => Promise.reject(advertisedEndpointsLoadCause),
168 getServerExposureState: async () => serverExposureState,
169 },
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​
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​
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});