The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Ref from "effect/Ref"; |
| 7 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 8 | |
| 9 | import * as RemoteEnvironmentAuthorization from "../authorization/service.ts"; |
| 10 | import * as RpcSession from "../rpc/session.ts"; |
| 11 | import type { ConnectionCatalogEntry } from "./catalog.ts"; |
| 12 | import * as ConnectionDriver from "./driver.ts"; |
| 13 | import { ConnectionTransientError, SshConnectionTarget, type PreparedConnection } from "./model.ts"; |
| 14 | import * as ConnectionResolver from "./resolver.ts"; |
| 15 | |
| 16 | const sshTarget = (connectionId: string) => |
| 17 | new SshConnectionTarget({ |
| 18 | environmentId: EnvironmentId.make("environment-1"), |
| 19 | label: "Test environment", |
| 20 | connectionId, |
| 21 | }); |
| 22 | |
| 23 | const ENTRY: ConnectionCatalogEntry = { |
| 24 | target: sshTarget("ssh-first"), |
| 25 | profile: Option.none(), |
| 26 | enabled: true, |
| 27 | alternateRoutes: [{ target: sshTarget("ssh-second"), profile: Option.none() }], |
| 28 | }; |
| 29 | |
| 30 | describe("ConnectionDriver", () => { |
| 31 | it.effect("reports preparing before each route it prepares", () => |
| 32 | Effect.gen(function* () { |
| 33 | const stages = yield* Ref.make<ReadonlyArray<string>>([]); |
| 34 | const driver = yield* ConnectionDriver.make.pipe( |
| 35 | Effect.provide( |
| 36 | Layer.mergeAll( |
| 37 | Layer.mock(ConnectionResolver.ConnectionResolver)({ |
| 38 | prepare: (entry) => |
| 39 | Effect.succeed({ |
| 40 | environmentId: entry.target.environmentId, |
| 41 | label: entry.target.label, |
| 42 | httpBaseUrl: "http://127.0.0.1:1", |
| 43 | socketUrl: "ws://127.0.0.1:1/ws", |
| 44 | httpAuthorization: null, |
| 45 | target: entry.target, |
| 46 | } satisfies PreparedConnection), |
| 47 | }), |
| 48 | Layer.mock(RpcSession.RpcSessionFactory)({ |
| 49 | connect: () => |
| 50 | Effect.fail( |
| 51 | new ConnectionTransientError({ reason: "transport", detail: "Socket closed." }), |
| 52 | ), |
| 53 | }), |
| 54 | Layer.mock(RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization)({}), |
| 55 | // SSH routes have no HTTP check, so the driver never sends a request. |
| 56 | FetchHttpClient.layer, |
| 57 | ), |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | yield* driver |
| 62 | .connect(ENTRY, (progress) => Ref.update(stages, (all) => [...all, progress.stage])) |
| 63 | .pipe(Effect.scoped, Effect.flip); |
| 64 | |
| 65 | expect(yield* Ref.get(stages)).toEqual([ |
| 66 | "preparing", |
| 67 | "preparing", |
| 68 | "opening", |
| 69 | "preparing", |
| 70 | "opening", |
| 71 | ]); |
| 72 | }), |
| 73 | ); |
| 74 | }); |