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

bootstrap.test.ts

128 lines3.9 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 readPrimaryEnvironmentTarget,
7 resolvePrimaryEnvironmentHttpUrl,
8 resolveInitialPrimaryEnvironmentDescriptor,
9 resetPrimaryEnvironmentDescriptorForTests,
10} from ".";
11import { installEnvironmentHttpTest } from "../../../test/environmentHttpTest";
12​
13const BASE_ENVIRONMENT = {
14 environmentId: EnvironmentId.make("environment-local"),
15 label: "Local environment",
16 platform: {
17 os: "darwin",
18 arch: "arm64",
19 },
20 serverVersion: "0.0.0-test",
21 capabilities: {
22 repositoryIdentity: true,
23 },
24} satisfies ExecutionEnvironmentDescriptor;
25​
26let disposeHttpTest: (() => Promise<void>) | undefined;
27​
28async function installDescriptorApi() {
29 const testApi = await installEnvironmentHttpTest({
30 descriptor: () => Effect.succeed(BASE_ENVIRONMENT),
31 });
32 disposeHttpTest = testApi.dispose;
33 return testApi;
35​
36function installTestBrowser(url: string) {
37 vi.stubGlobal("window", {
38 location: new URL(url),
39 history: {
40 replaceState: vi.fn(),
41 },
42 });
44​
45describe("environmentBootstrap", () => {
46 beforeEach(() => {
47 vi.restoreAllMocks();
48 vi.useRealTimers();
49 installTestBrowser("http://localhost/");
50 });
51​
52 afterEach(async () => {
53 await disposeHttpTest?.();
54 disposeHttpTest = undefined;
55 resetPrimaryEnvironmentDescriptorForTests();
56 vi.unstubAllEnvs();
57 vi.useRealTimers();
58 vi.restoreAllMocks();
59 vi.unstubAllGlobals();
60 });
61​
62 it("reuses an in-flight descriptor bootstrap request", async () => {
63 const testApi = await installDescriptorApi();
64​
65 await Promise.all([
66 resolveInitialPrimaryEnvironmentDescriptor(),
67 resolveInitialPrimaryEnvironmentDescriptor(),
68 ]);
69​
70 expect(testApi.calls.descriptor).toBe(1);
71 });
72​
73 it("uses https descriptor urls when the primary environment uses wss", async () => {
74 vi.stubEnv("VITE_HTTP_URL", "https://remote.example.com");
75 vi.stubEnv("VITE_WS_URL", "wss://remote.example.com");
76 await installDescriptorApi();
77​
78 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
79 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
80 "https://remote.example.com/.well-known/t3/environment",
81 );
82 });
83​
84 it("keeps an uppercase wss scheme secure when deriving the http url", () => {
85 vi.stubEnv("VITE_WS_URL", "WSS://remote.example.com");
86​
87 expect(readPrimaryEnvironmentTarget()?.target).toEqual({
88 httpBaseUrl: "https://remote.example.com/",
89 wsBaseUrl: "wss://remote.example.com/",
90 });
91 });
92​
93 it("keeps an uppercase https scheme secure when deriving the websocket url", () => {
94 vi.stubEnv("VITE_HTTP_URL", "HTTPS://remote.example.com");
95​
96 expect(readPrimaryEnvironmentTarget()?.target).toEqual({
97 httpBaseUrl: "https://remote.example.com/",
98 wsBaseUrl: "wss://remote.example.com/",
99 });
100 });
101​
102 it("uses the vite proxy for desktop-managed loopback descriptor requests during local dev", async () => {
103 vi.stubEnv("VITE_DEV_SERVER_URL", "http://127.0.0.1:5733");
104 vi.stubGlobal("window", {
105 location: new URL("http://127.0.0.1:5733/"),
106 history: {
107 replaceState: vi.fn(),
108 },
109 desktopBridge: {
110 getLocalEnvironmentBootstraps: () => [
111 {
112 id: "primary",
113 label: "Windows",
114 httpBaseUrl: "http://127.0.0.1:3773",
115 wsBaseUrl: "ws://127.0.0.1:3773",
116 bootstrapToken: "desktop-bootstrap-token",
117 },
118 ],
119 },
120 });
121 await installDescriptorApi();
122​
123 await expect(resolveInitialPrimaryEnvironmentDescriptor()).resolves.toEqual(BASE_ENVIRONMENT);
124 expect(resolvePrimaryEnvironmentHttpUrl("/.well-known/t3/environment")).toBe(
125 "http://127.0.0.1:5733/.well-known/t3/environment",
126 );
127 });
128});