Open History to see the last commit
1import {
2 EnvironmentId,
3 ProjectId,
4 SourceControlProviderKind,
5 ThreadId,
6 type PullRequestDetailView,
7} from "@t3tools/contracts";
8import { expect, it, vi } from "vite-plus/test";
9import { userEvent } from "vite-plus/test/browser";
10
11const linkState = vi.hoisted(() => ({
12 openLink: vi.fn(async () => {}),
13 useOpenLink: vi.fn(),
14}));
15vi.mock(import("~/browser/useOpenLink"), () => ({
16 useOpenLink: (threadRef: unknown) => {
17 linkState.useOpenLink(threadRef);
18 return linkState.openLink;
19 },
20}));
21
22import { render } from "../../../test/render";
23import "../../index.css";
24import { PullRequestTimelineTab } from "./PullRequestTimelineTab";
25
26const COMMITS = 2000;
27
28const detail: PullRequestDetailView = {
29 provider: SourceControlProviderKind.make("github"),
30 projectId: ProjectId.make("project"),
31 projectTitle: "Project",
32 workspaceRoot: "/workspace",
33 repository: "owner/repo",
34 number: 1,
35 title: "Huge pull request",
36 body: "",
37 url: "https://github.com/owner/repo/pull/1",
38 author: { login: "author", name: null, avatarUrl: null },
39 viewer: "author",
40 state: "open",
41 isDraft: false,
42 mergeability: "mergeable",
43 additions: 1,
44 deletions: 0,
45 changedFiles: 1,
46 headBranch: "feature",
47 baseBranch: "main",
48 createdAt: "2026-09-01T00:00:00Z",
49 updatedAt: "2026-09-01T00:00:00Z",
50 mergedAt: null,
51 closedAt: null,
52 reviewers: [],
53 labels: [],
54 checks: [],
55 comments: [],
56 commentCount: 0,
57 commentsTruncated: false,
58 reviewThreads: [],
59 commits: Array.from({ length: COMMITS }, (_, index) => ({
60 oid: `${index.toString(16).padStart(7, "0")}${"0".repeat(33)}`,
61 messageHeadline: `Commit number ${index}`,
62 committedDate: new Date(Date.UTC(2026, 8, 1, 0, index)).toISOString(),
63 })),
64 mergeCapabilities: { merge: false, squash: false, rebase: false },
65 capabilities: {
66 diff: true,
67 comment: true,
68 search: true,
69 actions: [],
70 mergeMethods: [],
71 review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
72 reviewers: { request: false, listCandidates: false },
73 edit: { changeRequest: false, comment: false },
74 },
75 viewerPermissions: {
76 actions: [],
77 comment: false,
78 resolve: false,
79 verdicts: [],
80 requestReviewers: false,
81 },
82};
83
84it("mounts only the timeline rows near the viewport of a huge pull request", async () => {
85 await using screen = await render(
86 <div style={{ height: 600 }}>
87 <PullRequestTimelineTab
88 scrollerRef={() => {}}
89 detail={detail}
90 environmentId={EnvironmentId.make("environment")}
91 reference={detail}
92 order="oldest"
93 onOpenCommit={() => {}}
94 onRefresh={() => {}}
95 />
96 </div>,
97 );
98 await expect.element(screen.getByText("Commit number 0")).toBeVisible();
99 const mounted = screen.container.querySelectorAll("button").length;
100 expect(mounted).toBeGreaterThan(0);
101 expect(mounted).toBeLessThan(COMMITS / 10);
102});
103
104it.each(["review", "issue-comment", "resolved-reply"] as const)(
105 "reveals a linked %s below the virtual viewport",
106 async (kind) => {
107 const url = `${detail.url}#${kind === "review" ? "pullrequestreview-123" : kind === "issue-comment" ? "issuecomment-123" : "discussion_r123"}`;
108 const linkedDetail: PullRequestDetailView = {
109 ...detail,
110 comments:
111 kind === "resolved-reply"
112 ? []
113 : [
114 {
115 id: "linked",
116 kind,
117 author: detail.author,
118 body: "Linked reply",
119 createdAt: "2026-10-01T00:00:00Z",
120 url,
121 path: null,
122 reviewState: kind === "review" ? "CHANGES_REQUESTED" : null,
123 },
124 ],
125 reviewThreads:
126 kind !== "resolved-reply"
127 ? []
128 : [
129 {
130 id: "thread",
131 path: "src/test.ts",
132 line: 5,
133 side: "right",
134 isResolved: true,
135 isOutdated: false,
136 comments: [
137 {
138 id: "root",
139 author: detail.author,
140 body: "Root comment",
141 createdAt: "2026-10-01T00:00:00Z",
142 url: `${detail.url}#discussion_r122`,
143 },
144 {
145 id: "reply",
146 author: detail.author,
147 body: "Linked reply",
148 createdAt: "2026-10-01T00:01:00Z",
149 url,
150 },
151 ],
152 },
153 ],
154 };
155 const settled = vi.fn();
156 await using screen = await render(
157 <div style={{ height: 600 }}>
158 <PullRequestTimelineTab
159 scrollerRef={() => {}}
160 detail={linkedDetail}
161 environmentId={EnvironmentId.make("environment")}
162 reference={detail}
163 order="oldest"
164 onOpenCommit={() => {}}
165 onRefresh={() => {}}
166 revealUrl={url}
167 onRevealSettled={settled}
168 />
169 </div>,
170 );
171 await expect.element(screen.getByText("Linked reply", { exact: true })).toBeVisible();
172 await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url));
173 const target = screen.container.querySelector("[data-pr-link-target=true]");
174 expect(target?.textContent).toContain("Linked reply");
175 expect(document.activeElement).toBe(target);
176 await screen.rerender(
177 <div style={{ height: 600 }}>
178 <PullRequestTimelineTab
179 scrollerRef={() => {}}
180 detail={linkedDetail}
181 environmentId={EnvironmentId.make("environment")}
182 reference={detail}
183 order="oldest"
184 onOpenCommit={() => {}}
185 onRefresh={() => {}}
186 onRevealSettled={settled}
187 />
188 </div>,
189 );
190 await expect.element(screen.getByText("Linked reply", { exact: true })).toBeVisible();
191 },
192);
193
194it("gives each comment and review a heading for screen reader navigation", async () => {
195 const commented: PullRequestDetailView = {
196 ...detail,
197 commits: [],
198 comments: [
199 {
200 id: "review",
201 kind: "review",
202 author: { login: "bob", name: null, avatarUrl: null },
203 body: "Looks good",
204 createdAt: "2026-10-01T00:00:00Z",
205 url: `${detail.url}#pullrequestreview-1`,
206 path: null,
207 reviewState: "APPROVED",
208 },
209 {
210 id: "comment",
211 kind: "issue-comment",
212 author: { login: "alice", name: null, avatarUrl: null },
213 body: "One question",
214 createdAt: "2026-10-01T00:01:00Z",
215 url: `${detail.url}#issuecomment-2`,
216 path: null,
217 reviewState: null,
218 },
219 ],
220 };
221 await using screen = await render(
222 <div style={{ height: 600 }}>
223 <PullRequestTimelineTab
224 scrollerRef={() => {}}
225 detail={commented}
226 environmentId={EnvironmentId.make("environment")}
227 reference={detail}
228 order="oldest"
229 onOpenCommit={() => {}}
230 onRefresh={() => {}}
231 />
232 </div>,
233 );
234 await expect.element(screen.getByRole("heading", { name: /bob approved/i })).toBeVisible();
235 await userEvent.click(screen.getByRole("button", { name: /1 comment/ }));
236 await expect.element(screen.getByText("One question", { exact: true })).toBeVisible();
237 await expect.element(screen.getByRole("heading", { name: /alice commented/ })).toBeVisible();
238});
239
240it("opens activity host links through the link-target setting", async () => {
241 linkState.openLink.mockClear();
242 linkState.useOpenLink.mockClear();
243 const reviewUrl = `${detail.url}#pullrequestreview-1`;
244 const commentUrl = `${detail.url}#issuecomment-2`;
245 const threadRef = {
246 environmentId: EnvironmentId.make("environment"),
247 threadId: ThreadId.make("thread-1"),
248 };
249 const commented: PullRequestDetailView = {
250 ...detail,
251 commits: [],
252 comments: [
253 {
254 id: "review",
255 kind: "review",
256 author: { login: "bob", name: null, avatarUrl: null },
257 body: "Looks good",
258 createdAt: "2026-10-01T00:00:00Z",
259 url: reviewUrl,
260 path: null,
261 reviewState: "APPROVED",
262 },
263 {
264 id: "comment",
265 kind: "issue-comment",
266 author: { login: "alice", name: null, avatarUrl: null },
267 body: "One question",
268 createdAt: "2026-10-01T00:01:00Z",
269 url: commentUrl,
270 path: null,
271 reviewState: null,
272 },
273 ],
274 };
275 await using screen = await render(
276 <div style={{ height: 600 }}>
277 <PullRequestTimelineTab
278 scrollerRef={() => {}}
279 detail={commented}
280 environmentId={threadRef.environmentId}
281 threadRef={threadRef}
282 reference={detail}
283 order="oldest"
284 onOpenCommit={() => {}}
285 onRefresh={() => {}}
286 />
287 </div>,
288 );
289 await userEvent.click(screen.getByRole("button", { name: "Open activity on host" }));
290 expect(linkState.useOpenLink).toHaveBeenCalledWith(threadRef);
291 expect(linkState.openLink).toHaveBeenLastCalledWith(reviewUrl, {
292 event: expect.objectContaining({ metaKey: false, ctrlKey: false }),
293 });
294
295 await userEvent.click(screen.getByRole("button", { name: /1 comment/ }));
296 const buttons = screen.getByRole("button", { name: "Open activity on host" }).all();
297 await userEvent.click(buttons[1]!);
298 expect(linkState.openLink).toHaveBeenLastCalledWith(commentUrl, {
299 event: expect.objectContaining({ metaKey: false, ctrlKey: false }),
300 });
301});