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