Skip to content
Source
Sign inSign up

driver.test.ts

74 lines2.7 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Ref from "effect/Ref";
7import * as FetchHttpClient from "effect/http/FetchHttpClient";
8​
9import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
10import * as RpcSession from "../rpc/session.ts";
11import type { ConnectionCatalogEntry } from "./catalog.ts";
12import * as ConnectionDriver from "./driver.ts";
13import { ConnectionTransientError, SshConnectionTarget, type PreparedConnection } from "./model.ts";
14import * as ConnectionResolver from "./resolver.ts";
15​
16const sshTarget = (connectionId: string) =>
17 new SshConnectionTarget({
18 environmentId: EnvironmentId.make("environment-1"),
19 label: "Test environment",
20 connectionId,
21 });
22​
23const 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​
30describe("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});