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

threadPullRequests.test.ts

464 lines16.1 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 {
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 };
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 };
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});
246​
247describe("resolveThreadPullRequestChains", () => {
248 it("chains links by base → head within a repository, bottom to top", () => {
249 const chains = resolveThreadPullRequestChains([
250 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "b" }) }),
251 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
252 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
253 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
254 ]);
255 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
256 ["derived", [1, 2, 3]],
257 ["derived", [9]],
258 ]);
259 });
260​
261 it("uses the native stack order when the host provides one", () => {
262 const stack = {
263 kind: "native" as const,
264 id: "s1",
265 number: 1,
266 url: "https://github.com/pingdotgg/t3code/stacks/1",
267 base: "main",
268 layers: [
269 { number: 5, headBranch: "a", state: "merged" as const },
270 { number: 6, headBranch: "b", state: "open" as const },
271 ],
272 };
273 const chains = resolveThreadPullRequestChains([
274 link(6, { snapshot: snapshot({ headBranch: "b", baseBranch: "main" }), stack }),
275 link(5, {
276 snapshot: snapshot({ state: "merged", headBranch: "a", baseBranch: "main" }),
277 stack,
278 }),
279 link(8),
280 ]);
281 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
282 ["native", [5, 6]],
283 ["derived", [8]],
284 ]);
285 });
286});
287​
288describe("chain selection and badge state", () => {
289 it.each([
290 ["open", false, "open", false, "open"],
291 ["closed", false, "closed", false, "closed"],
292 ["open", true, "open", true, "draft"],
293 ["open", false, "open", true, "open"],
294 ["closed", false, "open", true, "open"],
295 ["merged", false, "merged", false, "merged"],
296 ["merged", false, "closed", true, "closed"],
297 ] as const)(
298 "aggregates %s (draft %s) and %s (draft %s) as %s",
299 (firstState, firstDraft, secondState, secondDraft, state) => {
300 for (const stacked of [false, true]) {
301 const links = [
302 link(1, {
303 snapshot: snapshot({ state: firstState, isDraft: firstDraft, headBranch: "base" }),
304 }),
305 link(2, {
306 snapshot: snapshot({
307 state: secondState,
308 isDraft: secondDraft,
309 baseBranch: stacked ? "base" : "main",
310 }),
311 }),
312 link(3, { source: "stack-dismissed" }),
313 ];
314 expect(resolveThreadPullRequestBadge(links)).toEqual(
315 stacked
316 ? { kind: "stack", layers: 2, state }
317 : { kind: "pull-request", others: 1, state },
318 );
319 }
320 },
321 );
322​
323 it.each(["open", "merged", "closed"] as const)(
324 "targets the top of a derived %s chain despite a later bottom update and link",
325 (state) => {
326 const bottom = link(2, {
327 snapshot: snapshot({ state, headBranch: "base", updatedAt: "2026-02-01T00:00:00.000Z" }),
328 });
329 const top = link(1, {
330 snapshot: snapshot({ state, headBranch: "top", baseBranch: "base" }),
331 });
332 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject(
333 state === "open"
334 ? { kind: "stack", top: { number: 1 }, open: [top, bottom] }
335 : { kind: "single", link: { number: 1 } },
336 );
337 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
338 kind: "stack",
339 layers: 2,
340 state,
341 });
342 },
343 );
344​
345 it("keeps the top native layer after completion and treats an unsynced layer as open", () => {
346 const stack = {
347 kind: "native" as const,
348 id: "native-1",
349 number: 1,
350 url: "https://github.com/pingdotgg/t3code/stacks/1",
351 base: "main",
352 layers: [
353 { number: 2, headBranch: "base", state: "merged" as const },
354 { number: 1, headBranch: "top", state: "merged" as const },
355 ],
356 };
357 const bottom = link(2, {
358 stack,
359 snapshot: snapshot({ state: "merged", updatedAt: "2026-02-01T00:00:00.000Z" }),
360 });
361 const top = link(1, { stack, snapshot: snapshot({ state: "merged" }) });
362 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject({
363 kind: "single",
364 link: { number: 1 },
365 });
366 expect(resolveThreadPullRequestBadge([bottom, { ...top, snapshot: null }])).toEqual({
367 kind: "stack",
368 layers: 2,
369 state: "open",
370 });
371 });
372​
373 it("uses aggregate state and distinguishes unrelated work from a stack", () => {
374 const bottom = link(1, { snapshot: snapshot({ state: "merged", headBranch: "base" }) });
375 const top = link(2, { snapshot: snapshot({ state: "closed", baseBranch: "base" }) });
376 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
377 kind: "stack",
378 layers: 2,
379 state: "closed",
380 });
381 expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({
382 kind: "pull-request",
383 others: 2,
384 state: "open",
385 });
386 expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull();
387 });
388​
389 it("keeps branch matching case-sensitive while ignoring repository case", () => {
390 const bottom = link(1, {
391 repository: "PingDotGG/T3code",
392 snapshot: snapshot({ headBranch: "Base" }),
393 });
394 const top = link(2, { snapshot: snapshot({ headBranch: "top", baseBranch: "base" }) });
395 expect(resolveThreadPullRequestChains([bottom, top])).toHaveLength(2);
396 expect(
397 resolveThreadPullRequestChains([
398 bottom,
399 { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) },
400 ])[0]?.layers,
401 ).toEqual([bottom, { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) }]);
402 });
403​
404 it("preserves cyclic links without presenting a guessed stack order", () => {
405 const links = [
406 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "b" }) }),
407 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
408 ];
409 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
410 links.map((entry) => [entry]),
411 );
412 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
413 kind: "stack",
414 top: { number: 2 },
415 });
416 expect(resolveThreadPullRequestBadge(links)).toEqual({
417 kind: "pull-request",
418 others: 1,
419 state: "open",
420 });
421 });
422​
423 it("does not guess a parent when a head branch was reused", () => {
424 const links = [
425 link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
426 link(2, { snapshot: snapshot({ headBranch: "reused" }) }),
427 link(3, { snapshot: snapshot({ headBranch: "top", baseBranch: "reused" }) }),
428 ];
429 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
430 links.map((entry) => [entry]),
431 );
432 });
433});
434​
435describe("threadPullRequestSearchTerms", () => {
436 it("includes completed and unsynced links but excludes dismissed links", () => {
437 const terms = threadPullRequestSearchTerms({
438 pullRequests: [
439 link(12, { snapshot: snapshot({ title: "Fix login", state: "merged" }) }),
440 link(34),
441 link(56, { source: "stack-dismissed" }),
442 ],
443 });
444 expect(terms).toContain("#12");
445 expect(terms).toContain("pingdotgg/t3code#12");
446 expect(terms).toContain("https://github.com/pingdotgg/t3code/pull/12");
447 expect(terms).toContain("Fix login");
448 expect(terms).toContain("#34");
449 expect(terms.join(" ")).not.toContain("56");
450 });
451});
452​
453it("searches the legacy projection when old environments decode to an empty links list", () => {
454 const linkedPullRequest = {
455 projectId: ProjectId.make("project"),
456 repository: "pingdotgg/t3code",
457 number: 12,
458 url: "https://github.com/pingdotgg/t3code/pull/12",
459 };
460 expect(threadPullRequestSearchTerms({ pullRequests: [], linkedPullRequest })).toContain("#12");
461 expect(
462 threadPullRequestSearchTerms({ pullRequests: [link(34)], linkedPullRequest }),
463 ).not.toContain("#12");
464});