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});