Open History to see the last commit
1import type { AdvertisedEndpoint, DesktopUpdateState, DesktopWslState } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import {
4 applyWslEnableSelection,
5 canRevokeOtherClients,
6 isQrShareableEndpoint,
7 isWslSettingsRowVisible,
8 missingTailscaleEndpointDescription,
9 resolveLocalVersionStatusLabel,
10 selectQrEndpointOption,
11 togglePairingScopeSelection,
12} from "./ConnectionsSettings.logic";
13
14describe("togglePairingScopeSelection", () => {
15 it.each([
16 {
17 label: "adds terminal:read when terminal:operate is selected",
18 current: ["orchestration:read"],
19 scope: "terminal:operate",
20 checked: true,
21 expected: ["orchestration:read", "terminal:operate", "terminal:read"],
22 },
23 {
24 label: "drops terminal:operate when terminal:read is cleared",
25 current: ["terminal:read", "terminal:operate", "relay:read"],
26 scope: "terminal:read",
27 checked: false,
28 expected: ["relay:read"],
29 },
30 {
31 label: "keeps terminal:read when terminal:operate is cleared",
32 current: ["terminal:read", "terminal:operate"],
33 scope: "terminal:operate",
34 checked: false,
35 expected: ["terminal:read"],
36 },
37 {
38 label: "toggles unrelated scopes on their own",
39 current: ["terminal:read"],
40 scope: "filesystem:read",
41 checked: true,
42 expected: ["terminal:read", "filesystem:read"],
43 },
44 ] as const)("$label", ({ current, scope, checked, expected }) => {
45 expect(togglePairingScopeSelection(current, scope, checked)).toEqual(expected);
46 });
47});
48
49const baseWslState: DesktopWslState = {
50 enabled: false,
51 distro: null,
52 available: true,
53 wslOnly: true,
54 distros: [],
55 preflightError: null,
56};
57
58describe("resolveLocalVersionStatusLabel", () => {
59 const baseState: DesktopUpdateState = {
60 enabled: true,
61 status: "idle",
62 channel: "latest",
63 currentVersion: "1.0.0",
64 hostArch: "x64",
65 appArch: "x64",
66 runningUnderArm64Translation: false,
67 availableVersion: null,
68 downloadedVersion: null,
69 releaseNotes: [],
70 omittedReleaseCount: 0,
71 downloadPercent: null,
72 checkedAt: null,
73 message: null,
74 errorContext: null,
75 canRetry: false,
76 };
77
78 it("stays up to date on the web and when the desktop updater has nothing pending", () => {
79 expect(resolveLocalVersionStatusLabel(null)).toBe("Up to date");
80 expect(resolveLocalVersionStatusLabel(baseState)).toBe("Up to date");
81 });
82
83 it.each<[string, Partial<DesktopUpdateState>, string]>([
84 ["available", { status: "available", availableVersion: "1.2.0" }, "Update 1.2.0 available"],
85 [
86 "downloading",
87 { status: "downloading", availableVersion: "1.2.0" },
88 "Downloading update 1.2.0",
89 ],
90 [
91 "downloaded",
92 { status: "downloaded", availableVersion: "1.2.0", downloadedVersion: "1.1.0" },
93 "Update 1.1.0 downloaded",
94 ],
95 [
96 "a failed download that can be retried",
97 { status: "error", errorContext: "download", availableVersion: "1.2.0", canRetry: true },
98 "Update 1.2.0 available",
99 ],
100 [
101 "a failed install",
102 { status: "error", errorContext: "install", downloadedVersion: "1.1.0" },
103 "Update 1.1.0 downloaded",
104 ],
105 [
106 "a recheck that keeps a downloaded update",
107 { status: "checking", downloadedVersion: "1.1.0" },
108 "Update 1.1.0 downloaded",
109 ],
110 ])("names the pending update for %s", (_, state, label) => {
111 expect(resolveLocalVersionStatusLabel({ ...baseState, ...state })).toBe(label);
112 });
113});
114
115describe("isWslSettingsRowVisible", () => {
116 it("shows the retry row when the WSL state failed to load", () => {
117 expect(isWslSettingsRowVisible({ state: null, error: "load failed" })).toBe(true);
118 });
119
120 it("hides an unavailable and unused WSL snapshot", () => {
121 expect(
122 isWslSettingsRowVisible({
123 state: { ...baseWslState, available: false, wslOnly: false },
124 error: null,
125 }),
126 ).toBe(false);
127 });
128
129 it("shows an available WSL snapshot", () => {
130 expect(isWslSettingsRowVisible({ state: baseWslState, error: null })).toBe(true);
131 });
132});
133
134describe("applyWslEnableSelection", () => {
135 it("clears WSL-only and updates the distro before enabling both backends", async () => {
136 const calls: Array<string> = [];
137 let persistedWslOnly = true;
138 let persistedDistro: string | null = "Ubuntu";
139 const setWslDistro = vi.fn(async (distro: string | null) => {
140 calls.push(`setWslDistro:${distro ?? "default"}`);
141 persistedDistro = distro;
142 return { ...baseWslState, distro, wslOnly: persistedWslOnly };
143 });
144 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
145 calls.push(`setWslBackendEnabled:${enabled}`);
146 return {
147 ...baseWslState,
148 enabled,
149 distro: persistedDistro,
150 wslOnly: persistedWslOnly,
151 };
152 });
153 const setWslOnly = vi.fn(async (enabled: boolean) => {
154 calls.push(`setWslOnly:${enabled}`);
155 persistedWslOnly = enabled;
156 return { ...baseWslState, distro: persistedDistro, wslOnly: enabled };
157 });
158
159 const state = await applyWslEnableSelection({
160 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
161 mode: "both",
162 nextDistro: "Debian",
163 persistedDistro: "Ubuntu",
164 });
165
166 expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]);
167 expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false });
168 });
169
170 it("stages WSL-only before enabling without rewriting an unchanged distro", async () => {
171 const calls: Array<string> = [];
172 let persistedWslOnly = false;
173 const setWslDistro = vi.fn(async () => baseWslState);
174 const setWslOnly = vi.fn(async (enabled: boolean) => {
175 calls.push(`setWslOnly:${enabled}`);
176 persistedWslOnly = enabled;
177 return { ...baseWslState, wslOnly: enabled };
178 });
179 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
180 calls.push(`setWslBackendEnabled:${enabled}`);
181 return { ...baseWslState, enabled, wslOnly: persistedWslOnly };
182 });
183
184 const state = await applyWslEnableSelection({
185 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
186 mode: "wsl-only",
187 nextDistro: null,
188 persistedDistro: null,
189 });
190
191 expect(calls).toEqual(["setWslOnly:true", "setWslBackendEnabled:true"]);
192 expect(setWslDistro).not.toHaveBeenCalled();
193 expect(state).toMatchObject({ enabled: true, wslOnly: true });
194 });
195});
196
197function makeEndpoint(overrides: Partial<AdvertisedEndpoint>): AdvertisedEndpoint {
198 return {
199 id: "desktop-lan:http://192.168.1.42:4780",
200 label: "Local network",
201 provider: { id: "desktop-core", label: "Desktop", kind: "core", isAddon: false },
202 httpBaseUrl: "http://192.168.1.42:4780",
203 wsBaseUrl: "ws://192.168.1.42:4780",
204 reachability: "lan",
205 compatibility: { hostedHttpsApp: "unknown", desktopApp: "compatible" },
206 source: "desktop-core",
207 status: "available",
208 ...overrides,
209 };
210}
211
212describe("isQrShareableEndpoint", () => {
213 it("excludes loopback endpoints so a scanned phone never dials itself", () => {
214 expect(
215 isQrShareableEndpoint(
216 makeEndpoint({
217 id: "desktop-loopback:4780",
218 reachability: "loopback",
219 httpBaseUrl: "http://127.0.0.1:4780",
220 }),
221 ),
222 ).toBe(false);
223 });
224
225 it("excludes unavailable endpoints and keeps reachable ones", () => {
226 expect(isQrShareableEndpoint(makeEndpoint({ status: "unavailable" }))).toBe(false);
227 expect(isQrShareableEndpoint(makeEndpoint({}))).toBe(true);
228 expect(
229 isQrShareableEndpoint(makeEndpoint({ reachability: "private-network", status: "unknown" })),
230 ).toBe(true);
231 });
232});
233
234describe("selectQrEndpointOption", () => {
235 const options = [
236 {
237 id: "desktop-loopback:4780",
238 preferenceKey: "desktop-core:loopback:http",
239 qrShareable: false,
240 },
241 {
242 id: "tailscale-ip:http://100.84.12.7:4780",
243 preferenceKey: "tailscale:ip:http",
244 qrShareable: true,
245 },
246 {
247 id: "tailscale-ip:http://100.84.12.8:4780",
248 preferenceKey: "tailscale:ip:http",
249 qrShareable: true,
250 },
251 {
252 id: "desktop-lan:http://192.168.1.42:4780",
253 preferenceKey: "desktop-core:lan:http",
254 qrShareable: true,
255 },
256 ];
257
258 it("resolves an explicit selection by unique endpoint id, not the shared preference key", () => {
259 expect(selectQrEndpointOption(options, "tailscale-ip:http://100.84.12.8:4780", null)?.id).toBe(
260 "tailscale-ip:http://100.84.12.8:4780",
261 );
262 });
263
264 it("falls back to the saved default preference key when nothing is selected", () => {
265 expect(selectQrEndpointOption(options, null, "desktop-core:lan:http")?.id).toBe(
266 "desktop-lan:http://192.168.1.42:4780",
267 );
268 });
269
270 it("skips non-QR-shareable options in the fallback so the panel never opens on loopback", () => {
271 expect(selectQrEndpointOption(options, "tailscale-ip:gone", "nope")?.id).toBe(
272 "tailscale-ip:http://100.84.12.7:4780",
273 );
274 });
275
276 it("returns the first option when nothing is QR-shareable, and null when empty", () => {
277 const loopbackOnly = options.slice(0, 1);
278 expect(selectQrEndpointOption(loopbackOnly, null, null)?.id).toBe("desktop-loopback:4780");
279 expect(selectQrEndpointOption([], "anything", "anything")).toBeNull();
280 });
281});
282
283describe("canRevokeOtherClients", () => {
284 it("permits revocation when a write-only grant cannot list clients", () => {
285 expect(canRevokeOtherClients(null)).toBe(true);
286 });
287
288 it("disables revocation only when a loaded list has no other clients", () => {
289 expect(canRevokeOtherClients([])).toBe(false);
290 expect(canRevokeOtherClients([{ current: true }])).toBe(false);
291 expect(canRevokeOtherClients([{ current: true }, { current: false }])).toBe(true);
292 });
293});
294
295describe("missingTailscaleEndpointDescription", () => {
296 it("explains that detection waits for Network access when the probe was skipped", () => {
297 expect(
298 missingTailscaleEndpointDescription({ mode: "local-only", tailscaleServeEnabled: false }),
299 ).toBe("Turn on Network access to detect Tailscale.");
300 });
301
302 it.each([
303 { mode: "network-accessible", tailscaleServeEnabled: false },
304 { mode: "local-only", tailscaleServeEnabled: true },
305 ] as const)("asks to start Tailscale when it was probed and not found (%o)", (state) => {
306 expect(missingTailscaleEndpointDescription(state)).toBe(
307 "Start Tailscale to set up HTTPS access through MagicDNS.",
308 );
309 });
310});