The best way to control Claude Code, Codex and more from any computer or phone

main
ts 140 lines 4.98 KB
Raw
1 import { stripVTControlCharacters } from "node:util";
2 import { describe, expect, it, test } from "@alchemy.run/test";
3 import * as Console from "effect/Console";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import { LoggingCli, formatPlanLines } from "@/Cli/LoggingCli.ts";
7 import { Cli } from "@/Report.ts";
8 import { PlatformServices } from "@/Util/PlatformServices.ts";
9 import { createNode, deleteNode, planWith, replaceNode, updateNode } from "./PlanTestNodes.ts";
10
11 /**
12 * The plain renderer's printed lines, without the time and level prefix.
13 * It presents straight to the console, not through the logger.
14 */
15 const capturePresented = () => {
16 const lines: string[] = [];
17 const console = Object.assign(Object.create(globalThis.console), {
18 log: (...args: unknown[]) =>
19 lines.push(stripVTControlCharacters(args.join(" ")).replace(/^\[[\d:.]+\] \w+: /, "")),
20 });
21 return { lines, provide: Effect.provideService(Console.Console, console) };
22 };
23
24 describe("formatPlanLines", { tags: ["unit", "local"] }, () => {
25 test("keeps compact output unchanged by default", () => {
26 expect(
27 formatPlanLines(
28 planWith([
29 updateNode({ value: "old" }, { value: "new" }, "First"),
30 replaceNode({ engine: "v1" }, { engine: "v2" }, "Second"),
31 ]),
32 ),
33 ).toEqual(["Plan: 1 to update, 1 to replace", "[First] update", "[Second] replace"]);
34 });
35
36 test("renders detailed creates and updates as YAML", () => {
37 const output = formatPlanLines(
38 planWith([
39 createNode({ region: "iad", ports: [80, 443] }, "Api"),
40 updateNode({ retries: 2 }, { retries: 3 }, "Worker"),
41 ]),
42 { detailed: true },
43 ).join("\n");
44 expect(output).toContain(" properties:\n ports:\n - 80");
45 expect(output).toContain(" properties:\n - retries: 2");
46 expect(output).toContain(" + retries: 3");
47 });
48
49 test("keeps deletes compact and reports non-property replacements", () => {
50 const output = formatPlanLines(
51 planWith(
52 [replaceNode({ name: "same" }, { name: "same" })],
53 [deleteNode({ secret: "old" }, "OldWorker")],
54 ),
55 { detailed: true },
56 ).join("\n");
57 expect(output).toContain("no declared property changes");
58 expect(output).not.toContain("secret:");
59 });
60 });
61
62 it.effect(
63 "presents plain progress on the console",
64 () => {
65 const output = capturePresented();
66 return Effect.gen(function* () {
67 const cli = yield* Cli;
68
69 yield* cli.displayPlan(planWith([createNode({}, "Worker")]));
70
71 expect(output.lines).toEqual(["Plan: 1 to create", "[Worker] create"]);
72 }).pipe(Effect.provide(Layer.provide(LoggingCli, PlatformServices)), output.provide);
73 },
74 { tags: ["unit", "local"] },
75 );
76
77 it.effect(
78 "prints dev stack outputs with the plain apply renderer",
79 () => {
80 const output = capturePresented();
81 return Effect.gen(function* () {
82 const cli = yield* Cli;
83 const session = yield* cli.startApplySession(planWith([]), { dev: true });
84 expect(session.setOutput).toBeDefined();
85 yield* session.setOutput!({ api: "http://localhost:1337/" });
86 expect(output.lines).toContainEqual("{ api: 'http://localhost:1337/' }");
87 }).pipe(Effect.provide(Layer.provide(LoggingCli, PlatformServices)), output.provide);
88 },
89 { tags: ["unit", "local"] },
90 );
91
92 it.effect(
93 "streams apply notes as they arrive",
94 () => {
95 const output = capturePresented();
96 return Effect.gen(function* () {
97 const cli = yield* Cli;
98 const session = yield* cli.startApplySession(
99 planWith([updateNode({ v: 1 }, { v: 2 }, "Website")]),
100 );
101
102 const note = (message: string, kind?: "status" | "output") =>
103 session.emit({
104 type: "apply.resource.note",
105 fqn: "Website",
106 id: "Website",
107 message,
108 kind,
109 });
110 const status = (status: "updating" | "updated") =>
111 session.emit({
112 type: "apply.resource.status",
113 fqn: "Website",
114 id: "Website",
115 resourceType: "Cloudflare::Worker",
116 status,
117 });
118
119 yield* status("updating");
120 yield* note("Uploading worker (1.2 MB) ...", "status");
121 yield* note("build output line", "output");
122 yield* note("Uploaded 0 of 5195 assets...");
123 yield* note("Uploaded 490 of 5195 assets...");
124 // spinner-style refresh of the same message is deduped
125 yield* note("Uploaded 490 of 5195 assets...");
126 yield* note("Reconciling custom domains (1) ...", "status");
127 yield* status("updated");
128
129 const progress = output.lines.slice(3); // skip plan preview + blank line
130 expect(progress).toEqual([
131 "[Website] updating",
132 "[Website] build output line",
133 "[Website] Uploaded 0 of 5195 assets...",
134 "[Website] Uploaded 490 of 5195 assets...",
135 "[Website] updated — Reconciling custom domains (1) ... (0ms)",
136 ]);
137 }).pipe(Effect.provide(Layer.provide(LoggingCli, PlatformServices)), output.provide);
138 },
139 { tags: ["unit", "local"] },
140 );