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();
5
6vi.mock("~/state/session", () => ({ readPreparedConnection }));
7
8describe("browser target resolver", () => {
9 beforeEach(() => readPreparedConnection.mockReset());
10
11 it("maps environment ports onto a private network host", async () => {
12 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
13 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
14 expect(
15 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
16 kind: "environment-port",
17 port: 5173,
18 path: "/dashboard",
19 }),
20 ).toEqual({
21 requestedUrl: "http://localhost:5173/dashboard",
22 resolvedUrl: "http://192.168.1.25:5173/dashboard",
23 resolutionKind: "direct-private-network",
24 environmentId: "environment-1",
25 });
26 });
27
28 it("preserves explicit loopback URL navigation for a remote Tailscale environment", async () => {
29 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });
30 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
31 expect(
32 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
33 kind: "url",
34 url: "http://localhost:5173/dashboard?mode=test#results",
35 }),
36 ).toEqual({
37 requestedUrl: "http://localhost:5173/dashboard?mode=test#results",
38 resolvedUrl: "http://localhost:5173/dashboard?mode=test#results",
39 resolutionKind: "direct",
40 environmentId: "environment-1",
41 });
42 });
43
44 it("preserves explicit IPv4 loopback URL navigation for a private network environment", async () => {
45 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.50:3773" });
46 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
47 expect(
48 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
49 kind: "url",
50 url: "http://127.0.0.1:5999/",
51 }),
52 ).toEqual({
53 requestedUrl: "http://127.0.0.1:5999/",
54 resolvedUrl: "http://127.0.0.1:5999/",
55 resolutionKind: "direct",
56 environmentId: "environment-1",
57 });
58 });
59
60 it("preserves URL credentials on explicit loopback navigation", async () => {
61 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });
62 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
63 expect(
64 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
65 kind: "url",
66 url: "http://user:p%40ss@localhost:5173/dashboard",
67 }).resolvedUrl,
68 ).toBe("http://user:p%40ss@localhost:5173/dashboard");
69 });
70
71 it("preserves credentialed loopback URLs for private IPv6 environments", async () => {
72 readPreparedConnection.mockReturnValue({
73 httpBaseUrl: "http://[fd7a:115c:a1e0::53]:3773",
74 });
75 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
76 expect(
77 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
78 kind: "url",
79 url: "http://user:p%40ss@localhost:5173/dashboard?mode=test#results",
80 }).resolvedUrl,
81 ).toBe("http://user:p%40ss@localhost:5173/dashboard?mode=test#results");
82 });
83
84 it("preserves schemeless localhost navigation for a remote environment", async () => {
85 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
86 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
87 expect(
88 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
89 kind: "url",
90 url: "localhost:3000/app",
91 }).resolvedUrl,
92 ).toBe("localhost:3000/app");
93 });
94
95 it("keeps localhost navigation local for a local environment", async () => {
96 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
97 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
98 expect(
99 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
100 kind: "url",
101 url: "localhost:3000/app",
102 }),
103 ).toEqual({
104 requestedUrl: "localhost:3000/app",
105 resolvedUrl: "localhost:3000/app",
106 resolutionKind: "direct",
107 environmentId: "environment-1",
108 });
109 });
110
111 it("keeps localhost navigation local for the full IPv4 loopback range", async () => {
112 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.2:3773" });
113 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
114 expect(
115 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
116 kind: "url",
117 url: "http://localhost:3000/app",
118 }),
119 ).toEqual({
120 requestedUrl: "http://localhost:3000/app",
121 resolvedUrl: "http://localhost:3000/app",
122 resolutionKind: "direct",
123 environmentId: "environment-1",
124 });
125 });
126
127 it("refuses public relay hosts until the authenticated gateway exists", async () => {
128 readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
129 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
130 expect(() =>
131 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
132 kind: "environment-port",
133 port: 5173,
134 }),
135 ).toThrow(/authenticated preview gateway/);
136 expect(
137 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
138 kind: "url",
139 url: "http://localhost:5173",
140 }),
141 ).toMatchObject({ resolvedUrl: "http://localhost:5173", resolutionKind: "direct" });
142 });
143
144 it("normalizes schemeless localhost server-picker values", async () => {
145 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://localhost:3773" });
146 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
147 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173")).toBe(
148 "http://localhost:5173/",
149 );
150 expect(
151 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "0.0.0.0:3000/app"),
152 ).toBe("http://localhost:3000/app");
153 });
154
155 it("maps discovered loopback servers onto a remote environment host", async () => {
156 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
157 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
158 expect(
159 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3000/app"),
160 ).toBe("http://192.168.1.25:3000/app");
161 });
162
163 it("preserves localhost server-picker values when the prepared base is 127.0.0.1", async () => {
164 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
165 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
166 expect(
167 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173/app?x=1#top"),
168 ).toBe("http://localhost:5173/app?x=1#top");
169 });
170
171 it("normalizes public URLs without treating them as environment ports", async () => {
172 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
173 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "example.com/app")).toBe(
174 "https://example.com/app",
175 );
176 });
177
178 it("supports private IPv6 environment hosts", async () => {
179 readPreparedConnection.mockReturnValue({
180 httpBaseUrl: "http://[fd7a:115c:a1e0::53]:3773",
181 });
182 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
183 expect(
184 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
185 kind: "environment-port",
186 port: 5173,
187 path: "/app?mode=test",
188 }).resolvedUrl,
189 ).toBe("http://[fd7a:115c:a1e0::53]:5173/app?mode=test");
190 });
191
192 it("supports a local IPv6 environment host", async () => {
193 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://[::1]:3773" });
194 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
195 expect(
196 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
197 kind: "environment-port",
198 port: 5173,
199 }).resolvedUrl,
200 ).toBe("http://localhost:5173/");
201 });
202
203 it("maps local IPv4 environment ports onto localhost for dual-stack guests", async () => {
204 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
205 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
206 expect(
207 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
208 kind: "environment-port",
209 port: 5173,
210 path: "/app",
211 }).resolvedUrl,
212 ).toBe("http://localhost:5173/app");
213 });
214
215 it("leaves malformed input for the normal navigation error path", async () => {
216 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
217 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), " ")).toBe(" ");
218 });
219
220 it("classifies exact private IPv4 and IPv6 boundaries", async () => {
221 const { isPrivateNetworkHost } = await import("./browserTargetResolver");
222 const privateHosts = [
223 "0.0.0.0",
224 "10.0.0.0",
225 "10.255.255.255",
226 "100.64.0.0",
227 "100.127.255.255",
228 "127.0.0.0",
229 "127.255.255.255",
230 "169.254.0.0",
231 "169.254.255.255",
232 "172.16.0.0",
233 "172.31.255.255",
234 "192.168.0.0",
235 "192.168.255.255",
236 "198.18.0.0",
237 "198.19.255.255",
238 "fc00::",
239 "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
240 "fe80::",
241 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
242 "::ffff:192.168.1.1",
243 "localhost.",
244 "localhost..",
245 "devbox.",
246 "devbox..",
247 "printer.local.",
248 "printer.local..",
249 "printer.home.arpa.",
250 "printer.home.arpa..",
251 "devbox.example.ts.net.",
252 "devbox.example.ts.net..",
253 ];
254 const publicHosts = [
255 "1.0.0.0",
256 "100.63.255.255",
257 "100.128.0.0",
258 "169.253.255.255",
259 "169.255.0.0",
260 "172.15.255.255",
261 "172.32.0.0",
262 "192.167.255.255",
263 "192.169.0.0",
264 "198.17.255.255",
265 "198.20.0.0",
266 "fbff:ffff::",
267 "fec0::",
268 "2001:4860:4860::8888",
269 "::ffff:8.8.8.8",
270 "example.com.",
271 ];
272 expect(privateHosts.filter((host) => !isPrivateNetworkHost(host))).toEqual([]);
273 expect(publicHosts.filter(isPrivateNetworkHost)).toEqual([]);
274 });
275
276 it("allows only globally routable hosts to reach a public favicon provider", async () => {
277 const { isPublicFaviconHost } = await import("./browserTargetResolver");
278 const nonPublic = [
279 "192.0.0.0",
280 "192.0.0.255",
281 "192.0.2.0",
282 "192.0.2.255",
283 "192.88.99.0",
284 "192.88.99.255",
285 "198.51.100.0",
286 "198.51.100.255",
287 "203.0.113.0",
288 "203.0.113.255",
289 "224.0.0.0",
290 "255.255.255.255",
291 "::2",
292 "100::",
293 "100::ffff:ffff:ffff:ffff",
294 "100:0:0:1::",
295 "100:0:0:1:ffff:ffff:ffff:ffff",
296 "64:ff9b:1::1",
297 "64:ff9b::a00:1",
298 "64:ff9b::7f00:1",
299 "64:ff9b::c0a8:101",
300 "64:ff9b::c000:201",
301 "2001:5::1",
302 "2001:2::",
303 "2001:2:0:ffff:ffff:ffff:ffff:ffff",
304 "2001:db8::",
305 "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff",
306 "3fff::",
307 "3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff",
308 "5f00::1",
309 "fec0::",
310 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
311 "::ffff:192.0.2.1",
312 "app.test",
313 "app.test..",
314 "printer.local..",
315 "printer.home.arpa..",
316 "devbox.example.ts.net..",
317 "127.0.0.1..",
318 "127.1..",
319 "10.1..",
320 "172.16.1..",
321 "192.168.1..",
322 "service.internal",
323 "hidden.onion",
324 ];
325 const publicHosts = [
326 "191.255.255.255",
327 "192.0.1.255",
328 "192.0.3.0",
329 "198.51.99.255",
330 "198.51.101.0",
331 "203.0.112.255",
332 "203.0.114.0",
333 "223.255.255.255",
334 "1.1.1.1",
335 "2001:4860:4860::8888",
336 "2606:4700:4700::1111",
337 "64:ff9b::808:808",
338 "2001:1::1",
339 "2001:3::1",
340 "2001:4:112::1",
341 "2001:20::1",
342 "2001:30::1",
343 "::ffff:8.8.8.8",
344 "example.com",
345 "example.com.",
346 ];
347 expect(nonPublic.filter(isPublicFaviconHost)).toEqual([]);
348 expect(publicHosts.filter((host) => !isPublicFaviconHost(host))).toEqual([]);
349 });
350});