Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserTargetResolver.test.ts

261 lines8.8 KB

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 mode serves the backend on the distro IP, but a loopback-only dev
106 // server there answers only on localhost from the Windows desktop.
107 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
108 env.isElectron = true;
109 try {
110 for (const target of [
111 { _tag: "BearerConnectionTarget", connectionId: "local:wsl:Ubuntu" },
112 { _tag: "PrimaryConnectionTarget" },
113 ]) {
114 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://172.24.66.27:3773", target });
115 expect(
116 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
117 ).toBe("http://localhost:3001/");
118 }
119 readPreparedConnection.mockReturnValue({
120 httpBaseUrl: "http://192.168.1.25:3773",
121 target: { _tag: "BearerConnectionTarget", connectionId: "saved:remote" },
122 });
123 expect(
124 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
125 ).toBe("http://192.168.1.25:3001/");
126 } finally {
127 env.isElectron = false;
128 }
129 });
130​
131 it("classifies exact private IPv4 and IPv6 boundaries", async () => {
132 const { isPrivateNetworkHost } = await import("./browserTargetResolver");
133 const privateHosts = [
134 "0.0.0.0",
135 "10.0.0.0",
136 "10.255.255.255",
137 "100.64.0.0",
138 "100.127.255.255",
139 "127.0.0.0",
140 "127.255.255.255",
141 "169.254.0.0",
142 "169.254.255.255",
143 "172.16.0.0",
144 "172.31.255.255",
145 "192.168.0.0",
146 "192.168.255.255",
147 "198.18.0.0",
148 "198.19.255.255",
149 "fc00::",
150 "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
151 "fe80::",
152 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
153 "::ffff:192.168.1.1",
154 "localhost.",
155 "localhost..",
156 "devbox.",
157 "devbox..",
158 "printer.local.",
159 "printer.local..",
160 "printer.home.arpa.",
161 "printer.home.arpa..",
162 "devbox.example.ts.net.",
163 "devbox.example.ts.net..",
164 ];
165 const publicHosts = [
166 "1.0.0.0",
167 "100.63.255.255",
168 "100.128.0.0",
169 "169.253.255.255",
170 "169.255.0.0",
171 "172.15.255.255",
172 "172.32.0.0",
173 "192.167.255.255",
174 "192.169.0.0",
175 "198.17.255.255",
176 "198.20.0.0",
177 "fbff:ffff::",
178 "fec0::",
179 "2001:4860:4860::8888",
180 "::ffff:8.8.8.8",
181 "example.com.",
182 ];
183 expect(privateHosts.filter((host) => !isPrivateNetworkHost(host))).toEqual([]);
184 expect(publicHosts.filter(isPrivateNetworkHost)).toEqual([]);
185 });
186​
187 it("allows only globally routable hosts to reach a public favicon provider", async () => {
188 const { isPublicFaviconHost } = await import("./browserTargetResolver");
189 const nonPublic = [
190 "192.0.0.0",
191 "192.0.0.255",
192 "192.0.2.0",
193 "192.0.2.255",
194 "192.88.99.0",
195 "192.88.99.255",
196 "198.51.100.0",
197 "198.51.100.255",
198 "203.0.113.0",
199 "203.0.113.255",
200 "224.0.0.0",
201 "255.255.255.255",
202 "::2",
203 "100::",
204 "100::ffff:ffff:ffff:ffff",
205 "100:0:0:1::",
206 "100:0:0:1:ffff:ffff:ffff:ffff",
207 "64:ff9b:1::1",
208 "64:ff9b::a00:1",
209 "64:ff9b::7f00:1",
210 "64:ff9b::c0a8:101",
211 "64:ff9b::c000:201",
212 "2001:5::1",
213 "2001:2::",
214 "2001:2:0:ffff:ffff:ffff:ffff:ffff",
215 "2001:db8::",
216 "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff",
217 "3fff::",
218 "3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff",
219 "5f00::1",
220 "fec0::",
221 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
222 "::ffff:192.0.2.1",
223 "app.test",
224 "app.test..",
225 "printer.local..",
226 "printer.home.arpa..",
227 "devbox.example.ts.net..",
228 "127.0.0.1..",
229 "127.1..",
230 "10.1..",
231 "172.16.1..",
232 "192.168.1..",
233 "service.internal",
234 "hidden.onion",
235 ];
236 const publicHosts = [
237 "191.255.255.255",
238 "192.0.1.255",
239 "192.0.3.0",
240 "198.51.99.255",
241 "198.51.101.0",
242 "203.0.112.255",
243 "203.0.114.0",
244 "223.255.255.255",
245 "1.1.1.1",
246 "2001:4860:4860::8888",
247 "2606:4700:4700::1111",
248 "64:ff9b::808:808",
249 "2001:1::1",
250 "2001:3::1",
251 "2001:4:112::1",
252 "2001:20::1",
253 "2001:30::1",
254 "::ffff:8.8.8.8",
255 "example.com",
256 "example.com.",
257 ];
258 expect(nonPublic.filter(isPublicFaviconHost)).toEqual([]);
259 expect(publicHosts.filter((host) => !isPublicFaviconHost(host))).toEqual([]);
260 });
261});