Open History to see the last commit
1import { ProjectId, type PullRequestSummary } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { newestPullRequestObservation, newestPullRequestSummary } from "./pullRequests";
5
6function summary(overrides: Partial<PullRequestSummary> = {}): PullRequestSummary {
7 return {
8 provider: "github",
9 projectId: ProjectId.make("pull-request-cache-test"),
10 repository: "acme/widget",
11 number: 7,
12 title: "Improve widget",
13 url: "https://github.com/acme/widget/pull/7",
14 state: "open",
15 isDraft: false,
16 headBranch: "improve-widget",
17 baseBranch: "main",
18 updatedAt: "2026-09-10T00:00:00Z",
19 author: { login: "oliver", name: null, avatarUrl: null },
20 mergeability: "mergeable",
21 checksState: "passing",
22 ...overrides,
23 };
24}
25
26const observed = (value: PullRequestSummary, observedAt: number) => ({
27 summary: value,
28 observedAt,
29});
30
31describe("pull request summary cache", () => {
32 it("orders same-dated snapshots by the server's read time, not by arrival", () => {
33 const stale = observed(summary({ observedAt: 200 }), 500);
34 const held = observed(
35 summary({ mergeability: "conflicting", checksState: "failing", observedAt: 300 }),
36 100,
37 );
38 expect(newestPullRequestObservation(stale, held)?.summary).toMatchObject({
39 mergeability: "conflicting",
40 checksState: "failing",
41 observedAt: 300,
42 });
43 // An older filtered or server-cached response finishing last must not roll status back.
44 expect(newestPullRequestObservation(held, stale)).toBe(held);
45 // The same read seen again is not a new observation.
46 expect(newestPullRequestObservation(held, { ...held, observedAt: 900 })).toBe(held);
47 });
48
49 it("uses arrival order only when neither snapshot carries a server read time", () => {
50 const first = observed(summary(), 100);
51 const later = observed(summary({ checksState: "failing" }), 200);
52 expect(newestPullRequestObservation(first, later)).toMatchObject({ observedAt: 200 });
53 expect(newestPullRequestObservation(later, first)).toBe(later);
54 // A stamped read beats an unstamped one regardless of which arrived last.
55 const stamped = observed(summary({ observedAt: 50 }), 1);
56 expect(newestPullRequestObservation(later, stamped)?.summary.observedAt).toBe(50);
57 expect(newestPullRequestObservation(stamped, later)).toBe(stamped);
58 });
59
60 it("keeps known status when a newer snapshot omits it, and clears it when told to", () => {
61 const list = observed(summary({ reviewDecision: "approved", observedAt: 100 }), 100);
62 const sparse = observed(
63 summary({ checksState: undefined, reviewDecision: undefined, observedAt: 200 }),
64 200,
65 );
66 expect(newestPullRequestObservation(list, sparse)?.summary).toMatchObject({
67 checksState: "passing",
68 reviewDecision: "approved",
69 });
70 const cleared = observed(summary({ checksState: null, observedAt: 300 }), 300);
71 expect(newestPullRequestObservation(list, cleared)?.summary.checksState).toBeNull();
72 });
73
74 it("treats merged as final and otherwise prefers the later host update", () => {
75 const merged = summary({ state: "merged", updatedAt: "2026-09-01T00:00:00Z" });
76 const reopened = summary({ updatedAt: "2026-09-12T00:00:00Z", observedAt: 900 });
77 expect(newestPullRequestSummary(merged, reopened)).toBe(merged);
78 expect(newestPullRequestSummary(reopened, merged)).toBe(merged);
79 const older = summary({ updatedAt: "2026-09-11T00:00:00Z", observedAt: 999 });
80 expect(newestPullRequestSummary(older, reopened)).toBe(reopened);
81 expect(newestPullRequestSummary(reopened, older)).toBe(reopened);
82 });
83});