Open History to see the last commit
1import { EnvironmentId, ThreadId, type ThreadPullRequestLink } from "@t3tools/contracts";
2import { act, type ComponentProps } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6import "~/index.css";
7
8const watchCommand = vi.hoisted(() => vi.fn());
9const fixture = vi.hoisted(() => ({ realRows: false }));
10
11// List-only cases isolate query-backed rows; the density case uses their real layout.
12vi.mock(import("./ThreadDetailsPrRow"), async (importOriginal) => {
13 const actual = await importOriginal();
14 return {
15 ...actual,
16 ThreadDetailsPrRow: (props: ComponentProps<typeof actual.ThreadDetailsPrRow>) =>
17 fixture.realRows ? (
18 <actual.ThreadDetailsPrRow {...props} />
19 ) : (
20 <div data-row={String(props.number)}>
21 {props.onStopWatching ? (
22 <button onClick={props.onStopWatching}>Stop watching #{props.number}</button>
23 ) : null}
24 </div>
25 ),
26 };
27});
28vi.mock(import("~/state/entities"), async (importOriginal) => ({
29 ...(await importOriginal()),
30 useProjects: () => [],
31 useQuietThreadShell: (() => ({
32 environmentId: "environment",
33 projectId: "project",
34 worktreePath: null,
35 })) as never,
36 useProject: (() => ({ workspaceRoot: "/tmp/project" })) as never,
37 useThreadShellsForProjectRefs: () => [],
38}));
39vi.mock(import("~/state/use-atom-command"), async (importOriginal) => ({
40 ...(await importOriginal()),
41 useAtomCommand: (() => watchCommand) as never,
42}));
43
44vi.mock(import("./ChatCanvasContext"), async (original) => ({
45 ...(await original()),
46 useChatCanvas: (() => ({
47 container: { width: 1800, height: 500 },
48 lane: { padding: 48, minChatWidth: 640 },
49 layout: { frame: null },
50 })) as never,
51}));
52vi.mock(import("../../hooks/useT3ProjectFileScripts"), async (original) => ({
53 ...(await original()),
54 useT3ProjectFileScripts: () => [],
55}));
56vi.mock(import("./ThreadAutomationsPanel"), () => ({ ThreadAutomationsPanel: () => null }));
57vi.mock(import("./ThreadRelationshipsControl"), () => ({ ThreadRelationshipsPanel: () => null }));
58vi.mock(import("../pullRequest/usePullRequestActions"), async (original) => ({
59 ...(await original()),
60 usePullRequestHandoffs: () => ({ handoff: null, startAsk: vi.fn(), startHandoff: vi.fn() }),
61}));
62vi.mock(import("../BranchToolbarBranchSelector"), () => ({
63 BranchToolbarBranchSelector: () => (
64 <ThreadDetailsPrRows
65 threadRef={threadRef}
66 links={manyLinks}
67 currentLink={manyLinks[0]!}
68 onOpenLink={vi.fn()}
69 environmentId={threadRef.environmentId}
70 pr={null}
71 number={1}
72 reference={manyLinks[0]!}
73 status={null}
74 project={null}
75 label="#1"
76 openAriaLabel="Open pull request #1"
77 onOpen={vi.fn()}
78 />
79 ),
80}));
81
82import { PopoverCreateHandle } from "../ui/popover";
83import { ThreadDetailsPanel } from "./ThreadDetailsPanel";
84import { ThreadDetailsPrRows } from "./ThreadDetailsPrRows";
85
86const threadRef = {
87 environmentId: EnvironmentId.make("environment"),
88 threadId: ThreadId.make("thread"),
89};
90const manyLinks = Array.from({ length: 70 }, (_, i) =>
91 link(i + 1, `branch-${i + 1}`, "main", "2026-01-01T00:00:00.000Z"),
92);
93
94function link(
95 number: number,
96 headBranch: string,
97 baseBranch: string,
98 updatedAt: string,
99): ThreadPullRequestLink {
100 return {
101 host: "github.com",
102 repository: "pingdotgg/t3code",
103 number,
104 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
105 source: "manual",
106 linkedAt: updatedAt,
107 snapshot: {
108 state: "open",
109 title: `Change ${number}`,
110 headBranch,
111 baseBranch,
112 isDraft: false,
113 updatedAt,
114 syncedAt: updatedAt,
115 },
116 stack: null,
117 };
118}
119
120const bottom = link(1, "layer-one", "main", "2026-01-01T00:00:10.000Z");
121const top = link(2, "layer-two", "layer-one", "2026-01-01T00:00:20.000Z");
122const other = link(3, "unrelated", "main", "2026-01-01T00:00:05.000Z");
123
124let root: Root;
125let container: HTMLDivElement;
126beforeEach(() => {
127 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
128 container = document.createElement("div");
129 document.body.append(container);
130 root = createRoot(container);
131});
132afterEach(() => {
133 act(() => root.unmount());
134 container.remove();
135 vi.unstubAllGlobals();
136 fixture.realRows = false;
137});
138
139function render(links: ReadonlyArray<ThreadPullRequestLink>, current: ThreadPullRequestLink) {
140 act(() => {
141 root.render(
142 <ThreadDetailsPrRows
143 threadRef={{
144 environmentId: EnvironmentId.make("environment"),
145 threadId: ThreadId.make("thread"),
146 }}
147 links={links}
148 currentLink={current}
149 onOpenLink={vi.fn()}
150 environmentId={EnvironmentId.make("environment")}
151 pr={null}
152 number={current.number}
153 reference={current}
154 status={null}
155 project={null}
156 label={`#${current.number}`}
157 openAriaLabel="Open pull request"
158 onOpen={vi.fn()}
159 />,
160 );
161 });
162}
163
164const rows = () =>
165 [...container.querySelectorAll("[data-row]")].map((node) => node.getAttribute("data-row"));
166const toggleButton = () =>
167 [...container.querySelectorAll("button")].find((button) =>
168 /^(Show \d+ more|Show less)$/.test(button.textContent?.trim() ?? ""),
169 );
170const toggleLabel = () => toggleButton()?.textContent?.trim();
171
172function toggle() {
173 act(() => toggleButton()!.click());
174}
175
176it("shows only the current pull request until the rest are asked for", () => {
177 render([other, bottom, top], top);
178 expect(rows()).toEqual(["2"]);
179 expect(toggleLabel()).toBe("Show 2 more");
180
181 toggle();
182 expect(rows()).toEqual(["2", "1", "3"]);
183 expect(toggleLabel()).toBe("Show less");
184
185 toggle();
186 expect(rows()).toEqual(["2"]);
187 expect(toggleLabel()).toBe("Show 2 more");
188});
189
190it("keeps the single row untouched when the thread links one pull request", () => {
191 render([bottom], bottom);
192 expect(rows()).toEqual(["1"]);
193 expect(toggleLabel()).toBeUndefined();
194});
195
196it("lets only watched pull requests stop their watch", () => {
197 const watched: ThreadPullRequestLink = {
198 ...bottom,
199 watch: {
200 startedAt: "2026-01-01T00:00:30.000Z",
201 headSha: null,
202 failedChecks: [],
203 passed: false,
204 passedChecks: [],
205 remarksThrough: "2026-01-01T00:00:30.000Z",
206 remarkIds: [],
207 conflicting: false,
208 wakes: 0,
209 },
210 };
211 render([other, watched, top], top);
212 toggle();
213 const stopButtons = [...container.querySelectorAll("button")].filter((button) =>
214 button.textContent?.startsWith("Stop watching"),
215 );
216 expect(stopButtons.map((button) => button.textContent)).toEqual(["Stop watching #1"]);
217
218 act(() => stopButtons[0]!.click());
219 expect(watchCommand).toHaveBeenCalledWith({
220 environmentId: EnvironmentId.make("environment"),
221 input: {
222 threadId: ThreadId.make("thread"),
223 host: "github.com",
224 repository: "pingdotgg/t3code",
225 number: 1,
226 watching: false,
227 },
228 });
229});
230
231it("keeps workspace controls after expanding and collapsing 70 linked pull requests", async () => {
232 fixture.realRows = true;
233 container.style.cssText = "position:relative;width:1800px;height:500px";
234 await act(async () => {
235 root.render(
236 <ThreadDetailsPanel
237 {...threadRef}
238 anchor={{ current: null }}
239 handle={PopoverCreateHandle()}
240 onPresentationChange={vi.fn()}
241 activeProjectName={undefined}
242 activeProjectScripts={undefined}
243 preferredScriptId={null}
244 keybindings={[]}
245 availableEditors={[]}
246 showOpenInPicker
247 openInShortcutEnabled={false}
248 gitCwd="/tmp/project"
249 isGitRepo
250 envLocked={false}
251 availableEnvironments={[]}
252 onEnvironmentChange={vi.fn()}
253 onEnvModeChange={vi.fn()}
254 envMode="local"
255 startFromOrigin={false}
256 onStartFromOriginChange={vi.fn()}
257 onComposerFocusRequest={vi.fn()}
258 onRunProjectScript={vi.fn()}
259 onAddProjectScript={vi.fn()}
260 onUpdateProjectScript={vi.fn()}
261 onDeleteProjectScript={vi.fn()}
262 />,
263 );
264 });
265 const runContext = page.getByRole("button", { name: "Run context" });
266 const editor = page.getByRole("group", { name: "Open in editor" });
267 await expect.element(runContext).toBeVisible();
268 await expect.element(editor).toBeVisible();
269
270 await userEvent.click(page.getByRole("button", { name: "Show 69 more" }));
271 await expect.element(page.getByRole("button", { name: "Show less" })).toBeInTheDocument();
272 const extraRows = () =>
273 container.querySelectorAll('[aria-label^="https://github.com/pingdotgg/t3code/pull/"]');
274 expect(extraRows()).toHaveLength(69);
275 // Let native ResizeObserver deliver the expanded content size before collapsing it.
276 await act(async () => {
277 await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
278 });
279 await userEvent.click(page.getByRole("button", { name: "Show less" }));
280 await expect.element(page.getByRole("button", { name: "Show 69 more" })).toBeVisible();
281 expect(extraRows()).toHaveLength(0);
282 await expect.element(page.getByRole("button", { name: "Open pull request #1" })).toBeVisible();
283 await expect.element(runContext).toBeVisible();
284 await expect.element(editor).toBeVisible();
285});