219 lines
7.3 KiB
TypeScript
219 lines
7.3 KiB
TypeScript
import * as RelayClient from "@t3tools/shared/relayClient";
|
|
import { assert, it } from "@effect/vitest";
|
|
import * as Cause from "effect/Cause";
|
|
import * as ConfigProvider from "effect/ConfigProvider";
|
|
import * as Console from "effect/Console";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Exit from "effect/Exit";
|
|
import * as Logger from "effect/Logger";
|
|
import * as Option from "effect/Option";
|
|
import * as References from "effect/References";
|
|
import * as Terminal from "effect/Terminal";
|
|
|
|
import * as BootService from "../cloud/bootService.ts";
|
|
import {
|
|
acquireRelayClientForLink,
|
|
formatHeadlessAuthorizationPrompt,
|
|
formatRelayClientReady,
|
|
headlessSessionConfig,
|
|
isPublishAgentActivityEnabledValue,
|
|
reportCloudDisconnectResults,
|
|
} from "./connect.ts";
|
|
import { recoverServiceOnboardingOffer } from "./service.ts";
|
|
|
|
it("explains how to complete headless authorization", () => {
|
|
assert.equal(
|
|
formatHeadlessAuthorizationPrompt("https://example.test/connect"),
|
|
[
|
|
"Headless authorization",
|
|
"Open this URL on a device with a browser:",
|
|
" https://example.test/connect",
|
|
"",
|
|
"After signing in, return here and enter the code shown in your browser.",
|
|
].join("\n"),
|
|
);
|
|
});
|
|
|
|
it("formats relay readiness without printing its installation path", () => {
|
|
assert.equal(formatRelayClientReady("2026.5.2"), "✓ Relay client ready · cloudflared 2026.5.2");
|
|
});
|
|
|
|
const readHeadlessSessionConfig = (env: Record<string, string>) =>
|
|
headlessSessionConfig.pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))));
|
|
|
|
const managedExecutable = {
|
|
status: "available",
|
|
executablePath: "/tmp/cloudflared",
|
|
source: "managed",
|
|
version: RelayClient.CLOUDFLARED_VERSION,
|
|
} as const;
|
|
|
|
it.effect("detects headless operation from individual SSH config values", () =>
|
|
Effect.gen(function* () {
|
|
assert.isFalse(yield* readHeadlessSessionConfig({}));
|
|
assert.isFalse(yield* readHeadlessSessionConfig({ CI: "true" }));
|
|
assert.isTrue(yield* readHeadlessSessionConfig({ SSH_CONNECTION: "client server" }));
|
|
assert.isTrue(yield* readHeadlessSessionConfig({ SSH_TTY: "/dev/pts/1" }));
|
|
}),
|
|
);
|
|
|
|
it.effect("treats cancelling optional background setup as a successful skip", () =>
|
|
Effect.gen(function* () {
|
|
const result = yield* recoverServiceOnboardingOffer(Effect.fail(new Terminal.QuitError({})));
|
|
assert.isFalse(result);
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps a successful connection when a remote service update is pending", () =>
|
|
Effect.gen(function* () {
|
|
const result = yield* recoverServiceOnboardingOffer(
|
|
Effect.fail(new BootService.BootServiceUpdatePendingError()),
|
|
);
|
|
assert.isFalse(result);
|
|
}),
|
|
);
|
|
|
|
it.effect("does not install the relay client when the user declines the managed download", () =>
|
|
Effect.gen(function* () {
|
|
let installCalls = 0;
|
|
const result = yield* acquireRelayClientForLink(
|
|
{
|
|
resolve: Effect.succeed({
|
|
status: "missing",
|
|
version: RelayClient.CLOUDFLARED_VERSION,
|
|
}),
|
|
install: Effect.sync(() => {
|
|
installCalls += 1;
|
|
return managedExecutable;
|
|
}),
|
|
installWithProgress: () =>
|
|
Effect.sync(() => {
|
|
installCalls += 1;
|
|
return managedExecutable;
|
|
}),
|
|
},
|
|
() => Effect.succeed(false),
|
|
() => Effect.void,
|
|
);
|
|
|
|
assert.isTrue(Option.isNone(result));
|
|
assert.equal(installCalls, 0);
|
|
}),
|
|
);
|
|
|
|
it.effect("installs the relay client after the user accepts the managed download", () =>
|
|
Effect.gen(function* () {
|
|
let installCalls = 0;
|
|
const progress: Array<string> = [];
|
|
const result = yield* acquireRelayClientForLink(
|
|
{
|
|
resolve: Effect.succeed({
|
|
status: "missing",
|
|
version: RelayClient.CLOUDFLARED_VERSION,
|
|
}),
|
|
install: Effect.sync(() => {
|
|
installCalls += 1;
|
|
return managedExecutable;
|
|
}),
|
|
installWithProgress: (report) =>
|
|
report({ type: "progress", stage: "downloading" }).pipe(
|
|
Effect.andThen(
|
|
Effect.sync(() => {
|
|
installCalls += 1;
|
|
return managedExecutable;
|
|
}),
|
|
),
|
|
),
|
|
},
|
|
() => Effect.succeed(true),
|
|
(event) =>
|
|
Effect.sync(() => {
|
|
if (event.type === "progress") {
|
|
progress.push(event.stage);
|
|
}
|
|
}),
|
|
);
|
|
|
|
assert.deepEqual(Option.getOrThrow(result), managedExecutable);
|
|
assert.equal(installCalls, 1);
|
|
assert.deepEqual(progress, ["downloading"]);
|
|
}),
|
|
);
|
|
|
|
it.effect("reuses an available relay client executable without prompting", () =>
|
|
Effect.gen(function* () {
|
|
let promptCalls = 0;
|
|
const result = yield* acquireRelayClientForLink(
|
|
{
|
|
resolve: Effect.succeed(managedExecutable),
|
|
install: Effect.die("unexpected install"),
|
|
installWithProgress: () => Effect.die("unexpected install"),
|
|
},
|
|
() =>
|
|
Effect.sync(() => {
|
|
promptCalls += 1;
|
|
return false;
|
|
}),
|
|
() => Effect.void,
|
|
);
|
|
|
|
assert.deepEqual(Option.getOrThrow(result), managedExecutable);
|
|
assert.equal(promptCalls, 0);
|
|
}),
|
|
);
|
|
|
|
it.effect("keeps disconnect causes in structured logs and out of console warnings", () => {
|
|
const warnings: ReadonlyArray<unknown>[] = [];
|
|
const logs: Readonly<Record<string, unknown>>[] = [];
|
|
const testConsole = {
|
|
...globalThis.console,
|
|
warn: (...args: ReadonlyArray<unknown>) => {
|
|
warnings.push(args);
|
|
},
|
|
} satisfies Console.Console;
|
|
const logger = Logger.make(({ fiber }) => {
|
|
logs.push(fiber.getRef(References.CurrentLogAnnotations));
|
|
});
|
|
const liveFailure = "live unlink private diagnostic";
|
|
const relayFailure = "relay revoke private diagnostic";
|
|
|
|
return reportCloudDisconnectResults({
|
|
clearAuthorization: true,
|
|
liveResult: {
|
|
status: "failed",
|
|
cause: Cause.fail(new Error(liveFailure)),
|
|
},
|
|
relayResult: Exit.failCause(Cause.die(new Error(relayFailure))),
|
|
}).pipe(
|
|
Effect.provideService(Console.Console, testConsole),
|
|
Effect.provide(Logger.layer([logger], { mergeWithExisting: false })),
|
|
Effect.tap(() =>
|
|
Effect.sync(() => {
|
|
assert.lengthOf(warnings, 2);
|
|
const warningText = warnings.flat().map(String).join("\n");
|
|
assert.include(warningText, "running server could not stop its tunnel");
|
|
assert.include(warningText, "Could not revoke the relay-side environment record");
|
|
assert.notInclude(warningText, liveFailure);
|
|
assert.notInclude(warningText, relayFailure);
|
|
assert.deepEqual(
|
|
logs.map(({ operation, clearAuthorization }) => ({ operation, clearAuthorization })),
|
|
[
|
|
{ operation: "live-server-unlink", clearAuthorization: true },
|
|
{ operation: "relay-environment-unlink", clearAuthorization: true },
|
|
],
|
|
);
|
|
const loggedCauses = logs.map((log) => String(log.cause)).join("\n");
|
|
assert.include(loggedCauses, liveFailure);
|
|
assert.include(loggedCauses, relayFailure);
|
|
}),
|
|
),
|
|
);
|
|
});
|
|
|
|
it("treats only the literal 'true' as publish-enabled", () => {
|
|
assert.equal(isPublishAgentActivityEnabledValue("true"), true);
|
|
assert.equal(isPublishAgentActivityEnabledValue("false"), false);
|
|
assert.equal(isPublishAgentActivityEnabledValue(null), false);
|
|
assert.equal(isPublishAgentActivityEnabledValue("TRUE"), false);
|
|
});
|