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

api.test.ts

248 lines6.8 KB

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 and token from a hosted app pairing link", () => {
52 expect(
53 resolveRemotePairingTarget({
54 pairingUrl:
55 "https://app.t3.codes/pair?host=https%3A%2F%2Fdesktop.tailnet.ts.net%3A44342%2F#token=pairing-token",
56 }),
57 ).toEqual({
58 credential: "pairing-token",
59 httpBaseUrl: "https://desktop.tailnet.ts.net:44342/",
60 wsBaseUrl: "wss://desktop.tailnet.ts.net:44342/",
61 });
62 });
63​
64 it("derives backend urls from a host and pairing code", () => {
65 expect(
66 resolveRemotePairingTarget({
67 host: "https://remote.example.com",
68 pairingCode: "pairing-token",
69 }),
70 ).toEqual({
71 credential: "pairing-token",
72 httpBaseUrl: "https://remote.example.com/",
73 wsBaseUrl: "wss://remote.example.com/",
74 });
75 });
76​
77 it("preserves host ports when normalizing a bare host input", () => {
78 expect(
79 resolveRemotePairingTarget({
80 host: "myserver.com:3000",
81 pairingCode: "pairing-token",
82 }),
83 ).toEqual({
84 credential: "pairing-token",
85 httpBaseUrl: "https://myserver.com:3000/",
86 wsBaseUrl: "wss://myserver.com:3000/",
87 });
88 });
89​
90 it("bootstraps bearer auth against a remote backend", async () => {
91 const fetchMock = vi.fn().mockResolvedValue(
92 new Response(
93 JSON.stringify({
94 authenticated: true,
95 role: "client",
96 sessionMethod: "bearer-session-token",
97 expiresAt: "2026-05-01T12:00:00.000Z",
98 sessionToken: "bearer-token",
99 }),
100 { status: 200 },
101 ),
102 );
103 globalThis.fetch = fetchMock as typeof fetch;
104​
105 await expect(
106 bootstrapRemoteBearerSession({
107 httpBaseUrl: "https://remote.example.com/",
108 credential: "pairing-token",
109 }),
110 ).resolves.toMatchObject({
111 sessionMethod: "bearer-session-token",
112 sessionToken: "bearer-token",
113 });
114​
115 expect(fetchMock).toHaveBeenCalledWith("https://remote.example.com/api/auth/bootstrap/bearer", {
116 method: "POST",
117 headers: {
118 "content-type": "application/json",
119 },
120 body: JSON.stringify({
121 credential: "pairing-token",
122 }),
123 });
124 });
125​
126 it("loads remote session state and websocket tokens over bearer auth", async () => {
127 const fetchMock = vi
128 .fn()
129 .mockResolvedValueOnce(
130 new Response(
131 JSON.stringify({
132 environmentId: "environment-remote",
133 label: "Remote environment",
134 platform: {
135 os: "linux",
136 arch: "x64",
137 },
138 serverVersion: "0.0.0-test",
139 capabilities: {
140 repositoryIdentity: true,
141 },
142 }),
143 { status: 200 },
144 ),
145 )
146 .mockResolvedValueOnce(
147 new Response(
148 JSON.stringify({
149 authenticated: true,
150 auth: {
151 policy: "remote-reachable",
152 bootstrapMethods: ["one-time-token"],
153 sessionMethods: ["browser-session-cookie", "bearer-session-token"],
154 sessionCookieName: "t3_session",
155 },
156 role: "client",
157 sessionMethod: "bearer-session-token",
158 expiresAt: "2026-05-01T12:00:00.000Z",
159 }),
160 { status: 200 },
161 ),
162 )
163 .mockResolvedValueOnce(
164 new Response(
165 JSON.stringify({
166 token: "ws-token",
167 expiresAt: "2026-05-01T12:05:00.000Z",
168 }),
169 { status: 200 },
170 ),
171 );
172 globalThis.fetch = fetchMock as typeof fetch;
173​
174 await expect(
175 fetchRemoteEnvironmentDescriptor({
176 httpBaseUrl: "https://remote.example.com/",
177 }),
178 ).resolves.toMatchObject({
179 environmentId: "environment-remote",
180 label: "Remote environment",
181 });
182​
183 await expect(
184 fetchRemoteSessionState({
185 httpBaseUrl: "https://remote.example.com/",
186 bearerToken: "bearer-token",
187 }),
188 ).resolves.toMatchObject({
189 authenticated: true,
190 role: "client",
191 });
192​
193 await expect(
194 issueRemoteWebSocketToken({
195 httpBaseUrl: "https://remote.example.com/",
196 bearerToken: "bearer-token",
197 }),
198 ).resolves.toMatchObject({
199 token: "ws-token",
200 });
201​
202 expect(fetchMock).toHaveBeenNthCalledWith(
203 1,
204 "https://remote.example.com/.well-known/t3/environment",
205 {
206 method: "GET",
207 headers: {},
208 },
209 );
210 expect(fetchMock).toHaveBeenNthCalledWith(2, "https://remote.example.com/api/auth/session", {
211 method: "GET",
212 headers: {
213 authorization: "Bearer bearer-token",
214 },
215 });
216 expect(fetchMock).toHaveBeenNthCalledWith(3, "https://remote.example.com/api/auth/ws-token", {
217 method: "POST",
218 headers: {
219 authorization: "Bearer bearer-token",
220 },
221 });
222 });
223​
224 it("mints a websocket url with a short-lived ws token", async () => {
225 const fetchMock = vi.fn().mockResolvedValue(
226 new Response(
227 JSON.stringify({
228 token: "ws-token",
229 expiresAt: "2026-05-01T12:05:00.000Z",
230 }),
231 { status: 200 },
232 ),
233 );
234 globalThis.fetch = fetchMock as typeof fetch;
235​
236 await expect(
237 resolveRemoteWebSocketConnectionUrl({
238 wsBaseUrl: "wss://remote.example.com/",
239 httpBaseUrl: "https://remote.example.com/",
240 bearerToken: "bearer-token",
241 }),
242 ).resolves.toBe("wss://remote.example.com/?wsToken=ws-token");
243 });
244});
245​
246afterEach(() => {
247 globalThis.fetch = originalFetch;
248});