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

pullRequests.test.ts

116 lines4.6 KB

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 newestPullRequestSummary,
8 threadPullRequestLinkToSummary,
9} from "./pullRequests";
10​
11function summary(overrides: Partial<PullRequestSummary> = {}): PullRequestSummary {
12 return {
13 provider: SourceControlProviderKind.make("github"),
14 projectId: ProjectId.make("pull-request-cache-test"),
15 repository: "acme/widget",
16 number: 7,
17 title: "Improve widget",
18 url: "https://github.com/acme/widget/pull/7",
19 state: "open",
20 isDraft: false,
21 headBranch: "improve-widget",
22 baseBranch: "main",
23 updatedAt: "2026-09-10T00:00:00Z",
24 author: { login: "oliver", name: null, avatarUrl: null },
25 mergeability: "mergeable",
26 checksState: "passing",
27 ...overrides,
28 };
30​
31const observed = (value: PullRequestSummary, observedAt: number) => ({
32 summary: value,
33 observedAt,
34});
35​
36describe("pull request summary cache", () => {
37 it("orders same-dated snapshots by the server's read time, not by arrival", () => {
38 const stale = observed(summary({ observedAt: 200 }), 500);
39 const held = observed(
40 summary({ mergeability: "conflicting", checksState: "failing", observedAt: 300 }),
41 100,
42 );
43 expect(newestPullRequestObservation(stale, held)?.summary).toMatchObject({
44 mergeability: "conflicting",
45 checksState: "failing",
46 observedAt: 300,
47 });
48 expect(newestPullRequestObservation(held, stale)).toBe(held);
49 expect(newestPullRequestObservation(held, { ...held, observedAt: 900 })).toBe(held);
50 });
51​
52 it("uses arrival order only when neither snapshot carries a server read time", () => {
53 const first = observed(summary(), 100);
54 const later = observed(summary({ checksState: "failing" }), 200);
55 expect(newestPullRequestObservation(first, later)).toMatchObject({ observedAt: 200 });
56 expect(newestPullRequestObservation(later, first)).toBe(later);
57 const stamped = observed(summary({ observedAt: 50 }), 1);
58 expect(newestPullRequestObservation(later, stamped)?.summary.observedAt).toBe(50);
59 expect(newestPullRequestObservation(stamped, later)).toBe(stamped);
60 });
61​
62 it("keeps known status when a newer snapshot omits it, and clears it when told to", () => {
63 const list = observed(summary({ reviewDecision: "approved", observedAt: 100 }), 100);
64 const sparse = observed(
65 summary({ checksState: undefined, reviewDecision: undefined, observedAt: 200 }),
66 200,
67 );
68 expect(newestPullRequestObservation(list, sparse)?.summary).toMatchObject({
69 checksState: "passing",
70 reviewDecision: "approved",
71 });
72 const cleared = observed(summary({ checksState: null, observedAt: 300 }), 300);
73 expect(newestPullRequestObservation(list, cleared)?.summary.checksState).toBeNull();
74 });
75​
76 it("treats merged as final and otherwise prefers the later host update", () => {
77 const merged = summary({ state: "merged", updatedAt: "2026-09-01T00:00:00Z" });
78 const reopened = summary({ updatedAt: "2026-09-12T00:00:00Z", observedAt: 900 });
79 expect(newestPullRequestSummary(merged, reopened)).toBe(merged);
80 expect(newestPullRequestSummary(reopened, merged)).toBe(merged);
81 const older = summary({ updatedAt: "2026-09-11T00:00:00Z", observedAt: 999 });
82 expect(newestPullRequestSummary(older, reopened)).toBe(reopened);
83 expect(newestPullRequestSummary(reopened, older)).toBe(reopened);
84 });
85​
86 it("reuses saved thread state while detail is stale and lets newer detail replace it", () => {
87 const stale = summary();
88 const link = {
89 host: "github.com",
90 repository: stale.repository,
91 number: stale.number,
92 url: stale.url,
93 source: "manual" as const,
94 linkedAt: stale.updatedAt,
95 stack: null,
96 snapshot: {
97 ...stale,
98 state: "closed" as const,
99 isDraft: false,
100 updatedAt: "2026-09-11T00:00:00Z",
101 syncedAt: "2026-09-12T00:00:00Z",
102 },
103 };
104 const saved = threadPullRequestLinkToSummary(link, stale.projectId);
105 expect(newestPullRequestSummary(stale, saved)?.state).toBe("closed");
106 const fresh = summary({ isDraft: true, updatedAt: "2026-09-13T00:00:00Z" });
107 expect(newestPullRequestSummary(fresh, saved)).toBe(fresh);
108 expect(threadPullRequestLinkToSummary({ ...link, snapshot: null }, stale.projectId)).toBeNull();
109 expect(
110 threadPullRequestLinkToSummary(
111 { ...link, snapshot: { ...link.snapshot, updatedAt: null } },
112 stale.projectId,
113 ),
114 ).toBeNull();
115 });
116});