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

pullRequestList.logic.test.ts

825 lines28.2 KB

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