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

pullRequestList.test.ts

444 lines15.2 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 EnvironmentId,
4 SourceControlProviderKind,
5 type PullRequestListEntry,
6 type PullRequestListResult,
7} from "@t3tools/contracts";
8import {
9 appendPullRequestListPage,
10 DEFAULT_PR_LIST_CONTROLS,
11 filterPullRequestList,
12 mergePullRequestListEntries,
13 mergeEnvironmentPullRequestPages,
14 nextPullRequestListInput,
15 PR_LIST_PAGE_SIZE,
16 pullRequestListFacets,
17 pullRequestListHead,
18 pullRequestListInput,
19 pullRequestListReviewStatus,
20 pullRequestLabelColor,
21 scopedPullRequestListKey,
22 pullRequestListRowsAreEqual,
23 withoutScratchProjectScope,
24} from "./pullRequestList";
25​
26function entry(number: number, patch: Partial<PullRequestListEntry> = {}): PullRequestListEntry {
27 return {
28 number,
29 provider: SourceControlProviderKind.make("github"),
30 host: "github.com",
31 repository: "org/repo",
32 projectId: "project" as PullRequestListEntry["projectId"],
33 projectTitle: "Project",
34 title: `Change ${number}`,
35 url: `https://github.com/org/repo/pull/${number}`,
36 author: { login: "octocat", name: null, avatarUrl: null },
37 headBranch: `branch-${number}`,
38 baseBranch: "main",
39 state: "open",
40 isDraft: false,
41 mergeability: "unknown",
42 additions: 0,
43 deletions: 0,
44 createdAt: "2026-10-01T00:00:00Z",
45 updatedAt: `2026-10-${String(number).padStart(2, "0")}T00:00:00Z`,
46 observedAt: 1,
47 viewerReviewRequested: false,
48 labels: [],
49 ...patch,
50 };
52function page(
53 entries: readonly PullRequestListEntry[],
54 nextCursors: PullRequestListResult["nextCursors"] = {},
55): PullRequestListResult {
56 return {
57 entries,
58 nextCursors,
59 truncated: Object.keys(nextCursors).length > 0,
60 viewers: { "github.com": "octocat" },
61 providers: [],
62 errors: [],
63 };
65​
66describe("recycled PR list rows", () => {
67 const scopedEntry = (patch: Partial<PullRequestListEntry> = {}) => ({
68 ...entry(1, patch),
69 environmentId: EnvironmentId.make("env-one"),
70 });
71​
72 it("keeps an unchanged row through provider refreshes and newly allocated labels and authors", () => {
73 const labels = [{ name: "bug", color: "ff0000" }];
74 expect(
75 pullRequestListRowsAreEqual(
76 scopedEntry({ labels }),
77 scopedEntry({ labels: labels.map((label) => ({ ...label })), observedAt: 20 }),
78 ),
79 ).toBe(true);
80 });
81​
82 it.each([
83 { title: "New title" },
84 { additions: 8 },
85 { deletions: 4 },
86 { checksState: "failing" as const },
87 { reviewDecision: "approved" as const },
88 { viewerReviewRequested: true },
89 { isDraft: true },
90 { state: "closed" as const },
91 { mergeability: "conflicting" as const },
92 { baseBranch: "release" },
93 { author: { login: "another", name: null, avatarUrl: null } },
94 { author: { login: "octocat", name: null, avatarUrl: "https://example.com/avatar.png" } },
95 { labels: [{ name: "bug", color: "ff0000" }] },
96 { stack: { number: 1, position: 2, size: 3, base: "main" } },
97 ])("updates the row when visible status or quick actions change: %j", (patch) => {
98 expect(pullRequestListRowsAreEqual(scopedEntry(), scopedEntry(patch))).toBe(false);
99 });
100​
101 it("updates navigation and commands when a duplicate moves to another environment", () => {
102 const original = scopedEntry();
103 expect(
104 pullRequestListRowsAreEqual(original, {
105 ...original,
106 environmentId: EnvironmentId.make("env-two"),
107 }),
108 ).toBe(false);
109 });
110});
111​
112describe("mobile PR list pagination", () => {
113 it("continues only the repositories that have more, preserving the query and filters", () => {
114 const first = pullRequestListHead(
115 null,
116 page([entry(3)], { "github.com org/repo": "opaque-boundary" }),
117 );
118 expect(
119 nextPullRequestListInput(first, {
120 state: "open",
121 query: "fix",
122 filters: { draft: "hide" },
123 limit: 30,
124 }),
125 ).toEqual({
126 state: "open",
127 query: "fix",
128 filters: { draft: "hide" },
129 limit: 30,
130 cursors: { "github.com org/repo": "opaque-boundary" },
131 });
132 const next = appendPullRequestListPage(first, page([entry(2), entry(1)]), {
133 state: "open",
134 cursors: first.nextCursors,
135 });
136 expect(next.entries.map((row) => row.number)).toEqual([3, 2, 1]);
137 expect(nextPullRequestListInput(next, { state: "open" })).toBeNull();
138 });
139 it("deduplicates page boundaries and never overwrites a fresh row with an older slice", () => {
140 const current = entry(2, { title: "Retitled", observedAt: 20 });
141 expect(
142 mergePullRequestListEntries([current], [entry(2, { observedAt: 10 }), entry(1)]),
143 ).toEqual([current, entry(1)]);
144 expect(
145 mergePullRequestListEntries([entry(1)], [entry(2, { host: "enterprise.test" }), entry(2)]),
146 ).toHaveLength(3);
147 });
148 it("refreshes the head without dropping older loaded pages or rewinding their cursor", () => {
149 const first = pullRequestListHead(null, page([entry(3)], { repo: "first" }));
150 const continued = appendPullRequestListPage(first, page([entry(2)], { repo: "second" }), {
151 state: "open",
152 cursors: first.nextCursors,
153 });
154 const updated = pullRequestListHead(
155 continued,
156 page([entry(4), entry(3, { title: "New title", observedAt: 2 })], { repo: "first-new" }),
157 );
158 expect(updated.entries.map((row) => row.number)).toEqual([4, 3, 2]);
159 expect(updated.entries[1]?.title).toBe("New title");
160 expect(updated.nextCursors).toEqual({ repo: "second" });
161 expect(pullRequestListHead(updated, updated.head)).toBe(updated);
162 });
163 it("can grow legacy listings that provide no cursor, without exceeding the wire limit", () => {
164 const first = pullRequestListHead(null, { ...page([entry(1)]), truncated: true });
165 expect(nextPullRequestListInput(first, { state: "all" })?.limit).toBe(60);
166 expect(nextPullRequestListInput({ ...first, limit: 490 }, { state: "all" })?.limit).toBe(500);
167 expect(nextPullRequestListInput({ ...first, limit: 500 }, { state: "all" })).toBeNull();
168 });
169});
170​
171describe("all-environment PR list", () => {
172 const cups = EnvironmentId.make("cups");
173 const mbp = EnvironmentId.make("mbp");
174 it("combines independent pages, collapses duplicate PRs, and keeps their source for navigation", () => {
175 const first = pullRequestListHead(null, page([entry(3), entry(1)], { repo: "cups-next" }));
176 const second = pullRequestListHead(null, page([entry(2), entry(1)], { repo: "mbp-next" }));
177 const merged = mergeEnvironmentPullRequestPages([
178 { environmentId: cups, pages: first },
179 { environmentId: mbp, pages: second },
180 ]);
181 const rows = filterPullRequestList(
182 merged.entries,
183 DEFAULT_PR_LIST_CONTROLS,
184 merged.viewers,
185 "",
186 merged.localSearchHosts,
187 );
188 expect(rows.map((row) => [row.number, row.environmentId])).toEqual([
189 [3, cups],
190 [2, mbp],
191 [1, cups],
192 ]);
193 expect(scopedPullRequestListKey(rows[1]!)).toBe("mbp:github.com:org/repo:2");
194 expect(nextPullRequestListInput(first, { state: "open" })?.cursors).toEqual({
195 repo: "cups-next",
196 });
197 expect(nextPullRequestListInput(second, { state: "open" })?.cursors).toEqual({
198 repo: "mbp-next",
199 });
200 const continued = appendPullRequestListPage(first, page([entry(4)]), {
201 state: "open",
202 cursors: first.nextCursors,
203 });
204 expect(nextPullRequestListInput(continued, { state: "open" })).toBeNull();
205 expect(nextPullRequestListInput(second, { state: "open" })?.cursors).toEqual({
206 repo: "mbp-next",
207 });
208 });
209 it("uses the viewer account and search support from each environment before deduplicating", () => {
210 const remote = {
211 ...page([entry(1, { author: { login: "other", name: null, avatarUrl: null } })]),
212 viewers: { "github.com": "other" },
213 };
214 const local = page([entry(1), entry(2)]);
215 const merged = mergeEnvironmentPullRequestPages([
216 { environmentId: cups, pages: pullRequestListHead(null, remote) },
217 { environmentId: mbp, pages: pullRequestListHead(null, local) },
218 ]);
219 const byMe = filterPullRequestList(
220 merged.entries,
221 { ...DEFAULT_PR_LIST_CONTROLS, involvement: "authored" },
222 merged.viewers,
223 "",
224 merged.localSearchHosts,
225 );
226 expect(byMe.map((row) => [row.number, row.environmentId])).toEqual([
227 [2, mbp],
228 [1, cups],
229 ]);
230 const localOnlySearch = new Set([`${cups} github.com`]);
231 const searched = filterPullRequestList(
232 merged.entries,
233 DEFAULT_PR_LIST_CONTROLS,
234 merged.viewers,
235 "body match",
236 localOnlySearch,
237 );
238 expect(searched.map((row) => [row.number, row.environmentId])).toEqual([
239 [2, mbp],
240 [1, mbp],
241 ]);
242 });
243 it("scopes a project to its owning environment even if another has the same project id", () => {
244 const merged = mergeEnvironmentPullRequestPages([
245 { environmentId: cups, pages: pullRequestListHead(null, page([entry(1)])) },
246 { environmentId: mbp, pages: pullRequestListHead(null, page([entry(2)])) },
247 ]);
248 expect(
249 filterPullRequestList(
250 merged.entries,
251 { ...DEFAULT_PR_LIST_CONTROLS, projectId: "project", projectEnvironmentId: mbp },
252 merged.viewers,
253 "",
254 merged.localSearchHosts,
255 ).map((row) => row.environmentId),
256 ).toEqual([mbp]);
257 });
258});
259​
260describe("mobile PR scan and filters", () => {
261 it("shows review verdicts separately from checks and prioritizes changes requested", () => {
262 expect(
263 pullRequestListReviewStatus({
264 reviewDecision: "changes-requested",
265 viewerReviewRequested: true,
266 })?.accessibleLabel,
267 ).toBe("Changes requested");
268 expect(
269 pullRequestListReviewStatus({ reviewDecision: "approved", viewerReviewRequested: false })
270 ?.label,
271 ).toBe("Approved");
272 expect(pullRequestListReviewStatus({ viewerReviewRequested: true })?.label).toBe("Your review");
273 expect(pullRequestListReviewStatus({ viewerReviewRequested: false })).toBeNull();
274 });
275 it("resolves authored and author:me per host rather than mixing account identities", () => {
276 const rows = [entry(1), entry(2, { host: "enterprise.test" })];
277 const viewers = { "github.com": "Octocat", "enterprise.test": "someone-else" };
278 expect(
279 filterPullRequestList(
280 rows,
281 { ...DEFAULT_PR_LIST_CONTROLS, involvement: "authored" },
282 viewers,
283 "",
284 new Set(),
285 ).map((row) => row.number),
286 ).toEqual([1]);
287 expect(
288 filterPullRequestList(
289 rows,
290 { ...DEFAULT_PR_LIST_CONTROLS, filters: { author: "me" } },
291 viewers,
292 "",
293 new Set(),
294 ).map((row) => row.number),
295 ).toEqual([1]);
296 expect(
297 filterPullRequestList(rows, DEFAULT_PR_LIST_CONTROLS, viewers, "author:@me", new Set()).map(
298 (row) => row.number,
299 ),
300 ).toEqual([1]);
301 });
302 it("combines label groups, exclusions, draft and review filters", () => {
303 const rows = [
304 entry(1, {
305 labels: [
306 { name: "size:XS", color: null },
307 { name: "bug", color: null },
308 ],
309 reviewDecision: "approved",
310 }),
311 entry(2, { isDraft: true, labels: [{ name: "bug", color: null }] }),
312 entry(3, { labels: [{ name: "feature", color: null }] }),
313 ];
314 const filters = {
315 draft: "hide" as const,
316 review: "approved" as const,
317 labels: [["SIZE:XS", "size:S"], ["bug"]],
318 excludedLabels: ["feature"],
319 };
320 expect(
321 filterPullRequestList(rows, { ...DEFAULT_PR_LIST_CONTROLS, filters }, {}, "", new Set()).map(
322 (row) => row.number,
323 ),
324 ).toEqual([1]);
325 });
326 it("reads a typed draft qualifier as a filter and searches only the text left over", () => {
327 expect(pullRequestListInput(DEFAULT_PR_LIST_CONTROLS, "draft:false fix")).toEqual({
328 state: "open",
329 involvement: "all",
330 filters: { draft: "hide" },
331 limit: PR_LIST_PAGE_SIZE,
332 query: "fix",
333 });
334 const rows = [
335 entry(1, { title: "Fix parser" }),
336 entry(2, { title: "Fix drafts", isDraft: true }),
337 entry(3),
338 ];
339 expect(
340 filterPullRequestList(
341 rows,
342 DEFAULT_PR_LIST_CONTROLS,
343 {},
344 "draft:false fix",
345 new Set(["github.com"]),
346 ).map((row) => row.number),
347 ).toEqual([1]);
348 });
349 it("does not label unsupported review summaries as reviewed or match text locally on host-search results", () => {
350 expect(
351 filterPullRequestList(
352 [entry(1)],
353 { ...DEFAULT_PR_LIST_CONTROLS, filters: { review: "approved" } },
354 {},
355 "body text",
356 new Set(),
357 ),
358 ).toHaveLength(1);
359 expect(
360 filterPullRequestList(
361 [entry(1)],
362 DEFAULT_PR_LIST_CONTROLS,
363 {},
364 "body text",
365 new Set(["github.com"]),
366 ),
367 ).toHaveLength(0);
368 });
369 it("lists a shared repository's row from a connected environment over an offline one", () => {
370 const rows = [
371 { ...entry(1), environmentId: "laptop" },
372 { ...entry(1), environmentId: "server" },
373 ];
374 const pick = (connected: ReadonlySet<string>) =>
375 filterPullRequestList(rows, DEFAULT_PR_LIST_CONTROLS, {}, "", new Set(), connected).map(
376 (row) => row.environmentId,
377 );
378 expect(pick(new Set(["server"]))).toEqual(["server"]);
379 expect(pick(new Set(["laptop", "server"]))).toEqual(["laptop"]);
380 expect(pick(new Set())).toEqual(["laptop"]);
381 });
382 it("sorts loaded rows and builds unique author and label options", () => {
383 const rows = [
384 entry(1, { labels: [{ name: "bug", color: null }] }),
385 entry(2, {
386 labels: [
387 { name: "bug", color: null },
388 { name: "feature", color: null },
389 ],
390 }),
391 ];
392 expect(
393 filterPullRequestList(
394 rows,
395 { ...DEFAULT_PR_LIST_CONTROLS, sort: "oldest" },
396 {},
397 "",
398 new Set(),
399 ).map((row) => row.number),
400 ).toEqual([1, 2]);
401 expect(pullRequestListFacets(rows)).toEqual({
402 authors: ["octocat"],
403 labels: ["bug", "feature"],
404 });
405 expect(pullRequestLabelColor("ffab00")).toBe("#ffab00");
406 expect(pullRequestLabelColor("not-a-color")).toBeNull();
407 });
408});
409​
410describe("withoutScratchProjectScope", () => {
411 const projects = [
412 { id: "repo", environmentId: "env-a" },
413 { id: "scratch", environmentId: "env-a", isScratch: true as const },
414 ];
415​
416 it("reads a scope saved on the Scratch folder as all projects", () => {
417 const controls = withoutScratchProjectScope(
418 { ...DEFAULT_PR_LIST_CONTROLS, projectId: "scratch", projectEnvironmentId: "env-a" },
419 projects,
420 );
421 expect(controls.projectId).toBeUndefined();
422 expect(controls.projectEnvironmentId).toBeUndefined();
423 });
424​
425 it("keeps a repository scope, and one whose projects have not arrived yet", () => {
426 const repository = {
427 ...DEFAULT_PR_LIST_CONTROLS,
428 projectId: "repo",
429 projectEnvironmentId: "env-a",
430 };
431 expect(withoutScratchProjectScope(repository, projects)).toBe(repository);
432 const pending = { ...repository, projectId: "scratch" };
433 expect(withoutScratchProjectScope(pending, [])).toBe(pending);
434 });
435​
436 it("keeps another server's project that shares the Scratch folder's id", () => {
437 const elsewhere = {
438 ...DEFAULT_PR_LIST_CONTROLS,
439 projectId: "scratch",
440 projectEnvironmentId: "env-b",
441 };
442 expect(withoutScratchProjectScope(elsewhere, projects)).toBe(elsewhere);
443 });
444});