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