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 await screen.rerender(
174 <div style={{ height: 600 }}>
175 <PullRequestTimelineTab
176 scrollerRef={() => {}}
177 detail={linkedDetail}
178 environmentId={EnvironmentId.make("environment")}
179 reference={detail}
180 order="oldest"
181 onOpenCommit={() => {}}
182 onRefresh={() => {}}
183 onRevealSettled={settled}
184 />
185 </div>,
186 );
187 await expect.element(screen.getByText("Linked reply", { exact: true })).toBeVisible();
188 },
189);
190
191it("gives each comment and review a heading for screen reader navigation", async () => {
192 const commented: PullRequestDetailView = {
193 ...detail,
194 commits: [],
195 comments: [
196 {
197 id: "review",
198 kind: "review",
199 author: { login: "bob", name: null, avatarUrl: null },
200 body: "Looks good",
201 createdAt: "2026-10-01T00:00:00Z",
202 url: `${detail.url}#pullrequestreview-1`,
203 path: null,
204 reviewState: "APPROVED",
205 },
206 {
207 id: "comment",
208 kind: "issue-comment",
209 author: { login: "alice", name: null, avatarUrl: null },
210 body: "One question",
211 createdAt: "2026-10-01T00:01:00Z",
212 url: `${detail.url}#issuecomment-2`,
213 path: null,
214 reviewState: null,
215 },
216 ],
217 };
218 await using screen = await render(
219 <div style={{ height: 600 }}>
220 <PullRequestTimelineTab
221 scrollerRef={() => {}}
222 detail={commented}
223 environmentId={EnvironmentId.make("environment")}
224 reference={detail}
225 order="oldest"
226 onOpenCommit={() => {}}
227 onRefresh={() => {}}
228 />
229 </div>,
230 );
231 await expect.element(screen.getByRole("heading", { name: /bob approved/i })).toBeVisible();
232 await userEvent.click(screen.getByRole("button", { name: /1 comment/ }));
233 await expect.element(screen.getByText("One question", { exact: true })).toBeVisible();
234 await expect.element(screen.getByRole("heading", { name: /alice commented/ })).toBeVisible();
235});
236
237it("opens activity host links through the link-target setting", async () => {
238 linkState.openLink.mockClear();
239 linkState.useOpenLink.mockClear();
240 const reviewUrl = `${detail.url}#pullrequestreview-1`;
241 const commentUrl = `${detail.url}#issuecomment-2`;
242 const threadRef = {
243 environmentId: EnvironmentId.make("environment"),
244 threadId: ThreadId.make("thread-1"),
245 };
246 const commented: PullRequestDetailView = {
247 ...detail,
248 commits: [],
249 comments: [
250 {
251 id: "review",
252 kind: "review",
253 author: { login: "bob", name: null, avatarUrl: null },
254 body: "Looks good",
255 createdAt: "2026-10-01T00:00:00Z",
256 url: reviewUrl,
257 path: null,
258 reviewState: "APPROVED",
259 },
260 {
261 id: "comment",
262 kind: "issue-comment",
263 author: { login: "alice", name: null, avatarUrl: null },
264 body: "One question",
265 createdAt: "2026-10-01T00:01:00Z",
266 url: commentUrl,
267 path: null,
268 reviewState: null,
269 },
270 ],
271 };
272 await using screen = await render(
273 <div style={{ height: 600 }}>
274 <PullRequestTimelineTab
275 scrollerRef={() => {}}
276 detail={commented}
277 environmentId={threadRef.environmentId}
278 threadRef={threadRef}
279 reference={detail}
280 order="oldest"
281 onOpenCommit={() => {}}
282 onRefresh={() => {}}
283 />
284 </div>,
285 );
286 await userEvent.click(screen.getByRole("button", { name: "Open activity on host" }));
287 expect(linkState.useOpenLink).toHaveBeenCalledWith(threadRef);
288 expect(linkState.openLink).toHaveBeenLastCalledWith(reviewUrl, {
289 event: expect.objectContaining({ metaKey: false, ctrlKey: false }),
290 });
291
292 await userEvent.click(screen.getByRole("button", { name: /1 comment/ }));
293 const buttons = screen.getByRole("button", { name: "Open activity on host" }).all();
294 await userEvent.click(buttons[1]!);
295 expect(linkState.openLink).toHaveBeenLastCalledWith(commentUrl, {
296 event: expect.objectContaining({ metaKey: false, ctrlKey: false }),
297 });
298});