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