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