Open History to see the last commit
1import type { DesktopWslState } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import {
4 applyWslEnableSelection,
5 resolveTailscaleServeBaseUrl,
6 selectQrEndpointOption,
7 togglePairingScopeSelection,
8} from "./ConnectionsSettings.logic";
9
10describe("togglePairingScopeSelection", () => {
11 it.each([
12 {
13 label: "adds terminal:read when terminal:operate is selected",
14 current: ["orchestration:read"],
15 scope: "terminal:operate",
16 checked: true,
17 expected: ["orchestration:read", "terminal:operate", "terminal:read"],
18 },
19 {
20 label: "drops terminal:operate when terminal:read is cleared",
21 current: ["terminal:read", "terminal:operate", "relay:read"],
22 scope: "terminal:read",
23 checked: false,
24 expected: ["relay:read"],
25 },
26 {
27 label: "keeps terminal:read when terminal:operate is cleared",
28 current: ["terminal:read", "terminal:operate"],
29 scope: "terminal:operate",
30 checked: false,
31 expected: ["terminal:read"],
32 },
33 {
34 label: "toggles unrelated scopes on their own",
35 current: ["terminal:read"],
36 scope: "filesystem:read",
37 checked: true,
38 expected: ["terminal:read", "filesystem:read"],
39 },
40 ] as const)("$label", ({ current, scope, checked, expected }) => {
41 expect(togglePairingScopeSelection(current, scope, checked)).toEqual(expected);
42 });
43});
44
45describe("resolveTailscaleServeBaseUrl", () => {
46 it.each([
47 ["https://host.tail.ts.net:441/", 443, "https://host.tail.ts.net"],
48 ["https://host.tail.ts.net:441/", 444, "https://host.tail.ts.net:444"],
49 ] as const)("previews %s on port %i", (httpBaseUrl, port, expected) => {
50 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, true)).toBe(expected);
51 });
52
53 it("returns null without a pending endpoint", () => {
54 expect(resolveTailscaleServeBaseUrl(null, 443, true)).toBeNull();
55 });
56
57 it.each([
58 ["https://host.tail.ts.net:441/", 0, false],
59 ["not a URL", 443, true],
60 ] as const)("preserves %s when port %i cannot be previewed", (httpBaseUrl, port, isPortValid) => {
61 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, isPortValid)).toBe(httpBaseUrl);
62 });
63});
64
65const baseWslState: DesktopWslState = {
66 enabled: false,
67 distro: null,
68 available: true,
69 wslOnly: true,
70 distros: [],
71 preflightError: null,
72};
73
74describe("applyWslEnableSelection", () => {
75 it("clears WSL-only and updates the distro before enabling both backends", async () => {
76 const calls: Array<string> = [];
77 let persistedWslOnly = true;
78 let persistedDistro: string | null = "Ubuntu";
79 const setWslDistro = vi.fn(async (distro: string | null) => {
80 calls.push(`setWslDistro:${distro ?? "default"}`);
81 persistedDistro = distro;
82 return { ...baseWslState, distro, wslOnly: persistedWslOnly };
83 });
84 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
85 calls.push(`setWslBackendEnabled:${enabled}`);
86 return {
87 ...baseWslState,
88 enabled,
89 distro: persistedDistro,
90 wslOnly: persistedWslOnly,
91 };
92 });
93 const setWslOnly = vi.fn(async (enabled: boolean) => {
94 calls.push(`setWslOnly:${enabled}`);
95 persistedWslOnly = enabled;
96 return { ...baseWslState, distro: persistedDistro, wslOnly: enabled };
97 });
98
99 const state = await applyWslEnableSelection({
100 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
101 mode: "both",
102 nextDistro: "Debian",
103 persistedDistro: "Ubuntu",
104 });
105
106 expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]);
107 expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false });
108 });
109});
110
111describe("selectQrEndpointOption", () => {
112 const options = [
113 {
114 id: "desktop-loopback:4780",
115 preferenceKey: "desktop-core:loopback:http",
116 qrShareable: false,
117 },
118 {
119 id: "tailscale-ip:http://100.84.12.7:4780",
120 preferenceKey: "tailscale:ip:http",
121 qrShareable: true,
122 },
123 {
124 id: "tailscale-ip:http://100.84.12.8:4780",
125 preferenceKey: "tailscale:ip:http",
126 qrShareable: true,
127 },
128 {
129 id: "desktop-lan:http://192.168.1.42:4780",
130 preferenceKey: "desktop-core:lan:http",
131 qrShareable: true,
132 },
133 ];
134
135 it("resolves an explicit selection by unique endpoint id, not the shared preference key", () => {
136 expect(selectQrEndpointOption(options, "tailscale-ip:http://100.84.12.8:4780", null)?.id).toBe(
137 "tailscale-ip:http://100.84.12.8:4780",
138 );
139 });
140
141 it("skips non-QR-shareable options in the fallback so the panel never opens on loopback", () => {
142 expect(selectQrEndpointOption(options, "tailscale-ip:gone", "nope")?.id).toBe(
143 "tailscale-ip:http://100.84.12.7:4780",
144 );
145 });
146});