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

bootstrap.test.ts

260 lines8.4 KB

1import { EnvironmentId, type ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import {
6 getPrimaryKnownEnvironment,
7 isDesktopEnvironmentBootstrapIncompleteError,
8 isPrimaryEnvironmentProtocolUnsupportedError,
9 isPrimaryEnvironmentUrlInvalidError,
10 readPrimaryEnvironmentTarget,
11 resolvePrimaryEnvironmentHttpUrl,
12 resolveInitialPrimaryEnvironmentDescriptor,
13 resetPrimaryEnvironmentDescriptorForTests,
14 writePrimaryEnvironmentDescriptor,
15} from ".";
16import { installEnvironmentHttpTest } from "../../../test/environmentHttpTest";
17​
18const BASE_ENVIRONMENT = {
19 environmentId: EnvironmentId.make("environment-local"),
20 label: "Local environment",
21 platform: {
22 os: "darwin",
23 arch: "arm64",
24 },
25 serverVersion: "0.0.0-test",
26 capabilities: {
27 repositoryIdentity: true,
28 },
29} satisfies ExecutionEnvironmentDescriptor;
30​
31let disposeHttpTest: (() => Promise<void>) | undefined;
32​
33async function installDescriptorApi() {
34 const testApi = await installEnvironmentHttpTest({
35 descriptor: () => Effect.succeed(BASE_ENVIRONMENT),
36 });
37 disposeHttpTest = testApi.dispose;
38 return testApi;
40​
41function installTestBrowser(url: string) {
42 vi.stubGlobal("window", {
43 location: new URL(url),
44 history: {
45 replaceState: vi.fn(),
46 },
47 });
49​
50function captureThrown(run: () => unknown): unknown {
51 try {
52 run();
53 } catch (error) {
54 return error;
55 }
56 throw new Error("Expected the operation to throw.");
58​
59describe("environmentBootstrap", () => {
60 beforeEach(() => {
61 vi.restoreAllMocks();
62 vi.useRealTimers();
63 installTestBrowser("http://localhost/");
64 });
65​
66 afterEach(async () => {
67 await disposeHttpTest?.();
68 disposeHttpTest = undefined;
69 resetPrimaryEnvironmentDescriptorForTests();
70 vi.unstubAllEnvs();
71 vi.useRealTimers();
72 vi.restoreAllMocks();
73 vi.unstubAllGlobals();
74 });
75​
76 it("attaches the bootstrapped environment descriptor to the primary environment", () => {
77 vi.stubGlobal("window", {
78 location: {
79 origin: "http://localhost:3773",
80 },
81 desktopBridge: undefined,
82 });
83 writePrimaryEnvironmentDescriptor({
84 environmentId: EnvironmentId.make("environment-local"),
85 label: "Bootstrapped environment",
86 platform: {
87 os: "darwin",
88 arch: "arm64",
89 },
90 serverVersion: "0.0.0-test",
91 capabilities: {
92 repositoryIdentity: true,
93 },
94 });
95​
96 expect(getPrimaryKnownEnvironment()).toEqual({
97 id: "environment-local",
98 label: "Bootstrapped environment",
99 source: "window-origin",
100 environmentId: "environment-local",
101 target: {
102 httpBaseUrl: "http://localhost:3773/",
103 wsBaseUrl: "ws://localhost:3773/",
104 },
105 });
106 });
107​
108 it("reuses an in-flight descriptor bootstrap request", async () => {
109 const testApi = await installDescriptorApi();
110​
111 await Promise.all([
112 resolveInitialPrimaryEnvironmentDescriptor(),
113 resolveInitialPrimaryEnvironmentDescriptor(),
114 ]);
115​
116 expect(testApi.calls.descriptor).toBe(1);
117 });
118​
119 it("uses https descriptor urls when the primary environment uses wss", async () => {
120 vi.stubEnv("VITE_HTTP_URL", "https://remote.example.com");
121 vi.stubEnv("VITE_WS_URL", "wss://remote.example.com");
122 await installDescriptorApi();
123​
124 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
125 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
126 "https://remote.example.com/.well-known/t3/environment",
127 );
128 });
129​
130 it("derives the websocket url when only VITE_HTTP_URL is configured", async () => {
131 vi.stubEnv("VITE_HTTP_URL", "https://remote.example.com");
132 await installDescriptorApi();
133​
134 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
135 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
136 "https://remote.example.com/.well-known/t3/environment",
137 );
138 expect(getPrimaryKnownEnvironment()?.target).toEqual({
139 httpBaseUrl: "https://remote.example.com/",
140 wsBaseUrl: "wss://remote.example.com/",
141 });
142 });
143​
144 it("derives the http url when only VITE_WS_URL is configured", async () => {
145 vi.stubEnv("VITE_WS_URL", "wss://remote.example.com");
146 await installDescriptorApi();
147​
148 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
149 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
150 "https://remote.example.com/.well-known/t3/environment",
151 );
152 expect(getPrimaryKnownEnvironment()?.target).toEqual({
153 httpBaseUrl: "https://remote.example.com/",
154 wsBaseUrl: "wss://remote.example.com/",
155 });
156 });
157​
158 it("uses the current origin as the descriptor base for local dev environments", async () => {
159 installTestBrowser("http://localhost:5735/");
160 await installDescriptorApi();
161​
162 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
163 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
164 "http://localhost:5735/.well-known/t3/environment",
165 );
166 });
167​
168 it("uses the vite proxy for desktop-managed loopback descriptor requests during local dev", async () => {
169 vi.stubEnv("VITE_DEV_SERVER_URL", "http://127.0.0.1:5733");
170 vi.stubGlobal("window", {
171 location: new URL("http://127.0.0.1:5733/"),
172 history: {
173 replaceState: vi.fn(),
174 },
175 desktopBridge: {
176 getLocalEnvironmentBootstraps: () => [
177 {
178 id: "primary",
179 label: "Windows",
180 httpBaseUrl: "http://127.0.0.1:3773",
181 wsBaseUrl: "ws://127.0.0.1:3773",
182 bootstrapToken: "desktop-bootstrap-token",
183 },
184 ],
185 },
186 });
187 await installDescriptorApi();
188​
189 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
190 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
191 "http://127.0.0.1:5733/.well-known/t3/environment",
192 );
193 });
194​
195 it("retains the URL parser cause without exposing the configured URL in its message", () => {
196 vi.stubEnv("VITE_HTTP_URL", "http://[");
197​
198 const error = captureThrown(readPrimaryEnvironmentTarget);
199​
200 expect(isPrimaryEnvironmentUrlInvalidError(error)).toBe(true);
201 if (!isPrimaryEnvironmentUrlInvalidError(error)) {
202 throw new Error("Expected a structured primary environment URL error.");
203 }
204 expect(error).toMatchObject({
205 source: "configured",
206 urlKind: "http-base-url",
207 message: "Could not parse http-base-url for the configured primary environment target.",
208 });
209 expect(error.cause).toBeInstanceOf(TypeError);
210 expect(error.message).not.toContain("http://[");
211 });
212​
213 it("describes which desktop bootstrap endpoint is missing", () => {
214 vi.stubGlobal("window", {
215 location: new URL("http://127.0.0.1:5733/"),
216 history: { replaceState: vi.fn() },
217 desktopBridge: {
218 getLocalEnvironmentBootstraps: () => [
219 {
220 id: "primary",
221 label: "Local environment",
222 httpBaseUrl: "http://127.0.0.1:3773",
223 bootstrapToken: "desktop-bootstrap-token",
224 },
225 ],
226 },
227 });
228​
229 const error = captureThrown(readPrimaryEnvironmentTarget);
230​
231 expect(isDesktopEnvironmentBootstrapIncompleteError(error)).toBe(true);
232 if (!isDesktopEnvironmentBootstrapIncompleteError(error)) {
233 throw new Error("Expected a structured desktop bootstrap error.");
234 }
235 expect(error).toMatchObject({
236 hasHttpBaseUrl: true,
237 hasWsBaseUrl: false,
238 message: "Desktop bootstrap is missing wsBaseUrl for the local environment.",
239 });
240 });
241​
242 it("preserves an unsupported window-origin protocol", () => {
243 vi.stubGlobal("window", {
244 location: { origin: "file:///tmp/t3code/" },
245 history: { replaceState: vi.fn() },
246 });
247​
248 const error = captureThrown(readPrimaryEnvironmentTarget);
249​
250 expect(isPrimaryEnvironmentProtocolUnsupportedError(error)).toBe(true);
251 if (!isPrimaryEnvironmentProtocolUnsupportedError(error)) {
252 throw new Error("Expected a structured primary environment protocol error.");
253 }
254 expect(error).toMatchObject({
255 source: "window-origin",
256 protocol: "file:",
257 message: "The window-origin primary environment target uses unsupported protocol file:.",
258 });
259 });
260});