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

ConnectionsSettings.logic.test.ts

125 lines3.9 KB

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