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

threadPullRequests.test.ts

713 lines24.3 KB

1import {
2 ProjectId,
3 type ThreadPullRequestLink,
4 type ThreadPullRequestSnapshot,
5} from "@t3tools/contracts";
6import { beforeEach, describe, expect, it } from "vite-plus/test";
7​
8import { foldSearchQuery, scoreThreadSearch } from "@t3tools/shared/paletteSearch";
9import { threadSearchPullRequests } from "@t3tools/shared/threadPullRequests";
10​
11import {
12 legacyThreadPullRequestKey,
13 pullRequestListLines,
14 threadPullRequestSearchTerms,
15 resolveThreadCurrentPullRequest,
16 resolveThreadPullRequestChains,
17 resolveThreadPullRequestBadge,
18 threadPullRequestKeysEqual,
19 visibleThreadPullRequests,
20} from "./threadPullRequests.ts";
21​
22// Remove ES2023 array methods to match Hermes on mobile.
23beforeEach(() => {
24 const methods = ["toSorted", "toReversed", "toSpliced"] as const;
25 const descriptors = methods.map((method) =>
26 Object.getOwnPropertyDescriptor(Array.prototype, method),
27 );
28 for (const method of methods) Reflect.deleteProperty(Array.prototype, method);
29 return () => {
30 for (const [index, method] of methods.entries()) {
31 const descriptor = descriptors[index];
32 if (descriptor) Reflect.defineProperty(Array.prototype, method, descriptor);
33 }
34 };
35});
36​
37function snapshot(input: Partial<ThreadPullRequestSnapshot> = {}): ThreadPullRequestSnapshot {
38 return {
39 state: "open",
40 title: "Change",
41 headBranch: "feature",
42 baseBranch: "main",
43 isDraft: false,
44 updatedAt: "2026-01-01T00:00:00.000Z",
45 syncedAt: "2026-01-01T00:00:00.000Z",
46 ...input,
47 };
49​
50function link(
51 number: number,
52 input: Partial<Omit<ThreadPullRequestLink, "number">> = {},
53): ThreadPullRequestLink {
54 return {
55 host: "github.com",
56 repository: "pingdotgg/t3code",
57 number,
58 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
59 source: "manual",
60 linkedAt: `2026-01-01T00:00:${String(number).padStart(2, "0")}.000Z`,
61 snapshot: null,
62 stack: null,
63 ...input,
64 };
66​
67describe("threadPullRequestKeysEqual", () => {
68 it("recovers Forgejo ports from old stored URLs and keeps separate servers distinct", () => {
69 const old = link(1, {
70 host: "forge.example",
71 repository: "team/repo",
72 url: "http://forge.example:3000/team/repo/pulls/1",
73 });
74 expect(threadPullRequestKeysEqual(old, { ...old, host: "forge.example:3000" })).toBe(true);
75 expect(
76 threadPullRequestKeysEqual(old, {
77 host: "forge.example:3000",
78 repository: "team/repo",
79 number: 1,
80 }),
81 ).toBe(true);
82 expect(
83 threadPullRequestKeysEqual(old, {
84 ...old,
85 url: "http://forge.example:4000/team/repo/pulls/1",
86 }),
87 ).toBe(false);
88 });
89​
90 it("recovers Azure repository keys from stored URLs", () => {
91 const own = link(7, {
92 host: "dev.azure.com",
93 repository: "org-a/project/_git/web",
94 url: "https://dev.azure.com/org-a/project/_git/web/pullrequest/7",
95 });
96 expect(legacyThreadPullRequestKey({ ...own, repository: "web" })).toEqual({
97 host: "dev.azure.com",
98 repository: "org-a/project/_git/web",
99 number: 7,
100 });
101 expect(
102 threadPullRequestKeysEqual(own, {
103 host: "org-a.visualstudio.com",
104 repository: "DefaultCollection/project/_git/web",
105 number: 7,
106 }),
107 ).toBe(true);
108 });
109});
110​
111describe("resolveThreadCurrentPullRequest", () => {
112 it("selects the same terminal link as stable sorting across input orders", () => {
113 const dates = ["invalid", "1969-01-01T00:00:00.000Z", "2026-01-01T00:00:00.000Z"];
114 for (let offset = 0; offset < dates.length; offset++) {
115 const links = Array.from({ length: 12 }, (_, index) =>
116 link(index + 1, {
117 snapshot: snapshot({
118 state: "merged",
119 updatedAt: dates[(index + offset) % dates.length]!,
120 }),
121 }),
122 );
123 for (const input of [links, [...links].sort((left, right) => right.number - left.number)]) {
124 const expected = [...input].sort(
125 (left, right) =>
126 (Date.parse(right.snapshot?.updatedAt ?? right.linkedAt) || 0) -
127 (Date.parse(left.snapshot?.updatedAt ?? left.linkedAt) || 0),
128 )[0];
129 expect(resolveThreadCurrentPullRequest(input)).toEqual({ kind: "single", link: expected });
130 }
131 }
132 });
133​
134 it("preserves input order when open chains have equal link dates", () => {
135 const linkedAt = "2026-01-01T00:00:00.000Z";
136 const current = resolveThreadCurrentPullRequest([
137 link(3, { linkedAt }),
138 link(1, { linkedAt }),
139 link(2, { linkedAt }),
140 ]);
141 expect(current?.kind === "stack" ? current.open.map((entry) => entry.number) : null).toEqual([
142 3, 1, 2,
143 ]);
144 });
145​
146 it("keeps the first terminal link on ties and treats invalid update dates as zero", () => {
147 const first = link(3, { snapshot: snapshot({ state: "merged", updatedAt: "invalid" }) });
148 const second = link(1, {
149 snapshot: snapshot({ state: "closed", updatedAt: "1970-01-01T00:00:00.000Z" }),
150 });
151 expect(resolveThreadCurrentPullRequest([first, second])).toEqual({
152 kind: "single",
153 link: first,
154 });
155 expect(resolveThreadCurrentPullRequest([second, first])).toEqual({
156 kind: "single",
157 link: second,
158 });
159 });
160​
161 it("prefers the single open link over terminal ones", () => {
162 const current = resolveThreadCurrentPullRequest([
163 link(1, { snapshot: snapshot({ state: "merged" }) }),
164 link(2, { snapshot: snapshot({ state: "open" }) }),
165 link(3, { snapshot: snapshot({ state: "closed" }) }),
166 ]);
167 expect(current).toMatchObject({ kind: "single", link: { number: 2 } });
168 });
169​
170 it("reports a stack when several links are open and puts the highest layer on top", () => {
171 const stack = {
172 kind: "native" as const,
173 id: "s1",
174 number: 1,
175 url: "https://github.com/pingdotgg/t3code/stacks/1",
176 base: "main",
177 layers: [
178 { number: 10, headBranch: "a", state: "open" as const },
179 { number: 11, headBranch: "b", state: "open" as const },
180 ],
181 };
182 const current = resolveThreadCurrentPullRequest([
183 link(11, { snapshot: snapshot(), stack }),
184 link(10, { snapshot: snapshot(), stack }),
185 ]);
186 expect(current).toMatchObject({ kind: "stack", top: { number: 11 } });
187 if (current?.kind === "stack") {
188 expect(current.open.map((entry) => entry.number)).toEqual([11, 10]);
189 }
190 });
191​
192 it("orders an open set without stack data by most recent link", () => {
193 const current = resolveThreadCurrentPullRequest([link(1), link(2)]);
194 expect(current).toMatchObject({ kind: "stack", top: { number: 2 } });
195 });
196});
197​
198describe("resolveThreadPullRequestChains", () => {
199 it("chains links by base → head within a repository, bottom to top", () => {
200 const chains = resolveThreadPullRequestChains([
201 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "b" }) }),
202 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
203 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
204 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
205 ]);
206 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
207 ["derived", [1, 2, 3]],
208 ["derived", [9]],
209 ]);
210 });
211​
212 it("keeps every link built on the same parent under it", () => {
213 const links = [
214 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "a" }) }),
215 link(5, { snapshot: snapshot({ headBranch: "p", baseBranch: "main" }) }),
216 link(6, { snapshot: snapshot({ headBranch: "q", baseBranch: "p" }) }),
217 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "p" }) }),
218 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
219 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
220 ];
221 const chains = resolveThreadPullRequestChains(links);
222 expect(
223 chains.map((chain) => [chain.layers.map((layer) => layer.number), chain.depths]),
224 ).toEqual([
225 [
226 [5, 6, 1, 3, 2],
227 [0, 1, 1, 2, 2],
228 ],
229 [[9], [0]],
230 ]);
231 expect(resolveThreadPullRequestBadge(links.slice(0, 5))).toEqual({
232 kind: "stack",
233 layers: 5,
234 state: "open",
235 });
236 });
237​
238 it("points at the highest layer of a branched stack, not the last sibling listed", () => {
239 const branched = (state: ThreadPullRequestSnapshot["state"]) => [
240 link(1, { snapshot: snapshot({ state, headBranch: "a" }) }),
241 link(2, { snapshot: snapshot({ state, headBranch: "b", baseBranch: "a" }) }),
242 link(3, { snapshot: snapshot({ state, headBranch: "c", baseBranch: "b" }) }),
243 link(4, { snapshot: snapshot({ state, headBranch: "d", baseBranch: "a" }) }),
244 ];
245 const open = resolveThreadCurrentPullRequest(branched("open"));
246 expect(open?.kind === "stack" ? open.open.map((entry) => entry.number) : null).toEqual([
247 3, 4, 2, 1,
248 ]);
249 expect(resolveThreadCurrentPullRequest(branched("merged"))).toMatchObject({
250 kind: "single",
251 link: { number: 3 },
252 });
253 });
254​
255 it("uses a later complete native snapshot for layer order and the current pull request", () => {
256 const links = [18, 20, 83, 84].map((number) =>
257 link(number, {
258 linkedAt: "2026-01-01T00:00:00.000Z",
259 snapshot: snapshot(),
260 stack: {
261 kind: "native",
262 id: "80",
263 number: 80,
264 url: "https://github.com/pingdotgg/t3code/stacks/80",
265 base: "main",
266 layers: (number < 83
267 ? [79, 37, 38, 39, 76, 18, 20]
268 : [79, 37, 38, 39, 76, 18, 20, 83, 84]
269 ).map((number) => ({ number, headBranch: `pr-${number}`, state: "open" })),
270 },
271 }),
272 );
273 expect
274 .soft(
275 resolveThreadPullRequestChains(links).map((chain) =>
276 chain.layers.map((layer) => layer.number),
277 ),
278 )
279 .toEqual([[18, 20, 83, 84]]);
280 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
281 kind: "stack",
282 top: { number: 84 },
283 open: [{ number: 84 }, { number: 83 }, { number: 20 }, { number: 18 }],
284 });
285 });
286​
287 it("uses the earliest longest partial snapshot and appends missing members in stored order", () => {
288 const links = [
289 [3, 2],
290 [9, 2, 3],
291 [9, 3, 2],
292 [9, 3, 2],
293 ].map((numbers, index) =>
294 link(index + 1, {
295 stack: {
296 kind: "native",
297 id: "s1",
298 number: 1,
299 url: "https://github.com/pingdotgg/t3code/stacks/1",
300 base: "main",
301 layers: numbers.map((number) => ({ number, headBranch: `pr-${number}`, state: "open" })),
302 },
303 }),
304 );
305 expect(
306 resolveThreadPullRequestChains(links).map((chain) =>
307 chain.layers.map((layer) => layer.number),
308 ),
309 ).toEqual([[2, 3, 1, 4]]);
310 });
311​
312 it("uses the native stack order when the host provides one", () => {
313 const stack = {
314 kind: "native" as const,
315 id: "s1",
316 number: 1,
317 url: "https://github.com/pingdotgg/t3code/stacks/1",
318 base: "main",
319 layers: [
320 { number: 5, headBranch: "a", state: "merged" as const },
321 { number: 6, headBranch: "b", state: "open" as const },
322 ],
323 };
324 const chains = resolveThreadPullRequestChains([
325 link(6, { snapshot: snapshot({ headBranch: "b", baseBranch: "main" }), stack }),
326 link(5, {
327 snapshot: snapshot({ state: "merged", headBranch: "a", baseBranch: "main" }),
328 stack,
329 }),
330 link(8),
331 ]);
332 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
333 ["native", [5, 6]],
334 ["derived", [8]],
335 ]);
336 });
337});
338​
339describe("chain selection and badge state", () => {
340 it("does not stack PRs that merged into a branch before the PR from that branch was linked", () => {
341 // Feature PRs merged into staging, then a staging -> main PR opened from the same thread.
342 const feature = (number: number, mergedAt: string) =>
343 link(number, {
344 source: "created",
345 linkedAt: "2026-12-04T06:44:56.000Z",
346 snapshot: snapshot({
347 state: "merged",
348 headBranch: `feature-${number}`,
349 baseBranch: "staging",
350 mergedAt,
351 updatedAt: mergedAt,
352 }),
353 });
354 const promotion = link(41, {
355 source: "created",
356 linkedAt: "2026-12-04T08:25:17.000Z",
357 snapshot: snapshot({
358 state: "merged",
359 headBranch: "staging",
360 baseBranch: "main",
361 mergedAt: "2026-12-04T08:37:25.000Z",
362 updatedAt: "2026-12-04T08:37:26.000Z",
363 }),
364 });
365 const links = [
366 feature(36, "2026-12-04T06:45:45.000Z"),
367 feature(40, "2026-12-04T08:24:39.000Z"),
368 promotion,
369 ];
370 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
371 kind: "single",
372 link: { number: 41 },
373 });
374 expect(
375 resolveThreadPullRequestChains(links).map((chain) =>
376 chain.layers.map((layer) => layer.number),
377 ),
378 ).toEqual([[36], [40], [41]]);
379​
380 // One that merged into the branch after that PR was linked is still a layer on it.
381 const stacked = feature(42, "2026-12-04T08:30:00.000Z");
382 expect(
383 resolveThreadPullRequestChains([promotion, stacked]).map((chain) =>
384 chain.layers.map((layer) => layer.number),
385 ),
386 ).toEqual([[41, 42]]);
387 });
388​
389 it.each(["open", "merged", "closed"] as const)(
390 "targets the top of a derived %s chain despite a later bottom update and link",
391 (state) => {
392 const bottom = link(2, {
393 snapshot: snapshot({ state, headBranch: "base", updatedAt: "2026-02-01T00:00:00.000Z" }),
394 });
395 const top = link(1, {
396 snapshot: snapshot({ state, headBranch: "top", baseBranch: "base" }),
397 });
398 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject(
399 state === "open"
400 ? { kind: "stack", top: { number: 1 }, open: [top, bottom] }
401 : { kind: "single", link: { number: 1 } },
402 );
403 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
404 kind: "stack",
405 layers: 2,
406 state,
407 });
408 },
409 );
410​
411 it("keeps the top native layer after completion and treats an unsynced layer as open", () => {
412 const stack = {
413 kind: "native" as const,
414 id: "native-1",
415 number: 1,
416 url: "https://github.com/pingdotgg/t3code/stacks/1",
417 base: "main",
418 layers: [
419 { number: 2, headBranch: "base", state: "merged" as const },
420 { number: 1, headBranch: "top", state: "merged" as const },
421 ],
422 };
423 const bottom = link(2, {
424 stack,
425 snapshot: snapshot({ state: "merged", updatedAt: "2026-02-01T00:00:00.000Z" }),
426 });
427 const top = link(1, { stack, snapshot: snapshot({ state: "merged" }) });
428 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject({
429 kind: "single",
430 link: { number: 1 },
431 });
432 expect(resolveThreadPullRequestBadge([bottom, { ...top, snapshot: null }])).toEqual({
433 kind: "stack",
434 layers: 2,
435 state: "open",
436 });
437 });
438​
439 it("uses aggregate state and distinguishes unrelated work from a stack", () => {
440 const bottom = link(1, { snapshot: snapshot({ state: "merged", headBranch: "base" }) });
441 const top = link(2, { snapshot: snapshot({ state: "closed", baseBranch: "base" }) });
442 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
443 kind: "stack",
444 layers: 2,
445 state: "closed",
446 });
447 expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({
448 kind: "pull-request",
449 others: 2,
450 state: "open",
451 });
452 expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull();
453 });
454​
455 it("keeps branch matching case-sensitive while ignoring repository case", () => {
456 const bottom = link(1, {
457 repository: "PingDotGG/T3code",
458 snapshot: snapshot({ headBranch: "Base" }),
459 });
460 const top = link(2, { snapshot: snapshot({ headBranch: "top", baseBranch: "base" }) });
461 expect(resolveThreadPullRequestChains([bottom, top])).toHaveLength(2);
462 expect(
463 resolveThreadPullRequestChains([
464 bottom,
465 { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) },
466 ])[0]?.layers,
467 ).toEqual([bottom, { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) }]);
468 });
469​
470 it("preserves cyclic links without presenting a guessed stack order", () => {
471 const links = [
472 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "b" }) }),
473 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
474 ];
475 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
476 links.map((entry) => [entry]),
477 );
478 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
479 kind: "stack",
480 top: { number: 2 },
481 });
482 expect(resolveThreadPullRequestBadge(links)).toEqual({
483 kind: "pull-request",
484 others: 1,
485 state: "open",
486 });
487 });
488​
489 it("lists a pull request once when two links share its identity", () => {
490 const links = [
491 link(1, { snapshot: snapshot({ headBranch: "a" }) }),
492 link(1, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
493 link(1, { snapshot: snapshot({ headBranch: "c" }) }),
494 ];
495 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual([
496 [links[0]],
497 ]);
498 });
499​
500 it("does not guess a parent when a head branch was reused", () => {
501 const links = [
502 link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
503 link(2, { snapshot: snapshot({ headBranch: "reused" }) }),
504 link(3, { snapshot: snapshot({ headBranch: "top", baseBranch: "reused" }) }),
505 ];
506 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
507 links.map((entry) => [entry]),
508 );
509 });
510});
511​
512describe("threadSearchPullRequests", () => {
513 const branchPullRequest = {
514 projectId: ProjectId.make("project"),
515 repository: "PingDotGG/T3Code",
516 number: 42,
517 url: "https://github.com/pingdotgg/t3code/pull/42",
518 };
519​
520 it("drops a branch PR that duplicates a titled link", () => {
521 expect(
522 threadSearchPullRequests({
523 branchPullRequest,
524 pullRequests: [
525 link(42, {
526 source: "created",
527 snapshot: snapshot({ title: "Fix login crash" }),
528 }),
529 ],
530 }),
531 ).toEqual([
532 {
533 number: 42,
534 title: "Fix login crash",
535 references: ["pingdotgg/t3code#42", "https://github.com/pingdotgg/t3code/pull/42"],
536 },
537 ]);
538 });
539​
540 it("keeps a branch PR whose repository and number match a link on another host", () => {
541 for (const source of ["manual", "stack-dismissed"] as const) {
542 expect(
543 threadSearchPullRequests({
544 branchPullRequest,
545 pullRequests: [
546 link(42, {
547 host: "gitlab.com",
548 source,
549 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/42",
550 }),
551 ],
552 }).flatMap((entry) => entry.references),
553 ).toContain(branchPullRequest.url);
554 }
555 });
556​
557 it("keeps a branch PR the links do not include", () => {
558 expect(
559 threadSearchPullRequests({
560 branchPullRequest,
561 pullRequests: [link(7, { snapshot: snapshot({ title: "Other change" }) })],
562 }).map((entry) => [entry.number, entry.title]),
563 ).toEqual([
564 [42, null],
565 [7, "Other change"],
566 ]);
567 });
568​
569 it("drops a branch PR the user unlinked", () => {
570 expect(
571 threadSearchPullRequests({
572 branchPullRequest,
573 pullRequests: [link(42, { source: "stack-dismissed" }), link(7)],
574 }).map((entry) => entry.number),
575 ).toEqual([7]);
576 });
577​
578 it("drops a branch PR the user unlinked through a comment or files-tab link", () => {
579 for (const url of [
580 "https://github.com/pingdotgg/t3code/pull/42#issuecomment-42",
581 "https://github.com/pingdotgg/t3code/pull/42/files#r123",
582 ]) {
583 expect(
584 threadSearchPullRequests({
585 branchPullRequest,
586 pullRequests: [link(42, { source: "stack-dismissed", url })],
587 }),
588 ).toEqual([]);
589 }
590 });
591​
592 it.each(["#issuecomment-42", "/files#r123"])(
593 "keeps the branch PR search title and references when linked through %s",
594 (suffix) => {
595 const url = `${branchPullRequest.url}${suffix}`;
596 const pullRequests = threadSearchPullRequests({
597 branchPullRequest,
598 pullRequests: [link(42, { url, snapshot: snapshot({ title: "Fix auth" }) })],
599 });
600 const subject = { title: "Unrelated", activityAt: null, pullRequests };
601 expect(scoreThreadSearch(foldSearchQuery("#42"), subject, 0)?.explain?.()?.text).toBe(
602 "#42 Fix auth",
603 );
604 expect(pullRequests).toEqual([
605 {
606 number: 42,
607 title: "Fix auth",
608 references: ["pingdotgg/t3code#42", url, branchPullRequest.url],
609 },
610 ]);
611 expect(
612 scoreThreadSearch(
613 foldSearchQuery(`${branchPullRequest.url}/files`),
614 subject,
615 0,
616 )?.explain?.()?.field,
617 ).toBe("reference");
618 },
619 );
620});
621​
622describe("threadPullRequestSearchTerms", () => {
623 it("includes completed and unsynced links but excludes dismissed links", () => {
624 const terms = threadPullRequestSearchTerms({
625 pullRequests: [
626 link(12, { snapshot: snapshot({ title: "Fix login", state: "merged" }) }),
627 link(34),
628 link(56, { source: "stack-dismissed" }),
629 ],
630 });
631 expect(terms).toContain("#12");
632 expect(terms).toContain("pingdotgg/t3code#12");
633 expect(terms).toContain("https://github.com/pingdotgg/t3code/pull/12");
634 expect(terms).toContain("Fix login");
635 expect(terms).toContain("#34");
636 expect(terms.join(" ")).not.toContain("56");
637 });
638});
639​
640it("searches the legacy projection when old environments decode to an empty links list", () => {
641 const linkedPullRequest = {
642 projectId: ProjectId.make("project"),
643 repository: "pingdotgg/t3code",
644 number: 12,
645 url: "https://github.com/pingdotgg/t3code/pull/12",
646 };
647 expect(threadPullRequestSearchTerms({ pullRequests: [], linkedPullRequest })).toContain("#12");
648 expect(
649 threadPullRequestSearchTerms({ pullRequests: [link(34)], linkedPullRequest }),
650 ).not.toContain("#12");
651});
652​
653describe("pullRequestListLines and derived caching", () => {
654 it("orders chains by latest layer update, preserves ties, and treats invalid timestamps as zero", () => {
655 const base = link(1, {
656 snapshot: snapshot({
657 headBranch: "a",
658 baseBranch: "main",
659 updatedAt: "2026-01-01T00:00:00.000Z",
660 }),
661 });
662 const top = link(2, {
663 snapshot: snapshot({
664 headBranch: "b",
665 baseBranch: "a",
666 updatedAt: "2026-03-01T00:00:00.000Z",
667 }),
668 });
669 const tiedFirst = link(3, {
670 snapshot: snapshot({ headBranch: "c", updatedAt: "2026-02-01T00:00:00.000Z" }),
671 });
672 const tiedSecond = link(4, {
673 snapshot: snapshot({ headBranch: "d", updatedAt: "2026-02-01T00:00:00.000Z" }),
674 });
675 const invalid = link(5, {
676 snapshot: snapshot({ headBranch: "e", updatedAt: "invalid" }),
677 });
678 const chains = resolveThreadPullRequestChains([invalid, tiedFirst, base, top, tiedSecond]);
679 const lines = pullRequestListLines(chains);
680​
681 expect(lines.map((line) => [line.link.number, line.depth, line.stack])).toEqual([
682 [1, 0, { kind: "derived", size: 2 }],
683 [2, 1, null],
684 [3, 0, null],
685 [4, 0, null],
686 [5, 0, null],
687 ]);
688 });
689​
690 it("reuses cached derived results for the same immutable links array", () => {
691 const links = [
692 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
693 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
694 link(3, { source: "stack-dismissed" }),
695 ];
696​
697 const undismissed = links.slice(0, 2);
698 expect(visibleThreadPullRequests(undismissed)).toBe(undismissed);
699 const chains = resolveThreadPullRequestChains(links);
700 expect(resolveThreadPullRequestChains(links)).toBe(chains);
701 expect(pullRequestListLines(chains)).toBe(pullRequestListLines(chains));
702 expect(resolveThreadCurrentPullRequest(links)).toBe(resolveThreadCurrentPullRequest(links));
703 expect(resolveThreadPullRequestBadge(links)).toBe(resolveThreadPullRequestBadge(links));
704​
705 const updated = [...links.slice(0, 2), link(4)];
706 expect(resolveThreadPullRequestChains(updated)).not.toBe(chains);
707 expect(resolveThreadPullRequestBadge(updated)).toEqual({
708 kind: "pull-request",
709 others: 2,
710 state: "open",
711 });
712 });
713});