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

browserTargetResolver.test.ts

81 lines3.4 KB

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("refuses public relay hosts until the authenticated gateway exists", async () => {
29 readPreparedConnection.mockReturnValue({ httpBaseUrl: "https://relay.example.com" });
30 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
31 expect(() =>
32 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
33 kind: "environment-port",
34 port: 5173,
35 }),
36 ).toThrow(/authenticated preview gateway/);
37 });
38​
39 it("normalizes schemeless localhost server-picker values", async () => {
40 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://localhost:3773" });
41 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
42 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173")).toBe(
43 "http://localhost:5173/",
44 );
45 expect(
46 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "0.0.0.0:3000/app"),
47 ).toBe("http://localhost:3000/app");
48 });
49​
50 it("preserves localhost server-picker values when the prepared base is 127.0.0.1", async () => {
51 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
52 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
53 expect(
54 resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:5173/app?x=1#top"),
55 ).toBe("http://localhost:5173/app?x=1#top");
56 });
57​
58 it("normalizes public URLs without treating them as environment ports", async () => {
59 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
60 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "example.com/app")).toBe(
61 "https://example.com/app",
62 );
63 });
64​
65 it("supports private IPv6 environment hosts", async () => {
66 readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://[::1]:3773" });
67 const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
68 expect(
69 resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
70 kind: "environment-port",
71 port: 5173,
72 path: "/app?mode=test",
73 }).resolvedUrl,
74 ).toBe("http://[::1]:5173/app?mode=test");
75 });
76​
77 it("leaves malformed input for the normal navigation error path", async () => {
78 const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");
79 expect(resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), " ")).toBe(" ");
80 });
81});