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

ConnectionsSettings.logic.test.ts

165 lines5.6 KB

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