The best way to control Claude Code, Codex and more from any computer or phone

main
ts 200 lines 6.36 KB
Raw
1 import { EnvironmentId } from "@t3tools/contracts";
2 import { isPrivateNetworkHost, isPublicFaviconHost } from "@t3tools/shared/hostClassification";
3 import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5 const readPreparedConnection = vi.fn();
6 const env = vi.hoisted(() => ({ isElectron: false }));
7
8 vi.mock("~/state/session", () => ({ readPreparedConnection }));
9 vi.mock("~/env", () => env);
10
11 describe("browser target resolver", () => {
12 beforeEach(() => readPreparedConnection.mockReset());
13
14 it("refuses public relay hosts until the authenticated gateway exists", async () => {
15 readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
16 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
17 expect(
18 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "http://localhost:5173"),
19 ).toBe("http://localhost:5173");
20 });
21
22 it("normalizes schemeless localhost server-picker values", async () => {
23 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://localhost:3773" });
24 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
25 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173")).toBe(
26 "http://localhost:5173/",
27 );
28 expect(
29 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "0.0.0.0:3000/app"),
30 ).toBe("http://localhost:3000/app");
31 });
32
33 it("maps discovered loopback servers onto a remote environment host", async () => {
34 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });
35 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
36 expect(
37 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3000/app"),
38 ).toBe("http://192.168.1.25:3000/app");
39 });
40
41 it("keeps localhost for servers on the desktop's own WSL or primary backend", async () => {
42 // WSL NAT advertises a distro IP, but loopback-bound servers only answer on localhost.
43 const { resolveDiscoveredServerUrl, resolveExternalServerUrl } =
44 await import("./browserTargetResolver");
45 env.isElectron = true;
46 try {
47 for (const target of [
48 { _tag: "BearerConnectionTarget", connectionId: "local:wsl:Ubuntu" },
49 { _tag: "PrimaryConnectionTarget" },
50 ]) {
51 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://172.24.66.27:3773", target });
52 expect(
53 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
54 ).toBe("http://localhost:3001/");
55 }
56 readPreparedConnection.mockReturnValue({
57 httpBaseUrl: "http://192.168.1.25:3773",
58 target: { _tag: "BearerConnectionTarget", connectionId: "saved:remote" },
59 });
60 // The webview's partition tunnels to the environment; the system browser has no tunnel.
61 expect(
62 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/"),
63 ).toBe("http://localhost:3001/");
64 expect(resolveExternalServerUrl(EnvironmentId.make("environment-1"), "localhost:3001/")).toBe(
65 "http://192.168.1.25:3001/",
66 );
67 } finally {
68 env.isElectron = false;
69 }
70 });
71
72 it("classifies exact private IPv4 and IPv6 boundaries", async () => {
73 const privateHosts = [
74 "0.0.0.0",
75 "10.0.0.0",
76 "10.255.255.255",
77 "100.64.0.0",
78 "100.127.255.255",
79 "127.0.0.0",
80 "127.255.255.255",
81 "169.254.0.0",
82 "169.254.255.255",
83 "172.16.0.0",
84 "172.31.255.255",
85 "192.168.0.0",
86 "192.168.255.255",
87 "198.18.0.0",
88 "198.19.255.255",
89 "fc00::",
90 "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
91 "fe80::",
92 "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
93 "::ffff:192.168.1.1",
94 "localhost.",
95 "localhost..",
96 "devbox.",
97 "devbox..",
98 "printer.local.",
99 "printer.local..",
100 "printer.home.arpa.",
101 "printer.home.arpa..",
102 "devbox.example.ts.net.",
103 "devbox.example.ts.net..",
104 ];
105 const publicHosts = [
106 "1.0.0.0",
107 "100.63.255.255",
108 "100.128.0.0",
109 "169.253.255.255",
110 "169.255.0.0",
111 "172.15.255.255",
112 "172.32.0.0",
113 "192.167.255.255",
114 "192.169.0.0",
115 "198.17.255.255",
116 "198.20.0.0",
117 "fbff:ffff::",
118 "fec0::",
119 "2001:4860:4860::8888",
120 "::ffff:8.8.8.8",
121 "example.com.",
122 ];
123 expect(privateHosts.filter((host) => !isPrivateNetworkHost(host))).toEqual([]);
124 expect(publicHosts.filter(isPrivateNetworkHost)).toEqual([]);
125 });
126
127 it("allows only globally routable hosts to reach a public favicon provider", async () => {
128 const nonPublic = [
129 "192.0.0.0",
130 "192.0.0.255",
131 "192.0.2.0",
132 "192.0.2.255",
133 "192.88.99.0",
134 "192.88.99.255",
135 "198.51.100.0",
136 "198.51.100.255",
137 "203.0.113.0",
138 "203.0.113.255",
139 "224.0.0.0",
140 "255.255.255.255",
141 "::2",
142 "100::",
143 "100::ffff:ffff:ffff:ffff",
144 "100:0:0:1::",
145 "100:0:0:1:ffff:ffff:ffff:ffff",
146 "64:ff9b:1::1",
147 "64:ff9b::a00:1",
148 "64:ff9b::7f00:1",
149 "64:ff9b::c0a8:101",
150 "64:ff9b::c000:201",
151 "2001:5::1",
152 "2001:2::",
153 "2001:2:0:ffff:ffff:ffff:ffff:ffff",
154 "2001:db8::",
155 "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff",
156 "3fff::",
157 "3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff",
158 "5f00::1",
159 "fec0::",
160 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
161 "::ffff:192.0.2.1",
162 "app.test",
163 "app.test..",
164 "printer.local..",
165 "printer.home.arpa..",
166 "devbox.example.ts.net..",
167 "127.0.0.1..",
168 "127.1..",
169 "10.1..",
170 "172.16.1..",
171 "192.168.1..",
172 "service.internal",
173 "hidden.onion",
174 ];
175 const publicHosts = [
176 "191.255.255.255",
177 "192.0.1.255",
178 "192.0.3.0",
179 "198.51.99.255",
180 "198.51.101.0",
181 "203.0.112.255",
182 "203.0.114.0",
183 "223.255.255.255",
184 "1.1.1.1",
185 "2001:4860:4860::8888",
186 "2606:4700:4700::1111",
187 "64:ff9b::808:808",
188 "2001:1::1",
189 "2001:3::1",
190 "2001:4:112::1",
191 "2001:20::1",
192 "2001:30::1",
193 "::ffff:8.8.8.8",
194 "example.com",
195 "example.com.",
196 ];
197 expect(nonPublic.filter(isPublicFaviconHost)).toEqual([]);
198 expect(publicHosts.filter((host) => !isPublicFaviconHost(host))).toEqual([]);
199 });
200 });