Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import type { TerminalEvent, TerminalSessionSnapshot } from "@t3tools/contracts";
4import { terminalRunningSubprocessFromEvent } from "./terminalActivity";
5
6const snapshot: TerminalSessionSnapshot = {
7 threadId: "thread-1",
8 terminalId: "default",
9 cwd: "/tmp",
10 worktreePath: null,
11 status: "running",
12 pid: 1234,
13 history: "",
14 exitCode: null,
15 exitSignal: null,
16 updatedAt: "2026-01-01T00:00:00.000Z",
17};
18
19function eventBase() {
20 return {
21 threadId: "thread-1",
22 terminalId: "default",
23 createdAt: "2026-01-01T00:00:00.000Z",
24 };
25}
26
27describe("terminalRunningSubprocessFromEvent", () => {
28 it("returns the subprocess flag for terminal activity events", () => {
29 const active = terminalRunningSubprocessFromEvent({
30 ...eventBase(),
31 type: "activity",
32 hasRunningSubprocess: true,
33 });
34 const idle = terminalRunningSubprocessFromEvent({
35 ...eventBase(),
36 type: "activity",
37 hasRunningSubprocess: false,
38 });
39
40 expect(active).toBe(true);
41 expect(idle).toBe(false);
42 });
43
44 it("clears running state when a terminal session starts/restarts/exits", () => {
45 const events: TerminalEvent[] = [
46 { ...eventBase(), type: "started", snapshot },
47 { ...eventBase(), type: "restarted", snapshot },
48 { ...eventBase(), type: "exited", exitCode: 0, exitSignal: null },
49 ];
50
51 for (const event of events) {
52 expect(terminalRunningSubprocessFromEvent(event)).toBe(false);
53 }
54 });
55
56 it("ignores non-activity terminal events", () => {
57 expect(
58 terminalRunningSubprocessFromEvent({
59 ...eventBase(),
60 type: "output",
61 data: "hello",
62 }),
63 ).toBeNull();
64 expect(
65 terminalRunningSubprocessFromEvent({
66 ...eventBase(),
67 type: "error",
68 message: "oops",
69 }),
70 ).toBeNull();
71 });
72});