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

desktopLocal.test.ts

231 lines8.2 KB

1import {
2 BearerConnectionTarget,
3 PrimaryConnectionTarget,
4} from "@t3tools/client-runtime/connection";
5import {
6 type DesktopEnvironmentBootstrap,
7 EnvironmentId,
8 PRIMARY_LOCAL_ENVIRONMENT_ID,
9} from "@t3tools/contracts";
10import { describe, expect, it, vi } from "vite-plus/test";
11​
12import {
13 createDesktopSecondaryBootstrapsReader,
14 desktopLocalBackendId,
15 desktopLocalConnectionId,
16 isDesktopLocalConnectionTarget,
17 isWslConnectionTarget,
18} from "./desktopLocal";
19​
20describe("desktop local connection identity", () => {
21 it("preserves the desktop backend instance id", () => {
22 const target = new BearerConnectionTarget({
23 connectionId: desktopLocalConnectionId("wsl:Ubuntu"),
24 environmentId: EnvironmentId.make("environment-wsl"),
25 label: "WSL (Ubuntu)",
26 });
27​
28 expect(isDesktopLocalConnectionTarget(target)).toBe(true);
29 expect(desktopLocalBackendId(target)).toBe("wsl:Ubuntu");
30 expect(isWslConnectionTarget(target)).toBe(true);
31 });
32​
33 it("does not infer WSL for another desktop-local backend", () => {
34 const target = new BearerConnectionTarget({
35 connectionId: desktopLocalConnectionId("native-linux"),
36 environmentId: EnvironmentId.make("environment-native-linux"),
37 label: "Linux",
38 });
39​
40 expect(isDesktopLocalConnectionTarget(target)).toBe(true);
41 expect(isWslConnectionTarget(target)).toBe(false);
42 });
43​
44 it("does not classify the primary environment as desktop-local", () => {
45 const target = new PrimaryConnectionTarget({
46 environmentId: EnvironmentId.make("environment-primary"),
47 httpBaseUrl: "http://127.0.0.1:3773",
48 label: "This device",
49 wsBaseUrl: "ws://127.0.0.1:3773",
50 });
51​
52 expect(isDesktopLocalConnectionTarget(target)).toBe(false);
53 expect(desktopLocalBackendId(target)).toBeNull();
54 });
55});
56​
57describe("desktop local topology reads", () => {
58 it("refreshes topology asynchronously, retaining identity through unchanged reads and failures", async () => {
59 const secondary: DesktopEnvironmentBootstrap = {
60 id: "wsl:default",
61 label: "WSL",
62 runningDistro: "Ubuntu",
63 httpBaseUrl: "http://127.0.0.1:4000",
64 wsBaseUrl: "ws://127.0.0.1:4000",
65 bootstrapToken: "bootstrap-1",
66 };
67 let entries = [secondary, { ...secondary, id: PRIMARY_LOCAL_ENVIRONMENT_ID }];
68 let cached = entries;
69 const getLocalEnvironmentBootstraps = vi.fn(() => cached);
70 const readLocalEnvironmentBootstraps = vi.fn(async () => {
71 cached = entries.map((entry) => ({ ...entry }));
72 return cached;
73 });
74 const reader = createDesktopSecondaryBootstrapsReader(() => ({
75 getLocalEnvironmentBootstraps,
76 readLocalEnvironmentBootstraps,
77 }));
78​
79 const connected = await reader.refresh();
80 expect(connected).toEqual({ _tag: "Success", bootstraps: [secondary] });
81 expect(getLocalEnvironmentBootstraps).not.toHaveBeenCalled();
82 if (connected._tag !== "Success") throw new Error("expected connected topology");
83​
84 const unchanged = await reader.refresh();
85 if (unchanged._tag !== "Success") throw new Error("expected unchanged topology");
86 expect(unchanged.bootstraps).toBe(connected.bootstraps);
87 expect(getLocalEnvironmentBootstraps).not.toHaveBeenCalled();
88 expect(reader.readSnapshot()).toBe(connected.bootstraps);
89​
90 const cause = new Error("IPC unavailable");
91 readLocalEnvironmentBootstraps.mockRejectedValueOnce(cause);
92 expect(await reader.refresh()).toEqual({ _tag: "Failure", cause });
93 expect(reader.readSnapshot()).toBe(connected.bootstraps);
94​
95 entries = [];
96 const removed = await reader.refresh();
97 expect(removed).toEqual({ _tag: "Success", bootstraps: [] });
98 if (removed._tag !== "Success") throw new Error("expected empty topology");
99 expect(reader.readSnapshot()).toBe(removed.bootstraps);
100 expect(removed.bootstraps).not.toBe(connected.bootstraps);
101 });
102​
103 it("reuses snapshots when polling returns fresh objects for the same topology", async () => {
104 const secondary: DesktopEnvironmentBootstrap = {
105 id: "wsl:default",
106 label: "WSL",
107 runningDistro: "Ubuntu",
108 httpBaseUrl: "http://127.0.0.1:4000",
109 wsBaseUrl: "ws://127.0.0.1:4000",
110 bootstrapToken: "bootstrap-1",
111 };
112 let entries = [secondary];
113 const reader = createDesktopSecondaryBootstrapsReader(() => ({
114 getLocalEnvironmentBootstraps: () => entries.map((entry) => ({ ...entry })),
115 }));
116​
117 const connected = reader.readSnapshot();
118 const refreshed = await reader.refresh();
119 expect(refreshed).toEqual({ _tag: "Success", bootstraps: [secondary] });
120 if (refreshed._tag !== "Success") throw new Error("expected legacy topology");
121 expect(refreshed.bootstraps).toBe(connected);
122 expect(reader.readSnapshot()).toBe(connected);
123 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: connected });
124 expect(reader.readSnapshot()).toBe(connected);
125​
126 entries = [];
127 expect(await reader.refresh()).toEqual({ _tag: "Success", bootstraps: [] });
128 const empty = reader.readSnapshot();
129 expect(empty).toEqual([]);
130 expect(reader.readSnapshot()).toBe(empty);
131 });
132​
133 it.each<Partial<DesktopEnvironmentBootstrap>>([
134 { id: "wsl:Debian" },
135 { label: "Renamed backend" },
136 { runningDistro: "Debian" },
137 { httpBaseUrl: "http://127.0.0.1:4001" },
138 { wsBaseUrl: "ws://127.0.0.1:4001" },
139 { bootstrapToken: "bootstrap-2" },
140 ])("publishes bootstrap changes: %j", (change) => {
141 let secondary: DesktopEnvironmentBootstrap = {
142 id: "wsl:default",
143 label: "WSL",
144 runningDistro: "Ubuntu",
145 httpBaseUrl: "http://127.0.0.1:4000",
146 wsBaseUrl: "ws://127.0.0.1:4000",
147 bootstrapToken: "bootstrap-1",
148 };
149 const reader = createDesktopSecondaryBootstrapsReader(() => ({
150 getLocalEnvironmentBootstraps: () => [{ ...secondary }],
151 }));
152 const before = reader.readSnapshot();
153 secondary = { ...secondary, ...change };
154 const after = reader.readSnapshot();
155 expect(after).not.toBe(before);
156 expect(after).toEqual([secondary]);
157 expect(reader.readSnapshot()).toBe(after);
158 });
159​
160 it("keeps the empty snapshot stable without a desktop bridge", () => {
161 const reader = createDesktopSecondaryBootstrapsReader(() => undefined);
162 expect(reader.readSnapshot()).toBe(reader.readSnapshot());
163 });
164​
165 it("distinguishes a successful empty topology from a read failure", () => {
166 let readBootstraps = () => [];
167 const reader = createDesktopSecondaryBootstrapsReader(() => ({
168 getLocalEnvironmentBootstraps: () => readBootstraps(),
169 }));
170​
171 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [] });
172​
173 const cause = new Error("IPC unavailable");
174 readBootstraps = () => {
175 throw cause;
176 };
177 expect(reader.readResult()).toEqual({ _tag: "Failure", cause });
178 });
179​
180 it("filters the primary bootstrap from successful topology reads", () => {
181 const secondary = {
182 id: "wsl:Ubuntu",
183 label: "WSL: Ubuntu",
184 httpBaseUrl: "http://127.0.0.1:4000",
185 wsBaseUrl: "ws://127.0.0.1:4000",
186 };
187​
188 const reader = createDesktopSecondaryBootstrapsReader(() => ({
189 getLocalEnvironmentBootstraps: () => [
190 {
191 ...secondary,
192 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
193 label: "Windows",
194 },
195 secondary,
196 ],
197 }));
198​
199 expect(reader.readResult()).toEqual({ _tag: "Success", bootstraps: [secondary] });
200 });
201​
202 it("retains the last successful snapshot only until another read succeeds", () => {
203 const secondary = {
204 id: "wsl:Ubuntu",
205 label: "WSL: Ubuntu",
206 httpBaseUrl: "http://127.0.0.1:4000",
207 wsBaseUrl: "ws://127.0.0.1:4000",
208 };
209 let readBootstraps = () => [secondary];
210 const reader = createDesktopSecondaryBootstrapsReader(() => ({
211 getLocalEnvironmentBootstraps: () => readBootstraps(),
212 }));
213​
214 const connectedSnapshot = reader.readSnapshot();
215 expect(connectedSnapshot).toEqual([secondary]);
216​
217 readBootstraps = () => {
218 throw new Error("IPC unavailable");
219 };
220 expect(reader.readSnapshot()).toBe(connectedSnapshot);
221​
222 readBootstraps = () => [];
223 const removedSnapshot = reader.readSnapshot();
224 expect(removedSnapshot).toEqual([]);
225​
226 readBootstraps = () => {
227 throw new Error("IPC unavailable again");
228 };
229 expect(reader.readSnapshot()).toBe(removedSnapshot);
230 });
231});