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

ThreadDetailsPanel.browser.test.tsx

253 lines8.5 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 ThreadId,
5 type ScopedThreadRef,
6 type ThreadPullRequestLink,
7} from "@t3tools/contracts";
8import { AsyncResult, Atom } from "effect/reactivity";
9import { expect, it, vi } from "vite-plus/test";
10import { page } from "vite-plus/test/browser";
11​
12const SCRATCH_ROOT = "/home/user/.t3/scratch";
13​
14const state = vi.hoisted(() => ({
15 workspaceRoot: "",
16 folder: "",
17 pullRequests: [] as unknown[],
18 configLoaded: true,
19}));
20​
21vi.mock(import("~/state/entities"), async (importOriginal) => ({
22 ...(await importOriginal()),
23 useProject: ((ref: unknown) =>
24 ref === null
25 ? null
26 : {
27 id: "project",
28 environmentId: "environment",
29 title: state.workspaceRoot === SCRATCH_ROOT ? "No project" : "plain",
30 workspaceRoot: state.workspaceRoot,
31 scripts: [],
32 }) as never,
33 useProjects: (() => []) as never,
34 useQuietThreadShell: ((ref: unknown) =>
35 ref === null
36 ? null
37 : {
38 id: "thread",
39 environmentId: "environment",
40 projectId: "project",
41 worktreePath: state.folder,
42 branch: null,
43 pullRequests: state.pullRequests,
44 }) as never,
45 useServerConfigs: (() =>
46 new Map(
47 state.configLoaded
48 ? [
49 [
50 EnvironmentId.make("environment"),
51 {
52 scratchWorkspaceRoot: SCRATCH_ROOT,
53 environment: { capabilities: { pullRequests: true } },
54 },
55 ],
56 ]
57 : [],
58 )) as never,
59}));
60// Neither folder is a repository, which is what the server reports for both.
61vi.mock(import("~/state/vcs"), async (importOriginal) => {
62 const actual = await importOriginal();
63 const notARepo = Atom.make(
64 AsyncResult.success({
65 isRepo: false,
66 hasPrimaryRemote: false,
67 isDefaultRef: false,
68 refName: null,
69 hasWorkingTreeChanges: false,
70 workingTree: { files: [], insertions: 0, deletions: 0 },
71 hasUpstream: false,
72 aheadCount: 0,
73 behindCount: 0,
74 aheadOfDefaultCount: 0,
75 pr: null,
76 }),
77 );
78 return {
79 ...actual,
80 vcsEnvironment: { ...actual.vcsEnvironment, status: (() => notARepo) as never },
81 };
82});
83​
84import { render } from "../../../test/render";
85import { useRightPanelStore } from "../../rightPanelStore";
86import { PopoverCreateHandle } from "../ui/popover";
87import { ChatCanvasContext } from "./ChatCanvasContext";
88import { resolveChatCanvasLayout } from "./chatCanvasLayout";
89import { ThreadDetailsPanel } from "./ThreadDetailsPanel";
90​
91const threadRef: ScopedThreadRef = {
92 environmentId: EnvironmentId.make("environment"),
93 threadId: ThreadId.make("thread"),
94};
95const container = { width: 1200, height: 800 };
96const placement = { x: 908, y: 0, right: 12, width: 280, height: 400 } as const;
97const linkedPullRequest: ThreadPullRequestLink = {
98 host: "github.com",
99 repository: "pingdotgg/t3code",
100 number: 27513,
101 url: "https://github.com/pingdotgg/t3code/pull/27513",
102 source: "manual",
103 linkedAt: "2026-10-30T00:00:00.000Z",
104 snapshot: null,
105 stack: null,
106};
107​
108function setup(input: {
109 workspaceRoot: string;
110 folder: string;
111 pullRequests?: ThreadPullRequestLink[];
112 configLoaded?: boolean;
113}) {
114 state.configLoaded = input.configLoaded ?? true;
115 state.workspaceRoot = input.workspaceRoot;
116 state.folder = input.folder;
117 state.pullRequests = input.pullRequests ?? [];
118 useRightPanelStore.setState({ threadPanelVisibilityByThreadKey: {} });
119 const anchor = document.createElement("div");
120 document.body.append(anchor);
121 return {
122 anchor: { current: anchor },
123 handle: PopoverCreateHandle(),
124 [Symbol.dispose]: () => anchor.remove(),
125 };
127​
128const machines = (["This Mac", "Server"] as const).map((label, index) => ({
129 environmentId: EnvironmentId.make(index === 0 ? "environment" : "other"),
130 projectId: ProjectId.make("project"),
131 label,
132 isPrimary: index === 0,
133 machine: "server" as const,
134}));
135​
136function Panel({
137 fixture,
138 draftEnvMode,
139 onMachines = false,
140}: {
141 fixture: ReturnType<typeof setup>;
142 /** Renders an unsent draft in this mode instead of a started thread. */
143 draftEnvMode?: "local" | "worktree";
144 onMachines?: boolean;
145}) {
146 const noop = () => {};
147 const scriptResult = async () => ({ ok: true }) as never;
148 return (
149 <ChatCanvasContext
150 value={{
151 container,
152 lane: { padding: 48, minChatWidth: 640 },
153 layout: resolveChatCanvasLayout({ container, preview: null }),
154 previewKey: null,
155 registerTimeline: noop,
156 measureDetailsCard: noop,
157 detailsCardTopInset: 0,
158 preferredDetailsCard: placement,
159 detailsCardPlacement: placement,
160 }}
161 >
162 <ThreadDetailsPanel
163 anchor={fixture.anchor}
164 handle={fixture.handle}
165 environmentId={threadRef.environmentId}
166 threadId={threadRef.threadId}
167 projectId={ProjectId.make("project")}
168 composerDraftTarget={threadRef}
169 worktreePath={draftEnvMode === undefined ? state.folder : null}
170 isServerThread={draftEnvMode === undefined}
171 activeProjectName={state.workspaceRoot === SCRATCH_ROOT ? "No project" : "plain"}
172 activeProjectScripts={undefined}
173 preferredScriptId={null}
174 keybindings={[]}
175 availableEditors={[]}
176 showOpenInPicker={false}
177 openInShortcutEnabled={false}
178 gitCwd={state.folder}
179 workspaceMutationId={null}
180 isGitRepo={false}
181 offersWorkspaceModes={false}
182 envLocked={draftEnvMode === undefined}
183 availableEnvironments={onMachines ? machines : []}
184 {...(onMachines ? { onEnvironmentChange: noop } : {})}
185 onEnvModeChange={noop}
186 envMode={draftEnvMode ?? "local"}
187 startFromOrigin={false}
188 onStartFromOriginChange={noop}
189 onComposerFocusRequest={noop}
190 onRunProjectScript={noop}
191 onAddProjectScript={scriptResult}
192 onUpdateProjectScript={scriptResult}
193 onDeleteProjectScript={scriptResult}
194 usage={{} as never}
195 />
196 </ChatCanvasContext>
197 );
199​
200it("offers no git controls in a No project thread, but shows its linked pull request", async () => {
201 using fixture = setup({
202 workspaceRoot: SCRATCH_ROOT,
203 folder: `${SCRATCH_ROOT}/thread`,
204 pullRequests: [linkedPullRequest],
205 });
206 await using _screen = await render(<Panel fixture={fixture} />);
207 await expect.element(page.getByText("#27513")).toBeVisible();
208 await expect
209 .element(page.getByRole("button", { name: "Initialize Git" }))
210 .not.toBeInTheDocument();
211});
212​
213it("shows Current checkout where no worktree can come from, even when New worktree is the default", async () => {
214 using fixture = setup({ workspaceRoot: SCRATCH_ROOT, folder: SCRATCH_ROOT });
215 await using _screen = await render(<Panel fixture={fixture} draftEnvMode="worktree" />);
216 await expect.element(page.getByLabelText("Run context: Current checkout")).toBeVisible();
217 await expect.element(page.getByRole("button", { name: /Run context/ })).not.toBeInTheDocument();
218});
219​
220it("offers only machines where no worktree can come from and several machines exist", async () => {
221 using fixture = setup({ workspaceRoot: SCRATCH_ROOT, folder: SCRATCH_ROOT });
222 await using _screen = await render(
223 <Panel fixture={fixture} draftEnvMode="worktree" onMachines />,
224 );
225 await page.getByRole("button", { name: "Run context: This Mac, Current checkout" }).click();
226 await expect.element(page.getByText("Run on")).toBeVisible();
227 await expect.element(page.getByText("New worktree")).not.toBeInTheDocument();
228});
229​
230it("shows a linked pull request in a folder that is not a git repository", async () => {
231 using fixture = setup({
232 workspaceRoot: "/work/plain",
233 folder: "/work/plain",
234 pullRequests: [linkedPullRequest],
235 });
236 await using _screen = await render(<Panel fixture={fixture} />);
237 await expect.element(page.getByRole("region", { name: "Workspace" })).toBeVisible();
238 await expect.element(page.getByText("#27513")).toBeVisible();
239 await expect.element(page.getByRole("button", { name: "Initialize Git" })).toBeVisible();
240});
241​
242it("offers no Initialize Git before the server config says where No project lives", async () => {
243 using fixture = setup({
244 workspaceRoot: SCRATCH_ROOT,
245 folder: `${SCRATCH_ROOT}/thread`,
246 configLoaded: false,
247 });
248 await using _screen = await render(<Panel fixture={fixture} />);
249 await expect.element(page.getByRole("region", { name: "Workspace" })).toBeVisible();
250 await expect
251 .element(page.getByRole("button", { name: "Initialize Git" }))
252 .not.toBeInTheDocument();
253});