Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4const readPreparedConnection = vi.fn();
5const env = vi.hoisted(() => ({ isElectron: false }));
6
7vi.mock("~/state/session", () => ({ readPreparedConnection }));
8vi.mock("~/env", () => env);
9
10describe("browser target resolver", () => {
11 beforeEach(() => readPreparedConnection.mockReset());
12
13 it("maps environment ports onto a private network host", async () => {
14 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
15 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
16 expect(
17 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
18 kind: "environment-port",
19 port: 5173,
20 path: "/dashboard",
21 }),
22 ).toEqual({
23 requestedUrl: "http://localhost:5173/dashboard",
24 resolvedUrl: "http://192.168.1.25:5173/dashboard",
25 resolutionKind: "direct-private-network",
26 environmentId: "environment-1",
27 });
28 });
29
30 it("preserves explicit loopback URL navigation for a remote Tailscale environment", async () => {
31 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });
32 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
33 expect(
34 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
35 kind: "url",
36 url: "http://localhost:5173/dashboard?mode=test#results",
37 }),
38 ).toEqual({
39 requestedUrl: "http://localhost:5173/dashboard?mode=test#results",
40 resolvedUrl: "http://localhost:5173/dashboard?mode=test#results",
41 resolutionKind: "direct",
42 environmentId: "environment-1",
43 });
44 });
45
46 it("preserves URL credentials on explicit loopback navigation", async () => {
47 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });
48 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
49 expect(
50 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
51 kind: "url",
52 url: "http://user:p%40ss@localhost:5173/dashboard",
53 }).resolvedUrl,
54 ).toBe("http://user:p%40ss@localhost:5173/dashboard");
55 });
56
57 it("preserves schemeless localhost navigation for a remote environment", async () => {
58 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
59 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
60 expect(
61 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
62 kind: "url",
63 url: "localhost:3000/app",
64 }).resolvedUrl,
65 ).toBe("localhost:3000/app");
66 });
67
68 it("refuses public relay hosts until the authenticated gateway exists", async () => {
69 readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
70 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
71 expect(() =>
72 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
73 kind: "environment-port",
74 port: 5173,
75 }),
76 ).toThrow(/authenticated preview gateway/);
77 expect(
78 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
79 kind: "url",
80 url: "http://localhost:5173",
81 }),
82 ).toMatchObject({ resolvedUrl: "http://localhost:5173", resolutionKind: "direct" });
83 });
84
85 it("normalizes schemeless localhost server-picker values", async () => {
86 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://localhost:3773" });
87 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
88 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173")).toBe(
89 "http://localhost:5173/",
90 );
91 expect(
92 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "0.0.0.0:3000/app"),
93 ).toBe("http://localhost:3000/app");
94 });
95
96 it("maps discovered loopback servers onto a remote environment host", async () => {
97 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
98 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
99 expect(
100 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3000/app"),
101 ).toBe("http://192.168.1.25:3000/app");
102 });
103
104 it("keeps localhost for servers on the desktop's own WSL or primary backend", async () => {
105 // WSL NAT advertises a distro IP, but loopback-bound servers only answer on localhost.
106 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
107 env.isElectron = true;
108 try {
109 for (const target of [
110 { _tag: "BearerConnectionTarget", connectionId: "local:wsl:Ubuntu" },
111 { _tag: "PrimaryConnectionTarget" },
112 ]) {
113 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://172.24.66.27:3773", target });
114 expect(
115 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
116 ).toBe("http://localhost:3001/");
117 }
118 readPreparedConnection.mockReturnValue({
119 httpBaseUrl: "http://192.168.1.25:3773",
120 target: { _tag: "BearerConnectionTarget", connectionId: "saved:remote" },
121 });
122 expect(
123 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
124 ).toBe("http://192.168.1.25:3001/");
125 } finally {
126 env.isElectron = false;
127 }
128 });
129
130 it("classifies exact private IPv4 and IPv6 boundaries", async () => {
131 const { isPrivateNetworkHost } = await import("./browserTargetResolver");
132 const privateHosts = [
133 "0.0.0.0",
134 "10.0.0.0",
135 "10.255.255.255",
136 "100.64.0.0",
137 "100.127.255.255",
138 "127.0.0.0",
139 "127.255.255.255",
140 "169.254.0.0",
141 "169.254.255.255",
142 "172.16.0.0",
143 "172.31.255.255",
144 "192.168.0.0",
145 "192.168.255.255",
146 "198.18.0.0",
147 "198.19.255.255",
148 "fc00::",
149 "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
150 "fe80::",
151 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
152 "::ffff:192.168.1.1",
153 "localhost.",
154 "localhost..",
155 "devbox.",
156 "devbox..",
157 "printer.local.",
158 "printer.local..",
159 "printer.home.arpa.",
160 "printer.home.arpa..",
161 "devbox.example.ts.net.",
162 "devbox.example.ts.net..",
163 ];
164 const publicHosts = [
165 "1.0.0.0",
166 "100.63.255.255",
167 "100.128.0.0",
168 "169.253.255.255",
169 "169.255.0.0",
170 "172.15.255.255",
171 "172.32.0.0",
172 "192.167.255.255",
173 "192.169.0.0",
174 "198.17.255.255",
175 "198.20.0.0",
176 "fbff:ffff::",
177 "fec0::",
178 "2001:4860:4860::8888",
179 "::ffff:8.8.8.8",
180 "example.com.",
181 ];
182 expect(privateHosts.filter((host) => !isPrivateNetworkHost(host))).toEqual([]);
183 expect(publicHosts.filter(isPrivateNetworkHost)).toEqual([]);
184 });
185
186 it("allows only globally routable hosts to reach a public favicon provider", async () => {
187 const { isPublicFaviconHost } = await import("./browserTargetResolver");
188 const nonPublic = [
189 "192.0.0.0",
190 "192.0.0.255",
191 "192.0.2.0",
192 "192.0.2.255",
193 "192.88.99.0",
194 "192.88.99.255",
195 "198.51.100.0",
196 "198.51.100.255",
197 "203.0.113.0",
198 "203.0.113.255",
199 "224.0.0.0",
200 "255.255.255.255",
201 "::2",
202 "100::",
203 "100::ffff:ffff:ffff:ffff",
204 "100:0:0:1::",
205 "100:0:0:1:ffff:ffff:ffff:ffff",
206 "64:ff9b:1::1",
207 "64:ff9b::a00:1",
208 "64:ff9b::7f00:1",
209 "64:ff9b::c0a8:101",
210 "64:ff9b::c000:201",
211 "2001:5::1",
212 "2001:2::",
213 "2001:2:0:ffff:ffff:ffff:ffff:ffff",
214 "2001:db8::",
215 "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff",
216 "3fff::",
217 "3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff",
218 "5f00::1",
219 "fec0::",
220 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
221 "::ffff:192.0.2.1",
222 "app.test",
223 "app.test..",
224 "printer.local..",
225 "printer.home.arpa..",
226 "devbox.example.ts.net..",
227 "127.0.0.1..",
228 "127.1..",
229 "10.1..",
230 "172.16.1..",
231 "192.168.1..",
232 "service.internal",
233 "hidden.onion",
234 ];
235 const publicHosts = [
236 "191.255.255.255",
237 "192.0.1.255",
238 "192.0.3.0",
239 "198.51.99.255",
240 "198.51.101.0",
241 "203.0.112.255",
242 "203.0.114.0",
243 "223.255.255.255",
244 "1.1.1.1",
245 "2001:4860:4860::8888",
246 "2606:4700:4700::1111",
247 "64:ff9b::808:808",
248 "2001:1::1",
249 "2001:3::1",
250 "2001:4:112::1",
251 "2001:20::1",
252 "2001:30::1",
253 "::ffff:8.8.8.8",
254 "example.com",
255 "example.com.",
256 ];
257 expect(nonPublic.filter(isPublicFaviconHost)).toEqual([]);
258 expect(publicHosts.filter((host) => !isPublicFaviconHost(host))).toEqual([]);
259 });
260});