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

onboarding.test.ts

285 lines9.2 KB

1import {
2 AuthStandardClientScopes,
3 EnvironmentId,
4 ORCHESTRATION_PROTOCOL_VERSION,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10​
11import { remoteHttpClientLayer } from "../rpc/http.ts";
12import { ClientPresentation, SshEnvironmentGateway } from "../platform/capabilities.ts";
13import { BearerConnectionCredential, BearerConnectionProfile } from "./catalog.ts";
14import { BearerConnectionTarget } from "./model.ts";
15import {
16 prepareBearerConnectionUpdate,
17 preparePairingRegistration,
18 prepareSshRegistration,
19} from "./onboarding.ts";
20​
21const CLIENT_PRESENTATION_LAYER = Layer.succeed(
22 ClientPresentation,
23 ClientPresentation.of({
24 metadata: {
25 label: "T3 Code Test",
26 deviceType: "desktop",
27 os: "Test OS",
28 },
29 scopes: AuthStandardClientScopes,
30 }),
31);
32​
33function pairingHttpLayer(
34 calls: Array<{ readonly url: string; readonly init: RequestInit }>,
35 options?: { readonly failDescriptor?: boolean; readonly protocolVersion?: number },
36) {
37 const fetchFn = ((input, init = {}) => {
38 const url = String(input);
39 calls.push({ url, init });
40​
41 if (url.endsWith("/.well-known/t3/environment")) {
42 if (options?.failDescriptor === true) {
43 return Promise.resolve(
44 Response.json({ message: "descriptor unavailable" }, { status: 503 }),
45 );
46 }
47 return Promise.resolve(
48 Response.json({
49 environmentId: "environment-paired",
50 label: "Paired environment",
51 platform: {
52 os: "linux",
53 arch: "x64",
54 },
55 serverVersion: "0.0.0-test",
56 orchestrationProtocolVersion: options?.protocolVersion ?? ORCHESTRATION_PROTOCOL_VERSION,
57 capabilities: {
58 repositoryIdentity: true,
59 },
60 }),
61 );
62 }
63​
64 if (url.endsWith("/oauth/token")) {
65 return Promise.resolve(
66 Response.json({
67 access_token: "bearer-token",
68 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
69 token_type: "Bearer",
70 expires_in: 3600,
71 scope: AuthStandardClientScopes.join(" "),
72 }),
73 );
74 }
75​
76 return Promise.reject(new Error(`Unexpected request: ${url}`));
77 }) satisfies typeof fetch;
78​
79 return remoteHttpClientLayer(fetchFn);
81​
82describe("connection onboarding", () => {
83 it.effect("prepares a persisted bearer registration from pairing details", () =>
84 Effect.gen(function* () {
85 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
86 const registration = yield* preparePairingRegistration({
87 host: "remote.example.test",
88 pairingCode: "pairing-token",
89 }).pipe(Effect.provide(Layer.mergeAll(CLIENT_PRESENTATION_LAYER, pairingHttpLayer(calls))));
90​
91 expect(registration).toMatchObject({
92 _tag: "BearerConnectionRegistration",
93 target: {
94 environmentId: "environment-paired",
95 label: "Paired environment",
96 connectionId: "bearer:environment-paired",
97 },
98 profile: {
99 environmentId: "environment-paired",
100 label: "Paired environment",
101 connectionId: "bearer:environment-paired",
102 httpBaseUrl: "https://remote.example.test/",
103 wsBaseUrl: "wss://remote.example.test/",
104 },
105 credential: {
106 token: "bearer-token",
107 },
108 });
109 expect(calls.map((call) => call.url)).toEqual([
110 "https://remote.example.test/.well-known/t3/environment",
111 "https://remote.example.test/oauth/token",
112 ]);
113​
114 const tokenRequest = calls.find((call) => call.url.endsWith("/oauth/token"));
115 const tokenBody =
116 tokenRequest?.init.body instanceof Uint8Array
117 ? new TextDecoder().decode(tokenRequest.init.body)
118 : String(tokenRequest?.init.body);
119 const tokenParams = new URLSearchParams(tokenBody);
120 expect(tokenParams.get("subject_token")).toBe("pairing-token");
121 expect(tokenParams.get("scope")).toBe(AuthStandardClientScopes.join(" "));
122 expect(tokenParams.get("client_label")).toBe("T3 Code Test");
123 }),
124 );
125​
126 it.effect("rejects an incompatible server without consuming the pairing credential", () =>
127 Effect.gen(function* () {
128 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
129 const error = yield* preparePairingRegistration({
130 host: "remote.example.test",
131 pairingCode: "pairing-token",
132 }).pipe(
133 Effect.provide(
134 Layer.mergeAll(
135 CLIENT_PRESENTATION_LAYER,
136 pairingHttpLayer(calls, { protocolVersion: ORCHESTRATION_PROTOCOL_VERSION + 1 }),
137 ),
138 ),
139 Effect.flip,
140 );
141 expect(error).toMatchObject({ reason: "unsupported" });
142 expect(calls.map((call) => call.url)).toEqual([
143 "https://remote.example.test/.well-known/t3/environment",
144 ]);
145 }),
146 );
147​
148 it.effect("does not consume a pairing credential when descriptor discovery fails", () =>
149 Effect.gen(function* () {
150 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
151​
152 yield* preparePairingRegistration({
153 host: "remote.example.test",
154 pairingCode: "pairing-token",
155 }).pipe(
156 Effect.provide(
157 Layer.mergeAll(
158 CLIENT_PRESENTATION_LAYER,
159 pairingHttpLayer(calls, { failDescriptor: true }),
160 ),
161 ),
162 Effect.flip,
163 );
164​
165 expect(calls.map((call) => call.url)).toEqual([
166 "https://remote.example.test/.well-known/t3/environment",
167 ]);
168 }),
169 );
170​
171 it.effect("rejects invalid pairing details before making a request", () =>
172 Effect.gen(function* () {
173 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
174 const error = yield* preparePairingRegistration({
175 host: "",
176 pairingCode: "",
177 }).pipe(
178 Effect.provide(Layer.mergeAll(CLIENT_PRESENTATION_LAYER, pairingHttpLayer(calls))),
179 Effect.flip,
180 );
181​
182 expect(error).toMatchObject({
183 _tag: "ConnectionBlockedError",
184 reason: "configuration",
185 message: "Enter a backend URL.",
186 });
187 expect(calls).toEqual([]);
188 }),
189 );
190​
191 it.effect("updates bearer metadata while preserving the credential and identity", () =>
192 Effect.gen(function* () {
193 const environmentId = EnvironmentId.make("environment-paired");
194 const registration = yield* prepareBearerConnectionUpdate({
195 input: {
196 environmentId,
197 label: " Renamed environment ",
198 httpBaseUrl: "http://100.65.180.100:3773/path",
199 },
200 entry: Option.some({
201 target: new BearerConnectionTarget({
202 environmentId,
203 label: "Old label",
204 connectionId: "bearer:environment-paired",
205 }),
206 profile: Option.some(
207 new BearerConnectionProfile({
208 connectionId: "bearer:environment-paired",
209 environmentId,
210 label: "Old label",
211 httpBaseUrl: "http://old.example.test/",
212 wsBaseUrl: "ws://old.example.test/",
213 }),
214 ),
215 enabled: true,
216 }),
217 credential: Option.some(new BearerConnectionCredential({ token: "bearer-token" })),
218 });
219​
220 expect(registration).toMatchObject({
221 target: {
222 environmentId,
223 label: "Renamed environment",
224 connectionId: "bearer:environment-paired",
225 },
226 profile: {
227 environmentId,
228 label: "Renamed environment",
229 httpBaseUrl: "http://100.65.180.100:3773/",
230 wsBaseUrl: "ws://100.65.180.100:3773/",
231 },
232 credential: { token: "bearer-token" },
233 });
234 }),
235 );
236​
237 it.effect("prepares an SSH registration from the provisioned platform environment", () =>
238 Effect.gen(function* () {
239 const target = {
240 alias: "devbox",
241 hostname: "devbox.example.test",
242 username: "developer",
243 port: 22,
244 };
245 const registration = yield* prepareSshRegistration({
246 target,
247 }).pipe(
248 Effect.provideService(
249 SshEnvironmentGateway,
250 SshEnvironmentGateway.of({
251 provision: () =>
252 Effect.succeed({
253 environmentId: EnvironmentId.make("environment-ssh"),
254 label: "Remote development box",
255 bootstrap: {
256 target,
257 httpBaseUrl: "http://127.0.0.1:3201",
258 wsBaseUrl: "ws://127.0.0.1:3201",
259 pairingToken: "pairing-token",
260 },
261 bearerToken: "bearer-token",
262 }),
263 prepare: () => Effect.die("unused"),
264 disconnect: () => Effect.die("unused"),
265 }),
266 ),
267 );
268​
269 expect(registration).toMatchObject({
270 _tag: "SshConnectionRegistration",
271 target: {
272 environmentId: "environment-ssh",
273 label: "Remote development box",
274 connectionId: "ssh:environment-ssh",
275 },
276 profile: {
277 environmentId: "environment-ssh",
278 label: "Remote development box",
279 connectionId: "ssh:environment-ssh",
280 target,
281 },
282 });
283 }),
284 );
285});