import { AuthStandardClientScopes, EnvironmentId, type DesktopBridge, type DesktopSshEnvironmentTarget, } from "@t3tools/contracts"; import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import { provisionDesktopSshEnvironment } from "./platform.ts"; const TARGET: DesktopSshEnvironmentTarget = { alias: "devbox", hostname: "devbox.example.test", username: "developer", port: 22, }; function makeBridge( calls: string[], options?: { readonly failDescriptor?: boolean }, ): DesktopBridge { return { ensureSshEnvironment: async (target: DesktopSshEnvironmentTarget) => { calls.push("ensure"); return { target, httpBaseUrl: "http://127.0.0.1:3201/", wsBaseUrl: "ws://127.0.0.1:3201/", pairingToken: "pairing-token", }; }, fetchSshEnvironmentDescriptor: async () => { calls.push("descriptor"); if (options?.failDescriptor === true) { throw new Error("descriptor unavailable"); } return { environmentId: EnvironmentId.make("environment-ssh"), label: "SSH environment", platform: { os: "linux", arch: "x64", }, serverVersion: "0.0.0-test", capabilities: { repositoryIdentity: true, }, }; }, bootstrapSshBearerSession: async () => { calls.push("token"); return { access_token: "bearer-token", issued_token_type: "urn:ietf:params:oauth:token-type:access_token", token_type: "Bearer", expires_in: 3_600, scope: AuthStandardClientScopes.join(" "), }; }, } as unknown as DesktopBridge; } describe("desktop SSH pairing", () => { it.effect("fetches the descriptor before consuming the one-time credential", () => Effect.gen(function* () { const calls: string[] = []; const provisioned = yield* provisionDesktopSshEnvironment(makeBridge(calls), TARGET); expect(provisioned.environmentId).toBe(EnvironmentId.make("environment-ssh")); expect(calls).toEqual(["ensure", "descriptor", "token"]); }), ); it.effect("does not consume the credential when descriptor discovery fails", () => Effect.gen(function* () { const calls: string[] = []; yield* provisionDesktopSshEnvironment( makeBridge(calls, { failDescriptor: true }), TARGET, ).pipe(Effect.flip); expect(calls).toEqual(["ensure", "descriptor"]); }), ); });