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

pullRequestList.test.ts

348 lines11.8 KB

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