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

ConnectionsSettings.logic.test.ts

217 lines7.1 KB

1import { type DesktopWslState, EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import {
4 applyWslEnableSelection,
5 isServerNetworkAccessible,
6 resolveAccessEnvironmentId,
7 resolveTailscaleServeBaseUrl,
8 selectQrEndpointOption,
9 togglePairingScopeSelection,
10} from "./ConnectionsSettings.logic";
11​
12describe("togglePairingScopeSelection", () => {
13 it.each([
14 {
15 label: "adds terminal:read when terminal:operate is selected",
16 current: ["orchestration:read"],
17 scope: "terminal:operate",
18 checked: true,
19 expected: ["orchestration:read", "terminal:operate", "terminal:read"],
20 },
21 {
22 label: "drops terminal:operate when terminal:read is cleared",
23 current: ["terminal:read", "terminal:operate", "relay:read"],
24 scope: "terminal:read",
25 checked: false,
26 expected: ["relay:read"],
27 },
28 {
29 label: "keeps terminal:read when terminal:operate is cleared",
30 current: ["terminal:read", "terminal:operate"],
31 scope: "terminal:operate",
32 checked: false,
33 expected: ["terminal:read"],
34 },
35 {
36 label: "toggles unrelated scopes on their own",
37 current: ["terminal:read"],
38 scope: "filesystem:read",
39 checked: true,
40 expected: ["terminal:read", "filesystem:read"],
41 },
42 ] as const)("$label", ({ current, scope, checked, expected }) => {
43 expect(togglePairingScopeSelection(current, scope, checked)).toEqual(expected);
44 });
45});
46​
47describe("resolveTailscaleServeBaseUrl", () => {
48 it.each([
49 ["https://host.tail.ts.net:441/", 443, "https://host.tail.ts.net"],
50 ["https://host.tail.ts.net:441/", 444, "https://host.tail.ts.net:444"],
51 ] as const)("previews %s on port %i", (httpBaseUrl, port, expected) => {
52 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, true)).toBe(expected);
53 });
54​
55 it("returns null without a pending endpoint", () => {
56 expect(resolveTailscaleServeBaseUrl(null, 443, true)).toBeNull();
57 });
58​
59 it.each([
60 ["https://host.tail.ts.net:441/", 0, false],
61 ["not a URL", 443, true],
62 ] as const)("preserves %s when port %i cannot be previewed", (httpBaseUrl, port, isPortValid) => {
63 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, isPortValid)).toBe(httpBaseUrl);
64 });
65});
66​
67const baseWslState: DesktopWslState = {
68 enabled: false,
69 distro: null,
70 available: true,
71 wslOnly: true,
72 distros: [],
73 preflightError: null,
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​
113describe("selectQrEndpointOption", () => {
114 const options = [
115 {
116 id: "desktop-loopback:4780",
117 preferenceKey: "desktop-core:loopback:http",
118 qrShareable: false,
119 },
120 {
121 id: "tailscale-ip:http://100.84.12.7:4780",
122 preferenceKey: "tailscale:ip:http",
123 qrShareable: true,
124 },
125 {
126 id: "tailscale-ip:http://100.84.12.8:4780",
127 preferenceKey: "tailscale:ip:http",
128 qrShareable: true,
129 },
130 {
131 id: "desktop-lan:http://192.168.1.42:4780",
132 preferenceKey: "desktop-core:lan:http",
133 qrShareable: true,
134 },
135 ];
136​
137 it("resolves an explicit selection by unique endpoint id, not the shared preference key", () => {
138 expect(selectQrEndpointOption(options, "tailscale-ip:http://100.84.12.8:4780", null)?.id).toBe(
139 "tailscale-ip:http://100.84.12.8:4780",
140 );
141 });
142​
143 it("skips non-QR-shareable options in the fallback so the panel never opens on loopback", () => {
144 expect(selectQrEndpointOption(options, "tailscale-ip:gone", "nope")?.id).toBe(
145 "tailscale-ip:http://100.84.12.7:4780",
146 );
147 });
148});
149​
150describe("resolveAccessEnvironmentId", () => {
151 const primary = EnvironmentId.make("primary");
152 const service = EnvironmentId.make("service");
153 const laptop = EnvironmentId.make("laptop");
154 it.each([
155 {
156 label: "defaults to the primary environment",
157 primaryEnvironmentId: primary,
158 environments: [
159 { environmentId: primary, connected: true },
160 { environmentId: service, connected: true },
161 ],
162 selected: null,
163 expected: primary,
164 },
165 {
166 label: "falls back to a connected saved environment without a primary",
167 primaryEnvironmentId: null,
168 environments: [
169 { environmentId: laptop, connected: false },
170 { environmentId: service, connected: true },
171 ],
172 selected: null,
173 expected: service,
174 },
175 {
176 label: "keeps an explicit choice",
177 primaryEnvironmentId: primary,
178 environments: [
179 { environmentId: primary, connected: true },
180 { environmentId: laptop, connected: false },
181 ],
182 selected: laptop,
183 expected: laptop,
184 },
185 {
186 label: "drops a choice that is no longer listed",
187 primaryEnvironmentId: null,
188 environments: [{ environmentId: service, connected: true }],
189 selected: laptop,
190 expected: service,
191 },
192 {
193 label: "has nothing to show without environments",
194 primaryEnvironmentId: null,
195 environments: [],
196 selected: null,
197 expected: null,
198 },
199 ])("$label", ({ primaryEnvironmentId, environments, selected, expected }) => {
200 expect(resolveAccessEnvironmentId({ primaryEnvironmentId, environments, selected })).toBe(
201 expected,
202 );
203 });
204});
205​
206describe("isServerNetworkAccessible", () => {
207 const served = [{ kind: "tailnet", httpBaseUrl: "https://host.example.ts.net:8443/" }] as const;
208 it.each([
209 { authPolicy: "remote-reachable", directEndpoints: undefined, expected: true },
210 { authPolicy: "loopback-browser", directEndpoints: undefined, expected: false },
211 { authPolicy: "loopback-browser", directEndpoints: [], expected: false },
212 { authPolicy: "loopback-browser", directEndpoints: served, expected: true },
213 { authPolicy: null, directEndpoints: served, expected: false },
214 ] as const)("$authPolicy with $directEndpoints.length endpoints", (row) => {
215 expect(isServerNetworkAccessible(row)).toBe(row.expected);
216 });
217});