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 {
6 newestPullRequestObservation,
7 observedPullRequestSummary,
8 observePullRequestChecks,
9 observePullRequestMetadata,
10 newestPullRequestSummary,
11 threadPullRequestLinkToSummary,
12} from "./pullRequests";
13
14function summary(overrides: Partial<PullRequestSummary> = {}): PullRequestSummary {
15 return {
16 provider: SourceControlProviderKind.make("github"),
17 projectId: ProjectId.make("pull-request-cache-test"),
18 repository: "acme/widget",
19 number: 7,
20 title: "Improve widget",
21 url: "https://github.com/acme/widget/pull/7",
22 state: "open",
23 isDraft: false,
24 headBranch: "improve-widget",
25 baseBranch: "main",
26 updatedAt: "2026-09-10T00:00:00Z",
27 author: { login: "oliver", name: null, avatarUrl: null },
28 mergeability: "mergeable",
29 checksState: "passing",
30 ...overrides,
31 };
32}
33
34const observed = (value: PullRequestSummary, observedAt: number) => ({
35 summary: value,
36 observedAt,
37});
38
39describe("pull request summary cache", () => {
40 it("orders same-dated snapshots by the server's read time, not by arrival", () => {
41 const stale = observed(summary({ observedAt: 200 }), 500);
42 const held = observed(
43 summary({ mergeability: "conflicting", checksState: "failing", observedAt: 300 }),
44 100,
45 );
46 expect(newestPullRequestObservation(stale, held)?.summary).toMatchObject({
47 mergeability: "conflicting",
48 checksState: "failing",
49 observedAt: 300,
50 });
51 expect(newestPullRequestObservation(held, stale)).toBe(held);
52 expect(newestPullRequestObservation(held, { ...held, observedAt: 900 })).toBe(held);
53 });
54
55 it("uses arrival order only when neither snapshot carries a server read time", () => {
56 const first = observed(summary(), 100);
57 const later = observed(summary({ checksState: "failing" }), 200);
58 expect(newestPullRequestObservation(first, later)).toMatchObject({ observedAt: 200 });
59 expect(newestPullRequestObservation(later, first)).toBe(later);
60 const stamped = observed(summary({ observedAt: 50 }), 1);
61 expect(newestPullRequestObservation(later, stamped)?.summary.observedAt).toBe(50);
62 expect(newestPullRequestObservation(stamped, later)).toBe(stamped);
63 });
64
65 it("keeps known status when a newer snapshot omits it, and clears it when told to", () => {
66 const list = observed(summary({ reviewDecision: "approved", observedAt: 100 }), 100);
67 const sparse = observed(
68 summary({ checksState: undefined, reviewDecision: undefined, observedAt: 200 }),
69 200,
70 );
71 expect(newestPullRequestObservation(list, sparse)?.summary).toMatchObject({
72 checksState: "passing",
73 reviewDecision: "approved",
74 });
75 const cleared = observed(summary({ checksState: null, observedAt: 300 }), 300);
76 expect(newestPullRequestObservation(list, cleared)?.summary.checksState).toBeNull();
77 });
78
79 it("treats merged as final and otherwise prefers the later host update", () => {
80 const merged = summary({ state: "merged", updatedAt: "2026-09-01T00:00:00Z" });
81 const reopened = summary({ updatedAt: "2026-09-12T00:00:00Z", observedAt: 900 });
82 expect(newestPullRequestSummary(merged, reopened)).toBe(merged);
83 expect(newestPullRequestSummary(reopened, merged)).toBe(merged);
84 const older = summary({ updatedAt: "2026-09-11T00:00:00Z", observedAt: 999 });
85 expect(newestPullRequestSummary(older, reopened)).toBe(reopened);
86 expect(newestPullRequestSummary(reopened, older)).toBe(reopened);
87 });
88
89 it("reuses saved thread state while detail is stale and lets newer detail replace it", () => {
90 const stale = summary();
91 const link = {
92 host: "github.com",
93 repository: stale.repository,
94 number: stale.number,
95 url: stale.url,
96 source: "manual" as const,
97 linkedAt: stale.updatedAt,
98 stack: null,
99 snapshot: {
100 ...stale,
101 state: "closed" as const,
102 isDraft: false,
103 updatedAt: "2026-09-11T00:00:00Z",
104 syncedAt: "2026-09-12T00:00:00Z",
105 },
106 };
107 const saved = threadPullRequestLinkToSummary(link, stale.projectId);
108 expect(newestPullRequestSummary(stale, saved)?.state).toBe("closed");
109 const fresh = summary({ isDraft: true, updatedAt: "2026-09-13T00:00:00Z" });
110 expect(newestPullRequestSummary(fresh, saved)).toBe(fresh);
111 expect(threadPullRequestLinkToSummary({ ...link, snapshot: null }, stale.projectId)).toBeNull();
112 expect(
113 threadPullRequestLinkToSummary(
114 { ...link, snapshot: { ...link.snapshot, updatedAt: null } },
115 stale.projectId,
116 ),
117 ).toBeNull();
118 });
119});
120
121describe("pull request observations", () => {
122 const fromSummary = (value: PullRequestSummary, observedAt: number) =>
123 observePullRequestMetadata({ metadata: null, checks: null }, observed(value, observedAt));
124 const checksRead = (
125 checksState: "passing" | "failing" | "pending" | null,
126 readAt: number | undefined,
127 observedAt: number,
128 ) => ({ checksState, readAt, observedAt });
129
130 it("keeps a newer list's mergeability when a checks read lands after it", () => {
131 const detail = summary({
132 mergeability: "conflicting",
133 checksState: "pending",
134 observedAt: 100,
135 });
136 let state = fromSummary(detail, 1);
137 state = observePullRequestMetadata(
138 state,
139 observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 200 }), 2),
140 );
141 state = observePullRequestChecks(state, checksRead("passing", 300, 3));
142 expect(observedPullRequestSummary(state)).toMatchObject({
143 mergeability: "mergeable",
144 checksState: "passing",
145 });
146 });
147
148 it("keeps a newer checks read when an older list read lands after it", () => {
149 let state = fromSummary(
150 summary({ mergeability: "conflicting", checksState: "pending", observedAt: 100 }),
151 1,
152 );
153 state = observePullRequestChecks(state, checksRead("passing", 300, 2));
154 state = observePullRequestMetadata(
155 state,
156 observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 250 }), 3),
157 );
158 expect(observedPullRequestSummary(state)).toMatchObject({
159 mergeability: "mergeable",
160 checksState: "passing",
161 });
162 });
163
164 it("lets a newer read's checks replace an older checks read", () => {
165 let state = fromSummary(summary({ checksState: "pending", observedAt: 100 }), 1);
166 state = observePullRequestChecks(state, checksRead("passing", 300, 2));
167 state = observePullRequestMetadata(
168 state,
169 observed(summary({ checksState: "failing", observedAt: 400 }), 3),
170 );
171 expect(observedPullRequestSummary(state)?.checksState).toBe("failing");
172 });
173
174 it("orders checks by arrival when the server reports no read time", () => {
175 let state = fromSummary(summary({ checksState: "pending" }), 1);
176 state = observePullRequestChecks(state, checksRead("passing", undefined, 2));
177 expect(observedPullRequestSummary(state)?.checksState).toBe("passing");
178 state = observePullRequestChecks(state, checksRead("failing", undefined, 1));
179 expect(observedPullRequestSummary(state)?.checksState).toBe("passing");
180 });
181
182 it("shows nothing until some read has described the pull request", () => {
183 const state = observePullRequestChecks(
184 { metadata: null, checks: null },
185 checksRead("failing", 1, 1),
186 );
187 expect(observedPullRequestSummary(state)).toBeNull();
188 });
189});