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