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

platform.test.ts

88 lines2.5 KB

1import {
2 AuthStandardClientScopes,
3 EnvironmentId,
4 type DesktopBridge,
5 type DesktopSshEnvironmentTarget,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9​
10import { provisionDesktopSshEnvironment } from "./platform.ts";
11​
12const TARGET: DesktopSshEnvironmentTarget = {
13 alias: "devbox",
14 hostname: "devbox.example.test",
15 username: "developer",
16 port: 22,
17};
18​
19function makeBridge(
20 calls: string[],
21 options?: { readonly failDescriptor?: boolean },
22): DesktopBridge {
23 return {
24 ensureSshEnvironment: async (target: DesktopSshEnvironmentTarget) => {
25 calls.push("ensure");
26 return {
27 target,
28 httpBaseUrl: "http://127.0.0.1:3201/",
29 wsBaseUrl: "ws://127.0.0.1:3201/",
30 pairingToken: "pairing-token",
31 };
32 },
33 fetchSshEnvironmentDescriptor: async () => {
34 calls.push("descriptor");
35 if (options?.failDescriptor === true) {
36 throw new Error("descriptor unavailable");
37 }
38 return {
39 environmentId: EnvironmentId.make("environment-ssh"),
40 label: "SSH environment",
41 platform: {
42 os: "linux",
43 arch: "x64",
44 },
45 serverVersion: "0.0.0-test",
46 capabilities: {
47 repositoryIdentity: true,
48 },
49 };
50 },
51 bootstrapSshBearerSession: async () => {
52 calls.push("token");
53 return {
54 access_token: "bearer-token",
55 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
56 token_type: "Bearer",
57 expires_in: 3_600,
58 scope: AuthStandardClientScopes.join(" "),
59 };
60 },
61 } as unknown as DesktopBridge;
63​
64describe("desktop SSH pairing", () => {
65 it.effect("fetches the descriptor before consuming the one-time credential", () =>
66 Effect.gen(function* () {
67 const calls: string[] = [];
68​
69 const provisioned = yield* provisionDesktopSshEnvironment(makeBridge(calls), TARGET);
70​
71 expect(provisioned.environmentId).toBe(EnvironmentId.make("environment-ssh"));
72 expect(calls).toEqual(["ensure", "descriptor", "token"]);
73 }),
74 );
75​
76 it.effect("does not consume the credential when descriptor discovery fails", () =>
77 Effect.gen(function* () {
78 const calls: string[] = [];
79​
80 yield* provisionDesktopSshEnvironment(
81 makeBridge(calls, { failDescriptor: true }),
82 TARGET,
83 ).pipe(Effect.flip);
84​
85 expect(calls).toEqual(["ensure", "descriptor"]);
86 }),
87 );
88});