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