|
1 | | -import type { DesktopWslState } from "@t3tools/contracts"; |
| 1 | +import type { AdvertisedEndpoint, DesktopWslState } from "@t3tools/contracts"; |
2 | 2 | import { describe, expect, it, vi } from "vite-plus/test"; |
3 | | -import { applyWslEnableSelection } from "./ConnectionsSettings.logic"; |
| 3 | +import { |
| 4 | + applyWslEnableSelection, |
| 5 | + isQrShareableEndpoint, |
| 6 | + selectQrEndpointOption, |
| 7 | +} from "./ConnectionsSettings.logic"; |
4 | 8 |
|
5 | 9 | const baseWslState: DesktopWslState = { |
6 | 10 | enabled: false, |
@@ -73,3 +77,89 @@ describe("applyWslEnableSelection", () => { |
73 | 77 | expect(state).toMatchObject({ enabled: true, wslOnly: true }); |
74 | 78 | }); |
75 | 79 | }); |
| 80 | + |
| 81 | +function 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 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +describe("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 | + |
| 118 | +describe("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 | +}); |
0 commit comments