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

bootstrap.test.ts

254 lines8.3 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 getLocalEnvironmentBootstrap: () => ({
177 label: "Local environment",
178 httpBaseUrl: "http://127.0.0.1:3773",
179 wsBaseUrl: "ws://127.0.0.1:3773",
180 bootstrapToken: "desktop-bootstrap-token",
181 }),
182 },
183 });
184 await installDescriptorApi();
185​
186 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
187 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
188 "http://127.0.0.1:5733/.well-known/t3/environment",
189 );
190 });
191​
192 it("retains the URL parser cause without exposing the configured URL in its message", () => {
193 vi.stubEnv("VITE_HTTP_URL", "http://[");
194​
195 const error = captureThrown(readPrimaryEnvironmentTarget);
196​
197 expect(isPrimaryEnvironmentUrlInvalidError(error)).toBe(true);
198 if (!isPrimaryEnvironmentUrlInvalidError(error)) {
199 throw new Error("Expected a structured primary environment URL error.");
200 }
201 expect(error).toMatchObject({
202 source: "configured",
203 urlKind: "http-base-url",
204 message: "Could not parse http-base-url for the configured primary environment target.",
205 });
206 expect(error.cause).toBeInstanceOf(TypeError);
207 expect(error.message).not.toContain("http://[");
208 });
209​
210 it("describes which desktop bootstrap endpoint is missing", () => {
211 vi.stubGlobal("window", {
212 location: new URL("http://127.0.0.1:5733/"),
213 history: { replaceState: vi.fn() },
214 desktopBridge: {
215 getLocalEnvironmentBootstrap: () => ({
216 label: "Local environment",
217 httpBaseUrl: "http://127.0.0.1:3773",
218 bootstrapToken: "desktop-bootstrap-token",
219 }),
220 },
221 });
222​
223 const error = captureThrown(readPrimaryEnvironmentTarget);
224​
225 expect(isDesktopEnvironmentBootstrapIncompleteError(error)).toBe(true);
226 if (!isDesktopEnvironmentBootstrapIncompleteError(error)) {
227 throw new Error("Expected a structured desktop bootstrap error.");
228 }
229 expect(error).toMatchObject({
230 hasHttpBaseUrl: true,
231 hasWsBaseUrl: false,
232 message: "Desktop bootstrap is missing wsBaseUrl for the local environment.",
233 });
234 });
235​
236 it("preserves an unsupported window-origin protocol", () => {
237 vi.stubGlobal("window", {
238 location: { origin: "file:///tmp/t3code/" },
239 history: { replaceState: vi.fn() },
240 });
241​
242 const error = captureThrown(readPrimaryEnvironmentTarget);
243​
244 expect(isPrimaryEnvironmentProtocolUnsupportedError(error)).toBe(true);
245 if (!isPrimaryEnvironmentProtocolUnsupportedError(error)) {
246 throw new Error("Expected a structured primary environment protocol error.");
247 }
248 expect(error).toMatchObject({
249 source: "window-origin",
250 protocol: "file:",
251 message: "The window-origin primary environment target uses unsupported protocol file:.",
252 });
253 });
254});