Open History to see the last commit
1import { SourceControlProviderKind } from "@t3tools/contracts";
2import type {
3 EnvironmentId,
4 ProjectId,
5 PullRequestAction,
6 PullRequestListEntry,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9
10import {
11 pullRequestHostActions,
12 largestPullRequestReadCount,
13 pullRequestQuickActions,
14 filterPullRequestsByInvolvement,
15 findProjectForRepository,
16 findScopedProject,
17 mergePullRequestLists,
18 pullRequestEntryKey,
19 groupPullRequestsByInvolvement,
20 pullRequestStatsBatches,
21 pullRequestStatsKeysToRequest,
22 pullRequestStatsRefreshBatches,
23 mergePullRequestDiffStats,
24 partitionPullRequestsWithPriority,
25 readPullRequestListSnapshot,
26 writePullRequestListSnapshot,
27 rankPullRequestMatches,
28 rankPullRequestsBlockedOnAuthor,
29 rankPullRequestsByMergeReadiness,
30 sortPullRequestGroups,
31 resolveQueryEnvironmentIds,
32 resolveSelectedEnvironmentId,
33 type EnvironmentPullRequestEntry,
34 applyPullRequestOverrides,
35 reusePullRequestEntries,
36 settlePullRequestOverrides,
37} from "./pullRequestList.logic";
38import {
39 readPullRequestListPreferences,
40 writePullRequestListPreferences,
41} from "./pullRequestListPreferences";
42
43const VIEWERS = { "github.com": "Bilal" } as const;
44
45function entry(
46 overrides: Partial<EnvironmentPullRequestEntry> & Pick<PullRequestListEntry, "number">,
47) {
48 return {
49 environmentId: "env-1",
50 provider: "github",
51 host: "github.com",
52 projectId: "project-1",
53 projectTitle: "t3code",
54 repository: "pingdotgg/t3code",
55 title: "Add the pull requests page",
56 url: `https://github.com/pingdotgg/t3code/pull/${overrides.number}`,
57 author: { login: "octocat", name: null, avatarUrl: null },
58 headBranch: `feat/branch-${overrides.number}`,
59 baseBranch: "main",
60 state: "open",
61 isDraft: false,
62 mergeability: "mergeable",
63 additions: 1,
64 deletions: 0,
65 createdAt: "2026-07-01T00:00:00Z",
66 updatedAt: "2026-07-02T00:00:00Z",
67 viewerReviewRequested: false,
68 labels: [],
69 ...overrides,
70 } as EnvironmentPullRequestEntry;
71}
72
73describe("visible pull request line-count targets", () => {
74 it("does not request a row again after its received batch is pruned", () => {
75 const entries = [
76 entry({ additions: 0, deletions: 0, number: 1 }),
77 entry({ additions: 0, deletions: 0, number: 2 }),
78 ];
79 const entriesByKey = new Map(entries.map((item) => [pullRequestEntryKey(item), item]));
80 const firstKey = pullRequestEntryKey(entries[0]!);
81 const secondKey = pullRequestEntryKey(entries[1]!);
82 const completedStats = mergePullRequestDiffStats(new Map(), [
83 {
84 environmentId: ENV_1,
85 projectId: "project-1",
86 number: 1,
87 additions: 1,
88 deletions: 1,
89 },
90 ]);
91
92 const keys = pullRequestStatsKeysToRequest(
93 entriesByKey,
94 new Set([firstKey, secondKey]),
95 [],
96 completedStats,
97 );
98 expect([...keys]).toEqual([secondKey]);
99 expect(pullRequestStatsBatches(entriesByKey, keys)[0]?.input.refs).toEqual([
100 { projectId: "project-1", repository: "pingdotgg/t3code", number: 2 },
101 ]);
102 });
103
104 it("keeps every per-environment batch within the stats contract limit", () => {
105 const entries = Array.from({ length: 501 }, (_, index) =>
106 entry({ additions: 0, deletions: 0, number: index + 1 }),
107 );
108 const entriesByKey = new Map(entries.map((item) => [pullRequestEntryKey(item), item]));
109 const batches = pullRequestStatsBatches(
110 entriesByKey,
111 new Set(entries.map(pullRequestEntryKey)),
112 );
113
114 expect(batches.map((batch) => batch.input.refs.length)).toEqual([500, 1]);
115 expect(batches.flatMap((batch) => [...batch.keys])).toEqual(entries.map(pullRequestEntryKey));
116 });
117
118 it("ignores a late refresh after the filter or stats policy changes", () => {
119 const entries = [
120 entry({ additions: 0, deletions: 0, number: 1 }),
121 entry({ additions: 0, deletions: 0, number: 2 }),
122 ];
123 const entriesByKey = new Map(entries.map((item) => [pullRequestEntryKey(item), item]));
124 const visibleKeys = new Set([pullRequestEntryKey(entries[1]!)]);
125 const requestedScope = { key: "open", policy: "visible" } as const;
126 const refresh = (currentScope: { key: string; policy: "visible" | "eager" }) =>
127 pullRequestStatsRefreshBatches({
128 requestedScope,
129 currentScope,
130 entriesByKey,
131 candidateKeys: visibleKeys,
132 statsByRow: new Map(),
133 });
134
135 expect(refresh(requestedScope)?.[0]?.input.refs.map((ref) => ref.number)).toEqual([2]);
136 expect(refresh({ key: "closed", policy: "visible" })).toBeNull();
137 expect(refresh({ key: "open", policy: "eager" })).toBeNull();
138 });
139});
140
141describe("pull request involvement filtering", () => {
142 it("does not treat a matching login on another host as the viewer", () => {
143 const mixed = [
144 entry({ number: 1, author: { login: "Bilal", name: null, avatarUrl: null } }),
145 entry({
146 number: 2,
147 provider: SourceControlProviderKind.make("gitlab"),
148 host: "gitlab.com",
149 author: { login: "Bilal", name: null, avatarUrl: null },
150 }),
151 ];
152 expect(
153 filterPullRequestsByInvolvement(mixed, VIEWERS, "authored").map((item) => item.number),
154 ).toEqual([1]);
155 });
156});
157
158describe("ranking what a search found", () => {
159 const row = (overrides: Partial<PullRequestListEntry> & { number?: number }) =>
160 entry({ number: 1, ...overrides });
161
162 it("puts the pull request whose title says it above one that merely mentions it", () => {
163 const titled = row({
164 number: 1,
165 title: "Add the welcome wizard",
166 updatedAt: "2026-07-01T00:00:00Z",
167 });
168 const mentioned = row({
169 number: 2,
170 title: "Unrelated work",
171 updatedAt: "2026-08-01T00:00:00Z",
172 });
173 expect(
174 rankPullRequestMatches([mentioned, titled], "welcome wizard").map((entry) => entry.number),
175 ).toEqual([1, 2]);
176 });
177});
178
179describe("default merge-readiness ranking", () => {
180 it("puts ready work first, finished work after open work, and every conflict last", () => {
181 const conflict = entry({
182 number: 1,
183 mergeability: "conflicting",
184 checksState: "passing",
185 reviewDecision: "approved",
186 updatedAt: "2026-09-01T00:00:00Z",
187 });
188 const other = entry({
189 number: 2,
190 checksState: "pending",
191 updatedAt: "2026-08-01T00:00:00Z",
192 });
193 const green = entry({
194 number: 3,
195 checksState: "passing",
196 reviewDecision: "review-required",
197 updatedAt: "2026-07-01T00:00:00Z",
198 });
199 const approved = entry({
200 number: 4,
201 checksState: "passing",
202 reviewDecision: "approved",
203 additions: 1_000,
204 updatedAt: "2026-06-01T00:00:00Z",
205 });
206 const draft = entry({
207 number: 5,
208 isDraft: true,
209 checksState: "passing",
210 reviewDecision: "approved",
211 updatedAt: "2026-09-02T00:00:00Z",
212 });
213 const finished = entry({
214 number: 6,
215 state: "merged",
216 checksState: "passing",
217 reviewDecision: "approved",
218 updatedAt: "2026-09-03T00:00:00Z",
219 });
220
221 expect(
222 rankPullRequestsByMergeReadiness([conflict, other, green, approved, draft, finished]).map(
223 (row) => row.number,
224 ),
225 ).toEqual([4, 3, 5, 2, 6, 1]);
226 });
227
228 it("distinguishes measured empty diffs from missing counts when sorting groups", () => {
229 const unknown = entry({ number: 1, additions: 0, deletions: 0 });
230 const measured = entry({ number: 2 });
231 const empty = entry({ number: 3, additions: 0, deletions: 0 });
232 const groups = [
233 { key: "others", label: "Others", entries: [unknown, measured, empty] },
234 ] as const;
235
236 const sorted = sortPullRequestGroups(groups, "ready", "", (row) => row.number !== 1);
237
238 expect(sorted[0]!.entries.map((row) => row.number)).toEqual([3, 2, 1]);
239 expect(sortPullRequestGroups(groups, "ready", "sidebar", (row) => row.number !== 1)).toEqual(
240 groups,
241 );
242 });
243
244 // regression: #9453 authored pull requests were not prioritized
245 it("keeps authored work first and ranks each group by readiness", () => {
246 const authoredWaiting = entry({ number: 1, checksState: "pending" });
247 const authoredReady = entry({
248 number: 2,
249 checksState: "passing",
250 reviewDecision: "approved",
251 });
252 const otherReady = entry({
253 number: 3,
254 checksState: "passing",
255 reviewDecision: "approved",
256 });
257 const sorted = sortPullRequestGroups(
258 [
259 { key: "authored", label: "Authored", entries: [authoredWaiting, authoredReady] },
260 { key: "others", label: "Others", entries: [otherReady] },
261 ],
262 "ready",
263 "",
264 );
265
266 expect(sorted.map((group) => group.key)).toEqual(["authored", "others"]);
267 expect(sorted.flatMap((group) => group.entries).map((row) => row.number)).toEqual([2, 1, 3]);
268 });
269});
270
271describe("blocked-on-me ranking", () => {
272 it("ranks authored work by how surely it is the author's to unblock, newest first within a tier", () => {
273 const conflict = entry({
274 number: 1,
275 mergeability: "conflicting",
276 checksState: "failing",
277 updatedAt: "2026-08-01T00:00:00Z",
278 });
279 const olderFailing = entry({
280 number: 2,
281 checksState: "failing",
282 updatedAt: "2026-08-01T00:00:00Z",
283 });
284 const changesRequested = entry({
285 number: 3,
286 reviewDecision: "changes-requested",
287 checksState: "failing",
288 updatedAt: "2026-08-02T00:00:00Z",
289 });
290 const approved = entry({ number: 4, checksState: "passing", reviewDecision: "approved" });
291 const draft = entry({ number: 5, isDraft: true, checksState: "passing" });
292 const waiting = entry({ number: 6 });
293 const merged = entry({ number: 7, state: "merged", updatedAt: "2026-08-01T00:00:00Z" });
294 const newerClosed = entry({ number: 8, state: "closed", updatedAt: "2026-08-03T00:00:00Z" });
295 const newerFailing = entry({
296 number: 9,
297 checksState: "failing",
298 updatedAt: "2026-08-03T00:00:00Z",
299 });
300
301 expect(
302 rankPullRequestsBlockedOnAuthor([
303 waiting,
304 merged,
305 olderFailing,
306 draft,
307 approved,
308 changesRequested,
309 newerClosed,
310 conflict,
311 newerFailing,
312 ]).map((row) => row.number),
313 ).toEqual([1, 3, 9, 2, 5, 6, 4, 8, 7]);
314 });
315});
316
317describe("partitioning with the hosts' own priority reads", () => {
318 const authoredRow = (number: number, updatedAt: string) =>
319 entry({ number, updatedAt, author: { login: "Bilal", name: null, avatarUrl: null } });
320
321 it("keeps Others in feed order when a continuation lands an authored row already partitioned", () => {
322 const older = entry({ number: 1, updatedAt: "2026-07-05T00:00:00Z" });
323 const newer = entry({ number: 2, updatedAt: "2026-07-06T00:00:00Z" });
324 const mine = authoredRow(3, "2026-07-04T00:00:00Z");
325 const groups = partitionPullRequestsWithPriority([newer, older, mine], [mine], []);
326 expect(groups.map((group) => group.key)).toEqual(["authored", "others"]);
327 expect(groups[1]!.entries.map((item) => item.number)).toEqual([2, 1]);
328 expect(groups[0]!.entries.map((item) => item.number)).toEqual([3]);
329 });
330
331 it("gives authored precedence over review-requested and orders partitions by recency", () => {
332 const both = authoredRow(1, "2026-07-01T00:00:00Z");
333 const requested = entry({
334 number: 2,
335 viewerReviewRequested: true,
336 updatedAt: "2026-07-02T00:00:00Z",
337 });
338 const requestedOlder = entry({
339 number: 3,
340 viewerReviewRequested: true,
341 updatedAt: "2026-06-02T00:00:00Z",
342 });
343 const groups = partitionPullRequestsWithPriority([], [both], [both, requestedOlder, requested]);
344 expect(groups.map((group) => group.key)).toEqual(["authored", "reviewRequested"]);
345 expect(groups[0]!.entries.map((item) => item.number)).toEqual([1]);
346 expect(groups[1]!.entries.map((item) => item.number)).toEqual([2, 3]);
347 });
348});
349
350describe("the list snapshot across a reload", () => {
351 const makeStorage = () => {
352 const held = new Map<string, string>();
353 return {
354 getItem: (key: string) => held.get(key) ?? null,
355 setItem: (key: string, value: string) => void held.set(key, value),
356 };
357 };
358 const data = {
359 entries: [entry({ number: 1 })],
360 viewers: { "github.com": "Bilal" },
361 providers: [],
362 errors: [{ projectId: "project-1", message: "boom" }],
363 truncated: true,
364 nextCursors: { "pingdotgg/t3code": "cursor-1" },
365 } as never;
366
367 it("carries neither stale failures nor stale cursors", () => {
368 const storage = makeStorage();
369 writePullRequestListSnapshot(storage, "env-1", { scope: "s", data });
370 const snapshot = readPullRequestListSnapshot(storage, "env-1");
371 expect(snapshot?.data.errors).toEqual([]);
372 expect(snapshot?.data.nextCursors).toEqual({});
373 });
374
375 it("caps the accumulated rows but keeps the whole host set", () => {
376 const storage = makeStorage();
377 const manyEntries = Array.from({ length: 120 }, (_, index) => entry({ number: index + 1 }));
378 const viewers = { "github.com": "Bilal", "gitlab.com": "Bilal" };
379 const providers = [
380 {
381 host: "github.com",
382 kind: "github",
383 searchesOnHost: true,
384 projectCount: 1,
385 configured: true,
386 detail: null,
387 },
388 {
389 host: "gitlab.com",
390 kind: "gitlab",
391 searchesOnHost: true,
392 projectCount: 1,
393 configured: true,
394 detail: null,
395 },
396 ];
397 const accumulated = {
398 entries: manyEntries,
399 viewers,
400 providers,
401 errors: [],
402 truncated: true,
403 nextCursors: {},
404 } as never;
405 writePullRequestListSnapshot(storage, "env-1", { scope: "s", data: accumulated });
406 const snapshot = readPullRequestListSnapshot(storage, "env-1");
407 expect(snapshot?.data.entries).toHaveLength(99);
408 expect(snapshot?.data.viewers).toEqual(viewers);
409 expect(snapshot?.data.providers).toEqual(providers);
410 });
411});
412
413describe("remembered pull request list controls", () => {
414 const makeStorage = () => {
415 const held = new Map<string, string>();
416 return {
417 getItem: (key: string) => held.get(key) ?? null,
418 setItem: (key: string, value: string) => void held.set(key, value),
419 };
420 };
421
422 // regression: #9188 corrupt stored list controls broke the page
423 it("falls back to the default controls when storage is corrupt", () => {
424 const storage = makeStorage();
425 storage.setItem("t3.pullRequests.preferences", "{not json");
426 expect(readPullRequestListPreferences(storage)).toEqual({ involvement: "all", state: "open" });
427 });
428
429 it("falls back when browser policy denies storage", () => {
430 const denied = {
431 getItem: () => {
432 throw new Error("storage denied");
433 },
434 setItem: () => {
435 throw new Error("storage denied");
436 },
437 };
438 expect(readPullRequestListPreferences(denied)).toEqual({ involvement: "all", state: "open" });
439 expect(() =>
440 writePullRequestListPreferences({ involvement: "all", state: "open" }, denied),
441 ).not.toThrow();
442 });
443});
444
445const ENV_1 = "env-1" as EnvironmentId;
446const ENV_2 = "env-2" as EnvironmentId;
447
448describe("merging the environments' own listings", () => {
449 const answer = (
450 overrides: Partial<Parameters<typeof mergePullRequestLists>[0][number][1]> = {},
451 ) =>
452 ({
453 viewers: { "github.com": "Bilal" },
454 providers: [
455 {
456 host: "github.com",
457 kind: "github",
458 searchesOnHost: true,
459 projectCount: 1,
460 configured: true,
461 detail: null,
462 },
463 ],
464 entries: [],
465 errors: [],
466 truncated: false,
467 nextCursors: {},
468 ...overrides,
469 }) as Parameters<typeof mergePullRequestLists>[0][number][1];
470
471 it("tags every row with the environment that read it, newest first", () => {
472 const merged = mergePullRequestLists([
473 [ENV_1, answer({ entries: [entry({ number: 1, updatedAt: "2026-07-01T00:00:00Z" })] })],
474 [ENV_2, answer({ entries: [entry({ number: 2, updatedAt: "2026-08-01T00:00:00Z" })] })],
475 ]);
476 expect(merged?.entries.map((row) => [row.environmentId, row.number])).toEqual([
477 [ENV_2, 2],
478 [ENV_1, 1],
479 ]);
480 });
481
482 // regression: #6490 one environment's error showed on every environment
483 it("keeps project errors scoped to the environment that reported them", () => {
484 const error = {
485 projectId: "project-1" as ProjectId,
486 projectTitle: "Web",
487 message: "Not signed in",
488 } as const;
489 const merged = mergePullRequestLists([
490 [ENV_1, answer({ errors: [error] })],
491 [ENV_2, answer()],
492 ]);
493
494 expect(merged?.errors).toEqual([{ ...error, environmentId: ENV_1 }]);
495 });
496
497 it("folds a host reached from two environments into one switcher row", () => {
498 const merged = mergePullRequestLists([
499 [ENV_1, answer()],
500 [
501 ENV_2,
502 answer({
503 providers: [
504 {
505 host: "github.com",
506 kind: SourceControlProviderKind.make("github"),
507 searchesOnHost: false,
508 projectCount: 2,
509 configured: false,
510 detail: "Not signed in.",
511 },
512 ],
513 }),
514 ],
515 ]);
516 expect(merged?.providers).toEqual([
517 {
518 host: "github.com",
519 kind: "github",
520 searchesOnHost: false,
521 projectCount: 3,
522 configured: true,
523 detail: null,
524 },
525 ]);
526 });
527});
528
529describe('who "I" am, per server', () => {
530 const answer = (viewers: Record<string, string>, entries: ReadonlyArray<PullRequestListEntry>) =>
531 ({
532 viewers,
533 providers: [],
534 entries,
535 errors: [],
536 truncated: false,
537 nextCursors: {},
538 }) as Parameters<typeof mergePullRequestLists>[0][number][1];
539 const byBilal = { login: "Bilal", name: null, avatarUrl: null };
540
541 it("does not let one server's account decide who authored another server's rows", () => {
542 const merged = mergePullRequestLists([
543 [ENV_1, answer({ "github.com": "Bilal" }, [entry({ number: 1, author: byBilal })])],
544 [ENV_2, answer({ "github.com": "Octocat" }, [entry({ number: 2, author: byBilal })])],
545 ])!;
546
547 const groups = groupPullRequestsByInvolvement(merged.entries, merged.viewers);
548 expect(
549 groups.find((group) => group.key === "authored")?.entries.map((row) => row.number),
550 ).toEqual([1]);
551 expect(
552 filterPullRequestsByInvolvement(merged.entries, merged.viewers, "authored").map(
553 (row) => row.number,
554 ),
555 ).toEqual([1]);
556 });
557});
558
559describe("the project an id names", () => {
560 const projects = [
561 { id: "project-1", environmentId: ENV_1 },
562 { id: "project-1", environmentId: ENV_2 },
563 { id: "project-2", environmentId: ENV_2 },
564 ];
565
566 it("answers a bare id only where one server has it", () => {
567 expect(findScopedProject(projects, null, "project-2")?.environmentId).toBe(ENV_2);
568 expect(findScopedProject(projects, null, "project-1")).toBeUndefined();
569 });
570});
571
572describe("which environments a listing should ask", () => {
573 const ENV_3 = "env-3" as EnvironmentId;
574 const environmentIds = [ENV_1, ENV_2, ENV_3];
575
576 it("asks only the environments that actually hold an ambiguous bare id, never a third that doesn't", () => {
577 const projects = [
578 { id: "project-1", environmentId: ENV_1 },
579 { id: "project-1", environmentId: ENV_2 },
580 ];
581 expect(
582 resolveQueryEnvironmentIds(environmentIds, projects, undefined, "project-1", true),
583 ).toEqual([ENV_1, ENV_2]);
584 });
585});
586
587describe("the server a saved selection names", () => {
588 const known = new Set([ENV_1, ENV_2]);
589
590 it("resolves to nothing yet for a server that is known but not ready, rather than falling back", () => {
591 expect(resolveSelectedEnvironmentId(ENV_2, known, ENV_1)).toBe(ENV_2);
592 });
593});
594
595describe("the priority groups against a paginated feed", () => {
596 const authoredRow = (number: number, updatedAt: string) =>
597 entry({ number, updatedAt, author: { login: "Bilal", name: null, avatarUrl: null } });
598
599 it("shows every authored row the host reported, not only the ones the feed page holds", () => {
600 const newest = authoredRow(6039, "2026-08-11T08:00:00Z");
601 const older = [
602 authoredRow(5499, "2026-08-10T17:00:00Z"),
603 authoredRow(5544, "2026-08-10T16:00:00Z"),
604 ];
605 const feed = [newest, entry({ number: 6123, updatedAt: "2026-08-11T09:00:00Z" })];
606
607 const groups = partitionPullRequestsWithPriority(feed, [newest, ...older], []);
608
609 expect(
610 groups.find((group) => group.key === "authored")?.entries.map((row) => row.number),
611 ).toEqual([6039, 5499, 5544]);
612 expect(
613 groups.find((group) => group.key === "others")?.entries.map((row) => row.number),
614 ).toEqual([6123]);
615 });
616});
617
618describe("pull request list overrides", () => {
619 const entry = (number: number, state: "open" | "closed" | "merged") =>
620 ({
621 host: "github.com",
622 repository: "pingdotgg/t3code",
623 number,
624 state,
625 isDraft: false,
626 updatedAt: "2026-07-01T00:00:00Z",
627 labels: [],
628 }) as unknown as PullRequestListEntry;
629 const key = (row: { number: number }) => `#${row.number}`;
630
631 it("writes the override over the row and drops it from a list whose state it left", () => {
632 const rows = [entry(1, "open"), entry(2, "open")];
633 const overrides = new Map([
634 ["#1", { state: "closed" as const, updatedAt: "2026-07-03T00:00:00Z", token: 1, at: 0 }],
635 ]);
636 expect(
637 applyPullRequestOverrides(rows, overrides, key, "open").map((row) => row.number),
638 ).toEqual([2]);
639 const all = applyPullRequestOverrides(rows, overrides, key, "all");
640 expect(all.map((row) => [row.number, row.state])).toEqual([
641 [1, "closed"],
642 [2, "open"],
643 ]);
644 expect(applyPullRequestOverrides(rows, new Map(), key, "open")).toBe(rows);
645 });
646
647 it("keeps a confirmed close hidden when its override is cleared", () => {
648 const at = 1_000_000;
649 const rows = [entry(1, "closed"), entry(2, "open"), entry(3, "merged")];
650 const overrides = new Map([
651 ["#1", { state: "closed" as const, updatedAt: "2026-07-03T00:00:00Z", token: 1, at }],
652 ]);
653 const settled = settlePullRequestOverrides(overrides, rows, key, at + 5_000);
654 expect(settled.size).toBe(0);
655 expect(applyPullRequestOverrides(rows, settled, key, "open").map((row) => row.number)).toEqual([
656 2,
657 ]);
658 });
659});
660
661describe("pull request list override settlement", () => {
662 const entry = (number: number, state: "open" | "closed" | "merged") =>
663 ({ number, state, isDraft: false, labels: [] }) as unknown as PullRequestListEntry;
664 const key = (row: { number: number }) => `#${row.number}`;
665
666 it("keeps an override until an answer agrees with it", () => {
667 const at = 1_000_000;
668 const closed = { state: "closed" as const, updatedAt: "2026-07-03T00:00:00Z", token: 1, at };
669 const overrides = new Map([["#1", closed]]);
670 expect(settlePullRequestOverrides(overrides, [entry(1, "open")], key, at + 5_000)).toBe(
671 overrides,
672 );
673 expect(settlePullRequestOverrides(overrides, [entry(2, "open")], key, at + 5_000).size).toBe(1);
674 expect(settlePullRequestOverrides(overrides, [entry(1, "closed")], key, at + 5_000).size).toBe(
675 0,
676 );
677 expect(settlePullRequestOverrides(overrides, [entry(1, "open")], key, at + 90_000).size).toBe(
678 0,
679 );
680 });
681
682 it("does not hand back the old order when only the order changed", () => {
683 const previous = [entry(1, "open"), entry(2, "open")];
684 const swapped = reusePullRequestEntries(previous, [entry(2, "open"), entry(1, "open")], key);
685 expect(swapped).not.toBe(previous);
686 expect(swapped.map((row) => row.number)).toEqual([2, 1]);
687 });
688});
689
690describe("pull request quick actions", () => {
691 const summary = (host: string, kind: string, actions?: ReadonlyArray<PullRequestAction>) => ({
692 host,
693 kind: SourceControlProviderKind.make(kind),
694 searchesOnHost: true,
695 projectCount: 1,
696 configured: true,
697 detail: null,
698 ...(actions === undefined ? {} : { actions }),
699 });
700
701 it("offers what each host reports, and GitHub's legacy set when a server reports none", () => {
702 const actionsOf = pullRequestHostActions([
703 summary("bitbucket.org", "bitbucket", ["merge", "close"]),
704 summary("github.com", "github"),
705 ]);
706 const bitbucket = actionsOf({
707 host: "bitbucket.org",
708 provider: SourceControlProviderKind.make("bitbucket"),
709 });
710 expect(pullRequestQuickActions({ state: "closed", isDraft: false }, bitbucket)).toEqual([]);
711 const github = actionsOf({
712 host: "github.com",
713 provider: SourceControlProviderKind.make("github"),
714 });
715 expect(pullRequestQuickActions({ state: "open", isDraft: true }, github)).toEqual([
716 "close",
717 "ready",
718 ]);
719 });
720});
721
722describe("refresh page size", () => {
723 const twoPagesAcrossFourRepositories = (provider: "github" | "gitlab") =>
724 ["a", "b", "c", "d"].flatMap((name) =>
725 Array.from({ length: 198 }, (_, index) =>
726 entry({
727 number: index + 1,
728 provider: SourceControlProviderKind.make(provider),
729 repository: `pingdotgg/${name}`,
730 }),
731 ),
732 );
733
734 it("asks each repository for its own rows where repositories are read one at a time", () => {
735 expect(largestPullRequestReadCount(twoPagesAcrossFourRepositories("gitlab"))).toBe(198);
736 });
737
738 // regression: #19403 refreshes were not bounded by each tracker read
739 it("asks for the whole host where one search reads every repository", () => {
740 expect(largestPullRequestReadCount(twoPagesAcrossFourRepositories("github"))).toBe(792);
741 });
742});
743
744describe("findProjectForRepository", () => {
745 it("resolves Azure bare names using matching project and environment scope", () => {
746 const azure = (id: string, environmentId: string, path: string) => ({
747 id,
748 environmentId,
749 repositoryIdentity: {
750 canonicalKey: `dev.azure.com/${path}`,
751 locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: "" },
752 provider: "azure-devops",
753 displayName: path,
754 owner: "contoso",
755 name: path.split("/").at(-1)!,
756 },
757 });
758 const payments = azure("p1", ENV_1, "contoso/payments/_git/api");
759 const billing = azure("p2", ENV_1, "contoso/billing/_git/api");
760 const remoteBilling = azure("p2", ENV_2, "contoso/billing/_git/api");
761 const unrelated = azure("p3", ENV_2, "contoso/billing/_git/web");
762 const projects = [payments, billing, remoteBilling, unrelated];
763
764 expect(findProjectForRepository(projects, { repository: "api" })).toBe(payments);
765 expect(
766 findProjectForRepository(projects, {
767 repository: "api",
768 scopedProjectId: "p2",
769 scopedEnvironmentId: ENV_2,
770 }),
771 ).toBe(remoteBilling);
772 expect(
773 findProjectForRepository(projects, {
774 repository: "api",
775 scopedProjectId: "p3",
776 scopedEnvironmentId: ENV_2,
777 }),
778 ).toBe(payments);
779 });
780
781 it("keeps repository matches on the selected environment when project IDs overlap", () => {
782 const project = (id: string, environmentId: string, repository: string) => ({
783 id,
784 environmentId,
785 repositoryIdentity: {
786 canonicalKey: `github.com/${repository}`,
787 locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl: "" },
788 provider: "github",
789 displayName: repository,
790 },
791 });
792 const unrelated = project("p1", ENV_1, "acme/web");
793 const local = project("p2", ENV_1, "acme/api");
794 const remote = project("p1", ENV_2, "acme/api");
795 const projects = [unrelated, local, remote];
796
797 expect(findProjectForRepository(projects, { repository: "acme/api" })).toBe(local);
798 expect(
799 findProjectForRepository(projects, {
800 repository: "acme/api",
801 selectedEnvironmentId: ENV_2,
802 }),
803 ).toBe(remote);
804 expect(
805 findProjectForRepository(projects, {
806 repository: "acme/web",
807 selectedEnvironmentId: ENV_2,
808 }),
809 ).toBeUndefined();
810 expect(
811 findProjectForRepository(projects, {
812 repository: "acme/api",
813 selectedEnvironmentId: "env-connecting",
814 }),
815 ).toBeUndefined();
816 });
817});