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 }),
189 credential: Option.some(new BearerConnectionCredential({ token: "bearer-token" })),
190 });
191
192 expect(registration).toMatchObject({
193 target: {
194 environmentId,
195 label: "Renamed environment",
196 connectionId: "bearer:environment-paired",
197 },
198 profile: {
199 environmentId,
200 label: "Renamed environment",
201 httpBaseUrl: "http://100.65.180.100:3773/",
202 wsBaseUrl: "ws://100.65.180.100:3773/",
203 },
204 credential: { token: "bearer-token" },
205 });
206 }),
207 );
208
209 it.effect("prepares an SSH registration from the provisioned platform environment", () =>
210 Effect.gen(function* () {
211 const target = {
212 alias: "devbox",
213 hostname: "devbox.example.test",
214 username: "developer",
215 port: 22,
216 };
217 const registration = yield* prepareSshRegistration({
218 target,
219 }).pipe(
220 Effect.provideService(
221 SshEnvironmentGateway,
222 SshEnvironmentGateway.of({
223 provision: () =>
224 Effect.succeed({
225 environmentId: EnvironmentId.make("environment-ssh"),
226 label: "Remote development box",
227 bootstrap: {
228 target,
229 httpBaseUrl: "http://127.0.0.1:3201",
230 wsBaseUrl: "ws://127.0.0.1:3201",
231 pairingToken: "pairing-token",
232 },
233 bearerToken: "bearer-token",
234 }),
235 prepare: () => Effect.die("unused"),
236 disconnect: () => Effect.die("unused"),
237 }),
238 ),
239 );
240
241 expect(registration).toMatchObject({
242 _tag: "SshConnectionRegistration",
243 target: {
244 environmentId: "environment-ssh",
245 label: "Remote development box",
246 connectionId: "ssh:environment-ssh",
247 },
248 profile: {
249 environmentId: "environment-ssh",
250 label: "Remote development box",
251 connectionId: "ssh:environment-ssh",
252 target,
253 },
254 });
255 }),
256 );
257});