Repository navigation
Expand file tree
/
Copy pathPullRequestCodeTab.browser.test.tsx
More file actions
357 lines (331 loc) · 12 KB
/
Copy pathPullRequestCodeTab.browser.test.tsx
File metadata and controls
357 lines (331 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { sha256 } from "@noble/hashes/sha2";
import { bytesToHex } from "@noble/hashes/utils";
import {
EnvironmentId,
ProjectId,
SourceControlProviderKind,
type PullRequestDetailView,
type PullRequestDiffResult,
} from "@t3tools/contracts";
import { expect, it, vi } from "vite-plus/test";
import "~/index.css";
import { __setClientSettingsForTests } from "../../hooks/useSettings";
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
const { diffAtom, pagedDiffAtom } = await vi.hoisted(async () => {
const { AsyncResult, Atom } = await import("effect/reactivity");
const filePatch = (path: string) =>
[
`diff --git a/${path} b/${path}`,
"index 1111111..2222222 100644",
`--- a/${path}`,
`+++ b/${path}`,
"@@ -1,6 +1,6 @@",
"-before()",
"+after()",
" one()",
" two()",
" three()",
" four()",
" five()",
"",
].join("\n");
const firstPage: PullRequestDiffResult = {
patch: filePatch("a.ts"),
truncated: false,
nextCursor: null,
};
const pages: Record<string, PullRequestDiffResult> = {
first: {
patch: Array.from({ length: 40 }, (_, index) =>
filePatch(index === 1 ? "early-1.test.ts" : `early-${index}.ts`),
).join(""),
truncated: true,
nextCursor: "page-2",
},
"page-2": { patch: filePatch("late.test.ts"), truncated: false, nextCursor: null },
};
return {
// An environment query whose refetch returns equal content hands back the previous value
// itself, so a refresh publishes a new result around the very same page.
diffAtom: Atom.make(() => AsyncResult.success(firstPage)),
pagedDiffAtom: Atom.family((cursor: string) =>
Atom.make(() => AsyncResult.success(pages[cursor]!)),
),
};
});
const mocks = vi.hoisted(() => ({
paged: false,
diffCommits: [] as Array<string | undefined>,
viewedPaths: null as ReadonlySet<string> | null,
viewedListeners: new Set<() => void>(),
}));
function deliverViewedPaths(paths: ReadonlySet<string> | null) {
mocks.viewedPaths = paths;
for (const listener of mocks.viewedListeners) listener();
}
vi.mock(import("~/state/use-atom-command"), async (original) => ({
...(await original()),
useAtomCommand: (() => vi.fn()) as never,
}));
vi.mock(import("./usePullRequestFilesViewed"), async () => {
const { useSyncExternalStore } = await import("react");
const subscribe = (listener: () => void) => {
mocks.viewedListeners.add(listener);
return () => mocks.viewedListeners.delete(listener);
};
return {
usePullRequestFilesViewed: () => {
const viewedPaths = useSyncExternalStore(subscribe, () => mocks.viewedPaths);
return {
enabled: viewedPaths !== null,
isViewed: (path: string) => viewedPaths?.has(path) === true,
isStale: () => false,
setViewed: () => {},
truncated: false,
error: null,
refresh: () => {},
};
},
};
});
vi.mock(import("~/state/pullRequests"), async (original) => {
const actual = await original();
return {
...actual,
pullRequestEnvironment: {
...actual.pullRequestEnvironment,
diff: (({ input }: { input: { cursor?: string; commit?: string } }) => {
mocks.diffCommits.push(input.commit);
return mocks.paged ? pagedDiffAtom(input.cursor ?? "first") : diffAtom;
}) as never,
},
};
});
import { render } from "../../../test/render";
import PullRequestCodeTab from "./PullRequestCodeTab";
const detail: PullRequestDetailView = {
provider: SourceControlProviderKind.make("github"),
projectId: ProjectId.make("project"),
projectTitle: "Project",
workspaceRoot: "/workspace",
repository: "owner/repo",
number: 1,
title: "Test pull request",
body: "",
url: "https://github.com/owner/repo/pull/1",
author: { login: "author", name: null, avatarUrl: null },
viewer: "author",
state: "open",
isDraft: false,
mergeability: "mergeable",
additions: 1,
deletions: 1,
changedFiles: 1,
headBranch: "feature",
baseBranch: "main",
createdAt: "2026-09-01T00:00:00Z",
updatedAt: "2026-09-01T00:00:00Z",
mergedAt: null,
closedAt: null,
reviewers: [],
labels: [],
checks: [],
comments: [],
commentCount: 0,
commentsTruncated: false,
reviewThreads: [],
commits: [],
mergeCapabilities: { merge: false, squash: false, rebase: false },
capabilities: {
diff: true,
comment: false,
search: true,
actions: [],
mergeMethods: [],
review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
reviewers: { request: false, listCandidates: false },
edit: { changeRequest: false, comment: false },
},
viewerPermissions: {
actions: [],
comment: false,
resolve: false,
verdicts: [],
requestReviewers: false,
},
};
function setup({ paged }: { paged: boolean }) {
window.localStorage.removeItem("t3code.diffHideTestFiles");
__setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
mocks.paged = paged;
mocks.diffCommits = [];
mocks.viewedPaths = null;
mocks.viewedListeners.clear();
}
const tab = (
refreshToken: number,
revealRequest: { readonly path: string; readonly nonce: number } | null = null,
revealUrl?: string,
onRevealSettled?: (url: string) => void,
) => (
<PullRequestCodeTab
scrollerRef={() => {}}
environmentId={EnvironmentId.make("environment")}
reference={detail}
detail={detail}
selectedCommitOid={null}
onSelectedCommitChange={() => {}}
onRefresh={() => {}}
refreshToken={refreshToken}
revealRequest={revealRequest}
revealUrl={revealUrl}
onRevealSettled={onRevealSettled}
/>
);
it("keeps the diff after a refresh that brings back the same page", async () => {
setup({ paged: false });
const container = document.createElement("div");
container.className = "flex h-[700px] w-[900px]";
await using screen = await render(tab(0), { container });
await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
await screen.rerender(tab(1));
await expect
.element(screen.getByText("This pull request has no file changes."))
.not.toBeInTheDocument();
await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
});
// regression: #13279 a Viewed answer that lands after a link reveals a file must not fold it
it("keeps a revealed file open when its Viewed mark loads afterwards", async () => {
setup({ paged: false });
__setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false });
using _preferences = {
[Symbol.dispose]() {
__setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
},
};
mocks.viewedPaths = new Set();
const container = document.createElement("div");
container.className = "flex h-[700px] w-[900px]";
await using screen = await render(tab(0, { path: "a.ts", nonce: 1 }), { container });
await expect.element(screen.getByText("three()", { exact: true })).toBeVisible();
deliverViewedPaths(new Set(["a.ts"]));
await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).toBeChecked();
await expect
.element(screen.getByRole("button", { name: "Collapse diff" }))
.toHaveAttribute("aria-expanded", "true");
await expect.element(screen.getByText("three()", { exact: true })).toBeVisible();
});
function pagedContainer() {
const container = document.createElement("div");
container.className = "flex h-[500px] w-[900px]";
return container;
}
async function expectFileHeaderShown(
screen: Awaited<ReturnType<typeof render>>,
container: HTMLElement,
path: string,
) {
const header = screen.getByText(path, { exact: true }).first();
await expect.element(header).toBeInTheDocument();
await vi.waitFor(() => {
const bounds = container.querySelector(".diff-render-surface")!.getBoundingClientRect();
const { top, bottom } = header.element().getBoundingClientRect();
expect(top).toBeGreaterThanOrEqual(bounds.top);
expect(bottom).toBeLessThanOrEqual(bounds.bottom);
});
}
it("scrolls to a file asked for as the tab opens", async () => {
setup({ paged: true });
const container = pagedContainer();
await using screen = await render(tab(0, { path: "early-39.ts", nonce: 1 }), { container });
await expectFileHeaderShown(screen, container, "early-39.ts");
});
it("pages forward to a file asked for that is not loaded yet, then scrolls to it", async () => {
setup({ paged: true });
__setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: true });
using _preferences = {
[Symbol.dispose]() {
window.localStorage.removeItem("t3code.diffHideTestFiles");
__setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
},
};
const container = pagedContainer();
await using screen = await render(tab(0), { container });
await expect.element(screen.getByText("early-0.ts", { exact: true }).first()).toBeVisible();
await screen.getByRole("button", { name: "Hide test files" }).click();
const url = `${detail.url}/files#diff-${bytesToHex(sha256(new TextEncoder().encode("late.test.ts")))}R4-R5`;
const settled = vi.fn();
await screen.rerender(tab(0, null, url, settled));
await expectFileHeaderShown(screen, container, "late.test.ts");
await expect.element(screen.getByText("three()", { exact: true })).toBeVisible();
await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url));
// A settled link keeps its test file past the filter, and a later click reveals it again.
await screen.rerender(tab(0, { path: "early-0.ts", nonce: 1 }));
await expectFileHeaderShown(screen, container, "early-0.ts");
await screen.rerender(tab(0, { path: "early-0.ts", nonce: 1 }, url, settled));
await expectFileHeaderShown(screen, container, "late.test.ts");
});
it("never requests the diff of a selected commit the pull request no longer has", async () => {
setup({ paged: false });
const container = document.createElement("div");
container.className = "flex h-[700px] w-[900px]";
// The timeline can open a commit that a force-push removed from the pull request.
await using screen = await render(
<PullRequestCodeTab
scrollerRef={() => {}}
environmentId={EnvironmentId.make("environment")}
reference={detail}
detail={{
...detail,
commits: [
{
oid: "kept",
messageHeadline: "Kept commit",
committedDate: "2026-09-01T00:00:00Z",
},
],
}}
selectedCommitOid="gone"
onSelectedCommitChange={() => {}}
onRefresh={() => {}}
/>,
{ container },
);
await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
await expect
.element(screen.getByRole("button", { name: "Diff scope: All commits" }))
.toBeVisible();
expect(mocks.diffCommits).not.toContain("gone");
});
it("answers a commit's diff link only once that commit's diff is shown", async () => {
setup({ paged: false });
const container = document.createElement("div");
container.className = "flex h-[700px] w-[900px]";
const oid = "c".repeat(40);
const withCommit = {
...detail,
commits: [{ oid, messageHeadline: "Linked commit", committedDate: "2026-09-01T00:00:00Z" }],
};
const url = `${detail.url}/commits/${oid}#diff-${bytesToHex(sha256(new TextEncoder().encode("a.ts")))}R2`;
const settled = vi.fn();
const scoped = (selectedCommitOid: string | null, revealUrl?: string) => (
<PullRequestCodeTab
scrollerRef={() => {}}
environmentId={EnvironmentId.make("environment")}
reference={detail}
detail={withCommit}
selectedCommitOid={selectedCommitOid}
onSelectedCommitChange={() => {}}
onRefresh={() => {}}
revealUrl={revealUrl}
onRevealSettled={settled}
/>
);
await using screen = await render(scoped(null), { container });
await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
// The panel selects the link's commit after this tab has already seen the link.
await screen.rerender(scoped(null, url));
expect(settled).not.toHaveBeenCalled();
await screen.rerender(scoped(oid, url));
await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url));
});