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 legacyLinkedPullRequestOf,
10 legacyThreadPullRequestKey,
11 threadPullRequestSearchTerms,
12 resolveThreadCurrentPullRequest,
13 resolveThreadPullRequestChains,
14 resolveThreadPullRequestBadge,
15 threadPullRequestKeysEqual,
16} from "./threadPullRequests.ts";
17
18// Match Hermes: these ES2023 array methods are absent on mobile, and this module runs in
19// the home thread list on every launch.
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("ignores host and repository case", () => {
88 expect(
89 threadPullRequestKeysEqual(
90 { host: "GitHub.com", repository: "PingDotGG/t3code", number: 1 },
91 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
92 ),
93 ).toBe(true);
94 expect(
95 threadPullRequestKeysEqual(
96 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
97 { host: "gitlab.com", repository: "pingdotgg/t3code", number: 1 },
98 ),
99 ).toBe(false);
100 });
101});
102
103describe("resolveThreadCurrentPullRequest", () => {
104 it("returns null with no visible links", () => {
105 expect(resolveThreadCurrentPullRequest([])).toBeNull();
106 expect(resolveThreadCurrentPullRequest([link(1, { source: "stack-dismissed" })])).toBeNull();
107 });
108
109 it("treats an unsynced link as open", () => {
110 expect(resolveThreadCurrentPullRequest([link(1)])).toMatchObject({
111 kind: "single",
112 link: { number: 1 },
113 });
114 });
115
116 it("prefers the single open link over terminal ones", () => {
117 const current = resolveThreadCurrentPullRequest([
118 link(1, { snapshot: snapshot({ state: "merged" }) }),
119 link(2, { snapshot: snapshot({ state: "open" }) }),
120 link(3, { snapshot: snapshot({ state: "closed" }) }),
121 ]);
122 expect(current).toMatchObject({ kind: "single", link: { number: 2 } });
123 });
124
125 it("reports a stack when several links are open and puts the highest layer on top", () => {
126 const stack = {
127 kind: "native" as const,
128 id: "s1",
129 number: 1,
130 url: "https://github.com/pingdotgg/t3code/stacks/1",
131 base: "main",
132 layers: [
133 { number: 10, headBranch: "a", state: "open" as const },
134 { number: 11, headBranch: "b", state: "open" as const },
135 ],
136 };
137 const current = resolveThreadCurrentPullRequest([
138 link(11, { snapshot: snapshot(), stack }),
139 link(10, { snapshot: snapshot(), stack }),
140 ]);
141 expect(current).toMatchObject({ kind: "stack", top: { number: 11 } });
142 if (current?.kind === "stack") {
143 expect(current.open.map((entry) => entry.number)).toEqual([11, 10]);
144 }
145 });
146
147 it("orders an open set without stack data by most recent link", () => {
148 const current = resolveThreadCurrentPullRequest([link(1), link(2)]);
149 expect(current).toMatchObject({ kind: "stack", top: { number: 2 } });
150 });
151
152 it("falls back to the most recently updated terminal link", () => {
153 const current = resolveThreadCurrentPullRequest([
154 link(1, { snapshot: snapshot({ state: "merged", updatedAt: "2026-01-03T00:00:00.000Z" }) }),
155 link(2, { snapshot: snapshot({ state: "closed", updatedAt: "2026-01-02T00:00:00.000Z" }) }),
156 ]);
157 expect(current).toMatchObject({ kind: "single", link: { number: 1 } });
158 });
159});
160
161describe("legacyLinkedPullRequestOf", () => {
162 const identity = {
163 canonicalKey: "github.com/pingdotgg/t3code",
164 provider: "github",
165 displayName: "pingdotgg/t3code",
166 locator: {
167 source: "git-remote" as const,
168 remoteName: "origin",
169 remoteUrl: "https://github.com/pingdotgg/t3code.git",
170 },
171 };
172 it("projects only links the owning project can route without a host", () => {
173 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, identity)).toEqual({
174 projectId: "project-1",
175 repository: "pingdotgg/t3code",
176 number: 7,
177 url: "https://github.com/pingdotgg/t3code/pull/7",
178 });
179 expect(legacyLinkedPullRequestOf([], "project-1" as never, identity)).toBeNull();
180 });
181 it.each([
182 {
183 host: "github.enterprise.test",
184 url: "https://github.enterprise.test/pingdotgg/t3code/pull/7",
185 },
186 { repository: "acme/other", url: "https://github.com/acme/other/pull/7" },
187 ])("omits an unsafe legacy route %j", (foreign) => {
188 expect(
189 legacyLinkedPullRequestOf([link(7, foreign)], "project-1" as never, identity),
190 ).toBeNull();
191 expect(
192 legacyLinkedPullRequestOf([link(7, foreign), link(8)], "project-1" as never, identity)
193 ?.number,
194 ).toBe(8);
195 });
196 it.each([
197 "dev.azure.com/org-a/project/_git/web",
198 "ssh.dev.azure.com/v3/org-a/project/web",
199 "org-a.visualstudio.com/DefaultCollection/project/_git/web",
200 ])("projects Azure links through the legacy selector for %s", (canonicalKey) => {
201 const azureIdentity = {
202 ...identity,
203 provider: "azure-devops",
204 canonicalKey,
205 displayName: canonicalKey.slice(canonicalKey.indexOf("/") + 1),
206 name: "web",
207 };
208 const own = link(7, {
209 host: "dev.azure.com",
210 repository: "org-a/project/_git/web",
211 url: "https://dev.azure.com/org-a/project/_git/web/pullrequest/7",
212 });
213 const foreign = link(7, {
214 host: "dev.azure.com",
215 repository: "web",
216 url: "https://dev.azure.com/org-b/project/_git/web/pullrequest/7",
217 linkedAt: "2026-01-02T00:00:00.000Z",
218 });
219 for (const repository of ["web", "org-a/project/_git/web"]) {
220 expect(
221 legacyLinkedPullRequestOf(
222 [{ ...own, repository }, foreign],
223 "project-1" as never,
224 azureIdentity,
225 ),
226 ).toEqual({ projectId: "project-1", repository: "web", number: 7, url: own.url });
227 }
228 expect(legacyLinkedPullRequestOf([foreign], "project-1" as never, azureIdentity)).toBeNull();
229 expect(legacyThreadPullRequestKey({ ...own, repository: "web" })).toEqual({
230 host: "dev.azure.com",
231 repository: "org-a/project/_git/web",
232 number: 7,
233 });
234 expect(
235 threadPullRequestKeysEqual(own, {
236 host: "org-a.visualstudio.com",
237 repository: "DefaultCollection/project/_git/web",
238 number: 7,
239 }),
240 ).toBe(true);
241 });
242 it("does not guess when the project identity is unavailable", () => {
243 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, null)).toBeNull();
244 });
245 it("does not route links for a local-path remote with no host or provider", () => {
246 const localIdentity = {
247 canonicalKey: "/tmp/r/remote",
248 displayName: "remote",
249 locator: {
250 source: "git-remote" as const,
251 remoteName: "origin",
252 remoteUrl: "/tmp/r/remote.git",
253 },
254 };
255 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, localIdentity)).toBeNull();
256 });
257});
258
259describe("resolveThreadPullRequestChains", () => {
260 it("chains links by base → head within a repository, bottom to top", () => {
261 const chains = resolveThreadPullRequestChains([
262 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "b" }) }),
263 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
264 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
265 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
266 ]);
267 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
268 ["derived", [1, 2, 3]],
269 ["derived", [9]],
270 ]);
271 });
272
273 it("keeps every link built on the same parent under it", () => {
274 const links = [
275 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "a" }) }),
276 link(5, { snapshot: snapshot({ headBranch: "p", baseBranch: "main" }) }),
277 link(6, { snapshot: snapshot({ headBranch: "q", baseBranch: "p" }) }),
278 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "p" }) }),
279 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
280 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
281 ];
282 const chains = resolveThreadPullRequestChains(links);
283 expect(
284 chains.map((chain) => [chain.layers.map((layer) => layer.number), chain.depths]),
285 ).toEqual([
286 // #6 and #1 both build on #5; #3 and #2 both build on #1.
287 [
288 [5, 6, 1, 3, 2],
289 [0, 1, 1, 2, 2],
290 ],
291 [[9], [0]],
292 ]);
293 expect(resolveThreadPullRequestBadge(links.slice(0, 5))).toEqual({
294 kind: "stack",
295 layers: 5,
296 state: "open",
297 });
298 });
299
300 it("points at the highest layer of a branched stack, not the last sibling listed", () => {
301 // #1 → #2 → #3, and #4 also builds on #1.
302 const branched = (state: ThreadPullRequestSnapshot["state"]) => [
303 link(1, { snapshot: snapshot({ state, headBranch: "a" }) }),
304 link(2, { snapshot: snapshot({ state, headBranch: "b", baseBranch: "a" }) }),
305 link(3, { snapshot: snapshot({ state, headBranch: "c", baseBranch: "b" }) }),
306 link(4, { snapshot: snapshot({ state, headBranch: "d", baseBranch: "a" }) }),
307 ];
308 const open = resolveThreadCurrentPullRequest(branched("open"));
309 expect(open?.kind === "stack" ? open.open.map((entry) => entry.number) : null).toEqual([
310 3, 4, 2, 1,
311 ]);
312 expect(resolveThreadCurrentPullRequest(branched("merged"))).toMatchObject({
313 kind: "single",
314 link: { number: 3 },
315 });
316 });
317
318 it("uses a later complete native snapshot for layer order and the current pull request", () => {
319 const links = [18, 20, 83, 84].map((number) =>
320 link(number, {
321 linkedAt: "2026-01-01T00:00:00.000Z",
322 snapshot: snapshot(),
323 stack: {
324 kind: "native",
325 id: "80",
326 number: 80,
327 url: "https://github.com/pingdotgg/t3code/stacks/80",
328 base: "main",
329 layers: (number < 83
330 ? [79, 37, 38, 39, 76, 18, 20]
331 : [79, 37, 38, 39, 76, 18, 20, 83, 84]
332 ).map((number) => ({ number, headBranch: `pr-${number}`, state: "open" })),
333 },
334 }),
335 );
336 expect
337 .soft(
338 resolveThreadPullRequestChains(links).map((chain) =>
339 chain.layers.map((layer) => layer.number),
340 ),
341 )
342 .toEqual([[18, 20, 83, 84]]);
343 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
344 kind: "stack",
345 top: { number: 84 },
346 open: [{ number: 84 }, { number: 83 }, { number: 20 }, { number: 18 }],
347 });
348 });
349
350 it("uses the earliest longest partial snapshot and appends missing members in stored order", () => {
351 const links = [
352 [3, 2],
353 [9, 2, 3],
354 [9, 3, 2],
355 [9, 3, 2],
356 ].map((numbers, index) =>
357 link(index + 1, {
358 stack: {
359 kind: "native",
360 id: "s1",
361 number: 1,
362 url: "https://github.com/pingdotgg/t3code/stacks/1",
363 base: "main",
364 layers: numbers.map((number) => ({ number, headBranch: `pr-${number}`, state: "open" })),
365 },
366 }),
367 );
368 expect(
369 resolveThreadPullRequestChains(links).map((chain) =>
370 chain.layers.map((layer) => layer.number),
371 ),
372 ).toEqual([[2, 3, 1, 4]]);
373 });
374
375 it("uses the native stack order when the host provides one", () => {
376 const stack = {
377 kind: "native" as const,
378 id: "s1",
379 number: 1,
380 url: "https://github.com/pingdotgg/t3code/stacks/1",
381 base: "main",
382 layers: [
383 { number: 5, headBranch: "a", state: "merged" as const },
384 { number: 6, headBranch: "b", state: "open" as const },
385 ],
386 };
387 const chains = resolveThreadPullRequestChains([
388 link(6, { snapshot: snapshot({ headBranch: "b", baseBranch: "main" }), stack }),
389 link(5, {
390 snapshot: snapshot({ state: "merged", headBranch: "a", baseBranch: "main" }),
391 stack,
392 }),
393 link(8),
394 ]);
395 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
396 ["native", [5, 6]],
397 ["derived", [8]],
398 ]);
399 });
400});
401
402describe("chain selection and badge state", () => {
403 it.each([
404 ["open", false, "open", false, "open"],
405 ["closed", false, "closed", false, "closed"],
406 ["open", true, "open", true, "draft"],
407 ["open", false, "open", true, "open"],
408 ["closed", false, "open", true, "open"],
409 ["merged", false, "merged", false, "merged"],
410 ["merged", false, "closed", true, "closed"],
411 ] as const)(
412 "aggregates %s (draft %s) and %s (draft %s) as %s",
413 (firstState, firstDraft, secondState, secondDraft, state) => {
414 for (const stacked of [false, true]) {
415 const links = [
416 link(1, {
417 snapshot: snapshot({ state: firstState, isDraft: firstDraft, headBranch: "base" }),
418 }),
419 link(2, {
420 snapshot: snapshot({
421 state: secondState,
422 isDraft: secondDraft,
423 baseBranch: stacked ? "base" : "main",
424 }),
425 }),
426 link(3, { source: "stack-dismissed" }),
427 ];
428 expect(resolveThreadPullRequestBadge(links)).toEqual(
429 stacked
430 ? { kind: "stack", layers: 2, state }
431 : { kind: "pull-request", others: 1, state },
432 );
433 }
434 },
435 );
436
437 it.each(["open", "merged", "closed"] as const)(
438 "targets the top of a derived %s chain despite a later bottom update and link",
439 (state) => {
440 const bottom = link(2, {
441 snapshot: snapshot({ state, headBranch: "base", updatedAt: "2026-02-01T00:00:00.000Z" }),
442 });
443 const top = link(1, {
444 snapshot: snapshot({ state, headBranch: "top", baseBranch: "base" }),
445 });
446 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject(
447 state === "open"
448 ? { kind: "stack", top: { number: 1 }, open: [top, bottom] }
449 : { kind: "single", link: { number: 1 } },
450 );
451 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
452 kind: "stack",
453 layers: 2,
454 state,
455 });
456 },
457 );
458
459 it("keeps the top native layer after completion and treats an unsynced layer as open", () => {
460 const stack = {
461 kind: "native" as const,
462 id: "native-1",
463 number: 1,
464 url: "https://github.com/pingdotgg/t3code/stacks/1",
465 base: "main",
466 layers: [
467 { number: 2, headBranch: "base", state: "merged" as const },
468 { number: 1, headBranch: "top", state: "merged" as const },
469 ],
470 };
471 const bottom = link(2, {
472 stack,
473 snapshot: snapshot({ state: "merged", updatedAt: "2026-02-01T00:00:00.000Z" }),
474 });
475 const top = link(1, { stack, snapshot: snapshot({ state: "merged" }) });
476 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject({
477 kind: "single",
478 link: { number: 1 },
479 });
480 expect(resolveThreadPullRequestBadge([bottom, { ...top, snapshot: null }])).toEqual({
481 kind: "stack",
482 layers: 2,
483 state: "open",
484 });
485 });
486
487 it("uses aggregate state and distinguishes unrelated work from a stack", () => {
488 const bottom = link(1, { snapshot: snapshot({ state: "merged", headBranch: "base" }) });
489 const top = link(2, { snapshot: snapshot({ state: "closed", baseBranch: "base" }) });
490 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
491 kind: "stack",
492 layers: 2,
493 state: "closed",
494 });
495 expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({
496 kind: "pull-request",
497 others: 2,
498 state: "open",
499 });
500 expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull();
501 });
502
503 it("keeps branch matching case-sensitive while ignoring repository case", () => {
504 const bottom = link(1, {
505 repository: "PingDotGG/T3code",
506 snapshot: snapshot({ headBranch: "Base" }),
507 });
508 const top = link(2, { snapshot: snapshot({ headBranch: "top", baseBranch: "base" }) });
509 expect(resolveThreadPullRequestChains([bottom, top])).toHaveLength(2);
510 expect(
511 resolveThreadPullRequestChains([
512 bottom,
513 { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) },
514 ])[0]?.layers,
515 ).toEqual([bottom, { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) }]);
516 });
517
518 it("preserves cyclic links without presenting a guessed stack order", () => {
519 const links = [
520 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "b" }) }),
521 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
522 ];
523 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
524 links.map((entry) => [entry]),
525 );
526 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
527 kind: "stack",
528 top: { number: 2 },
529 });
530 expect(resolveThreadPullRequestBadge(links)).toEqual({
531 kind: "pull-request",
532 others: 1,
533 state: "open",
534 });
535 });
536
537 it("lists a pull request once when two links share its identity", () => {
538 const links = [
539 link(1, { snapshot: snapshot({ headBranch: "a" }) }),
540 link(1, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
541 link(1, { snapshot: snapshot({ headBranch: "c" }) }),
542 ];
543 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual([
544 [links[0]],
545 ]);
546 });
547
548 it("does not guess a parent when a head branch was reused", () => {
549 const links = [
550 link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
551 link(2, { snapshot: snapshot({ headBranch: "reused" }) }),
552 link(3, { snapshot: snapshot({ headBranch: "top", baseBranch: "reused" }) }),
553 ];
554 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
555 links.map((entry) => [entry]),
556 );
557 });
558});
559
560describe("threadPullRequestSearchTerms", () => {
561 it.each([undefined, [], [link(34)]])(
562 "searches the branch PR alongside existing links (%j)",
563 (pullRequests) => {
564 const branchPullRequest = {
565 projectId: ProjectId.make("project"),
566 repository: "pingdotgg/t3code",
567 number: 11809,
568 url: "https://github.com/pingdotgg/t3code/pull/11809",
569 };
570 const terms = threadPullRequestSearchTerms({
571 pullRequests,
572 branchPullRequest,
573 linkedPullRequest: {
574 ...branchPullRequest,
575 number: 12,
576 url: "https://github.com/pingdotgg/t3code/pull/12",
577 },
578 });
579 for (const query of ["11809", "#11809", "pingdotgg/t3code#11809", branchPullRequest.url]) {
580 expect(terms.some((term) => term.includes(query))).toBe(true);
581 }
582 expect(terms).toContain(pullRequests?.length ? "#34" : "#12");
583 },
584 );
585
586 it("includes completed and unsynced links but excludes dismissed links", () => {
587 const terms = threadPullRequestSearchTerms({
588 pullRequests: [
589 link(12, { snapshot: snapshot({ title: "Fix login", state: "merged" }) }),
590 link(34),
591 link(56, { source: "stack-dismissed" }),
592 ],
593 });
594 expect(terms).toContain("#12");
595 expect(terms).toContain("pingdotgg/t3code#12");
596 expect(terms).toContain("https://github.com/pingdotgg/t3code/pull/12");
597 expect(terms).toContain("Fix login");
598 expect(terms).toContain("#34");
599 expect(terms.join(" ")).not.toContain("56");
600 });
601});
602
603it("searches the legacy projection when old environments decode to an empty links list", () => {
604 const linkedPullRequest = {
605 projectId: ProjectId.make("project"),
606 repository: "pingdotgg/t3code",
607 number: 12,
608 url: "https://github.com/pingdotgg/t3code/pull/12",
609 };
610 expect(threadPullRequestSearchTerms({ pullRequests: [], linkedPullRequest })).toContain("#12");
611 expect(
612 threadPullRequestSearchTerms({ pullRequests: [link(34)], linkedPullRequest }),
613 ).not.toContain("#12");
614});