Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
3import {
4 bootstrapRemoteBearerSession,
5 fetchRemoteEnvironmentDescriptor,
6 fetchRemoteSessionState,
7 issueRemoteWebSocketToken,
8 resolveRemoteWebSocketConnectionUrl,
9} from "./api";
10import { resolveRemotePairingTarget } from "./target";
11
12const originalFetch = globalThis.fetch;
13
14beforeEach(() => {
15 Object.defineProperty(globalThis, "window", {
16 configurable: true,
17 value: {
18 location: {
19 origin: "https://app.example.com",
20 },
21 },
22 });
23 vi.restoreAllMocks();
24});
25
26describe("remote environment api", () => {
27 it("derives backend urls and token from a pairing url", () => {
28 expect(
29 resolveRemotePairingTarget({
30 pairingUrl: "https://remote.example.com/pair#token=pairing-token",
31 }),
32 ).toEqual({
33 credential: "pairing-token",
34 httpBaseUrl: "https://remote.example.com/",
35 wsBaseUrl: "wss://remote.example.com/",
36 });
37 });
38
39 it("accepts pairing urls that still use a query token", () => {
40 expect(
41 resolveRemotePairingTarget({
42 pairingUrl: "https://remote.example.com/pair?token=pairing-token",
43 }),
44 ).toEqual({
45 credential: "pairing-token",
46 httpBaseUrl: "https://remote.example.com/",
47 wsBaseUrl: "wss://remote.example.com/",
48 });
49 });
50
51 it("derives backend urls from a host and pairing code", () => {
52 expect(
53 resolveRemotePairingTarget({
54 host: "https://remote.example.com",
55 pairingCode: "pairing-token",
56 }),
57 ).toEqual({
58 credential: "pairing-token",
59 httpBaseUrl: "https://remote.example.com/",
60 wsBaseUrl: "wss://remote.example.com/",
61 });
62 });
63
64 it("preserves host ports when normalizing a bare host input", () => {
65 expect(
66 resolveRemotePairingTarget({
67 host: "myserver.com:3000",
68 pairingCode: "pairing-token",
69 }),
70 ).toEqual({
71 credential: "pairing-token",
72 httpBaseUrl: "https://myserver.com:3000/",
73 wsBaseUrl: "wss://myserver.com:3000/",
74 });
75 });
76
77 it("bootstraps bearer auth against a remote backend", async () => {
78 const fetchMock = vi.fn().mockResolvedValue(
79 new Response(
80 JSON.stringify({
81 authenticated: true,
82 role: "client",
83 sessionMethod: "bearer-session-token",
84 expiresAt: "2026-05-01T12:00:00.000Z",
85 sessionToken: "bearer-token",
86 }),
87 { status: 200 },
88 ),
89 );
90 globalThis.fetch = fetchMock as typeof fetch;
91
92 await expect(
93 bootstrapRemoteBearerSession({
94 httpBaseUrl: "https://remote.example.com/",
95 credential: "pairing-token",
96 }),
97 ).resolves.toMatchObject({
98 sessionMethod: "bearer-session-token",
99 sessionToken: "bearer-token",
100 });
101
102 expect(fetchMock).toHaveBeenCalledWith("https://remote.example.com/api/auth/bootstrap/bearer", {
103 method: "POST",
104 headers: {
105 "content-type": "application/json",
106 },
107 body: JSON.stringify({
108 credential: "pairing-token",
109 }),
110 });
111 });
112
113 it("loads remote session state and websocket tokens over bearer auth", async () => {
114 const fetchMock = vi
115 .fn()
116 .mockResolvedValueOnce(
117 new Response(
118 JSON.stringify({
119 environmentId: "environment-remote",
120 label: "Remote environment",
121 platform: {
122 os: "linux",
123 arch: "x64",
124 },
125 serverVersion: "0.0.0-test",
126 capabilities: {
127 repositoryIdentity: true,
128 },
129 }),
130 { status: 200 },
131 ),
132 )
133 .mockResolvedValueOnce(
134 new Response(
135 JSON.stringify({
136 authenticated: true,
137 auth: {
138 policy: "remote-reachable",
139 bootstrapMethods: ["one-time-token"],
140 sessionMethods: ["browser-session-cookie", "bearer-session-token"],
141 sessionCookieName: "t3_session",
142 },
143 role: "client",
144 sessionMethod: "bearer-session-token",
145 expiresAt: "2026-05-01T12:00:00.000Z",
146 }),
147 { status: 200 },
148 ),
149 )
150 .mockResolvedValueOnce(
151 new Response(
152 JSON.stringify({
153 token: "ws-token",
154 expiresAt: "2026-05-01T12:05:00.000Z",
155 }),
156 { status: 200 },
157 ),
158 );
159 globalThis.fetch = fetchMock as typeof fetch;
160
161 await expect(
162 fetchRemoteEnvironmentDescriptor({
163 httpBaseUrl: "https://remote.example.com/",
164 }),
165 ).resolves.toMatchObject({
166 environmentId: "environment-remote",
167 label: "Remote environment",
168 });
169
170 await expect(
171 fetchRemoteSessionState({
172 httpBaseUrl: "https://remote.example.com/",
173 bearerToken: "bearer-token",
174 }),
175 ).resolves.toMatchObject({
176 authenticated: true,
177 role: "client",
178 });
179
180 await expect(
181 issueRemoteWebSocketToken({
182 httpBaseUrl: "https://remote.example.com/",
183 bearerToken: "bearer-token",
184 }),
185 ).resolves.toMatchObject({
186 token: "ws-token",
187 });
188
189 expect(fetchMock).toHaveBeenNthCalledWith(
190 1,
191 "https://remote.example.com/.well-known/t3/environment",
192 {
193 method: "GET",
194 headers: {},
195 },
196 );
197 expect(fetchMock).toHaveBeenNthCalledWith(2, "https://remote.example.com/api/auth/session", {
198 method: "GET",
199 headers: {
200 authorization: "Bearer bearer-token",
201 },
202 });
203 expect(fetchMock).toHaveBeenNthCalledWith(3, "https://remote.example.com/api/auth/ws-token", {
204 method: "POST",
205 headers: {
206 authorization: "Bearer bearer-token",
207 },
208 });
209 });
210
211 it("mints a websocket url with a short-lived ws token", async () => {
212 const fetchMock = vi.fn().mockResolvedValue(
213 new Response(
214 JSON.stringify({
215 token: "ws-token",
216 expiresAt: "2026-05-01T12:05:00.000Z",
217 }),
218 { status: 200 },
219 ),
220 );
221 globalThis.fetch = fetchMock as typeof fetch;
222
223 await expect(
224 resolveRemoteWebSocketConnectionUrl({
225 wsBaseUrl: "wss://remote.example.com/",
226 httpBaseUrl: "https://remote.example.com/",
227 bearerToken: "bearer-token",
228 }),
229 ).resolves.toBe("wss://remote.example.com/?wsToken=ws-token");
230 });
231});
232
233afterEach(() => {
234 globalThis.fetch = originalFetch;
235});