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

pullRequests.test.ts

470 lines17.4 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import {
3 EnvironmentId,
4 ProjectId,
5 type PullRequestListEntry,
6 type PullRequestListInput,
7 type PullRequestListResult,
8 type PullRequestSummary,
9} from "@t3tools/contracts";
10import { describe, expect, it, vi } from "vite-plus/test";
11​
12import {
13 newestPullRequestObservation,
14 observedPullRequestSummary,
15 observePullRequestChecks,
16 observePullRequestMetadata,
17 newestPullRequestSummary,
18 rememberPullRequestListAnswer,
19 restorePullRequestList,
20 restoredPullRequestList,
21 threadPullRequestLinkToSummary,
22} from "./pullRequests";
23​
24function summary(overrides: Partial<PullRequestSummary> = {}): PullRequestSummary {
25 return {
26 provider: SourceControlProviderKind.make("github"),
27 projectId: ProjectId.make("pull-request-cache-test"),
28 repository: "acme/widget",
29 number: 7,
30 title: "Improve widget",
31 url: "https://github.com/acme/widget/pull/7",
32 state: "open",
33 isDraft: false,
34 headBranch: "improve-widget",
35 baseBranch: "main",
36 updatedAt: "2026-09-10T00:00:00Z",
37 author: { login: "oliver", name: null, avatarUrl: null },
38 mergeability: "mergeable",
39 checksState: "passing",
40 ...overrides,
41 };
43​
44const observed = (value: PullRequestSummary, observedAt: number) => ({
45 summary: value,
46 observedAt,
47});
48​
49describe("pull request summary cache", () => {
50 it("orders same-dated snapshots by the server's read time, not by arrival", () => {
51 const stale = observed(summary({ observedAt: 200 }), 500);
52 const held = observed(
53 summary({ mergeability: "conflicting", checksState: "failing", observedAt: 300 }),
54 100,
55 );
56 expect(newestPullRequestObservation(stale, held)?.summary).toMatchObject({
57 mergeability: "conflicting",
58 checksState: "failing",
59 observedAt: 300,
60 });
61 expect(newestPullRequestObservation(held, stale)).toBe(held);
62 expect(newestPullRequestObservation(held, { ...held, observedAt: 900 })).toBe(held);
63 });
64​
65 it("uses arrival order only when neither snapshot carries a server read time", () => {
66 const first = observed(summary(), 100);
67 const later = observed(summary({ checksState: "failing" }), 200);
68 expect(newestPullRequestObservation(first, later)).toMatchObject({ observedAt: 200 });
69 expect(newestPullRequestObservation(later, first)).toBe(later);
70 const stamped = observed(summary({ observedAt: 50 }), 1);
71 expect(newestPullRequestObservation(later, stamped)?.summary.observedAt).toBe(50);
72 expect(newestPullRequestObservation(stamped, later)).toBe(stamped);
73 });
74​
75 it("keeps known status when a newer snapshot omits it, and clears it when told to", () => {
76 const list = observed(summary({ reviewDecision: "approved", observedAt: 100 }), 100);
77 const sparse = observed(
78 summary({ checksState: undefined, reviewDecision: undefined, observedAt: 200 }),
79 200,
80 );
81 expect(newestPullRequestObservation(list, sparse)?.summary).toMatchObject({
82 checksState: "passing",
83 reviewDecision: "approved",
84 });
85 const cleared = observed(summary({ checksState: null, observedAt: 300 }), 300);
86 expect(newestPullRequestObservation(list, cleared)?.summary.checksState).toBeNull();
87 });
88​
89 it("treats merged as final and otherwise prefers the later host update", () => {
90 const merged = summary({ state: "merged", updatedAt: "2026-09-01T00:00:00Z" });
91 const reopened = summary({ updatedAt: "2026-09-12T00:00:00Z", observedAt: 900 });
92 expect(newestPullRequestSummary(merged, reopened)).toBe(merged);
93 expect(newestPullRequestSummary(reopened, merged)).toBe(merged);
94 const older = summary({ updatedAt: "2026-09-11T00:00:00Z", observedAt: 999 });
95 expect(newestPullRequestSummary(older, reopened)).toBe(reopened);
96 expect(newestPullRequestSummary(reopened, older)).toBe(reopened);
97 });
98​
99 it("reuses saved thread state while detail is stale and lets newer detail replace it", () => {
100 const stale = summary();
101 const link = {
102 host: "github.com",
103 repository: stale.repository,
104 number: stale.number,
105 url: stale.url,
106 source: "manual" as const,
107 linkedAt: stale.updatedAt,
108 stack: null,
109 snapshot: {
110 ...stale,
111 state: "closed" as const,
112 isDraft: false,
113 updatedAt: "2026-09-11T00:00:00Z",
114 syncedAt: "2026-09-12T00:00:00Z",
115 },
116 };
117 const saved = threadPullRequestLinkToSummary(link, stale.projectId);
118 expect(newestPullRequestSummary(stale, saved)?.state).toBe("closed");
119 const fresh = summary({ isDraft: true, updatedAt: "2026-09-13T00:00:00Z" });
120 expect(newestPullRequestSummary(fresh, saved)).toBe(fresh);
121 expect(threadPullRequestLinkToSummary({ ...link, snapshot: null }, stale.projectId)).toBeNull();
122 expect(
123 threadPullRequestLinkToSummary(
124 { ...link, snapshot: { ...link.snapshot, updatedAt: null } },
125 stale.projectId,
126 ),
127 ).toBeNull();
128 });
129});
130​
131describe("pull request observations", () => {
132 const fromSummary = (value: PullRequestSummary, observedAt: number) =>
133 observePullRequestMetadata({ metadata: null, checks: null }, observed(value, observedAt));
134 const checksRead = (
135 checksState: "passing" | "failing" | "pending" | null,
136 readAt: number | undefined,
137 observedAt: number,
138 ) => ({ checksState, readAt, observedAt });
139​
140 it("keeps a newer list's mergeability when a checks read lands after it", () => {
141 const detail = summary({
142 mergeability: "conflicting",
143 checksState: "pending",
144 observedAt: 100,
145 });
146 let state = fromSummary(detail, 1);
147 state = observePullRequestMetadata(
148 state,
149 observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 200 }), 2),
150 );
151 state = observePullRequestChecks(state, checksRead("passing", 300, 3));
152 expect(observedPullRequestSummary(state)).toMatchObject({
153 mergeability: "mergeable",
154 checksState: "passing",
155 });
156 });
157​
158 it("keeps a newer checks read when an older list read lands after it", () => {
159 let state = fromSummary(
160 summary({ mergeability: "conflicting", checksState: "pending", observedAt: 100 }),
161 1,
162 );
163 state = observePullRequestChecks(state, checksRead("passing", 300, 2));
164 state = observePullRequestMetadata(
165 state,
166 observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 250 }), 3),
167 );
168 expect(observedPullRequestSummary(state)).toMatchObject({
169 mergeability: "mergeable",
170 checksState: "passing",
171 });
172 });
173​
174 it("lets a newer read's checks replace an older checks read", () => {
175 let state = fromSummary(summary({ checksState: "pending", observedAt: 100 }), 1);
176 state = observePullRequestChecks(state, checksRead("passing", 300, 2));
177 state = observePullRequestMetadata(
178 state,
179 observed(summary({ checksState: "failing", observedAt: 400 }), 3),
180 );
181 expect(observedPullRequestSummary(state)?.checksState).toBe("failing");
182 });
183​
184 it("orders checks by arrival when the server reports no read time", () => {
185 let state = fromSummary(summary({ checksState: "pending" }), 1);
186 state = observePullRequestChecks(state, checksRead("passing", undefined, 2));
187 expect(observedPullRequestSummary(state)?.checksState).toBe("passing");
188 state = observePullRequestChecks(state, checksRead("failing", undefined, 1));
189 expect(observedPullRequestSummary(state)?.checksState).toBe("passing");
190 });
191​
192 it("shows nothing until some read has described the pull request", () => {
193 const state = observePullRequestChecks(
194 { metadata: null, checks: null },
195 checksRead("failing", 1, 1),
196 );
197 expect(observedPullRequestSummary(state)).toBeNull();
198 });
199});
200​
201describe("pull request list answers across a reload", () => {
202 const makeStorage = () => {
203 const held = new Map<string, string>();
204 return {
205 held,
206 getItem: (key: string) => held.get(key) ?? null,
207 setItem: (key: string, value: string) => void held.set(key, value),
208 removeItem: (key: string) => void held.delete(key),
209 key: (index: number) => [...held.keys()][index] ?? null,
210 get length() {
211 return held.size;
212 },
213 };
214 };
215 const listEntry = (number: number, updatedAt = "2026-07-02T00:00:00Z") =>
216 ({
217 provider: SourceControlProviderKind.make("github"),
218 host: "github.com",
219 projectId: ProjectId.make("project-1"),
220 projectTitle: "t3code",
221 repository: "pingdotgg/t3code",
222 number,
223 title: `Pull request ${number}`,
224 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
225 author: { login: "octocat", name: null, avatarUrl: null },
226 headBranch: `feat/branch-${number}`,
227 baseBranch: "main",
228 state: "open",
229 isDraft: false,
230 mergeability: "mergeable",
231 additions: 1,
232 deletions: 0,
233 createdAt: "2026-07-01T00:00:00Z",
234 updatedAt,
235 viewerReviewRequested: false,
236 labels: [],
237 }) as PullRequestListEntry;
238 const answer = (entries: ReadonlyArray<PullRequestListEntry>, host = "github.com") =>
239 ({
240 entries,
241 viewers: { [host]: "Bilal" },
242 providers: [
243 {
244 host,
245 kind: SourceControlProviderKind.make("github"),
246 searchesOnHost: true,
247 projectCount: 1,
248 configured: true,
249 detail: null,
250 },
251 ],
252 errors: [{ projectId: ProjectId.make("project-1"), projectTitle: "t3code", message: "boom" }],
253 truncated: true,
254 nextCursors: { "pingdotgg/t3code": "cursor-1" },
255 }) as PullRequestListResult;
256 const local = EnvironmentId.make("env-local");
257 const remote = EnvironmentId.make("env-remote");
258 const input: PullRequestListInput = { state: "open", involvement: "all", limit: 99 };
259​
260 it("restores every environment's first page as one merged list without stale failures or cursors", () => {
261 const storage = makeStorage();
262 rememberPullRequestListAnswer(
263 storage,
264 { environmentId: local, input },
265 answer([listEntry(1, "2026-07-01T00:00:00Z")]),
266 );
267 rememberPullRequestListAnswer(
268 storage,
269 { environmentId: remote, input },
270 answer([listEntry(2, "2026-07-03T00:00:00Z")], "gitlab.com"),
271 );
272 const restored = restorePullRequestList(storage, [
273 { environmentId: local, input },
274 { environmentId: remote, input },
275 ]);
276 expect(restored?.entries.map((row) => [row.environmentId, row.number])).toEqual([
277 [remote, 2],
278 [local, 1],
279 ]);
280 expect(restored?.viewers).toEqual({
281 "env-local github.com": "Bilal",
282 "env-remote gitlab.com": "Bilal",
283 });
284 expect(restored?.providers.map((provider) => provider.host)).toEqual([
285 "github.com",
286 "gitlab.com",
287 ]);
288 expect(restored?.errors).toEqual([]);
289 expect(restored?.nextCursors).toEqual({});
290 });
291​
292 it("falls back to a fresh read when the browser blocks storage", () => {
293 vi.stubGlobal("window", {
294 get localStorage(): Storage {
295 throw new DOMException("The operation is insecure.", "SecurityError");
296 },
297 });
298 try {
299 expect(restoredPullRequestList([{ environmentId: local, input }])).toBeNull();
300 } finally {
301 vi.unstubAllGlobals();
302 }
303 });
304​
305 it("keeps a page of rows and only first-page reads", () => {
306 const storage = makeStorage();
307 const rows = Array.from({ length: 120 }, (_, index) => listEntry(index + 1));
308 rememberPullRequestListAnswer(storage, { environmentId: local, input }, answer(rows));
309 const searched = { ...input, query: "fix" };
310 rememberPullRequestListAnswer(storage, { environmentId: local, input: searched }, answer(rows));
311 const continued = { ...input, cursors: { "pingdotgg/t3code": "cursor-1" } };
312 rememberPullRequestListAnswer(
313 storage,
314 { environmentId: local, input: continued },
315 answer(rows),
316 );
317 expect(
318 restorePullRequestList(storage, [{ environmentId: local, input }])?.entries,
319 ).toHaveLength(99);
320 expect(restorePullRequestList(storage, [{ environmentId: local, input: searched }])).toBeNull();
321 expect(
322 restorePullRequestList(storage, [{ environmentId: local, input: continued }]),
323 ).toBeNull();
324 });
325​
326 it("restores answers kept under a project assignment for a cold start that has none yet", () => {
327 const storage = makeStorage();
328 rememberPullRequestListAnswer(
329 storage,
330 {
331 environmentId: local,
332 input: { ...input, projectIds: [ProjectId.make("project-1")] },
333 },
334 answer([listEntry(1)]),
335 );
336 rememberPullRequestListAnswer(
337 storage,
338 {
339 environmentId: remote,
340 input: { ...input, projectIds: [ProjectId.make("project-2")] },
341 },
342 answer([listEntry(2, "2026-07-03T00:00:00Z")]),
343 );
344 const restored = restorePullRequestList(storage, [
345 { environmentId: local, input },
346 { environmentId: remote, input },
347 ]);
348 expect(restored?.entries.map((row) => [row.environmentId, row.number])).toEqual([
349 [remote, 2],
350 [local, 1],
351 ]);
352 });
353​
354 it("shows a pull request once when answers from before a reassignment overlap", () => {
355 const storage = makeStorage();
356 rememberPullRequestListAnswer(
357 storage,
358 { environmentId: local, input },
359 answer([listEntry(1, "2026-07-01T00:00:00Z")]),
360 );
361 rememberPullRequestListAnswer(
362 storage,
363 { environmentId: remote, input },
364 answer([listEntry(1, "2026-07-03T00:00:00Z")]),
365 );
366 const restored = restorePullRequestList(storage, [
367 { environmentId: local, input },
368 { environmentId: remote, input },
369 ]);
370 expect(restored?.entries.map((row) => [row.environmentId, row.updatedAt])).toEqual([
371 [remote, "2026-07-03T00:00:00Z"],
372 ]);
373 });
374​
375 it("keeps a shared repository's rows when it moves to an environment that has not answered yet", () => {
376 const storage = makeStorage();
377 const own = ProjectId.make("project-own");
378 const shared = { ...listEntry(2), projectId: ProjectId.make("project-shared") };
379 rememberPullRequestListAnswer(
380 storage,
381 { environmentId: local, input },
382 answer([{ ...listEntry(1), projectId: own }, shared]),
383 );
384 rememberPullRequestListAnswer(
385 storage,
386 { environmentId: remote, input },
387 answer([listEntry(3, "2026-07-03T00:00:00Z")]),
388 );
389 // The shared repository now belongs to the remote environment, which has not answered yet.
390 rememberPullRequestListAnswer(
391 storage,
392 { environmentId: local, input: { ...input, projectIds: [own] } },
393 answer([{ ...listEntry(1), projectId: own }]),
394 );
395 const restored = restorePullRequestList(storage, [
396 { environmentId: local, input },
397 { environmentId: remote, input },
398 ]);
399 expect(restored?.entries.map((row) => row.number).toSorted()).toEqual([1, 2, 3]);
400 });
401​
402 it("keeps one page per environment when retained rows join a partial answer", () => {
403 const storage = makeStorage();
404 const own = ProjectId.make("project-own");
405 const shared = ProjectId.make("project-shared");
406 const rows = (projectId: ProjectId, first: number) =>
407 Array.from({ length: 99 }, (_, index) => ({ ...listEntry(first + index), projectId }));
408 rememberPullRequestListAnswer(
409 storage,
410 { environmentId: local, input },
411 answer(rows(shared, 1)),
412 );
413 rememberPullRequestListAnswer(
414 storage,
415 { environmentId: local, input: { ...input, projectIds: [own] } },
416 answer(rows(own, 101)),
417 );
418 const restored = restorePullRequestList(storage, [{ environmentId: local, input }]);
419 expect(restored?.entries).toHaveLength(99);
420 expect(restored?.entries.every((row) => row.projectId === own)).toBe(true);
421 });
422​
423 it("does not keep grown pages", () => {
424 const storage = makeStorage();
425 const grown = { ...input, limit: 198 };
426 rememberPullRequestListAnswer(storage, { environmentId: local, input: grown }, answer([]));
427 expect(restorePullRequestList(storage, [{ environmentId: local, input: grown }])).toBeNull();
428 });
429​
430 it("forgets whole scopes, never one environment of a scope that is kept", () => {
431 const storage = makeStorage();
432 const scoped = (projectIndex: number) => ({
433 ...input,
434 projectId: ProjectId.make(`p-${projectIndex}`),
435 });
436 for (let projectIndex = 0; projectIndex < 9; projectIndex++) {
437 for (const [offset, environmentId] of [local, remote].entries()) {
438 rememberPullRequestListAnswer(
439 storage,
440 { environmentId, input: scoped(projectIndex) },
441 answer([listEntry(projectIndex * 2 + offset + 1, `2026-07-0${2 - offset}T00:00:00Z`)]),
442 );
443 }
444 }
445 const restore = (projectIndex: number) =>
446 restorePullRequestList(storage, [
447 { environmentId: local, input: scoped(projectIndex) },
448 { environmentId: remote, input: scoped(projectIndex) },
449 ]);
450 expect(restore(0)).toBeNull();
451 for (let projectIndex = 1; projectIndex < 9; projectIndex++) {
452 expect(restore(projectIndex)?.entries.map((row) => row.environmentId)).toEqual([
453 local,
454 remote,
455 ]);
456 }
457 });
458​
459 it("removes the composite snapshots it replaced", () => {
460 const storage = makeStorage();
461 storage.held.set("t3.pullRequests.list:env-local,env-remote", "{}");
462 storage.held.set("t3.pullRequests.list:env-local", "{}");
463 storage.held.set("t3.other", "kept");
464 rememberPullRequestListAnswer(storage, { environmentId: local, input }, answer([]));
465 expect(
466 [...storage.held.keys()].filter((key) => key.startsWith("t3.pullRequests.list:")),
467 ).toEqual([]);
468 expect(storage.held.get("t3.other")).toBe("kept");
469 });
470});