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