// The live frame through `CliUiTest.live`, then `withLiveView` on a session's terminal. import { assert, describe, it } from "@effect/vitest"; import { Render } from "@effected/cli-kit"; import { CliUiTest } from "@effected/cli-kit/ui/testing"; import type { BuildProgress } from "@effected/engine/build"; import { Console, Effect, Exit } from "effect"; import { initialPackage, reducePackage } from "../../src/render/from/package.js"; import { liveFrame, withLiveView } from "../../src/render/LiveView.js"; import type { RunView } from "../../src/render/RunView.js"; import { renderRows } from "../../src/render/ViewDoc.js"; import { packageClean } from "./views.js"; const human = Render.contextOf({ audience: "human" }); const rowsOf = (view: RunView) => Render.plain(renderRows(view, { compact: false }), human).split("\n"); const progress = (event: BuildProgress) => ({ _tag: "Progress", event }) as const; const planned: BuildProgress = { _tag: "Planned", groups: [{ id: "node", passes: ["js", "dts"] }], meta: true }; describe("liveFrame", () => { it.effect("a package build's frame settles to the rows renderDoc draws for the finished view", () => Effect.gen(function* () { const view = yield* CliUiTest.live({ ...liveFrame(initialPackage("@effected/yaml"), reducePackage), columns: 100, }); yield* view.publish(progress(planned)); yield* view.publish(progress({ _tag: "PassStarted", group: "node", pass: "js" })); yield* view.advance("80 millis"); const running = yield* view.transcript; assert.include(running, "effected package build @effected/yaml"); assert.match(running, /\S js/, "a running row has a mark"); assert.match(running, /^ {4}dts$/m, "a pending row is its name alone"); yield* view.publish( progress({ _tag: "PassDone", group: "node", pass: "js", ms: 1200, files: 3, bytes: 4096, gzip: 1024, ok: true, }), ); yield* view.advance("80 millis"); assert.match(yield* view.transcript, /✓ js +1\.2s/); yield* view.publish({ _tag: "Ended", view: packageClean }); yield* view.end; const settled = (yield* view.transcript).split("\n"); const expected = rowsOf(packageClean); assert.isAbove(expected.length, 3); assert.deepStrictEqual(settled.slice(-expected.length), expected); }).pipe(Effect.scoped), ); it.effect("an end with no finished view keeps the rows it reached, with nothing left spinning", () => Effect.gen(function* () { const view = yield* CliUiTest.live({ ...liveFrame(initialPackage("pkg"), reducePackage), columns: 100 }); yield* view.publish(progress(planned)); yield* view.publish(progress({ _tag: "PassStarted", group: "node", pass: "js" })); yield* view.publish({ _tag: "Ended" }); yield* view.end; const reached = reducePackage(reducePackage(initialPackage("pkg"), planned), { _tag: "PassStarted", group: "node", pass: "js", }); const expected = rowsOf(reached); assert.deepStrictEqual((yield* view.transcript).split("\n").slice(-expected.length), expected); }).pipe(Effect.scoped), ); }); describe("withLiveView", () => { const live = ( walk: (onProgress: (event: BuildProgress) => Effect.Effect) => Effect.Effect, settle?: (answer: A) => RunView, ) => Effect.gen(function* () { const ui = yield* CliUiTest.session({ columns: 100 }); const exit = yield* Effect.exit( withLiveView({ initial: initialPackage("pkg"), reduce: reducePackage, walk, ...(settle !== undefined ? { settle } : {}), }).pipe(Effect.provide(ui.layer)), ); return { exit, transcript: yield* ui.transcript, mounts: yield* ui.mounts }; }).pipe(Effect.scoped) as Effect.Effect<{ readonly exit: Exit.Exit; readonly transcript: string; readonly mounts: number; }>; it.effect("hands back the walk's answer, with the finished view's rows as the last frame", () => Effect.gen(function* () { const result = yield* live( (onProgress) => Effect.as(onProgress(planned), 7), () => packageClean, ); assert.deepStrictEqual(result.exit, Exit.succeed(7)); assert.strictEqual(result.mounts, 1); const expected = rowsOf(packageClean); assert.deepStrictEqual(result.transcript.trimEnd().split("\n").slice(-expected.length), expected); }), ); it.effect("a walk that fails keeps the rows it reached and rethrows", () => Effect.gen(function* () { const result = yield* live((onProgress) => Effect.andThen(onProgress(planned), Effect.fail("boom" as const))); assert.deepStrictEqual(result.exit, Exit.fail("boom")); assert.match(result.transcript, /^node$/m); assert.match(result.transcript, /^ {2}◯ js$/m, "settled: a pass never reached has the pending mark"); }), ); it.effect("a walk that fails before any progress mounts nothing", () => Effect.gen(function* () { const result = yield* live(() => Effect.fail("early" as const)); assert.deepStrictEqual(result.exit, Exit.fail("early")); assert.strictEqual(result.mounts, 0); }), ); it.effect("a log line during the walk prints above the frame", () => Effect.gen(function* () { const result = yield* live((onProgress) => Effect.gen(function* () { yield* onProgress(planned); yield* Console.log("a line from the walk"); yield* onProgress({ _tag: "PassStarted", group: "node", pass: "js" }); }), ); const lines = result.transcript.split("\n"); const logged = lines.indexOf("a line from the walk"); assert.isAtLeast(logged, 0, result.transcript); assert.isBelow(logged, lines.lastIndexOf("node"), result.transcript); }), ); });