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

ConnectionsSettings.logic.test.ts

290 lines9.9 KB

1import { type DesktopWslState, EnvironmentId } from "@t3tools/contracts";
2import { createAdvertisedEndpoint } from "@t3tools/shared/advertisedEndpoint";
3import { describe, expect, it, vi } from "vite-plus/test";
4import {
5 applyWslEnableSelection,
6 endpointDefaultPreferenceKey,
7 formatAccessTimestamp,
8 isServerNetworkAccessible,
9 parseManualDesktopSshTarget,
10 resolveAccessEnvironmentId,
11 resolveCurrentOriginPairingUrl,
12 resolveTailscaleServeBaseUrl,
13 selectQrEndpointOption,
14 togglePairingScopeSelection,
15} from "./ConnectionsSettings.logic";
16import { formatDesktopSshTarget } from "./EnvironmentRow";
17​
18describe("togglePairingScopeSelection", () => {
19 it.each([
20 {
21 label: "adds terminal:read when terminal:operate is selected",
22 current: ["orchestration:read"],
23 scope: "terminal:operate",
24 checked: true,
25 expected: ["orchestration:read", "terminal:operate", "terminal:read"],
26 },
27 {
28 label: "drops terminal:operate when terminal:read is cleared",
29 current: ["terminal:read", "terminal:operate", "relay:read"],
30 scope: "terminal:read",
31 checked: false,
32 expected: ["relay:read"],
33 },
34 {
35 label: "keeps terminal:read when terminal:operate is cleared",
36 current: ["terminal:read", "terminal:operate"],
37 scope: "terminal:operate",
38 checked: false,
39 expected: ["terminal:read"],
40 },
41 {
42 label: "toggles unrelated scopes on their own",
43 current: ["terminal:read"],
44 scope: "filesystem:read",
45 checked: true,
46 expected: ["terminal:read", "filesystem:read"],
47 },
48 ] as const)("$label", ({ current, scope, checked, expected }) => {
49 expect(togglePairingScopeSelection(current, scope, checked)).toEqual(expected);
50 });
51});
52​
53describe("formatAccessTimestamp", () => {
54 const afternoon = new Date(2026, 0, 2, 13, 5).toISOString();
55​
56 it("follows the Time format setting", () => {
57 expect(formatAccessTimestamp(afternoon, "24-hour")).toContain("13:05");
58 expect(formatAccessTimestamp(afternoon, "12-hour")).toMatch(/1:05\s?PM/);
59 });
60​
61 it("keeps a value it cannot parse", () => {
62 expect(formatAccessTimestamp("soon", "24-hour")).toBe("soon");
63 });
64});
65​
66describe("parseManualDesktopSshTarget", () => {
67 it.each(["22.5", "1e3", "2222garbage"])("rejects explicit port %j", (port) => {
68 expect(() => parseManualDesktopSshTarget({ host: "devbox", username: "", port })).toThrow(
69 "SSH port must be between 1 and 65535.",
70 );
71 });
72});
73​
74describe("formatDesktopSshTarget", () => {
75 const target = { alias: "devbox", username: "root", hostname: "::1", port: 2222 };
76​
77 it("brackets an IPv6 host when a port is shown", () => {
78 expect(formatDesktopSshTarget(target)).toBe("root@[::1]:2222");
79 expect(
80 parseManualDesktopSshTarget({ host: formatDesktopSshTarget(target), username: "", port: "" }),
81 ).toMatchObject({ username: "root", hostname: "::1", port: 2222 });
82 });
83​
84 it("leaves IPv6 without a port and named hosts unchanged", () => {
85 expect(formatDesktopSshTarget({ ...target, username: null, port: null })).toBe("::1");
86 expect(formatDesktopSshTarget({ ...target, hostname: "devbox" })).toBe("root@devbox:2222");
87 });
88});
89​
90describe("resolveCurrentOriginPairingUrl", () => {
91 it.each([
92 ["t3code://app/settings/connections", null],
93 ["http://127.0.0.1:3774/settings", null],
94 ["http://localhost:5173/", null],
95 ["https://host.example.ts.net/settings", "https://host.example.ts.net/pair#token=abc"],
96 ["http://192.168.1.20:3773/", "http://192.168.1.20:3773/pair#token=abc"],
97 ])("from %s", (href, expected) => {
98 expect(resolveCurrentOriginPairingUrl("abc", href)).toBe(expected);
99 });
100});
101​
102describe("resolveTailscaleServeBaseUrl", () => {
103 it.each([
104 ["https://host.tail.ts.net:441/", 443, "https://host.tail.ts.net"],
105 ["https://host.tail.ts.net:441/", 444, "https://host.tail.ts.net:444"],
106 ] as const)("previews %s on port %i", (httpBaseUrl, port, expected) => {
107 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, true)).toBe(expected);
108 });
109​
110 it("returns null without a pending endpoint", () => {
111 expect(resolveTailscaleServeBaseUrl(null, 443, true)).toBeNull();
112 });
113​
114 it.each([
115 ["https://host.tail.ts.net:441/", 0, false],
116 ["not a URL", 443, true],
117 ] as const)("preserves %s when port %i cannot be previewed", (httpBaseUrl, port, isPortValid) => {
118 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, isPortValid)).toBe(httpBaseUrl);
119 });
120});
121​
122const baseWslState: DesktopWslState = {
123 enabled: false,
124 distro: null,
125 available: true,
126 wslOnly: true,
127 distros: [],
128 preflightError: null,
129};
130​
131describe("applyWslEnableSelection", () => {
132 it("clears WSL-only and updates the distro before enabling both backends", async () => {
133 const calls: Array<string> = [];
134 let persistedWslOnly = true;
135 let persistedDistro: string | null = "Ubuntu";
136 const setWslDistro = vi.fn(async (distro: string | null) => {
137 calls.push(`setWslDistro:${distro ?? "default"}`);
138 persistedDistro = distro;
139 return { ...baseWslState, distro, wslOnly: persistedWslOnly };
140 });
141 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
142 calls.push(`setWslBackendEnabled:${enabled}`);
143 return {
144 ...baseWslState,
145 enabled,
146 distro: persistedDistro,
147 wslOnly: persistedWslOnly,
148 };
149 });
150 const setWslOnly = vi.fn(async (enabled: boolean) => {
151 calls.push(`setWslOnly:${enabled}`);
152 persistedWslOnly = enabled;
153 return { ...baseWslState, distro: persistedDistro, wslOnly: enabled };
154 });
155​
156 const state = await applyWslEnableSelection({
157 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
158 mode: "both",
159 nextDistro: "Debian",
160 persistedDistro: "Ubuntu",
161 });
162​
163 expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]);
164 expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false });
165 });
166});
167​
168describe("selectQrEndpointOption", () => {
169 const options = [
170 {
171 id: "desktop-loopback:4780",
172 preferenceKey: "desktop-core:loopback:http",
173 qrShareable: false,
174 },
175 {
176 id: "tailscale-ip:http://100.84.12.7:4780",
177 preferenceKey: "tailscale:ip:http",
178 qrShareable: true,
179 },
180 {
181 id: "tailscale-ip:http://100.84.12.8:4780",
182 preferenceKey: "tailscale:ip:http",
183 qrShareable: true,
184 },
185 {
186 id: "desktop-lan:http://192.168.1.42:4780",
187 preferenceKey: "desktop-core:lan:http",
188 qrShareable: true,
189 },
190 ];
191​
192 it("resolves an explicit selection by unique endpoint id, not the shared preference key", () => {
193 expect(selectQrEndpointOption(options, "tailscale-ip:http://100.84.12.8:4780", null)?.id).toBe(
194 "tailscale-ip:http://100.84.12.8:4780",
195 );
196 });
197​
198 it("skips non-QR-shareable options in the fallback so the panel never opens on loopback", () => {
199 expect(selectQrEndpointOption(options, "tailscale-ip:gone", "nope")?.id).toBe(
200 "tailscale-ip:http://100.84.12.7:4780",
201 );
202 });
203});
204​
205describe("endpointDefaultPreferenceKey", () => {
206 it("keeps two VPN addresses apart so setting one default does not mark the other", () => {
207 const vpnEndpoint = (address: string) =>
208 createAdvertisedEndpoint({
209 id: `vpn-ip:http://${address}:3773`,
210 label: "VPN IP",
211 provider: { id: "vpn", label: "VPN", kind: "private-network", isAddon: true },
212 httpBaseUrl: `http://${address}:3773`,
213 reachability: "private-network",
214 source: "desktop-addon",
215 });
216​
217 expect(endpointDefaultPreferenceKey(vpnEndpoint("100.81.22.33"))).not.toBe(
218 endpointDefaultPreferenceKey(vpnEndpoint("100.82.22.33")),
219 );
220 });
221});
222​
223describe("resolveAccessEnvironmentId", () => {
224 const primary = EnvironmentId.make("primary");
225 const service = EnvironmentId.make("service");
226 const laptop = EnvironmentId.make("laptop");
227 it.each([
228 {
229 label: "defaults to the primary environment",
230 primaryEnvironmentId: primary,
231 environments: [
232 { environmentId: primary, connected: true },
233 { environmentId: service, connected: true },
234 ],
235 selected: null,
236 expected: primary,
237 },
238 {
239 label: "falls back to a connected saved environment without a primary",
240 primaryEnvironmentId: null,
241 environments: [
242 { environmentId: laptop, connected: false },
243 { environmentId: service, connected: true },
244 ],
245 selected: null,
246 expected: service,
247 },
248 {
249 label: "keeps an explicit choice",
250 primaryEnvironmentId: primary,
251 environments: [
252 { environmentId: primary, connected: true },
253 { environmentId: laptop, connected: false },
254 ],
255 selected: laptop,
256 expected: laptop,
257 },
258 {
259 label: "drops a choice that is no longer listed",
260 primaryEnvironmentId: null,
261 environments: [{ environmentId: service, connected: true }],
262 selected: laptop,
263 expected: service,
264 },
265 {
266 label: "has nothing to show without environments",
267 primaryEnvironmentId: null,
268 environments: [],
269 selected: null,
270 expected: null,
271 },
272 ])("$label", ({ primaryEnvironmentId, environments, selected, expected }) => {
273 expect(resolveAccessEnvironmentId({ primaryEnvironmentId, environments, selected })).toBe(
274 expected,
275 );
276 });
277});
278​
279describe("isServerNetworkAccessible", () => {
280 const served = [{ kind: "tailnet", httpBaseUrl: "https://host.example.ts.net:8443/" }] as const;
281 it.each([
282 { authPolicy: "remote-reachable", directEndpoints: undefined, expected: true },
283 { authPolicy: "loopback-browser", directEndpoints: undefined, expected: false },
284 { authPolicy: "loopback-browser", directEndpoints: [], expected: false },
285 { authPolicy: "loopback-browser", directEndpoints: served, expected: true },
286 { authPolicy: null, directEndpoints: served, expected: false },
287 ] as const)("$authPolicy with $directEndpoints.length endpoints", (row) => {
288 expect(isServerNetworkAccessible(row)).toBe(row.expected);
289 });
290});