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

use-thread-pr.test.ts

206 lines6.5 KB

1import {
2 ProjectId,
3 type PullRequestSummary,
4 SourceControlProviderKind,
5 type ThreadPullRequestLink,
6} from "@t3tools/contracts";
7import { reconcileThreadPullRequestSnapshots } from "@t3tools/client-runtime/state/pull-requests";
8import { describe, expect, it } from "vite-plus/test";
9​
10import {
11 knownPullRequestSummary,
12 presentThreadLinkedPullRequests,
13 rememberBounded,
14 resolveThreadPrSource,
15} from "./thread-pr-presentation";
16​
17function linkedPr(
18 number: number,
19 overrides: Partial<ThreadPullRequestLink> = {},
20): ThreadPullRequestLink {
21 return {
22 host: "github.com",
23 repository: "t3tools/t3code",
24 number,
25 url: `https://github.com/t3tools/t3code/pull/${number}`,
26 source: "manual",
27 linkedAt: "2026-09-08T00:00:00.000Z",
28 stack: null,
29 snapshot: {
30 state: "open",
31 title: `Change ${number}`,
32 headBranch: `change-${number}`,
33 baseBranch: "main",
34 isDraft: false,
35 updatedAt: null,
36 syncedAt: "2026-09-08T00:00:00.000Z",
37 },
38 ...overrides,
39 };
41​
42describe("presentThreadLinkedPullRequests", () => {
43 it("uses the top of a derived stack even when its bottom was linked later", () => {
44 const bottom = linkedPr(1, { linkedAt: "2026-09-09T00:00:00.000Z" });
45 const top = linkedPr(2);
46 expect(
47 presentThreadLinkedPullRequests([
48 bottom,
49 {
50 ...top,
51 snapshot: { ...top.snapshot!, baseBranch: "change-1" },
52 },
53 ]),
54 ).toMatchObject({ kind: "stack", label: "2", number: 2, url: top.url });
55 });
56​
57 it("names a single link with its host's term", () => {
58 expect(presentThreadLinkedPullRequests([linkedPr(7)])?.accessibilityLabel).toBe(
59 "#7 pull request open",
60 );
61 expect(
62 presentThreadLinkedPullRequests([
63 linkedPr(7, {
64 host: "gitlab.com",
65 repository: "t3tools/t3code",
66 url: "https://gitlab.com/t3tools/t3code/-/merge_requests/7",
67 }),
68 ])?.accessibilityLabel,
69 ).toBe("#7 merge request open");
70 });
71});
72​
73describe("linked pull requests reconciled with host answers", () => {
74 it("shows a merge answered after the link was saved, and never reopens a saved merge", () => {
75 const saved = [linkedPr(3)];
76 const merged = {
77 state: "merged" as const,
78 isDraft: false,
79 title: "Change 3",
80 updatedAt: "2026-09-10T00:00:00.000Z",
81 };
82 const reconciled = reconcileThreadPullRequestSnapshots(saved, () => merged);
83 expect(
84 resolveThreadPrSource(
85 { pullRequests: reconciled, linkedPullRequest: null, branchPullRequest: null },
86 true,
87 ),
88 ).toMatchObject({
89 linkedPresentation: { number: 3, state: "merged" },
90 });
91 expect(
92 reconcileThreadPullRequestSnapshots(reconciled, () => ({
93 ...merged,
94 state: "open",
95 updatedAt: "2026-09-11T00:00:00.000Z",
96 })),
97 ).toBe(reconciled);
98 });
99});
100​
101describe("resolveThreadPrSource compatibility", () => {
102 const legacyRef = {
103 projectId: ProjectId.make("project"),
104 repository: "t3tools/t3code",
105 number: 1,
106 url: "https://github.com/t3tools/t3code/pull/1",
107 };
108 const branchRef = { ...legacyRef, number: 2, url: "https://github.com/t3tools/t3code/pull/2" };
109​
110 it("polls the stored reference before the server config loads", () => {
111 expect(
112 resolveThreadPrSource(
113 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
114 false,
115 ),
116 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
117 });
118​
119 it("uses snapshots without polling once the server config loads", () => {
120 expect(
121 resolveThreadPrSource(
122 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
123 true,
124 ),
125 ).toMatchObject({ linkedPresentation: { number: 3 }, pullRequestRef: null });
126 });
127​
128 it("does not revive a removed modern link from the compatibility reference", () => {
129 expect(
130 resolveThreadPrSource(
131 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
132 true,
133 ),
134 ).toEqual({ linkedPresentation: null, pullRequestRef: branchRef });
135 });
136​
137 it("does not poll when a modern link is waiting for its first snapshot", () => {
138 expect(
139 resolveThreadPrSource(
140 {
141 pullRequests: [linkedPr(3, { snapshot: null })],
142 linkedPullRequest: legacyRef,
143 branchPullRequest: branchRef,
144 },
145 true,
146 ),
147 ).toMatchObject({ linkedPresentation: { number: 3, state: null }, pullRequestRef: null });
148 });
149});
150​
151describe("knownPullRequestSummary", () => {
152 it("seeds from the newest summary of the same pull request, never another's", () => {
153 const projectId = ProjectId.make("project");
154 const link = linkedPr(7);
155 const threads = [
156 {
157 projectId,
158 pullRequests: [
159 { ...link, snapshot: { ...link.snapshot!, updatedAt: "2026-09-08T00:00:00.000Z" } },
160 ],
161 },
162 ];
163 const listed: PullRequestSummary = {
164 provider: SourceControlProviderKind.make("github"),
165 projectId,
166 repository: "t3tools/t3code",
167 number: 7,
168 title: "Listed title",
169 url: "https://github.com/t3tools/t3code/pull/7",
170 state: "open",
171 headBranch: "change-7",
172 baseBranch: "main",
173 updatedAt: "2026-09-09T00:00:00.000Z",
174 };
175 const lookup = (host: string, number: number, summaries = [listed]) =>
176 knownPullRequestSummary({ host, repository: "t3tools/t3code", number }, threads, summaries);
177​
178 expect(lookup("github.com", 7, [])?.title).toBe("Change 7");
179 expect(lookup("github.com", 7)?.title).toBe("Listed title");
180 expect(lookup("github.com", 8)).toBeUndefined();
181 expect(lookup("gitlab.com", 7)).toBeUndefined();
182​
183 const sameVersion = { ...listed, updatedAt: "2026-09-08T00:00:00.000Z" };
184 const syncedAt = Date.parse(link.snapshot!.syncedAt);
185 expect(lookup("github.com", 7, [{ ...sameVersion, observedAt: syncedAt - 1 }])?.title).toBe(
186 "Change 7",
187 );
188 expect(lookup("github.com", 7, [{ ...sameVersion, observedAt: syncedAt + 1 }])?.title).toBe(
189 "Listed title",
190 );
191 });
192});
193​
194describe("rememberBounded", () => {
195 it("moves a re-answered entry to the newest slot and evicts the oldest past the limit", () => {
196 let snapshots: ReadonlyMap<string, number> = new Map();
197 snapshots = rememberBounded(snapshots, "a", 1, 2);
198 snapshots = rememberBounded(snapshots, "b", 2, 2);
199 snapshots = rememberBounded(snapshots, "a", 3, 2);
200 snapshots = rememberBounded(snapshots, "c", 4, 2);
201 expect([...snapshots]).toEqual([
202 ["a", 3],
203 ["c", 4],
204 ]);
205 });
206});