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

terminalSessions.test.ts

79 lines2.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId, ThreadId, type TerminalSummary } from "@t3tools/contracts";
3import { selectRunningSubprocessTerminalIds } from "@t3tools/client-runtime/state/terminal";
4​
5import { selectKnownTerminalSessions } from "./terminalSessions";
6​
7vi.mock("./terminal", () => ({ terminalEnvironment: {} }));
8​
9const environmentA = EnvironmentId.make("environment-a");
10const threadA = ThreadId.make("thread-a");
11const threadB = ThreadId.make("thread-b");
12​
13function summary(
14 threadId: string,
15 terminalId: string,
16 changes: Partial<TerminalSummary> = {},
17): TerminalSummary {
18 return {
19 threadId,
20 terminalId,
21 cwd: "/repo",
22 worktreePath: null,
23 status: "running",
24 pid: 123,
25 exitCode: null,
26 exitSignal: null,
27 hasRunningSubprocess: true,
28 label: "Terminal",
29 updatedAt: "2026-09-04T00:00:00.000Z",
30 ...changes,
31 };
33​
34describe("selectKnownTerminalSessions", () => {
35 it("changes only the affected group and preserves earlier snapshots", () => {
36 const first = summary(threadA, "terminal-1");
37 const second = summary(threadA, "terminal-2");
38 const other = summary(threadB, "terminal-1");
39 const metadata = [first, second, other];
40 const beforeA = selectKnownTerminalSessions(metadata, environmentA, threadA);
41 const beforeB = selectKnownTerminalSessions(metadata, environmentA, threadB);
42 Object.freeze(beforeA);
43 Object.freeze(beforeB);
44 const exited = {
45 ...second,
46 status: "exited" as const,
47 pid: null,
48 exitCode: 0,
49 hasRunningSubprocess: false,
50 label: "Finished",
51 updatedAt: "2026-09-04T00:00:01.000Z",
52 };
53 const next = [first, other, exited];
54 const afterA = selectKnownTerminalSessions(next, environmentA, threadA);
55 expect(selectKnownTerminalSessions(next, environmentA, threadB)).toBe(beforeB);
56 expect(afterA).not.toBe(beforeA);
57 expect(afterA[0]).toBe(beforeA[0]);
58 expect(afterA[1]?.state).toMatchObject({
59 status: "exited",
60 hasRunningSubprocess: false,
61 summary: { label: "Finished", pid: null, exitCode: 0 },
62 });
63 expect(selectRunningSubprocessTerminalIds(afterA)).toEqual(["terminal-1"]);
64 expect(beforeA[1]?.state).toMatchObject({
65 status: "running",
66 hasRunningSubprocess: true,
67 summary: { label: "Terminal", pid: 123 },
68 });
69 expect(selectKnownTerminalSessions(metadata, environmentA, threadA)).toBe(beforeA);
70​
71 const renamed = [first, exited, { ...other, label: "Renamed" }];
72 expect(selectKnownTerminalSessions(renamed, environmentA, threadA)).toBe(afterA);
73 expect(
74 selectKnownTerminalSessions(renamed, environmentA, threadB)[0]?.state.summary?.label,
75 ).toBe("Renamed");
76 expect(selectKnownTerminalSessions([other], environmentA, threadA)).toEqual([]);
77 expect(selectKnownTerminalSessions([other], environmentA, threadB)).toBe(beforeB);
78 });
79});