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

bootstrap.test.ts

170 lines5.7 KB

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 });
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 });
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.make("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("derives the websocket url when only VITE_HTTP_URL is configured", async () => {
114 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
115 vi.stubGlobal("fetch", fetchMock);
116 vi.stubEnv("VITE_HTTP_URL", "https://remote.example.com");
117​
118 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
119 expect(fetchMock).toHaveBeenCalledWith("https://remote.example.com/.well-known/t3/environment");
120 expect(getPrimaryKnownEnvironment()?.target).toEqual({
121 httpBaseUrl: "https://remote.example.com/",
122 wsBaseUrl: "wss://remote.example.com/",
123 });
124 });
125​
126 it("derives the http url when only VITE_WS_URL is configured", async () => {
127 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
128 vi.stubGlobal("fetch", fetchMock);
129 vi.stubEnv("VITE_WS_URL", "wss://remote.example.com");
130​
131 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
132 expect(fetchMock).toHaveBeenCalledWith("https://remote.example.com/.well-known/t3/environment");
133 expect(getPrimaryKnownEnvironment()?.target).toEqual({
134 httpBaseUrl: "https://remote.example.com/",
135 wsBaseUrl: "wss://remote.example.com/",
136 });
137 });
138​
139 it("uses the current origin as the descriptor base for local dev environments", async () => {
140 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
141 vi.stubGlobal("fetch", fetchMock);
142 installTestBrowser("http://localhost:5735/");
143​
144 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
145 expect(fetchMock).toHaveBeenCalledWith("http://localhost:5735/.well-known/t3/environment");
146 });
147​
148 it("uses the vite proxy for desktop-managed loopback descriptor requests during local dev", async () => {
149 const fetchMock = vi.fn<typeof fetch>().mockResolvedValue(jsonResponse(BASE_ENVIRONMENT));
150 vi.stubGlobal("fetch", fetchMock);
151 vi.stubEnv("VITE_DEV_SERVER_URL", "http://127.0.0.1:5733");
152 vi.stubGlobal("window", {
153 location: new URL("http://127.0.0.1:5733/"),
154 history: {
155 replaceState: vi.fn(),
156 },
157 desktopBridge: {
158 getLocalEnvironmentBootstrap: () => ({
159 label: "Local environment",
160 httpBaseUrl: "http://127.0.0.1:3773",
161 wsBaseUrl: "ws://127.0.0.1:3773",
162 bootstrapToken: "desktop-bootstrap-token",
163 }),
164 },
165 });
166​
167 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
168 expect(fetchMock).toHaveBeenCalledWith("http://127.0.0.1:5733/.well-known/t3/environment");
169 });
170});