Open History to see the last commit
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";
4import { AtomRegistry } from "effect/reactivity";
5
6import { attachedTerminalSessionAtom, selectKnownTerminalSessions } from "./terminalSessions";
7
8const attachCalls = vi.hoisted(() => [] as unknown[]);
9vi.mock("./terminal", async () => {
10 const { AsyncResult, Atom } = await import("effect/reactivity");
11 return {
12 terminalEnvironment: {
13 attach: (request: unknown) => {
14 attachCalls.push(request);
15 return Atom.make(AsyncResult.initial(true));
16 },
17 metadata: () => Atom.make(AsyncResult.initial(true)),
18 },
19 };
20});
21vi.mock("./session", async () => {
22 const { AsyncResult, Atom } = await import("effect/reactivity");
23 const granted = Atom.make(AsyncResult.success({}));
24 return {
25 environmentSession: { sessionStateAtom: () => granted },
26 sessionHasScope: () => true,
27 useEnvironmentScope: () => true,
28 };
29});
30
31const environmentA = EnvironmentId.make("environment-a");
32const threadA = ThreadId.make("thread-a");
33const threadB = ThreadId.make("thread-b");
34
35function summary(
36 threadId: string,
37 terminalId: string,
38 changes: Partial<TerminalSummary> = {},
39): TerminalSummary {
40 return {
41 threadId,
42 terminalId,
43 cwd: "/repo",
44 worktreePath: null,
45 status: "running",
46 pid: 123,
47 exitCode: null,
48 exitSignal: null,
49 hasRunningSubprocess: true,
50 label: "Terminal",
51 updatedAt: "2026-09-04T00:00:00.000Z",
52 ...changes,
53 };
54}
55
56describe("selectKnownTerminalSessions", () => {
57 it("changes only the affected group and preserves earlier snapshots", () => {
58 const first = summary(threadA, "terminal-1");
59 const second = summary(threadA, "terminal-2");
60 const other = summary(threadB, "terminal-1");
61 const metadata = [first, second, other];
62 const beforeA = selectKnownTerminalSessions(metadata, environmentA, threadA);
63 const beforeB = selectKnownTerminalSessions(metadata, environmentA, threadB);
64 Object.freeze(beforeA);
65 Object.freeze(beforeB);
66 const exited = {
67 ...second,
68 status: "exited" as const,
69 pid: null,
70 exitCode: 0,
71 hasRunningSubprocess: false,
72 label: "Finished",
73 updatedAt: "2026-09-04T00:00:01.000Z",
74 };
75 const next = [first, other, exited];
76 const afterA = selectKnownTerminalSessions(next, environmentA, threadA);
77 expect(selectKnownTerminalSessions(next, environmentA, threadB)).toBe(beforeB);
78 expect(afterA).not.toBe(beforeA);
79 expect(afterA[0]).toBe(beforeA[0]);
80 expect(afterA[1]?.state).toMatchObject({
81 status: "exited",
82 hasRunningSubprocess: false,
83 summary: { label: "Finished", pid: null, exitCode: 0 },
84 });
85 expect(selectRunningSubprocessTerminalIds(afterA)).toEqual(["terminal-1"]);
86 expect(beforeA[1]?.state).toMatchObject({
87 status: "running",
88 hasRunningSubprocess: true,
89 summary: { label: "Terminal", pid: 123 },
90 });
91 expect(selectKnownTerminalSessions(metadata, environmentA, threadA)).toBe(beforeA);
92
93 const renamed = [first, exited, { ...other, label: "Renamed" }];
94 expect(selectKnownTerminalSessions(renamed, environmentA, threadA)).toBe(afterA);
95 expect(
96 selectKnownTerminalSessions(renamed, environmentA, threadB)[0]?.state.summary?.label,
97 ).toBe("Renamed");
98 expect(selectKnownTerminalSessions([other], environmentA, threadA)).toEqual([]);
99 expect(selectKnownTerminalSessions([other], environmentA, threadB)).toBe(beforeB);
100 });
101});
102
103describe("attachedTerminalSessionAtom", () => {
104 it("attaches with the launch input it was keyed by", () => {
105 attachCalls.length = 0;
106 const input = {
107 threadId: threadA,
108 terminalId: "terminal-1",
109 cwd: "/repo",
110 worktreePath: null,
111 env: { T3CODE_PROJECT_ROOT: "/repo" },
112 };
113 const atom = attachedTerminalSessionAtom(environmentA, input);
114 expect(attachedTerminalSessionAtom(environmentA, { ...input })).toBe(atom);
115
116 AtomRegistry.make().get(atom);
117 expect(attachCalls).toEqual([{ environmentId: environmentA, input }]);
118 });
119});