Open History to see the last commit
1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { it } from "@effect/vitest";
4import { DEFAULT_TERMINAL_ID, ThreadId } from "@t3tools/contracts";
5import { SpawnExecutableResolution } from "@t3tools/shared/shell";
6import * as Deferred from "effect/Deferred";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as Stream from "effect/Stream";
11import { expect, vi } from "vite-plus/test";
12
13import * as ProcessRunner from "../processRunner.ts";
14import { makeWithOptions } from "./Manager.ts";
15
16const node = Layer.mergeAll(NodeServices.layer, NodeCrypto.layer);
17
18it.layer(Layer.mergeAll(node, ProcessRunner.layer.pipe(Layer.provide(node))), {
19 excludeTestServices: true,
20})("TerminalManager clear", (it) => {
21 it.effect.each([
22 {
23 part: "opener",
24 publishing: "old output\r\n",
25 queued: "\u001b]0;Build ",
26 pending: "\u001b]0;Build ",
27 afterClear: "progress\u0007ready\r\n",
28 },
29 {
30 part: "CSI",
31 publishing: "old output\r\n",
32 queued: "\u001b[\n",
33 pending: "\u001b[",
34 afterClear: "mready\r\n",
35 },
36 {
37 part: "terminator",
38 publishing: "old output\r\n\u001b]0;Build ",
39 queued: "progress\u0007",
40 afterClear: "ready\r\n",
41 },
42 {
43 part: "exit",
44 publishing: "old output\r\n\u001b]0;Build ",
45 queued: "progress\u0007",
46 afterClear: "ready\r\n",
47 exit: { exitCode: 42, signal: null },
48 },
49 ])("keeps visible history correct when the title $part is queued", (row) =>
50 Effect.gen(function* () {
51 const fs = yield* FileSystem.FileSystem;
52 const logsDir = yield* fs.makeTempDirectoryScoped();
53 const onData = vi.fn<(callback: (data: string) => void) => () => void>(() => () => {});
54 const onExit = vi.fn<
55 (callback: (event: { exitCode: number; signal: number | null }) => void) => () => void
56 >(() => () => {});
57 const manager = yield* makeWithOptions({
58 logsDir,
59 ptyAdapter: {
60 spawn: () =>
61 Effect.succeed({
62 pid: 9000,
63 write: () => {},
64 resize: () => {},
65 kill: () => {},
66 onData,
67 onExit,
68 }),
69 },
70 foregroundProcessGroup: (pid) => Effect.succeed(pid),
71 processKillGraceMs: 0,
72 }).pipe(Effect.provideService(SpawnExecutableResolution, (command) => command));
73 const input = { threadId: ThreadId.make("thread-1"), terminalId: DEFAULT_TERMINAL_ID };
74 yield* manager.open({ ...input, cwd: logsDir });
75 const live = yield* Stream.toPull(manager.attachOutputStream(input, "live"));
76 yield* live;
77
78 const publishing = yield* Deferred.make<void>();
79 const resume = yield* Deferred.make<void>();
80 const printed = yield* Deferred.make<void>();
81 yield* Effect.acquireRelease(
82 manager.subscribe((event) => {
83 if (event.type !== "output") return Effect.void;
84 if (event.data === row.publishing) {
85 return Deferred.succeed(publishing, undefined).pipe(
86 Effect.andThen(Deferred.await(resume)),
87 );
88 }
89 if (event.data === row.afterClear) return Deferred.succeed(printed, undefined);
90 return Effect.void;
91 }),
92 (unsubscribe) =>
93 Deferred.succeed(resume, undefined).pipe(Effect.andThen(Effect.sync(unsubscribe))),
94 );
95 const emitData = onData.mock.calls[0]![0];
96 emitData(row.publishing);
97 yield* Deferred.await(publishing);
98 yield* live;
99 emitData(row.queued);
100 if (row.exit) onExit.mock.calls[0];
101 yield* manager.clear(input);
102 const [cleared] = yield* live;
103 expect(cleared?.type).toBe("cleared");
104 yield* Deferred.succeed(resume, undefined);
105
106 if (row.exit) {
107 emitData(row.afterClear);
108 const [exited] = yield* live;
109 expect(exited).toMatchObject({ type: "exited", exitCode: row.exit.exitCode });
110 return;
111 }
112 if (row.pending) expect(cleared).toMatchObject({ pendingControlSequence: row.pending });
113 emitData(row.afterClear);
114 yield* Deferred.await(printed);
115
116 const reopened = yield* Stream.toPull(manager.attachOutputStream(input, "reopened"));
117 const [event] = yield* reopened;
118 if (event?.type !== "snapshot") throw new Error("Expected a terminal snapshot");
119 const visible = event.snapshot.history.replace(/\u001b\]0;[^\u0007]*\u0007|\u001b\[m/g, "");
120 expect(visible).toBe("ready\r\n");
121 }),
122 );
123});