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