Files
t3code/apps/web/src/connection/platform.test.ts
T

106 lines
3.1 KiB
TypeScript

import { ConnectionTransientError } from "@t3tools/client-runtime/connection";
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 descriptorError?: Error },
): 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?.descriptorError !== undefined) {
throw options.descriptorError;
}
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, { descriptorError: new Error("descriptor unavailable") }),
TARGET,
).pipe(Effect.flip);
expect(calls).toEqual(["ensure", "descriptor"]);
}),
);
it.effect("preserves SSH preparation causes without exposing their message", () =>
Effect.gen(function* () {
const cause = new Error("descriptor response contained a private endpoint");
const error = yield* provisionDesktopSshEnvironment(
makeBridge([], { descriptorError: cause }),
TARGET,
).pipe(Effect.flip);
expect(error).toBeInstanceOf(ConnectionTransientError);
expect(error.message).toBe("Could not prepare the SSH environment.");
expect(error.message).not.toContain(cause.message);
expect(error.cause).toBe(cause);
}),
);
});