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

ConnectionsSettings.logic.test.ts

185 lines6.2 KB

1import type { AdvertisedEndpoint, DesktopWslState } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import {
4 applyWslEnableSelection,
5 isQrShareableEndpoint,
6 isWslSettingsRowVisible,
7 selectQrEndpointOption,
8} from "./ConnectionsSettings.logic";
9​
10const baseWslState: DesktopWslState = {
11 enabled: false,
12 distro: null,
13 available: true,
14 wslOnly: true,
15 distros: [],
16 preflightError: null,
17};
18​
19describe("isWslSettingsRowVisible", () => {
20 it("shows the retry row when the WSL state failed to load", () => {
21 expect(isWslSettingsRowVisible({ state: null, error: "load failed" })).toBe(true);
22 });
23​
24 it("hides an unavailable and unused WSL snapshot", () => {
25 expect(
26 isWslSettingsRowVisible({
27 state: { ...baseWslState, available: false, wslOnly: false },
28 error: null,
29 }),
30 ).toBe(false);
31 });
32​
33 it("shows an available WSL snapshot", () => {
34 expect(isWslSettingsRowVisible({ state: baseWslState, error: null })).toBe(true);
35 });
36});
37​
38describe("applyWslEnableSelection", () => {
39 it("clears WSL-only and updates the distro before enabling both backends", async () => {
40 const calls: Array<string> = [];
41 let persistedWslOnly = true;
42 let persistedDistro: string | null = "Ubuntu";
43 const setWslDistro = vi.fn(async (distro: string | null) => {
44 calls.push(`setWslDistro:${distro ?? "default"}`);
45 persistedDistro = distro;
46 return { ...baseWslState, distro, wslOnly: persistedWslOnly };
47 });
48 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
49 calls.push(`setWslBackendEnabled:${enabled}`);
50 return {
51 ...baseWslState,
52 enabled,
53 distro: persistedDistro,
54 wslOnly: persistedWslOnly,
55 };
56 });
57 const setWslOnly = vi.fn(async (enabled: boolean) => {
58 calls.push(`setWslOnly:${enabled}`);
59 persistedWslOnly = enabled;
60 return { ...baseWslState, distro: persistedDistro, wslOnly: enabled };
61 });
62​
63 const state = await applyWslEnableSelection({
64 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
65 mode: "both",
66 nextDistro: "Debian",
67 persistedDistro: "Ubuntu",
68 });
69​
70 expect(calls).toEqual(["setWslOnly:false", "setWslDistro:Debian", "setWslBackendEnabled:true"]);
71 expect(state).toMatchObject({ enabled: true, distro: "Debian", wslOnly: false });
72 });
73​
74 it("stages WSL-only before enabling without rewriting an unchanged distro", async () => {
75 const calls: Array<string> = [];
76 let persistedWslOnly = false;
77 const setWslDistro = vi.fn(async () => baseWslState);
78 const setWslOnly = vi.fn(async (enabled: boolean) => {
79 calls.push(`setWslOnly:${enabled}`);
80 persistedWslOnly = enabled;
81 return { ...baseWslState, wslOnly: enabled };
82 });
83 const setWslBackendEnabled = vi.fn(async (enabled: boolean) => {
84 calls.push(`setWslBackendEnabled:${enabled}`);
85 return { ...baseWslState, enabled, wslOnly: persistedWslOnly };
86 });
87​
88 const state = await applyWslEnableSelection({
89 bridge: { setWslDistro, setWslBackendEnabled, setWslOnly },
90 mode: "wsl-only",
91 nextDistro: null,
92 persistedDistro: null,
93 });
94​
95 expect(calls).toEqual(["setWslOnly:true", "setWslBackendEnabled:true"]);
96 expect(setWslDistro).not.toHaveBeenCalled();
97 expect(state).toMatchObject({ enabled: true, wslOnly: true });
98 });
99});
100​
101function makeEndpoint(overrides: Partial<AdvertisedEndpoint>): AdvertisedEndpoint {
102 return {
103 id: "desktop-lan:http://192.168.1.42:4780",
104 label: "Local network",
105 provider: { id: "desktop-core", label: "Desktop", kind: "core", isAddon: false },
106 httpBaseUrl: "http://192.168.1.42:4780",
107 wsBaseUrl: "ws://192.168.1.42:4780",
108 reachability: "lan",
109 compatibility: { hostedHttpsApp: "unknown", desktopApp: "compatible" },
110 source: "desktop-core",
111 status: "available",
112 ...overrides,
113 };
115​
116describe("isQrShareableEndpoint", () => {
117 it("excludes loopback endpoints so a scanned phone never dials itself", () => {
118 expect(
119 isQrShareableEndpoint(
120 makeEndpoint({
121 id: "desktop-loopback:4780",
122 reachability: "loopback",
123 httpBaseUrl: "http://127.0.0.1:4780",
124 }),
125 ),
126 ).toBe(false);
127 });
128​
129 it("excludes unavailable endpoints and keeps reachable ones", () => {
130 expect(isQrShareableEndpoint(makeEndpoint({ status: "unavailable" }))).toBe(false);
131 expect(isQrShareableEndpoint(makeEndpoint({}))).toBe(true);
132 expect(
133 isQrShareableEndpoint(makeEndpoint({ reachability: "private-network", status: "unknown" })),
134 ).toBe(true);
135 });
136});
137​
138describe("selectQrEndpointOption", () => {
139 const options = [
140 {
141 id: "desktop-loopback:4780",
142 preferenceKey: "desktop-core:loopback:http",
143 qrShareable: false,
144 },
145 {
146 id: "tailscale-ip:http://100.84.12.7:4780",
147 preferenceKey: "tailscale:ip:http",
148 qrShareable: true,
149 },
150 {
151 id: "tailscale-ip:http://100.84.12.8:4780",
152 preferenceKey: "tailscale:ip:http",
153 qrShareable: true,
154 },
155 {
156 id: "desktop-lan:http://192.168.1.42:4780",
157 preferenceKey: "desktop-core:lan:http",
158 qrShareable: true,
159 },
160 ];
161​
162 it("resolves an explicit selection by unique endpoint id, not the shared preference key", () => {
163 expect(selectQrEndpointOption(options, "tailscale-ip:http://100.84.12.8:4780", null)?.id).toBe(
164 "tailscale-ip:http://100.84.12.8:4780",
165 );
166 });
167​
168 it("falls back to the saved default preference key when nothing is selected", () => {
169 expect(selectQrEndpointOption(options, null, "desktop-core:lan:http")?.id).toBe(
170 "desktop-lan:http://192.168.1.42:4780",
171 );
172 });
173​
174 it("skips non-QR-shareable options in the fallback so the panel never opens on loopback", () => {
175 expect(selectQrEndpointOption(options, "tailscale-ip:gone", "nope")?.id).toBe(
176 "tailscale-ip:http://100.84.12.7:4780",
177 );
178 });
179​
180 it("returns the first option when nothing is QR-shareable, and null when empty", () => {
181 const loopbackOnly = options.slice(0, 1);
182 expect(selectQrEndpointOption(loopbackOnly, null, null)?.id).toBe("desktop-loopback:4780");
183 expect(selectQrEndpointOption([], "anything", "anything")).toBeNull();
184 });
185});