Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalActivity.test.ts

71 lines1.8 KB

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