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