Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

connect.test.ts

166 lines5.4 KB

1import * as RelayClient from "@v12/shared/relayClient";
2import { assert, it } from "@effect/vitest";
3import * as Cause from "effect/Cause";
4import * as Console from "effect/Console";
5import * as Effect from "effect/Effect";
6import * as Exit from "effect/Exit";
7import * as Logger from "effect/Logger";
8import * as Option from "effect/Option";
9import * as References from "effect/References";
10​
11import {
12 acquireRelayClientForLink,
13 isPublishAgentActivityEnabledValue,
14 reportCloudDisconnectResults,
15} from "./connect.ts";
16​
17const managedExecutable = {
18 status: "available",
19 executablePath: "/tmp/cloudflared",
20 source: "managed",
21 version: RelayClient.CLOUDFLARED_VERSION,
22} as const;
23​
24it.effect("does not install the relay client when the user declines the managed download", () =>
25 Effect.gen(function* () {
26 let installCalls = 0;
27 const result = yield* acquireRelayClientForLink(
28 {
29 resolve: Effect.succeed({
30 status: "missing",
31 version: RelayClient.CLOUDFLARED_VERSION,
32 }),
33 install: Effect.sync(() => {
34 installCalls += 1;
35 return managedExecutable;
36 }),
37 installWithProgress: () =>
38 Effect.sync(() => {
39 installCalls += 1;
40 return managedExecutable;
41 }),
42 },
43 () => Effect.succeed(false),
44 () => Effect.void,
45 );
46​
47 assert.isTrue(Option.isNone(result));
48 assert.equal(installCalls, 0);
49 }),
50);
51​
52it.effect("installs the relay client after the user accepts the managed download", () =>
53 Effect.gen(function* () {
54 let installCalls = 0;
55 const progress: Array<string> = [];
56 const result = yield* acquireRelayClientForLink(
57 {
58 resolve: Effect.succeed({
59 status: "missing",
60 version: RelayClient.CLOUDFLARED_VERSION,
61 }),
62 install: Effect.sync(() => {
63 installCalls += 1;
64 return managedExecutable;
65 }),
66 installWithProgress: (report) =>
67 report({ type: "progress", stage: "downloading" }).pipe(
68 Effect.andThen(
69 Effect.sync(() => {
70 installCalls += 1;
71 return managedExecutable;
72 }),
73 ),
74 ),
75 },
76 () => Effect.succeed(true),
77 (event) =>
78 Effect.sync(() => {
79 if (event.type === "progress") {
80 progress.push(event.stage);
81 }
82 }),
83 );
84​
85 assert.deepEqual(Option.getOrThrow(result), managedExecutable);
86 assert.equal(installCalls, 1);
87 assert.deepEqual(progress, ["downloading"]);
88 }),
89);
90​
91it.effect("reuses an available relay client executable without prompting", () =>
92 Effect.gen(function* () {
93 let promptCalls = 0;
94 const result = yield* acquireRelayClientForLink(
95 {
96 resolve: Effect.succeed(managedExecutable),
97 install: Effect.die("unexpected install"),
98 installWithProgress: () => Effect.die("unexpected install"),
99 },
100 () =>
101 Effect.sync(() => {
102 promptCalls += 1;
103 return false;
104 }),
105 () => Effect.void,
106 );
107​
108 assert.deepEqual(Option.getOrThrow(result), managedExecutable);
109 assert.equal(promptCalls, 0);
110 }),
111);
112​
113it.effect("keeps disconnect causes in structured logs and out of console warnings", () => {
114 const warnings: ReadonlyArray<unknown>[] = [];
115 const logs: Readonly<Record<string, unknown>>[] = [];
116 const testConsole = {
117 ...globalThis.console,
118 warn: (...args: ReadonlyArray<unknown>) => {
119 warnings.push(args);
120 },
121 } satisfies Console.Console;
122 const logger = Logger.make(({ fiber }) => {
123 logs.push(fiber.getRef(References.CurrentLogAnnotations));
124 });
125 const liveFailure = "live unlink private diagnostic";
126 const relayFailure = "relay revoke private diagnostic";
127​
128 return reportCloudDisconnectResults({
129 clearAuthorization: true,
130 liveResult: {
131 status: "failed",
132 cause: Cause.fail(new Error(liveFailure)),
133 },
134 relayResult: Exit.failCause(Cause.die(new Error(relayFailure))),
135 }).pipe(
136 Effect.provideService(Console.Console, testConsole),
137 Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
138 Effect.tap(() =>
139 Effect.sync(() => {
140 assert.lengthOf(warnings, 2);
141 const warningText = warnings.flat().map(String).join("\n");
142 assert.include(warningText, "running server could not stop its tunnel");
143 assert.include(warningText, "Could not revoke the relay-side environment record");
144 assert.notInclude(warningText, liveFailure);
145 assert.notInclude(warningText, relayFailure);
146 assert.deepEqual(
147 logs.map(({ operation, clearAuthorization }) => ({ operation, clearAuthorization })),
148 [
149 { operation: "live-server-unlink", clearAuthorization: true },
150 { operation: "relay-environment-unlink", clearAuthorization: true },
151 ],
152 );
153 const loggedCauses = logs.map((log) => String(log.cause)).join("\n");
154 assert.include(loggedCauses, liveFailure);
155 assert.include(loggedCauses, relayFailure);
156 }),
157 ),
158 );
159});
160​
161it("treats only the literal 'true' as publish-enabled", () => {
162 assert.equal(isPublishAgentActivityEnabledValue("true"), true);
163 assert.equal(isPublishAgentActivityEnabledValue("false"), false);
164 assert.equal(isPublishAgentActivityEnabledValue(null), false);
165 assert.equal(isPublishAgentActivityEnabledValue("TRUE"), false);
166});