Open History to see the last commit
1import {
2 ProjectId,
3 type ThreadPullRequestLink,
4 type ThreadPullRequestSnapshot,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7
8import {
9 legacyLinkedPullRequestOf,
10 legacyThreadPullRequestKey,
11 threadPullRequestSearchTerms,
12 resolveThreadCurrentPullRequest,
13 resolveThreadPullRequestChains,
14 resolveThreadPullRequestBadge,
15 threadPullRequestKeysEqual,
16} from "./threadPullRequests.ts";
17
18function snapshot(input: Partial<ThreadPullRequestSnapshot> = {}): ThreadPullRequestSnapshot {
19 return {
20 state: "open",
21 title: "Change",
22 headBranch: "feature",
23 baseBranch: "main",
24 isDraft: false,
25 updatedAt: "2026-01-01T00:00:00.000Z",
26 syncedAt: "2026-01-01T00:00:00.000Z",
27 ...input,
28 };
29}
30
31function link(
32 number: number,
33 input: Partial<Omit<ThreadPullRequestLink, "number">> = {},
34): ThreadPullRequestLink {
35 return {
36 host: "github.com",
37 repository: "pingdotgg/t3code",
38 number,
39 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
40 source: "manual",
41 linkedAt: `2026-01-01T00:00:${String(number).padStart(2, "0")}.000Z`,
42 snapshot: null,
43 stack: null,
44 ...input,
45 };
46}
47
48describe("threadPullRequestKeysEqual", () => {
49 it("ignores host and repository case", () => {
50 expect(
51 threadPullRequestKeysEqual(
52 { host: "GitHub.com", repository: "PingDotGG/t3code", number: 1 },
53 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
54 ),
55 ).toBe(true);
56 expect(
57 threadPullRequestKeysEqual(
58 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
59 { host: "gitlab.com", repository: "pingdotgg/t3code", number: 1 },
60 ),
61 ).toBe(false);
62 });
63});
64
65describe("resolveThreadCurrentPullRequest", () => {
66 it("returns null with no visible links", () => {
67 expect(resolveThreadCurrentPullRequest([])).toBeNull();
68 expect(resolveThreadCurrentPullRequest([link(1, { source: "stack-dismissed" })])).toBeNull();
69 });
70
71 it("treats an unsynced link as open", () => {
72 expect(resolveThreadCurrentPullRequest([link(1)])).toMatchObject({
73 kind: "single",
74 link: { number: 1 },
75 });
76 });
77
78 it("prefers the single open link over terminal ones", () => {
79 const current = resolveThreadCurrentPullRequest([
80 link(1, { snapshot: snapshot({ state: "merged" }) }),
81 link(2, { snapshot: snapshot({ state: "open" }) }),
82 link(3, { snapshot: snapshot({ state: "closed" }) }),
83 ]);
84 expect(current).toMatchObject({ kind: "single", link: { number: 2 } });
85 });
86
87 it("reports a stack when several links are open and puts the highest layer on top", () => {
88 const stack = {
89 kind: "native" as const,
90 id: "s1",
91 number: 1,
92 url: "https://github.com/pingdotgg/t3code/stacks/1",
93 base: "main",
94 layers: [
95 { number: 10, headBranch: "a", state: "open" as const },
96 { number: 11, headBranch: "b", state: "open" as const },
97 ],
98 };
99 const current = resolveThreadCurrentPullRequest([
100 link(11, { snapshot: snapshot(), stack }),
101 link(10, { snapshot: snapshot(), stack }),
102 ]);
103 expect(current).toMatchObject({ kind: "stack", top: { number: 11 } });
104 if (current?.kind === "stack") {
105 expect(current.open.map((entry) => entry.number)).toEqual([11, 10]);
106 }
107 });
108
109 it("orders an open set without stack data by most recent link", () => {
110 const current = resolveThreadCurrentPullRequest([link(1), link(2)]);
111 expect(current).toMatchObject({ kind: "stack", top: { number: 2 } });
112 });
113
114 it("falls back to the most recently updated terminal link", () => {
115 const current = resolveThreadCurrentPullRequest([
116 link(1, { snapshot: snapshot({ state: "merged", updatedAt: "2026-01-03T00:00:00.000Z" }) }),
117 link(2, { snapshot: snapshot({ state: "closed", updatedAt: "2026-01-02T00:00:00.000Z" }) }),
118 ]);
119 expect(current).toMatchObject({ kind: "single", link: { number: 1 } });
120 });
121});
122
123describe("legacyLinkedPullRequestOf", () => {
124 const identity = {
125 canonicalKey: "github.com/pingdotgg/t3code",
126 provider: "github",
127 displayName: "pingdotgg/t3code",
128 locator: {
129 source: "git-remote" as const,
130 remoteName: "origin",
131 remoteUrl: "https://github.com/pingdotgg/t3code.git",
132 },
133 };
134 it("projects only links the owning project can route without a host", () => {
135 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, identity)).toEqual({
136 projectId: "project-1",
137 repository: "pingdotgg/t3code",
138 number: 7,
139 url: "https://github.com/pingdotgg/t3code/pull/7",
140 });
141 expect(legacyLinkedPullRequestOf([], "project-1" as never, identity)).toBeNull();
142 });
143 it.each([
144 {
145 host: "github.enterprise.test",
146 url: "https://github.enterprise.test/pingdotgg/t3code/pull/7",
147 },
148 { repository: "acme/other", url: "https://github.com/acme/other/pull/7" },
149 ])("omits an unsafe legacy route %j", (foreign) => {
150 expect(
151 legacyLinkedPullRequestOf([link(7, foreign)], "project-1" as never, identity),
152 ).toBeNull();
153 expect(
154 legacyLinkedPullRequestOf([link(7, foreign), link(8)], "project-1" as never, identity)
155 ?.number,
156 ).toBe(8);
157 });
158 it.each([
159 "dev.azure.com/org-a/project/_git/web",
160 "ssh.dev.azure.com/v3/org-a/project/web",
161 "org-a.visualstudio.com/DefaultCollection/project/_git/web",
162 ])("projects Azure links through the legacy selector for %s", (canonicalKey) => {
163 const azureIdentity = {
164 ...identity,
165 provider: "azure-devops",
166 canonicalKey,
167 displayName: canonicalKey.slice(canonicalKey.indexOf("/") + 1),
168 name: "web",
169 };
170 const own = link(7, {
171 host: "dev.azure.com",
172 repository: "org-a/project/_git/web",
173 url: "https://dev.azure.com/org-a/project/_git/web/pullrequest/7",
174 });
175 const foreign = link(7, {
176 host: "dev.azure.com",
177 repository: "web",
178 url: "https://dev.azure.com/org-b/project/_git/web/pullrequest/7",
179 linkedAt: "2026-01-02T00:00:00.000Z",
180 });
181 for (const repository of ["web", "org-a/project/_git/web"]) {
182 expect(
183 legacyLinkedPullRequestOf(
184 [{ ...own, repository }, foreign],
185 "project-1" as never,
186 azureIdentity,
187 ),
188 ).toEqual({ projectId: "project-1", repository: "web", number: 7, url: own.url });
189 }
190 expect(legacyLinkedPullRequestOf([foreign], "project-1" as never, azureIdentity)).toBeNull();
191 expect(legacyThreadPullRequestKey({ ...own, repository: "web" })).toEqual({
192 host: "dev.azure.com",
193 repository: "org-a/project/_git/web",
194 number: 7,
195 });
196 expect(
197 threadPullRequestKeysEqual(own, {
198 host: "org-a.visualstudio.com",
199 repository: "DefaultCollection/project/_git/web",
200 number: 7,
201 }),
202 ).toBe(true);
203 });
204 it("does not guess when the project identity is unavailable", () => {
205 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, null)).toBeNull();
206 });
207});
208
209describe("resolveThreadPullRequestChains", () => {
210 it("chains links by base → head within a repository, bottom to top", () => {
211 const chains = resolveThreadPullRequestChains([
212 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "b" }) }),
213 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
214 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
215 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
216 ]);
217 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
218 ["derived", [1, 2, 3]],
219 ["derived", [9]],
220 ]);
221 });
222
223 it("uses the native stack order when the host provides one", () => {
224 const stack = {
225 kind: "native" as const,
226 id: "s1",
227 number: 1,
228 url: "https://github.com/pingdotgg/t3code/stacks/1",
229 base: "main",
230 layers: [
231 { number: 5, headBranch: "a", state: "merged" as const },
232 { number: 6, headBranch: "b", state: "open" as const },
233 ],
234 };
235 const chains = resolveThreadPullRequestChains([
236 link(6, { snapshot: snapshot({ headBranch: "b", baseBranch: "main" }), stack }),
237 link(5, {
238 snapshot: snapshot({ state: "merged", headBranch: "a", baseBranch: "main" }),
239 stack,
240 }),
241 link(8),
242 ]);
243 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
244 ["native", [5, 6]],
245 ["derived", [8]],
246 ]);
247 });
248});
249
250describe("chain selection and badge state", () => {
251 it.each(["open", "merged", "closed"] as const)(
252 "targets the top of a derived %s chain despite a later bottom update and link",
253 (state) => {
254 const bottom = link(2, {
255 snapshot: snapshot({ state, headBranch: "base", updatedAt: "2026-02-01T00:00:00.000Z" }),
256 });
257 const top = link(1, {
258 snapshot: snapshot({ state, headBranch: "top", baseBranch: "base" }),
259 });
260 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject(
261 state === "open"
262 ? { kind: "stack", top: { number: 1 }, open: [top, bottom] }
263 : { kind: "single", link: { number: 1 } },
264 );
265 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
266 kind: "stack",
267 layers: 2,
268 state,
269 });
270 },
271 );
272
273 it("keeps the top native layer after completion and treats an unsynced layer as open", () => {
274 const stack = {
275 kind: "native" as const,
276 id: "native-1",
277 number: 1,
278 url: "https://github.com/pingdotgg/t3code/stacks/1",
279 base: "main",
280 layers: [
281 { number: 2, headBranch: "base", state: "merged" as const },
282 { number: 1, headBranch: "top", state: "merged" as const },
283 ],
284 };
285 const bottom = link(2, {
286 stack,
287 snapshot: snapshot({ state: "merged", updatedAt: "2026-02-01T00:00:00.000Z" }),
288 });
289 const top = link(1, { stack, snapshot: snapshot({ state: "merged" }) });
290 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject({
291 kind: "single",
292 link: { number: 1 },
293 });
294 expect(resolveThreadPullRequestBadge([bottom, { ...top, snapshot: null }])).toEqual({
295 kind: "stack",
296 layers: 2,
297 state: "open",
298 });
299 });
300
301 it("uses aggregate state and distinguishes unrelated work from a stack", () => {
302 const bottom = link(1, { snapshot: snapshot({ state: "merged", headBranch: "base" }) });
303 const top = link(2, { snapshot: snapshot({ state: "closed", baseBranch: "base" }) });
304 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
305 kind: "stack",
306 layers: 2,
307 state: "closed",
308 });
309 expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({
310 kind: "pull-request",
311 others: 2,
312 });
313 expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull();
314 });
315
316 it("keeps branch matching case-sensitive while ignoring repository case", () => {
317 const bottom = link(1, {
318 repository: "PingDotGG/T3code",
319 snapshot: snapshot({ headBranch: "Base" }),
320 });
321 const top = link(2, { snapshot: snapshot({ headBranch: "top", baseBranch: "base" }) });
322 expect(resolveThreadPullRequestChains([bottom, top])).toHaveLength(2);
323 expect(
324 resolveThreadPullRequestChains([
325 bottom,
326 { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) },
327 ])[0]?.layers,
328 ).toEqual([bottom, { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) }]);
329 });
330
331 it("preserves cyclic links without presenting a guessed stack order", () => {
332 const links = [
333 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "b" }) }),
334 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
335 ];
336 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
337 links.map((entry) => [entry]),
338 );
339 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
340 kind: "stack",
341 top: { number: 2 },
342 });
343 expect(resolveThreadPullRequestBadge(links)).toEqual({ kind: "pull-request", others: 1 });
344 });
345
346 it("does not guess a parent when a head branch was reused", () => {
347 const links = [
348 link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
349 link(2, { snapshot: snapshot({ headBranch: "reused" }) }),
350 link(3, { snapshot: snapshot({ headBranch: "top", baseBranch: "reused" }) }),
351 ];
352 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
353 links.map((entry) => [entry]),
354 );
355 });
356});
357
358describe("threadPullRequestSearchTerms", () => {
359 it("includes completed and unsynced links but excludes dismissed links", () => {
360 const terms = threadPullRequestSearchTerms({
361 pullRequests: [
362 link(12, { snapshot: snapshot({ title: "Fix login", state: "merged" }) }),
363 link(34),
364 link(56, { source: "stack-dismissed" }),
365 ],
366 });
367 expect(terms).toContain("#12");
368 expect(terms).toContain("pingdotgg/t3code#12");
369 expect(terms).toContain("https://github.com/pingdotgg/t3code/pull/12");
370 expect(terms).toContain("Fix login");
371 expect(terms).toContain("#34");
372 expect(terms.join(" ")).not.toContain("56");
373 });
374});
375
376it("searches the legacy projection when old environments decode to an empty links list", () => {
377 const linkedPullRequest = {
378 projectId: ProjectId.make("project"),
379 repository: "pingdotgg/t3code",
380 number: 12,
381 url: "https://github.com/pingdotgg/t3code/pull/12",
382 };
383 expect(threadPullRequestSearchTerms({ pullRequests: [], linkedPullRequest })).toContain("#12");
384 expect(
385 threadPullRequestSearchTerms({ pullRequests: [link(34)], linkedPullRequest }),
386 ).not.toContain("#12");
387});