@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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