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