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