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

ConnectionsSettings.logic.test.ts

203 lines6.4 KB

1import { type DesktopWslState, EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import {
4 applyWslEnableSelection,
5 resolveAccessEnvironmentId,
6 resolveTailscaleServeBaseUrl,
7 selectQrEndpointOption,
8 togglePairingScopeSelection,
9} from "./ConnectionsSettings.logic";
10​
11describe("togglePairingScopeSelection", () => {
12 it.each([
13 {
14 label: "adds terminal:read when terminal:operate is selected",
15 current: ["orchestration:read"],
16 scope: "terminal:operate",
17 checked: true,
18 expected: ["orchestration:read", "terminal:operate", "terminal:read"],
19 },
20 {
21 label: "drops terminal:operate when terminal:read is cleared",
22 current: ["terminal:read", "terminal:operate", "relay:read"],
23 scope: "terminal:read",
24 checked: false,
25 expected: ["relay:read"],
26 },
27 {
28 label: "keeps terminal:read when terminal:operate is cleared",
29 current: ["terminal:read", "terminal:operate"],
30 scope: "terminal:operate",
31 checked: false,
32 expected: ["terminal:read"],
33 },
34 {
35 label: "toggles unrelated scopes on their own",
36 current: ["terminal:read"],
37 scope: "filesystem:read",
38 checked: true,
39 expected: ["terminal:read", "filesystem:read"],
40 },
41 ] as const)("$label", ({ current, scope, checked, expected }) => {
42 expect(togglePairingScopeSelection(current, scope, checked)).toEqual(expected);
43 });
44});
45​
46describe("resolveTailscaleServeBaseUrl", () => {
47 it.each([
48 ["https://host.tail.ts.net:441/", 443, "https://host.tail.ts.net"],
49 ["https://host.tail.ts.net:441/", 444, "https://host.tail.ts.net:444"],
50 ] as const)("previews %s on port %i", (httpBaseUrl, port, expected) => {
51 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, true)).toBe(expected);
52 });
53​
54 it("returns null without a pending endpoint", () => {
55 expect(resolveTailscaleServeBaseUrl(null, 443, true)).toBeNull();
56 });
57​
58 it.each([
59 ["https://host.tail.ts.net:441/", 0, false],
60 ["not a URL", 443, true],
61 ] as const)("preserves %s when port %i cannot be previewed", (httpBaseUrl, port, isPortValid) => {
62 expect(resolveTailscaleServeBaseUrl({ httpBaseUrl }, port, isPortValid)).toBe(httpBaseUrl);
63 });
64});
65​
66const baseWslState: DesktopWslState = {
67 enabled: false,
68 distro: null,
69 available: true,
70 wslOnly: true,
71 distros: [],
72 preflightError: null,
73};
74​
75describe("applyWslEnableSelection", () => {
76 it("clears WSL-only and updates the distro before enabling both backends", async () => {
77 const calls: Array<string> = [];
78 let persistedWslOnly = true;
79 let persistedDistro: string | null = "Ubuntu";
80 const setWslDistro = vi.fn(async (distro: string | null) => {
81 calls.push(`setWslDistro:${distro ?? "default"}`);
82 persistedDistro = distro;
83 return { ...baseWslState, distro, wslOnly: persistedWslOnly };
84 });
85 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
86 calls.push(`setWslBackendEnabled:${enabled}`);
87 return {
88 ...baseWslState,
89 enabled,
90 distro: persistedDistro,
91 wslOnly: persistedWslOnly,
92 };
93 });
94 const setWslOnly = vi.fn(async (enabled: boolean) => {
95 calls.push(`setWslOnly:${enabled}`);
96 persistedWslOnly = enabled;
97 return { ...baseWslState, distro: persistedDistro, wslOnly: enabled };
98 });
99​
100 const state = await applyWslEnableSelection({
101 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
102 mode: "both",
103 nextDistro: "Debian",
104 persistedDistro: "Ubuntu",
105 });
106​
107 expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]);
108 expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false });
109 });
110});
111​
112describe("selectQrEndpointOption", () => {
113 const options = [
114 {
115 id: "desktop-loopback:4780",
116 preferenceKey: "desktop-core:loopback:http",
117 qrShareable: false,
118 },
119 {
120 id: "tailscale-ip:http://100.84.12.7:4780",
121 preferenceKey: "tailscale:ip:http",
122 qrShareable: true,
123 },
124 {
125 id: "tailscale-ip:http://100.84.12.8:4780",
126 preferenceKey: "tailscale:ip:http",
127 qrShareable: true,
128 },
129 {
130 id: "desktop-lan:http://192.168.1.42:4780",
131 preferenceKey: "desktop-core:lan:http",
132 qrShareable: true,
133 },
134 ];
135​
136 it("resolves an explicit selection by unique endpoint id, not the shared preference key", () => {
137 expect(selectQrEndpointOption(options, "tailscale-ip:http://100.84.12.8:4780", null)?.id).toBe(
138 "tailscale-ip:http://100.84.12.8:4780",
139 );
140 });
141​
142 it("skips non-QR-shareable options in the fallback so the panel never opens on loopback", () => {
143 expect(selectQrEndpointOption(options, "tailscale-ip:gone", "nope")?.id).toBe(
144 "tailscale-ip:http://100.84.12.7:4780",
145 );
146 });
147});
148​
149describe("resolveAccessEnvironmentId", () => {
150 const primary = EnvironmentId.make("primary");
151 const service = EnvironmentId.make("service");
152 const laptop = EnvironmentId.make("laptop");
153 it.each([
154 {
155 label: "defaults to the primary environment",
156 primaryEnvironmentId: primary,
157 environments: [
158 { environmentId: primary, connected: true },
159 { environmentId: service, connected: true },
160 ],
161 selected: null,
162 expected: primary,
163 },
164 {
165 label: "falls back to a connected saved environment without a primary",
166 primaryEnvironmentId: null,
167 environments: [
168 { environmentId: laptop, connected: false },
169 { environmentId: service, connected: true },
170 ],
171 selected: null,
172 expected: service,
173 },
174 {
175 label: "keeps an explicit choice",
176 primaryEnvironmentId: primary,
177 environments: [
178 { environmentId: primary, connected: true },
179 { environmentId: laptop, connected: false },
180 ],
181 selected: laptop,
182 expected: laptop,
183 },
184 {
185 label: "drops a choice that is no longer listed",
186 primaryEnvironmentId: null,
187 environments: [{ environmentId: service, connected: true }],
188 selected: laptop,
189 expected: service,
190 },
191 {
192 label: "has nothing to show without environments",
193 primaryEnvironmentId: null,
194 environments: [],
195 selected: null,
196 expected: null,
197 },
198 ])("$label", ({ primaryEnvironmentId, environments, selected, expected }) => {
199 expect(resolveAccessEnvironmentId({ primaryEnvironmentId, environments, selected })).toBe(
200 expected,
201 );
202 });
203});