Open History to see the last commit
1import * as RelayClient from "@t3tools/shared/relayClient";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5
6import { acquireRelayClientForLink } from "./connect.ts";
7
8const managedExecutable = {
9 status: "available",
10 executablePath: "/tmp/cloudflared",
11 source: "managed",
12 version: RelayClient.CLOUDFLARED_VERSION,
13} as const;
14
15it.effect("does not install the relay client when the user declines the managed download", () =>
16 Effect.gen(function* () {
17 let installCalls = 0;
18 const result = yield* acquireRelayClientForLink(
19 {
20 resolve: Effect.succeed({
21 status: "missing",
22 version: RelayClient.CLOUDFLARED_VERSION,
23 }),
24 install: Effect.sync(() => {
25 installCalls += 1;
26 return managedExecutable;
27 }),
28 installWithProgress: () =>
29 Effect.sync(() => {
30 installCalls += 1;
31 return managedExecutable;
32 }),
33 },
34 () => Effect.succeed(false),
35 () => Effect.void,
36 );
37
38 assert.isTrue(Option.isNone(result));
39 assert.equal(installCalls, 0);
40 }),
41);
42
43it.effect("installs the relay client after the user accepts the managed download", () =>
44 Effect.gen(function* () {
45 let installCalls = 0;
46 const progress: Array<string> = [];
47 const result = yield* acquireRelayClientForLink(
48 {
49 resolve: Effect.succeed({
50 status: "missing",
51 version: RelayClient.CLOUDFLARED_VERSION,
52 }),
53 install: Effect.sync(() => {
54 installCalls += 1;
55 return managedExecutable;
56 }),
57 installWithProgress: (report) =>
58 report({ type: "progress", stage: "downloading" }).pipe(
59 Effect.andThen(
60 Effect.sync(() => {
61 installCalls += 1;
62 return managedExecutable;
63 }),
64 ),
65 ),
66 },
67 () => Effect.succeed(true),
68 (event) =>
69 Effect.sync(() => {
70 if (event.type === "progress") {
71 progress.push(event.stage);
72 }
73 }),
74 );
75
76 assert.deepEqual(Option.getOrThrow(result), managedExecutable);
77 assert.equal(installCalls, 1);
78 assert.deepEqual(progress, ["downloading"]);
79 }),
80);
81
82it.effect("reuses an available relay client executable without prompting", () =>
83 Effect.gen(function* () {
84 let promptCalls = 0;
85 const result = yield* acquireRelayClientForLink(
86 {
87 resolve: Effect.succeed(managedExecutable),
88 install: Effect.die("unexpected install"),
89 installWithProgress: () => Effect.die("unexpected install"),
90 },
91 () =>
92 Effect.sync(() => {
93 promptCalls += 1;
94 return false;
95 }),
96 () => Effect.void,
97 );
98
99 assert.deepEqual(Option.getOrThrow(result), managedExecutable);
100 assert.equal(promptCalls, 0);
101 }),
102);