Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2import { EnvironmentId } from "@t3tools/contracts";
3
4import {
5 getPrimaryKnownEnvironment,
6 resolveInitialPrimaryEnvironmentDescriptor,
7 resetPrimaryEnvironmentDescriptorForTests,
8 writePrimaryEnvironmentDescriptor,
9} from ".";
10
11function jsonResponse(body: unknown, init?: ResponseInit) {
12 return new Response(JSON.stringify(body), {
13 headers: {
14 "content-type": "application/json",
15 },
16 status: 200,
17 ...init,
18 });
19}
20
21const BASE_ENVIRONMENT = {
22 environmentId: "environment-local",
23 label: "Local environment",
24 platform: {
25 os: "darwin",
26 arch: "arm64",
27 },
28 serverVersion: "0.0.0-test",
29 capabilities: {
30 repositoryIdentity: true,
31 },
32};
33
34function installTestBrowser(url: string) {
35 vi.stubGlobal("window", {
36 location: new URL(url),
37 history: {
38 replaceState: vi.fn(),
39 },
40 });
41}
42
43describe("environmentBootstrap", () => {
44 beforeEach(() => {
45 vi.restoreAllMocks();
46 vi.useRealTimers();
47 installTestBrowser("http://localhost/");
48 });
49
50 afterEach(() => {
51 resetPrimaryEnvironmentDescriptorForTests();
52 vi.unstubAllEnvs();
53 vi.useRealTimers();
54 vi.restoreAllMocks();
55 vi.unstubAllGlobals();
56 });
57
58 it("attaches the bootstrapped environment descriptor to the primary environment", () => {
59 vi.stubGlobal("window", {
60 location: {
61 origin: "http://localhost:3773",
62 },
63 desktopBridge: undefined,
64 });
65 writePrimaryEnvironmentDescriptor({
66 environmentId: EnvironmentId.makeUnsafe("environment-local"),
67 label: "Bootstrapped environment",
68 platform: {
69 os: "darwin",
70 arch: "arm64",
71 },
72 serverVersion: "0.0.0-test",
73 capabilities: {
74 repositoryIdentity: true,
75 },
76 });
77
78 expect(getPrimaryKnownEnvironment()).toEqual({
79 id: "environment-local",
80 label: "Bootstrapped environment",
81 source: "window-origin",
82 environmentId: "environment-local",
83 target: {
84 httpBaseUrl: "http://localhost:3773/",
85 wsBaseUrl: "ws://localhost:3773/",
86 },
87 });
88 });
89
90 it("reuses an in-flight descriptor bootstrap request", async () => {
91 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
92 vi.stubGlobal("fetch", fetchMock);
93
94 await Promise.all([
95 resolveInitialPrimaryEnvironmentDescriptor(),
96 resolveInitialPrimaryEnvironmentDescriptor(),
97 ]);
98
99 expect(fetchMock).toHaveBeenCalledTimes(1);
100 expect(fetchMock).toHaveBeenCalledWith("http://localhost/.well-known/t3/environment");
101 });
102
103 it("uses https descriptor urls when the primary environment uses wss", async () => {
104 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
105 vi.stubGlobal("fetch", fetchMock);
106 vi.stubEnv("VITE_HTTP_URL", "https://remote.example.com");
107 vi.stubEnv("VITE_WS_URL", "wss://remote.example.com");
108
109 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
110 expect(fetchMock).toHaveBeenCalledWith("https://remote.example.com/.well-known/t3/environment");
111 });
112
113 it("uses the current origin as the descriptor base for local dev environments", async () => {
114 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
115 vi.stubGlobal("fetch", fetchMock);
116 installTestBrowser("http://localhost:5735/");
117
118 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
119 expect(fetchMock).toHaveBeenCalledWith("http://localhost:5735/.well-known/t3/environment");
120 });
121
122 it("uses the vite proxy for desktop-managed loopback descriptor requests during local dev", async () => {
123 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
124 vi.stubGlobal("fetch", fetchMock);
125 vi.stubEnv("VITE_DEV_SERVER_URL", "http://127.0.0.1:5733");
126 vi.stubGlobal("window", {
127 location: new URL("http://127.0.0.1:5733/"),
128 history: {
129 replaceState: vi.fn(),
130 },
131 desktopBridge: {
132 getLocalEnvironmentBootstrap: () => ({
133 label: "Local environment",
134 httpBaseUrl: "http://127.0.0.1:3773",
135 wsBaseUrl: "ws://127.0.0.1:3773",
136 bootstrapToken: "desktop-bootstrap-token",
137 }),
138 },
139 });
140
141 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
142 expect(fetchMock).toHaveBeenCalledWith("http://127.0.0.1:5733/.well-known/t3/environment");
143 });
144});