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

onboarding.test.ts

670 lines25.0 KB

1import {
2 AuthStandardClientScopes,
3 EnvironmentId,
4 type AuthEnvironmentScope,
5 ORCHESTRATION_PROTOCOL_VERSION,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9import * as Fiber from "effect/Fiber";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Stream from "effect/Stream";
13import * as SubscriptionRef from "effect/SubscriptionRef";
14​
15import * as RpcHttp from "../rpc/http.ts";
16import * as ClientCapabilities from "../platform/capabilities.ts";
17import * as Persistence from "../platform/persistence.ts";
18import { fetchRemoteSessionState } from "../authorization/remote.ts";
19import {
20 BearerConnectionCredential,
21 BearerConnectionProfile,
22 BearerConnectionRegistration,
23 PrimaryConnectionRegistration,
24} from "./catalog.ts";
25import * as Connectivity from "./connectivity.ts";
26import * as ConnectionCredentialStore from "./credentialStore.ts";
27import * as ConnectionDriver from "./driver.ts";
28import {
29 BearerConnectionTarget,
30 ConnectionBlockedError,
31 PrimaryConnectionTarget,
32} from "./model.ts";
33import * as Onboarding from "./onboarding.ts";
34import {
35 prepareBearerConnectionUpdate,
36 preparePairingRegistration,
37 prepareSshRegistration,
38} from "./onboarding.ts";
39import * as EnvironmentRegistry from "./registry.ts";
40import * as ConnectionProfileStore from "./profileStore.ts";
41import { connectionRoutes, routeHttpBaseUrl } from "./routes.ts";
42import * as ConnectionWakeups from "./wakeups.ts";
43​
44const layerClientPresentation = Layer.succeed(
45 ClientCapabilities.ClientPresentation,
46 ClientCapabilities.ClientPresentation.of({
47 metadata: {
48 label: "T3 Code Test",
49 deviceType: "desktop",
50 os: "Test OS",
51 },
52 }),
53);
54​
55function layerPairingHttp(
56 calls: Array<{ readonly url: string; readonly init: RequestInit }>,
57 options?: {
58 readonly failDescriptor?: boolean;
59 readonly failExchange?: boolean;
60 readonly protocolVersion?: number;
61 readonly selfUpdate?: boolean;
62 readonly grantScopes?: ReadonlyArray<AuthEnvironmentScope>;
63 readonly exchangeGate?: { readonly reached: () => void; readonly release: Promise<void> };
64 },
65) {
66 const grantScopes = options?.grantScopes ?? AuthStandardClientScopes;
67 let sessionScopes: ReadonlyArray<string> = [];
68 const fetchFn = ((input, init = {}) => {
69 const url = String(input);
70 calls.push({ url, init });
71​
72 if (url.endsWith("/.well-known/t3/environment")) {
73 if (options?.failDescriptor === true) {
74 return Promise.resolve(
75 Response.json({ message: "descriptor unavailable" }, { status: 503 }),
76 );
77 }
78 return Promise.resolve(
79 Response.json({
80 environmentId: "environment-paired",
81 label: "Paired environment",
82 platform: {
83 os: "linux",
84 arch: "x64",
85 },
86 serverVersion: "0.0.0-test",
87 orchestrationProtocolVersion: options?.protocolVersion ?? ORCHESTRATION_PROTOCOL_VERSION,
88 capabilities: {
89 repositoryIdentity: true,
90 ...(options?.selfUpdate === true ? { serverSelfUpdate: "boot-service" } : {}),
91 },
92 }),
93 );
94 }
95​
96 if (url.endsWith("/oauth/token")) {
97 if (options?.failExchange === true) {
98 return Promise.resolve(
99 Response.json(
100 {
101 _tag: "EnvironmentAuthInvalidError",
102 code: "auth_invalid",
103 reason: "invalid_credential",
104 traceId: "trace-pairing-test",
105 },
106 { status: 401 },
107 ),
108 );
109 }
110 const body =
111 init.body instanceof Uint8Array ? new TextDecoder().decode(init.body) : String(init.body);
112 const requestedScope = new URLSearchParams(body).get("scope");
113 sessionScopes = requestedScope === null ? grantScopes : requestedScope.split(" ");
114 if (!sessionScopes.every((scope) => grantScopes.some((granted) => granted === scope))) {
115 return Promise.resolve(
116 Response.json(
117 {
118 _tag: "EnvironmentRequestInvalidError",
119 code: "invalid_request",
120 reason: "scope_not_granted",
121 traceId: "pairing-scope-test",
122 },
123 { status: 400 },
124 ),
125 );
126 }
127 const granted = Response.json({
128 access_token: "bearer-token",
129 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
130 token_type: "Bearer",
131 expires_in: 3600,
132 scope: sessionScopes.join(" "),
133 });
134 const gate = options?.exchangeGate;
135 if (gate === undefined) return Promise.resolve(granted);
136 gate.reached();
137 return gate.release.then(() => granted);
138 }
139​
140 if (url.endsWith("/api/auth/session")) {
141 return Promise.resolve(
142 Response.json({
143 authenticated: true,
144 auth: {
145 policy: "remote-reachable",
146 bootstrapMethods: ["one-time-token"],
147 sessionMethods: ["bearer-access-token"],
148 sessionCookieName: "t3_session",
149 },
150 scopes: sessionScopes,
151 sessionMethod: "bearer-access-token",
152 }),
153 );
154 }
155​
156 return Promise.reject(new Error(`Unexpected request: ${url}`));
157 }) satisfies typeof fetch;
158​
159 return RpcHttp.layerRemoteHttpClient(fetchFn);
161​
162function layerPairingOnboarding(
163 options?: Parameters<typeof layerPairingHttp>[1],
164 calls: Array<{ readonly url: string; readonly init: RequestInit }> = [],
165) {
166 const dependencies = Layer.mergeAll(
167 layerClientPresentation,
168 layerPairingHttp(calls, options),
169 Layer.mock(Persistence.ConnectionTargetStore)({
170 list: Effect.succeed([]),
171 listDisabled: Effect.succeed([]),
172 }),
173 Layer.mock(Persistence.ConnectionRegistrationStore)({
174 register: () => Effect.void,
175 setEnabled: () => Effect.void,
176 }),
177 Layer.mock(Persistence.EnvironmentCacheStore)({}),
178 Layer.mock(ConnectionProfileStore.ConnectionProfileStore)({}),
179 Layer.mock(ConnectionCredentialStore.ConnectionCredentialStore)({
180 get: () => Effect.succeedNone,
181 put: () => Effect.void,
182 }),
183 Layer.mock(ClientCapabilities.SshEnvironmentGateway)({}),
184 Layer.succeed(
185 Connectivity.Connectivity,
186 Connectivity.Connectivity.of({ status: Effect.succeed("online"), changes: Stream.never }),
187 ),
188 Layer.mock(ConnectionDriver.ConnectionDriver)({ connect: () => Effect.never }),
189 Layer.succeed(
190 ConnectionWakeups.ConnectionWakeups,
191 ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.never }),
192 ),
193 );
194 return Onboarding.layer.pipe(
195 Layer.provideMerge(EnvironmentRegistry.layer.pipe(Layer.provideMerge(dependencies))),
196 );
198​
199const PAIRED_ENVIRONMENT_ID = EnvironmentId.make("environment-paired");
200const PAIRING_INPUT = { host: "remote.example.test", pairingCode: "pairing-token" };
201const PAIRED_PROFILE = new BearerConnectionProfile({
202 connectionId: "bearer:environment-paired:https://remote.example.test",
203 environmentId: PAIRED_ENVIRONMENT_ID,
204 label: "Paired environment",
205 httpBaseUrl: "https://remote.example.test/",
206 wsBaseUrl: "wss://remote.example.test/",
207});
208const UNSUPPORTED_ERROR = new ConnectionBlockedError({
209 reason: "unsupported",
210 detail: "The saved server used an incompatible orchestration protocol.",
211});
212​
213const registerSwitchedOffEnvironment = Effect.gen(function* () {
214 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
215 const registration = new BearerConnectionRegistration({
216 target: new BearerConnectionTarget({
217 environmentId: PAIRED_ENVIRONMENT_ID,
218 connectionId: PAIRED_PROFILE.connectionId,
219 label: "Old label",
220 }),
221 profile: new BearerConnectionProfile({ ...PAIRED_PROFILE, label: "Old label" }),
222 credential: new BearerConnectionCredential({ token: "old-token" }),
223 });
224 yield* registry.register(registration);
225 yield* registry.setEnabled(PAIRED_ENVIRONMENT_ID, false);
226 yield* registry.setCompatibility(PAIRED_ENVIRONMENT_ID, UNSUPPORTED_ERROR);
227 const entries = yield* SubscriptionRef.get(registry.entries);
228 expect(entries.get(PAIRED_ENVIRONMENT_ID)).toEqual({
229 target: registration.target,
230 profile: Option.some(registration.profile),
231 enabled: false,
232 unsupportedReason: UNSUPPORTED_ERROR.message,
233 });
234 return entries;
235});
236​
237describe("connection onboarding", () => {
238 it.effect("prepares a persisted bearer registration from pairing details", () =>
239 Effect.gen(function* () {
240 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
241 const registration = yield* preparePairingRegistration({
242 host: "remote.example.test",
243 pairingCode: "pairing-token",
244 }).pipe(Effect.provide(layerPairingOnboarding(undefined, calls)));
245​
246 expect(registration).toMatchObject({
247 _tag: "BearerConnectionRegistration",
248 target: {
249 environmentId: "environment-paired",
250 label: "Paired environment",
251 connectionId: "bearer:environment-paired:https://remote.example.test",
252 },
253 profile: {
254 environmentId: "environment-paired",
255 label: "Paired environment",
256 connectionId: "bearer:environment-paired:https://remote.example.test",
257 httpBaseUrl: "https://remote.example.test/",
258 wsBaseUrl: "wss://remote.example.test/",
259 },
260 credential: {
261 token: "bearer-token",
262 },
263 });
264 expect(calls.map((call) => call.url)).toEqual([
265 "https://remote.example.test/.well-known/t3/environment",
266 "https://remote.example.test/oauth/token",
267 ]);
268​
269 const tokenRequest = calls.find((call) => call.url.endsWith("/oauth/token"));
270 const tokenBody =
271 tokenRequest?.init.body instanceof Uint8Array
272 ? new TextDecoder().decode(tokenRequest.init.body)
273 : String(tokenRequest?.init.body);
274 const tokenParams = new URLSearchParams(tokenBody);
275 expect(tokenParams.get("subject_token")).toBe("pairing-token");
276 expect(tokenParams.has("scope")).toBe(false);
277 expect(tokenParams.get("client_label")).toBe("T3 Code Test");
278 expect(tokenParams.get("client_device_type")).toBe("desktop");
279 expect(tokenParams.get("client_os")).toBe("Test OS");
280 }),
281 );
282​
283 it.effect("turns a switched-off environment back on when it is paired again", () =>
284 Effect.gen(function* () {
285 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
286 const onboarding = yield* Onboarding.ConnectionOnboarding;
287 yield* registerSwitchedOffEnvironment;
288​
289 const environmentId = yield* onboarding.registerPairing(PAIRING_INPUT);
290​
291 expect(environmentId).toBe(PAIRED_ENVIRONMENT_ID);
292 const entries = yield* SubscriptionRef.get(registry.entries);
293 const entry = entries.get(environmentId);
294 expect(entries.size).toBe(1);
295 expect(entry?.enabled).toBe(true);
296 expect(entry?.unsupportedReason).toBeUndefined();
297 expect(entry?.profile).toEqual(Option.some(PAIRED_PROFILE));
298 expect(entry?.target).toEqual(
299 new BearerConnectionTarget({
300 environmentId,
301 connectionId: PAIRED_PROFILE.connectionId,
302 label: PAIRED_PROFILE.label,
303 }),
304 );
305 }).pipe(Effect.provide(layerPairingOnboarding())),
306 );
307​
308 it.effect("refuses a pairing link that claims a saved environment from a new address", () => {
309 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
310 return Effect.gen(function* () {
311 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
312 const onboarding = yield* Onboarding.ConnectionOnboarding;
313 const before = yield* registerSwitchedOffEnvironment;
314​
315 const error = yield* onboarding
316 .registerPairing({ host: "attacker.example.test", pairingCode: "pairing-token" })
317 .pipe(Effect.flip);
318​
319 expect(error).toMatchObject({ _tag: "ConnectionBlockedError", reason: "configuration" });
320 expect(yield* SubscriptionRef.get(registry.entries)).toEqual(before);
321 expect(calls.map((call) => call.url)).toEqual([
322 "https://attacker.example.test/.well-known/t3/environment",
323 ]);
324 }).pipe(Effect.provide(layerPairingOnboarding(undefined, calls)));
325 });
326​
327 it.effect("refuses a new address when the environment is saved during the exchange", () => {
328 let reached!: () => void;
329 let release!: () => void;
330 const exchangeReached = new Promise<void>((resolve) => (reached = resolve));
331 const exchangeGate = {
332 reached: () => reached(),
333 release: new Promise<void>((resolve) => (release = resolve)),
334 };
335 return Effect.gen(function* () {
336 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
337 const onboarding = yield* Onboarding.ConnectionOnboarding;
338 const pairing = yield* onboarding
339 .registerPairing({ host: "attacker.example.test", pairingCode: "pairing-token" })
340 .pipe(Effect.result, Effect.forkChild);
341 yield* Effect.promise(() => exchangeReached);
342 const before = yield* registerSwitchedOffEnvironment;
343 release();
344​
345 const outcome = yield* Fiber.join(pairing);
346​
347 expect(outcome).toMatchObject({
348 _tag: "Failure",
349 failure: { _tag: "ConnectionBlockedError", reason: "configuration" },
350 });
351 expect(yield* SubscriptionRef.get(registry.entries)).toEqual(before);
352 }).pipe(Effect.provide(layerPairingOnboarding({ exchangeGate })));
353 });
354​
355 it.effect("adds a new address as a route when pairing targets that environment", () =>
356 Effect.gen(function* () {
357 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
358 const onboarding = yield* Onboarding.ConnectionOnboarding;
359 yield* registerSwitchedOffEnvironment;
360​
361 yield* onboarding.registerPairing({
362 host: "lan.example.test",
363 pairingCode: "pairing-token",
364 expectedEnvironmentId: PAIRED_ENVIRONMENT_ID,
365 });
366​
367 const entry = (yield* SubscriptionRef.get(registry.entries)).get(PAIRED_ENVIRONMENT_ID);
368 expect(entry === undefined ? [] : connectionRoutes(entry).map(routeHttpBaseUrl)).toEqual(
369 expect.arrayContaining(["https://remote.example.test/", "https://lan.example.test/"]),
370 );
371 }).pipe(Effect.provide(layerPairingOnboarding())),
372 );
373​
374 it.effect(
375 "re-pairs an outdated self-updatable environment so its connection can be rechecked",
376 () =>
377 Effect.gen(function* () {
378 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
379 const onboarding = yield* Onboarding.ConnectionOnboarding;
380 yield* registerSwitchedOffEnvironment;
381 yield* registry.setCompatibility(
382 PAIRED_ENVIRONMENT_ID,
383 new ConnectionBlockedError({
384 reason: "unsupported",
385 detail: "The saved server needs an update.",
386 serverUpdateRequired: true,
387 }),
388 );
389 expect(
390 (yield* SubscriptionRef.get(registry.entries)).get(PAIRED_ENVIRONMENT_ID),
391 ).toMatchObject({
392 enabled: false,
393 serverUpdateRequired: true,
394 });
395​
396 const environmentId = yield* onboarding.registerPairing(PAIRING_INPUT);
397​
398 expect(environmentId).toBe(PAIRED_ENVIRONMENT_ID);
399 const entry = (yield* SubscriptionRef.get(registry.entries)).get(environmentId);
400 expect(entry?.enabled).toBe(true);
401 expect(entry?.unsupportedReason).toBeUndefined();
402 expect(entry?.serverUpdateRequired).toBeUndefined();
403 expect(entry?.profile).toEqual(Option.some(PAIRED_PROFILE));
404 }).pipe(
405 Effect.provide(
406 layerPairingOnboarding({
407 protocolVersion: ORCHESTRATION_PROTOCOL_VERSION - 1,
408 selfUpdate: true,
409 }),
410 ),
411 ),
412 );
413​
414 it.effect.each([
415 {
416 kind: "primary",
417 registration: new PrimaryConnectionRegistration({
418 target: new PrimaryConnectionTarget({
419 environmentId: PAIRED_ENVIRONMENT_ID,
420 label: "Host environment",
421 httpBaseUrl: "http://127.0.0.1:3773",
422 wsBaseUrl: "ws://127.0.0.1:3773",
423 }),
424 }),
425 },
426 {
427 kind: "bearer",
428 registration: new BearerConnectionRegistration({
429 target: new BearerConnectionTarget({
430 environmentId: PAIRED_ENVIRONMENT_ID,
431 connectionId: "platform:wsl",
432 label: "WSL",
433 }),
434 profile: new BearerConnectionProfile({
435 ...PAIRED_PROFILE,
436 connectionId: "platform:wsl",
437 label: "WSL",
438 httpBaseUrl: "http://127.0.0.1:4000",
439 wsBaseUrl: "ws://127.0.0.1:4000",
440 }),
441 credential: new BearerConnectionCredential({ token: "platform-token" }),
442 }),
443 },
444 ] as const)(
445 "leaves a platform-managed $kind environment untouched when its id is paired",
446 ({ registration }) =>
447 Effect.gen(function* () {
448 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
449 const onboarding = yield* Onboarding.ConnectionOnboarding;
450 yield* registry.reconcilePlatform([registration]);
451 yield* registry.setCompatibility(PAIRED_ENVIRONMENT_ID, UNSUPPORTED_ERROR);
452 const before = yield* SubscriptionRef.get(registry.entries);
453 expect(before.get(PAIRED_ENVIRONMENT_ID)).toMatchObject({
454 enabled: true,
455 unsupportedReason: UNSUPPORTED_ERROR.message,
456 });
457​
458 const environmentId = yield* onboarding.registerPairing(PAIRING_INPUT);
459​
460 expect(environmentId).toBe(PAIRED_ENVIRONMENT_ID);
461 expect(yield* SubscriptionRef.get(registry.entries)).toEqual(before);
462 }).pipe(Effect.provide(layerPairingOnboarding())),
463 );
464​
465 it.effect("leaves a switched-off environment unchanged when the pairing exchange fails", () =>
466 Effect.gen(function* () {
467 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
468 const onboarding = yield* Onboarding.ConnectionOnboarding;
469 const before = yield* registerSwitchedOffEnvironment;
470​
471 const error = yield* onboarding.registerPairing(PAIRING_INPUT).pipe(Effect.flip);
472​
473 expect(error).toMatchObject({
474 _tag: "ConnectionBlockedError",
475 reason: "authentication",
476 traceId: "trace-pairing-test",
477 });
478 const after = yield* SubscriptionRef.get(registry.entries);
479 expect(after).toEqual(before);
480 expect(after.get(PAIRED_ENVIRONMENT_ID)?.enabled).toBe(false);
481 expect(after.get(PAIRED_ENVIRONMENT_ID)?.unsupportedReason).toBe(UNSUPPORTED_ERROR.message);
482 }).pipe(Effect.provide(layerPairingOnboarding({ failExchange: true }))),
483 );
484​
485 it.effect("rejects an incompatible server without consuming the pairing credential", () =>
486 Effect.gen(function* () {
487 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
488 const error = yield* preparePairingRegistration({
489 host: "remote.example.test",
490 pairingCode: "pairing-token",
491 }).pipe(
492 Effect.provide(
493 layerPairingOnboarding({ protocolVersion: ORCHESTRATION_PROTOCOL_VERSION + 1 }, calls),
494 ),
495 Effect.flip,
496 );
497 expect(error).toMatchObject({ reason: "unsupported" });
498 expect(calls.map((call) => call.url)).toEqual([
499 "https://remote.example.test/.well-known/t3/environment",
500 ]);
501 }),
502 );
503 it.effect.each([
504 { label: "read-only", scopes: ["orchestration:read"] },
505 { label: "default", scopes: AuthStandardClientScopes },
506 ] as const)("preserves the $label grant when pairing a remote environment", ({ scopes }) =>
507 Effect.gen(function* () {
508 const registration = yield* preparePairingRegistration({
509 host: "remote.example.test",
510 pairingCode: "pairing-token",
511 });
512​
513 const session = yield* fetchRemoteSessionState({
514 httpBaseUrl: registration.profile.httpBaseUrl,
515 bearerToken: registration.credential.token,
516 });
517​
518 expect(session.authenticated).toBe(true);
519 expect(session.scopes).toEqual(scopes);
520 }).pipe(Effect.provide(layerPairingOnboarding({ grantScopes: scopes }))),
521 );
522​
523 it.effect("refuses to add a route that reaches a different machine, keeping the code", () =>
524 Effect.gen(function* () {
525 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
526 const error = yield* preparePairingRegistration({
527 host: "remote.example.test",
528 pairingCode: "pairing-token",
529 expectedEnvironmentId: EnvironmentId.make("some-other-machine"),
530 }).pipe(Effect.provide(layerPairingOnboarding(undefined, calls)), Effect.flip);
531 expect(error).toMatchObject({ reason: "configuration" });
532 expect(error.message).toContain("different machine");
533 expect(calls.map((call) => call.url)).toEqual([
534 "https://remote.example.test/.well-known/t3/environment",
535 ]);
536 }),
537 );
538​
539 it.effect("refuses an outdated server that cannot update itself", () =>
540 Effect.gen(function* () {
541 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
542 const error = yield* preparePairingRegistration({
543 host: "remote.example.test",
544 pairingCode: "pairing-token",
545 }).pipe(
546 Effect.provide(
547 layerPairingOnboarding({ protocolVersion: ORCHESTRATION_PROTOCOL_VERSION - 1 }, calls),
548 ),
549 Effect.flip,
550 );
551 expect(error).toMatchObject({ reason: "unsupported" });
552 expect(error).not.toHaveProperty("serverUpdateRequired");
553 expect(calls.map((call) => call.url)).toEqual([
554 "https://remote.example.test/.well-known/t3/environment",
555 ]);
556 }),
557 );
558​
559 it.effect("does not consume a pairing credential when descriptor discovery fails", () =>
560 Effect.gen(function* () {
561 const calls: Array<{ readonly url: string; readonly init: RequestInit }> = [];
562​
563 yield* preparePairingRegistration({
564 host: "remote.example.test",
565 pairingCode: "pairing-token",
566 }).pipe(Effect.provide(layerPairingOnboarding({ failDescriptor: true }, calls)), Effect.flip);
567​
568 expect(calls.map((call) => call.url)).toEqual([
569 "https://remote.example.test/.well-known/t3/environment",
570 ]);
571 }),
572 );
573​
574 it.effect("updates bearer metadata while preserving the credential and identity", () =>
575 Effect.gen(function* () {
576 const environmentId = EnvironmentId.make("environment-paired");
577 const registration = yield* prepareBearerConnectionUpdate({
578 input: {
579 environmentId,
580 label: " Renamed environment ",
581 httpBaseUrl: "http://100.65.180.100:3773/path",
582 },
583 entry: Option.some({
584 target: new BearerConnectionTarget({
585 environmentId,
586 label: "Old label",
587 connectionId: "bearer:environment-paired",
588 }),
589 profile: Option.some(
590 new BearerConnectionProfile({
591 connectionId: "bearer:environment-paired",
592 environmentId,
593 label: "Old label",
594 httpBaseUrl: "http://old.example.test/",
595 wsBaseUrl: "ws://old.example.test/",
596 }),
597 ),
598 enabled: true,
599 }),
600 credential: Option.some(new BearerConnectionCredential({ token: "bearer-token" })),
601 });
602​
603 expect(registration).toMatchObject({
604 target: {
605 environmentId,
606 label: "Renamed environment",
607 connectionId: "bearer:environment-paired",
608 },
609 profile: {
610 environmentId,
611 label: "Renamed environment",
612 httpBaseUrl: "http://100.65.180.100:3773/",
613 wsBaseUrl: "ws://100.65.180.100:3773/",
614 },
615 credential: { token: "bearer-token" },
616 });
617 }),
618 );
619​
620 it.effect("prepares an SSH registration from the provisioned platform environment", () =>
621 Effect.gen(function* () {
622 const target = {
623 alias: "devbox",
624 hostname: "devbox.example.test",
625 username: "developer",
626 port: 22,
627 };
628 const registration = yield* prepareSshRegistration({
629 target,
630 }).pipe(
631 Effect.provideService(
632 ClientCapabilities.SshEnvironmentGateway,
633 ClientCapabilities.SshEnvironmentGateway.of({
634 provision: () =>
635 Effect.succeed({
636 environmentId: EnvironmentId.make("environment-ssh"),
637 label: "Remote development box",
638 bootstrap: {
639 target,
640 httpBaseUrl: "http://127.0.0.1:3201",
641 wsBaseUrl: "ws://127.0.0.1:3201",
642 pairingToken: "pairing-token",
643 },
644 bearerToken: "bearer-token",
645 }),
646 prepare: () => Effect.die("unused"),
647 disconnect: () => Effect.die("unused"),
648 }),
649 ),
650 );
651​
652 expect(registration).toMatchObject({
653 _tag: "SshConnectionRegistration",
654 target: {
655 environmentId: "environment-ssh",
656 label: "Remote development box",
657 connectionId: 'ssh:environment-ssh:["devbox","devbox.example.test","developer",22]',
658 },
659 profile: {
660 environmentId: "environment-ssh",
661 label: "Remote development box",
662 connectionId: 'ssh:environment-ssh:["devbox","devbox.example.test","developer",22]',
663 target,
664 },
665 // Saved so the first connect reuses this pairing instead of adding another.
666 credential: { token: "bearer-token" },
667 });
668 }),
669 );
670});