Open History to see the last commit
1import {
2 ProjectId,
3 type ThreadPullRequestLink,
4 type ThreadPullRequestSnapshot,
5} from "@t3tools/contracts";
6import { beforeEach, describe, expect, it } from "vite-plus/test";
7
8import {
9 legacyThreadPullRequestKey,
10 threadPullRequestSearchTerms,
11 resolveThreadCurrentPullRequest,
12 resolveThreadPullRequestChains,
13 resolveThreadPullRequestBadge,
14 threadPullRequestKeysEqual,
15} from "./threadPullRequests.ts";
16
17// Remove ES2023 array methods to match Hermes on mobile.
18beforeEach(() => {
19 const methods = ["toSorted", "toReversed", "toSpliced"] as const;
20 const descriptors = methods.map((method) =>
21 Object.getOwnPropertyDescriptor(Array.prototype, method),
22 );
23 for (const method of methods) Reflect.deleteProperty(Array.prototype, method);
24 return () => {
25 for (const [index, method] of methods.entries()) {
26 const descriptor = descriptors[index];
27 if (descriptor) Reflect.defineProperty(Array.prototype, method, descriptor);
28 }
29 };
30});
31
32function snapshot(input: Partial<ThreadPullRequestSnapshot> = {}): ThreadPullRequestSnapshot {
33 return {
34 state: "open",
35 title: "Change",
36 headBranch: "feature",
37 baseBranch: "main",
38 isDraft: false,
39 updatedAt: "2026-01-01T00:00:00.000Z",
40 syncedAt: "2026-01-01T00:00:00.000Z",
41 ...input,
42 };
43}
44
45function link(
46 number: number,
47 input: Partial<Omit<ThreadPullRequestLink, "number">> = {},
48): ThreadPullRequestLink {
49 return {
50 host: "github.com",
51 repository: "pingdotgg/t3code",
52 number,
53 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
54 source: "manual",
55 linkedAt: `2026-01-01T00:00:${String(number).padStart(2, "0")}.000Z`,
56 snapshot: null,
57 stack: null,
58 ...input,
59 };
60}
61
62describe("threadPullRequestKeysEqual", () => {
63 it("recovers Forgejo ports from old stored URLs and keeps separate servers distinct", () => {
64 const old = link(1, {
65 host: "forge.example",
66 repository: "team/repo",
67 url: "http://forge.example:3000/team/repo/pulls/1",
68 });
69 expect(threadPullRequestKeysEqual(old, { ...old, host: "forge.example:3000" })).toBe(true);
70 expect(
71 threadPullRequestKeysEqual(old, {
72 host: "forge.example:3000",
73 repository: "team/repo",
74 number: 1,
75 }),
76 ).toBe(true);
77 expect(
78 threadPullRequestKeysEqual(old, {
79 ...old,
80 url: "http://forge.example:4000/team/repo/pulls/1",
81 }),
82 ).toBe(false);
83 });
84
85 it("recovers Azure repository keys from stored URLs", () => {
86 const own = link(7, {
87 host: "dev.azure.com",
88 repository: "org-a/project/_git/web",
89 url: "https://dev.azure.com/org-a/project/_git/web/pullrequest/7",
90 });
91 expect(legacyThreadPullRequestKey({ ...own, repository: "web" })).toEqual({
92 host: "dev.azure.com",
93 repository: "org-a/project/_git/web",
94 number: 7,
95 });
96 expect(
97 threadPullRequestKeysEqual(own, {
98 host: "org-a.visualstudio.com",
99 repository: "DefaultCollection/project/_git/web",
100 number: 7,
101 }),
102 ).toBe(true);
103 });
104});
105
106describe("resolveThreadCurrentPullRequest", () => {
107 it("prefers the single open link over terminal ones", () => {
108 const current = resolveThreadCurrentPullRequest([
109 link(1, { snapshot: snapshot({ state: "merged" }) }),
110 link(2, { snapshot: snapshot({ state: "open" }) }),
111 link(3, { snapshot: snapshot({ state: "closed" }) }),
112 ]);
113 expect(current).toMatchObject({ kind: "single", link: { number: 2 } });
114 });
115
116 it("reports a stack when several links are open and puts the highest layer on top", () => {
117 const stack = {
118 kind: "native" as const,
119 id: "s1",
120 number: 1,
121 url: "https://github.com/pingdotgg/t3code/stacks/1",
122 base: "main",
123 layers: [
124 { number: 10, headBranch: "a", state: "open" as const },
125 { number: 11, headBranch: "b", state: "open" as const },
126 ],
127 };
128 const current = resolveThreadCurrentPullRequest([
129 link(11, { snapshot: snapshot(), stack }),
130 link(10, { snapshot: snapshot(), stack }),
131 ]);
132 expect(current).toMatchObject({ kind: "stack", top: { number: 11 } });
133 if (current?.kind === "stack") {
134 expect(current.open.map((entry) => entry.number)).toEqual([11, 10]);
135 }
136 });
137
138 it("orders an open set without stack data by most recent link", () => {
139 const current = resolveThreadCurrentPullRequest([link(1), link(2)]);
140 expect(current).toMatchObject({ kind: "stack", top: { number: 2 } });
141 });
142
143 it("falls back to the most recently updated terminal link", () => {
144 const current = resolveThreadCurrentPullRequest([
145 link(1, { snapshot: snapshot({ state: "merged", updatedAt: "2026-01-03T00:00:00.000Z" }) }),
146 link(2, { snapshot: snapshot({ state: "closed", updatedAt: "2026-01-02T00:00:00.000Z" }) }),
147 ]);
148 expect(current).toMatchObject({ kind: "single", link: { number: 1 } });
149 });
150});
151
152describe("resolveThreadPullRequestChains", () => {
153 it("chains links by base → head within a repository, bottom to top", () => {
154 const chains = resolveThreadPullRequestChains([
155 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "b" }) }),
156 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
157 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
158 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
159 ]);
160 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
161 ["derived", [1, 2, 3]],
162 ["derived", [9]],
163 ]);
164 });
165
166 it("keeps every link built on the same parent under it", () => {
167 const links = [
168 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "a" }) }),
169 link(5, { snapshot: snapshot({ headBranch: "p", baseBranch: "main" }) }),
170 link(6, { snapshot: snapshot({ headBranch: "q", baseBranch: "p" }) }),
171 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "p" }) }),
172 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
173 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
174 ];
175 const chains = resolveThreadPullRequestChains(links);
176 expect(
177 chains.map((chain) => [chain.layers.map((layer) => layer.number), chain.depths]),
178 ).toEqual([
179 [
180 [5, 6, 1, 3, 2],
181 [0, 1, 1, 2, 2],
182 ],
183 [[9], [0]],
184 ]);
185 expect(resolveThreadPullRequestBadge(links.slice(0, 5))).toEqual({
186 kind: "stack",
187 layers: 5,
188 state: "open",
189 });
190 });
191
192 it("points at the highest layer of a branched stack, not the last sibling listed", () => {
193 const branched = (state: ThreadPullRequestSnapshot["state"]) => [
194 link(1, { snapshot: snapshot({ state, headBranch: "a" }) }),
195 link(2, { snapshot: snapshot({ state, headBranch: "b", baseBranch: "a" }) }),
196 link(3, { snapshot: snapshot({ state, headBranch: "c", baseBranch: "b" }) }),
197 link(4, { snapshot: snapshot({ state, headBranch: "d", baseBranch: "a" }) }),
198 ];
199 const open = resolveThreadCurrentPullRequest(branched("open"));
200 expect(open?.kind === "stack" ? open.open.map((entry) => entry.number) : null).toEqual([
201 3, 4, 2, 1,
202 ]);
203 expect(resolveThreadCurrentPullRequest(branched("merged"))).toMatchObject({
204 kind: "single",
205 link: { number: 3 },
206 });
207 });
208
209 it("uses a later complete native snapshot for layer order and the current pull request", () => {
210 const links = [18, 20, 83, 84].map((number) =>
211 link(number, {
212 linkedAt: "2026-01-01T00:00:00.000Z",
213 snapshot: snapshot(),
214 stack: {
215 kind: "native",
216 id: "80",
217 number: 80,
218 url: "https://github.com/pingdotgg/t3code/stacks/80",
219 base: "main",
220 layers: (number < 83
221 ? [79, 37, 38, 39, 76, 18, 20]
222 : [79, 37, 38, 39, 76, 18, 20, 83, 84]
223 ).map((number) => ({ number, headBranch: `pr-${number}`, state: "open" })),
224 },
225 }),
226 );
227 expect
228 .soft(
229 resolveThreadPullRequestChains(links).map((chain) =>
230 chain.layers.map((layer) => layer.number),
231 ),
232 )
233 .toEqual([[18, 20, 83, 84]]);
234 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
235 kind: "stack",
236 top: { number: 84 },
237 open: [{ number: 84 }, { number: 83 }, { number: 20 }, { number: 18 }],
238 });
239 });
240
241 it("uses the earliest longest partial snapshot and appends missing members in stored order", () => {
242 const links = [
243 [3, 2],
244 [9, 2, 3],
245 [9, 3, 2],
246 [9, 3, 2],
247 ].map((numbers, index) =>
248 link(index + 1, {
249 stack: {
250 kind: "native",
251 id: "s1",
252 number: 1,
253 url: "https://github.com/pingdotgg/t3code/stacks/1",
254 base: "main",
255 layers: numbers.map((number) => ({ number, headBranch: `pr-${number}`, state: "open" })),
256 },
257 }),
258 );
259 expect(
260 resolveThreadPullRequestChains(links).map((chain) =>
261 chain.layers.map((layer) => layer.number),
262 ),
263 ).toEqual([[2, 3, 1, 4]]);
264 });
265
266 it("uses the native stack order when the host provides one", () => {
267 const stack = {
268 kind: "native" as const,
269 id: "s1",
270 number: 1,
271 url: "https://github.com/pingdotgg/t3code/stacks/1",
272 base: "main",
273 layers: [
274 { number: 5, headBranch: "a", state: "merged" as const },
275 { number: 6, headBranch: "b", state: "open" as const },
276 ],
277 };
278 const chains = resolveThreadPullRequestChains([
279 link(6, { snapshot: snapshot({ headBranch: "b", baseBranch: "main" }), stack }),
280 link(5, {
281 snapshot: snapshot({ state: "merged", headBranch: "a", baseBranch: "main" }),
282 stack,
283 }),
284 link(8),
285 ]);
286 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
287 ["native", [5, 6]],
288 ["derived", [8]],
289 ]);
290 });
291});
292
293describe("chain selection and badge state", () => {
294 it.each(["open", "merged", "closed"] as const)(
295 "targets the top of a derived %s chain despite a later bottom update and link",
296 (state) => {
297 const bottom = link(2, {
298 snapshot: snapshot({ state, headBranch: "base", updatedAt: "2026-02-01T00:00:00.000Z" }),
299 });
300 const top = link(1, {
301 snapshot: snapshot({ state, headBranch: "top", baseBranch: "base" }),
302 });
303 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject(
304 state === "open"
305 ? { kind: "stack", top: { number: 1 }, open: [top, bottom] }
306 : { kind: "single", link: { number: 1 } },
307 );
308 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
309 kind: "stack",
310 layers: 2,
311 state,
312 });
313 },
314 );
315
316 it("keeps the top native layer after completion and treats an unsynced layer as open", () => {
317 const stack = {
318 kind: "native" as const,
319 id: "native-1",
320 number: 1,
321 url: "https://github.com/pingdotgg/t3code/stacks/1",
322 base: "main",
323 layers: [
324 { number: 2, headBranch: "base", state: "merged" as const },
325 { number: 1, headBranch: "top", state: "merged" as const },
326 ],
327 };
328 const bottom = link(2, {
329 stack,
330 snapshot: snapshot({ state: "merged", updatedAt: "2026-02-01T00:00:00.000Z" }),
331 });
332 const top = link(1, { stack, snapshot: snapshot({ state: "merged" }) });
333 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject({
334 kind: "single",
335 link: { number: 1 },
336 });
337 expect(resolveThreadPullRequestBadge([bottom, { ...top, snapshot: null }])).toEqual({
338 kind: "stack",
339 layers: 2,
340 state: "open",
341 });
342 });
343
344 it("uses aggregate state and distinguishes unrelated work from a stack", () => {
345 const bottom = link(1, { snapshot: snapshot({ state: "merged", headBranch: "base" }) });
346 const top = link(2, { snapshot: snapshot({ state: "closed", baseBranch: "base" }) });
347 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
348 kind: "stack",
349 layers: 2,
350 state: "closed",
351 });
352 expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({
353 kind: "pull-request",
354 others: 2,
355 state: "open",
356 });
357 expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull();
358 });
359
360 it("keeps branch matching case-sensitive while ignoring repository case", () => {
361 const bottom = link(1, {
362 repository: "PingDotGG/T3code",
363 snapshot: snapshot({ headBranch: "Base" }),
364 });
365 const top = link(2, { snapshot: snapshot({ headBranch: "top", baseBranch: "base" }) });
366 expect(resolveThreadPullRequestChains([bottom, top])).toHaveLength(2);
367 expect(
368 resolveThreadPullRequestChains([
369 bottom,
370 { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) },
371 ])[0]?.layers,
372 ).toEqual([bottom, { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) }]);
373 });
374
375 it("preserves cyclic links without presenting a guessed stack order", () => {
376 const links = [
377 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "b" }) }),
378 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
379 ];
380 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
381 links.map((entry) => [entry]),
382 );
383 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
384 kind: "stack",
385 top: { number: 2 },
386 });
387 expect(resolveThreadPullRequestBadge(links)).toEqual({
388 kind: "pull-request",
389 others: 1,
390 state: "open",
391 });
392 });
393
394 it("lists a pull request once when two links share its identity", () => {
395 const links = [
396 link(1, { snapshot: snapshot({ headBranch: "a" }) }),
397 link(1, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
398 link(1, { snapshot: snapshot({ headBranch: "c" }) }),
399 ];
400 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual([
401 [links[0]],
402 ]);
403 });
404
405 it("does not guess a parent when a head branch was reused", () => {
406 const links = [
407 link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
408 link(2, { snapshot: snapshot({ headBranch: "reused" }) }),
409 link(3, { snapshot: snapshot({ headBranch: "top", baseBranch: "reused" }) }),
410 ];
411 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
412 links.map((entry) => [entry]),
413 );
414 });
415});
416
417describe("threadPullRequestSearchTerms", () => {
418 it("includes completed and unsynced links but excludes dismissed links", () => {
419 const terms = threadPullRequestSearchTerms({
420 pullRequests: [
421 link(12, { snapshot: snapshot({ title: "Fix login", state: "merged" }) }),
422 link(34),
423 link(56, { source: "stack-dismissed" }),
424 ],
425 });
426 expect(terms).toContain("#12");
427 expect(terms).toContain("pingdotgg/t3code#12");
428 expect(terms).toContain("https://github.com/pingdotgg/t3code/pull/12");
429 expect(terms).toContain("Fix login");
430 expect(terms).toContain("#34");
431 expect(terms.join(" ")).not.toContain("56");
432 });
433});
434
435it("searches the legacy projection when old environments decode to an empty links list", () => {
436 const linkedPullRequest = {
437 projectId: ProjectId.make("project"),
438 repository: "pingdotgg/t3code",
439 number: 12,
440 url: "https://github.com/pingdotgg/t3code/pull/12",
441 };
442 expect(threadPullRequestSearchTerms({ pullRequests: [], linkedPullRequest })).toContain("#12");
443 expect(
444 threadPullRequestSearchTerms({ pullRequests: [link(34)], linkedPullRequest }),
445 ).not.toContain("#12");
446});