Open History to see the last commit
1import { EnvironmentId, ThreadId, type TerminalSummary } from "@t3tools/contracts";
2import { act, useEffect } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5
6import { useKnownTerminalSessions } from "./terminalSessions";
7
8const state = vi.hoisted(() => ({
9 canRead: true,
10 data: null as ReadonlyArray<TerminalSummary> | null,
11 error: null as string | null,
12}));
13
14vi.mock(import("./session"), async (original) => ({
15 ...(await original()),
16 useEnvironmentScope: () => state.canRead,
17}));
18vi.mock(import("./query"), async (original) => ({
19 ...(await original()),
20 useEnvironmentQuery: ((atom: unknown) =>
21 atom === null
22 ? { data: null, error: null }
23 : { data: state.data, error: state.error }) as never,
24}));
25
26let root: Root;
27let current: ReturnType<typeof useKnownTerminalSessions>;
28const target = {
29 environmentId: EnvironmentId.make("secondary"),
30 threadId: ThreadId.make("thread"),
31};
32const terminal: TerminalSummary = {
33 threadId: "thread",
34 terminalId: "term-1",
35 cwd: "/repo",
36 worktreePath: null,
37 status: "running",
38 pid: 123,
39 exitCode: null,
40 exitSignal: null,
41 hasRunningSubprocess: true,
42 label: "Busy process",
43 updatedAt: "2026-09-05T00:00:00.000Z",
44};
45
46function Probe() {
47 const sessions = useKnownTerminalSessions(target);
48 useEffect(() => {
49 current = sessions;
50 }, [sessions]);
51 return null;
52}
53
54function render() {
55 return act(() => root.render(<Probe />));
56}
57
58beforeEach(() => {
59 state.canRead = true;
60 state.data = null;
61 state.error = null;
62 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
63 root = createRoot(document.createElement("div"));
64});
65
66afterEach(async () => {
67 await act(() => root.unmount());
68 vi.unstubAllGlobals();
69});
70
71it("distinguishes missing metadata from an authoritative empty list across scope changes", async () => {
72 await render();
73 expect(current).toBeNull();
74
75 state.data = [terminal];
76 await render();
77 expect(current?.[0]).toMatchObject({
78 target: { environmentId: "secondary", terminalId: "term-1" },
79 state: { hasRunningSubprocess: true },
80 });
81
82 state.canRead = false;
83 await render();
84 expect(current).toBeNull();
85
86 state.canRead = true;
87 state.data = [];
88 await render();
89 expect(current).toEqual([]);
90});
91
92it("does not reuse cached terminal metadata after a subscription failure", async () => {
93 state.data = [terminal];
94 await render();
95 state.error = "Access denied";
96 await render();
97 expect(current).toBeNull();
98});