Skip to content
Closed
80 changes: 76 additions & 4 deletions apps/server/src/cli/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import type { ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
import * as NetService from "@t3tools/shared/Net";
import { expect, it } from "@effect/vitest";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Queue from "effect/Queue";
import * as Terminal from "effect/Terminal";
import * as TestConsole from "effect/testing/TestConsole";
import { Command } from "effect/cli";

Expand All @@ -28,6 +32,16 @@ const reports = TestConsole.logLines.pipe(
),
);

/** A scoped base dir whose settings turn linked environments on. */
const linkingBaseDir = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-environment-cli-" });
const { settingsPath } = yield* ServerConfig.deriveServerPaths(baseDir, undefined);
yield* fs.makeDirectory(NodePath.dirname(settingsPath), { recursive: true });
yield* fs.writeFileString(settingsPath, JSON.stringify({ linkedEnvironmentsEnabled: true }));
return baseDir;
});

/** A machine of the fleet, on a socket of its own. */
const serveMachine = (descriptor: ExecutionEnvironmentDescriptor) =>
Layer.build(Layer.fresh(NodeHttpServer.layerTest)).pipe(
Expand All @@ -40,10 +54,7 @@ it.effect("links a file of environments, reports each line, and fails when one d
Effect.scoped(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-environment-cli-" });
const { settingsPath } = yield* ServerConfig.deriveServerPaths(baseDir, undefined);
yield* fs.makeDirectory(NodePath.dirname(settingsPath), { recursive: true });
yield* fs.writeFileString(settingsPath, JSON.stringify({ linkedEnvironmentsEnabled: true }));
const baseDir = yield* linkingBaseDir;

const one = yield* serveMachine(descriptorOf("environment-vm-1", "VM 1"));
const two = yield* serveMachine(descriptorOf("environment-vm-2", "VM 2"));
Expand Down Expand Up @@ -123,3 +134,64 @@ it.effect("links a file of environments, reports each line, and fails when one d
}),
).pipe(Effect.provide(Layer.mergeAll(layerCliRuntime, TestConsole.layer))),
);

/** A terminal that answers each prompt with the next text and Enter, and keeps every frame it shows. */
const scriptedTerminal = (answers: ReadonlyArray<string>) => {
const shown: Array<string> = [];
const pending = [...answers];
const press = (name: string, input = Option.none<string>()): Terminal.UserInput => ({
input,
key: { name, ctrl: false, meta: false, shift: false },
});
const terminal = Terminal.make({
columns: Effect.succeed(80),
rows: Effect.succeed(24),
size: Queue.unbounded<Terminal.Size, Cause.Done>(),
readInput: Effect.gen(function* () {
const input = yield* Queue.unbounded<Terminal.UserInput, Cause.Done>();
const answer = pending.shift() ?? "";
if (answer !== "") yield* Queue.offer(input, press(answer, Option.some(answer)));
yield* Queue.offer(input, press("enter"));
return input;
}),
readLine: Effect.die("unused"),
display: (text) => Effect.sync(() => void shown.push(text)),
});
return { terminal, shown };
};

it.effect("the wizard links with the pairing code it never shows", () =>
Effect.scoped(
Effect.gen(function* () {
const baseDir = yield* linkingBaseDir;
const peer = yield* serveMachine(descriptorOf("environment-box", "Box"));
const code = (yield* peer.auth.issuePairingCredential()).credential;

// One answer per prompt, in the wizard's order; "" takes the default.
const { terminal, shown } = scriptedTerminal([
"", // --base-dir: no, it is already given
"", // --dev-url: no
"", // --url count: 0
"y", // --pairing-code: yes
code,
"", // --from: no
"", // --access: no
"y", // url: yes
peer.url,
"", // run this command: yes
]);
yield* runCli(["--wizard", "environment", "link", "--base-dir", baseDir]).pipe(
Effect.provideService(Terminal.Terminal, terminal),
);

// The wizard wraps its command at 88 characters, so the temp dir's length moves each break.
const output = [...shown, ...(yield* TestConsole.logLines).map(String)]
.join("\n")
.replaceAll(" \\\n ", " ");
expect(output).toContain("--pairing-code '<redacted>'");
expect(output).toContain("Linked Box (environment-box)");
expect(output).not.toContain(code);
expect((yield* peer.linkedSessions).length).toBe(1);
}),
).pipe(Effect.provide(Layer.mergeAll(layerCliRuntime, TestConsole.layer))),
);
5 changes: 3 additions & 2 deletions apps/server/src/cli/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as References from "effect/References";
import * as Option from "effect/Option";
import * as Redacted from "effect/Redacted";
import * as Schema from "effect/Schema";
import * as Stdio from "effect/Stdio";
import * as Stream from "effect/Stream";
Expand Down Expand Up @@ -210,7 +211,7 @@ const environmentLinkCommand = Command.make("link", {
Flag.withDescription("Another address for the same environment, tried after the first."),
Flag.atLeast(0),
),
pairingCode: Flag.String("pairing-code").pipe(
pairingCode: Flag.Redacted("pairing-code").pipe(
Flag.withDescription("A pairing code from the other environment (`t3 pair` there)."),
Flag.optional,
),
Expand Down Expand Up @@ -247,7 +248,7 @@ const environmentLinkCommand = Command.make("link", {
});
}
const url = flags.url.value;
const pairingCode = flags.pairingCode.value;
const pairingCode = Redacted.value(flags.pairingCode.value);
return yield* runWithPeerLinks(flags, (links) =>
Effect.gen(function* () {
const linked = yield* links.link({
Expand Down
Loading