@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 158 lines 6.37 KB
Raw
1 import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
2 import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3 import { expect, it, vi } from "vite-plus/test";
4 import { act, render } from "../../test/render";
5 import { selectThreadRightPanelState, useRightPanelStore } from "../rightPanelStore";
6 import { useTerminalUiStateStore } from "../terminalUiStateStore";
7 import { useRightPanelSurfaceClose } from "./useRightPanelSurfaceClose";
8
9 const fixture = vi.hoisted(() => ({
10 confirm: (_labels: readonly string[]): Promise<boolean> => Promise.resolve(false),
11 allowed: new Set<string>(),
12 sessions: new Map<string, string>(),
13 failClose: false,
14 }));
15 vi.mock("../lib/terminalCloseConfirm", () => ({
16 confirmTerminalClose: (labels: readonly string[]) => fixture.confirm(labels),
17 }));
18 vi.mock("../components/ChatView.logic", () => ({
19 agentControlledBrowserCloseConfirmation: () => null,
20 }));
21 vi.mock("../previewStateStore", () => ({
22 useThreadPreviewState: () => ({ sessions: {}, desktopByTabId: {} }),
23 setActivePreviewTab: () => {},
24 beginPreviewSessionClose: () => {},
25 cancelPreviewSessionClose: () => {},
26 }));
27 vi.mock("../browser/previewRuntime", () => ({ rendersServerTabNatively: () => false }));
28 vi.mock("../state/primaryEnvironment", async () => {
29 const { Atom } = await import("effect/reactivity");
30 return { primaryEnvironmentIdAtom: Atom.make(null) };
31 });
32 vi.mock("../state/session", () => ({
33 useEnvironmentScope: (id: string) => fixture.allowed.has(id),
34 readEnvironmentScope: (id: string) => fixture.allowed.has(id),
35 }));
36 vi.mock("../state/entities", () => ({
37 useQuietThreadShell: () => null,
38 useProject: () => null,
39 readThreadShell: () => null,
40 }));
41 vi.mock("../state/terminalSessions", () => ({ useKnownTerminalSessions: () => null }));
42 vi.mock("../composerDraftStore", async () => {
43 const Schema = await import("effect/Schema");
44 return {
45 useComposerDraftStore: () => null,
46 DraftId: Schema.String.pipe(Schema.brand("DraftId")),
47 };
48 });
49 vi.mock("../state/use-atom-command", () => ({
50 useAtomCommand:
51 (_command: unknown, label: string) =>
52 async (request: {
53 environmentId: string;
54 input: { threadId: string; terminalId: string; data?: string };
55 }) => {
56 const key = `${request.environmentId}/${request.input.threadId}/${request.input.terminalId}`;
57 if (label === "terminal close" && fixture.failClose) return { _tag: "Failure" };
58 if (label === "terminal close" || label === "terminal write") fixture.sessions.delete(key);
59 return { _tag: "Success" };
60 },
61 }));
62 // The commands are replaced above; importing their runtime would connect a client.
63 vi.mock("../state/terminal", () => ({ terminalEnvironment: { open: {}, close: {}, write: {} } }));
64 vi.mock("../state/preview", () => ({ previewEnvironment: { close: {} } }));
65 vi.mock("../state/threads", () => ({ threadEnvironment: { delete: {} } }));
66 vi.mock("../state/shell", () => ({ environmentShell: { stateValueAtom: () => null } }));
67 vi.mock("@t3tools/client-runtime/state/runtime", () => ({
68 isAtomCommandInterrupted: () => false,
69 }));
70
71 function setup(mode: "single" | "all") {
72 fixture.allowed = new Set(["environment-a", "environment-b"]);
73 fixture.sessions = new Map();
74 fixture.failClose = false;
75 useRightPanelStore.setState({ byThreadKey: {} });
76 useTerminalUiStateStore.setState({ terminalUiStateByThreadKey: {} });
77 const original: ScopedThreadRef = {
78 environmentId: EnvironmentId.make("environment-a"),
79 threadId: ThreadId.make("thread-a"),
80 };
81 const other: ScopedThreadRef = {
82 environmentId: EnvironmentId.make("environment-b"),
83 threadId: ThreadId.make("thread-b"),
84 };
85 for (const ref of [original, other]) {
86 fixture.sessions.set(`${ref.environmentId}/${ref.threadId}/term-1`, "history to preserve");
87 useRightPanelStore.getState().openTerminal(ref, "term-1");
88 }
89 const surface = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, original)
90 .surfaces[0]!;
91 let resolve!: (confirmed: boolean) => void;
92 const confirmation = new Promise<boolean>((done) => {
93 resolve = done;
94 });
95 fixture.confirm = () => confirmation;
96 function Harness({ threadRef }: { threadRef: ScopedThreadRef }) {
97 const { closeSurface, closeAllSurfaces } = useRightPanelSurfaceClose({
98 threadRef,
99 terminalLabelsById: new Map(),
100 });
101 return (
102 <button onClick={() => (mode === "all" ? closeAllSurfaces() : closeSurface(surface))}>
103 Close original terminal
104 </button>
105 );
106 }
107 return { original, other, Harness, resolve };
108 }
109
110 it.each([
111 { mode: "single", failClose: false },
112 { mode: "single", failClose: true },
113 { mode: "all", failClose: false },
114 { mode: "all", failClose: true },
115 ] as const)(
116 "keeps a pending $mode close on its original environment and thread, fallback=$failClose",
117 async ({ mode, failClose }) => {
118 const { original, other, Harness, resolve } = setup(mode);
119 fixture.failClose = failClose;
120 await using screen = await render(<Harness threadRef={original} />);
121 await screen.getByRole("button").click();
122 expect(fixture.sessions.size).toBe(2);
123 await screen.rerender(<Harness threadRef={other} />);
124 await act(async () => {
125 resolve(true);
126 });
127 expect([...fixture.sessions]).toEqual([
128 ["environment-b/thread-b/term-1", "history to preserve"],
129 ]);
130 const panels = useRightPanelStore.getState().byThreadKey;
131 expect(selectThreadRightPanelState(panels, original).surfaces).toEqual([]);
132 expect(selectThreadRightPanelState(panels, other).surfaces).toHaveLength(1);
133 },
134 );
135
136 it.each([
137 { mode: "single", outcome: "cancel" },
138 { mode: "single", outcome: "revoked" },
139 { mode: "all", outcome: "cancel" },
140 { mode: "all", outcome: "revoked" },
141 ] as const)(
142 "leaves both sessions intact when $mode confirmation is $outcome",
143 async ({ mode, outcome }) => {
144 const { original, other, Harness, resolve } = setup(mode);
145 await using screen = await render(<Harness threadRef={original} />);
146 await screen.getByRole("button").click();
147 expect(fixture.sessions.size).toBe(2);
148 await screen.rerender(<Harness threadRef={other} />);
149 if (outcome === "revoked") fixture.allowed.delete(original.environmentId);
150 await act(async () => {
151 resolve(outcome !== "cancel");
152 });
153 expect(fixture.sessions.size).toBe(2);
154 expect(
155 useRightPanelStore.getState().byThreadKey[scopedThreadKey(other)]?.surfaces,
156 ).toHaveLength(1);
157 },
158 );