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