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

issueList.test.ts

427 lines15.1 KB

1import type {
2 EnvironmentId,
3 IssueListEntry,
4 IssueListResult,
5 IssueTrackerKind,
6 IssueTrackerSummary,
7 ProjectId,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "vite-plus/test";
10​
11import {
12 continueIssuePage,
13 createIssuePageRereader,
14 groupIssues,
15 holdIssuePages,
16 issueCursorsForHost,
17 issueFilterScopes,
18 issueMoreFailureForHost,
19 rereadIssuePage,
20 type HeldIssuePage,
21} from "./issueList.ts";
22​
23const environmentId = "env-1" as EnvironmentId;
24const api = "api" as ProjectId;
25​
26function entry(overrides: Partial<IssueListEntry> & { key: string }): IssueListEntry {
27 return {
28 tracker: "github" as IssueTrackerKind,
29 host: "github.com",
30 scope: "acme/api",
31 displayKey: `#${overrides.key}`,
32 title: `Issue ${overrides.key}`,
33 url: `https://github.com/acme/api/issues/${overrides.key}`,
34 author: null,
35 state: { category: "open", name: "open" },
36 createdAt: "2026-10-01T00:00:00.000Z",
37 updatedAt: "2026-10-01T00:00:00.000Z",
38 closedAt: null,
39 assignees: [],
40 labels: [],
41 commentCount: 0,
42 projectId: api,
43 projectTitle: "api",
44 ...overrides,
45 };
47​
48function answer(overrides: Partial<IssueListResult>): IssueListResult {
49 return { viewers: {}, trackers: [], entries: [], errors: [], nextCursors: {}, ...overrides };
51​
52function hold(first: IssueListResult) {
53 return holdIssuePages([[environmentId, first]], new Map()).get(environmentId)!;
55​
56describe("continueIssuePage", () => {
57 const failure = { projectId: api, projectTitle: "api", message: "rate limited" };
58 const firstPageFailure = { projectId: "web" as ProjectId, projectTitle: "web", message: "boom" };
59 const cursors = { "github.com acme/api": "c1", "linear.app ENG": "l1" };
60​
61 it("drops a continuation's error once a retry of the same cursors works", () => {
62 const first = hold(answer({ errors: [firstPageFailure], nextCursors: cursors }));
63 const failed = continueIssuePage(
64 first,
65 answer({ errors: [failure], nextCursors: cursors }),
66 cursors,
67 );
68 expect(failed.errors.map((error) => error.message)).toEqual(["boom", "rate limited"]);
69 // The server keeps a failed scope's cursor, so only a retry may ask for that page again.
70 expect(failed.moreFailed).toBe(true);
71 const retried = continueIssuePage(failed, answer({ entries: [entry({ key: "2" })] }), cursors);
72 // The first page's failure has no cursor, so no continuation retried it.
73 expect(retried.errors.map((error) => error.message)).toEqual(["boom"]);
74 expect(retried.moreFailed).toBe(false);
75 });
76​
77 it("moves only the cursors asked for and keeps the other trackers' errors", () => {
78 const first = hold(answer({ nextCursors: cursors }));
79 const linear = issueCursorsForHost(cursors, "linear.app");
80 expect(linear).toEqual({ "linear.app ENG": "l1" });
81 const linearFailed = continueIssuePage(
82 first,
83 answer({ errors: [failure], nextCursors: linear }),
84 linear,
85 );
86 const github = issueCursorsForHost(linearFailed.nextCursors, "github.com");
87 const next = continueIssuePage(
88 linearFailed,
89 answer({ entries: [entry({ key: "2" })], nextCursors: { "github.com acme/api": "c2" } }),
90 github,
91 );
92 expect(next.nextCursors).toEqual({ "github.com acme/api": "c2", "linear.app ENG": "l1" });
93 expect(next.errors).toEqual([failure]);
94 const done = continueIssuePage(
95 next,
96 answer({}),
97 issueCursorsForHost(next.nextCursors, "github.com"),
98 );
99 expect(done.nextCursors).toEqual({ "linear.app ENG": "l1" });
100 });
101​
102 it("shows a failed Load more only under the tracker it asked for", () => {
103 const both = { "github.com owner/repo": "g2", "linear.app team": "l2" };
104 const first = hold(answer({ nextCursors: both }));
105 const githubFailed = continueIssuePage(
106 first,
107 { failure: "GitHub page failed" },
108 issueCursorsForHost(both, "github.com"),
109 );
110 expect(issueMoreFailureForHost(githubFailed, "github.com")).toBe("GitHub page failed");
111 expect(issueMoreFailureForHost(githubFailed, "linear.app")).toBeNull();
112 const linearFailed = continueIssuePage(
113 githubFailed,
114 { failure: "Linear page failed" },
115 issueCursorsForHost(both, "linear.app"),
116 );
117 const linearRetried = continueIssuePage(
118 linearFailed,
119 answer({}),
120 issueCursorsForHost(both, "linear.app"),
121 );
122 expect(issueMoreFailureForHost(linearRetried, "github.com")).toBe("GitHub page failed");
123 expect(issueMoreFailureForHost(linearRetried, "linear.app")).toBeNull();
124 });
125});
126​
127describe("holdIssuePages", () => {
128 it("keeps the loaded pages when a refresh after a ticket action changes the first page", () => {
129 const cursors = { "github.com acme/api": "page-2" };
130 const first = answer({ entries: [entry({ key: "3" })], nextCursors: cursors });
131 const continued = continueIssuePage(
132 hold(first),
133 answer({ entries: [entry({ key: "2" }), entry({ key: "1" })] }),
134 cursors,
135 );
136 // Closing #1 moves it to the top of the first page the refresh reads.
137 const refreshed = answer({
138 entries: [entry({ key: "1", updatedAt: "2026-10-02T00:00:00.000Z" }), entry({ key: "3" })],
139 nextCursors: cursors,
140 });
141 const held = holdIssuePages(
142 [[environmentId, refreshed]],
143 new Map([[environmentId, continued]]),
144 ).get(environmentId)!;
145 expect(held.entries.map((issue) => issue.key)).toContain("2");
146 });
147​
148 it("starts over when the list's filters change", () => {
149 const cursors = { "github.com acme/api": "page-2" };
150 const first = holdIssuePages(
151 [[environmentId, answer({ entries: [entry({ key: "3" })], nextCursors: cursors })]],
152 new Map(),
153 "open",
154 ).get(environmentId)!;
155 const continued = continueIssuePage(first, answer({ entries: [entry({ key: "2" })] }), cursors);
156 const closed = answer({ entries: [entry({ key: "1" })], nextCursors: cursors });
157 const held = holdIssuePages(
158 [[environmentId, closed]],
159 new Map([[environmentId, continued]]),
160 "closed",
161 ).get(environmentId)!;
162 expect(held.entries.map((issue) => issue.key)).toEqual(["1"]);
163 expect(held.nextCursors).toEqual(cursors);
164 });
165​
166 it("reads the loaded pages again, dropping a continuation ticket closed out of an open list", async () => {
167 const cursors = { "github.com acme/api": "page-2" };
168 const firstPage = [entry({ key: "4" }), entry({ key: "3" })];
169 const continued = continueIssuePage(
170 hold(answer({ entries: firstPage, nextCursors: cursors })),
171 answer({ entries: [entry({ key: "2" }), entry({ key: "1" })] }),
172 cursors,
173 );
174 // Closing #2 leaves the open first page as it was.
175 const refreshed = answer({ entries: firstPage, nextCursors: cursors });
176 const held = holdIssuePages(
177 [[environmentId, refreshed]],
178 new Map([[environmentId, continued]]),
179 ).get(environmentId)!;
180 expect(held.rereading).toBe(refreshed);
181 const asked: unknown[] = [];
182 const reread = await rereadIssuePage({ ...held, rereading: refreshed }, async (next) => {
183 asked.push(next);
184 return answer({ entries: [entry({ key: "1" })] });
185 });
186 expect(asked).toEqual([cursors]);
187 expect(reread.entries.map((issue) => issue.key)).toEqual(["4", "3", "1"]);
188 expect(reread.nextCursors).toEqual({});
189 expect(
190 holdIssuePages([[environmentId, refreshed]], new Map([[environmentId, reread]])).get(
191 environmentId,
192 ),
193 ).toBe(reread);
194 });
195});
196​
197describe("createIssuePageRereader", () => {
198 it("keeps the newest refresh when an older reread answers after it", async () => {
199 const cursors = { "github.com acme/api": "page-2" };
200 const continued = continueIssuePage(
201 hold(answer({ entries: [entry({ key: "4" }), entry({ key: "3" })], nextCursors: cursors })),
202 answer({ entries: [entry({ key: "2" }), entry({ key: "1" })] }),
203 cursors,
204 );
205 // Closing #4, then #3: B still pages, C fits on one page.
206 const b = answer({ entries: [entry({ key: "3" }), entry({ key: "2" })], nextCursors: cursors });
207 const c = answer({ entries: [entry({ key: "2" }), entry({ key: "1" })] });
208 let pages: ReadonlyMap<EnvironmentId, HeldIssuePage> = new Map([[environmentId, continued]]);
209 const reread = createIssuePageRereader();
210 const commit = (id: EnvironmentId, page: HeldIssuePage) => {
211 pages = new Map(pages).set(id, page);
212 };
213 let answerB: (value: IssueListResult) => void = () => {};
214 const slow = new Promise<IssueListResult>((resolve) => (answerB = resolve));
215 reread(holdIssuePages([[environmentId, b]], pages), () => slow, commit);
216 reread(holdIssuePages([[environmentId, c]], pages), () => slow, commit);
217 await Promise.resolve();
218 answerB(answer({ entries: [entry({ key: "1" })] }));
219 await slow;
220 await Promise.resolve();
221 const held = holdIssuePages([[environmentId, c]], pages).get(environmentId)!;
222 expect(held.rereading).toBeNull();
223 expect(held.entries.map((issue) => issue.key)).toEqual(["2", "1"]);
224 });
225});
226​
227describe("issueFilterScopes", () => {
228 const tracker = (overrides: Partial<IssueTrackerSummary>): IssueTrackerSummary =>
229 ({
230 tracker: "linear",
231 host: "linear.app",
232 projectCount: 1,
233 capabilities: { labels: true, assignees: true },
234 configured: true,
235 detail: null,
236 ...overrides,
237 }) as IssueTrackerSummary;
238 const linearRow = entry({
239 key: "ENG-1",
240 tracker: "linear" as IssueTrackerKind,
241 host: "linear.app",
242 scope: "ENG",
243 });
244​
245 it("offers a bound standalone tracker's scope, named by the rows it listed", () => {
246 const held = new Map([
247 [
248 environmentId,
249 hold(
250 answer({
251 trackers: [tracker({})],
252 entries: [linearRow, { ...linearRow, key: "ENG-2" }],
253 }),
254 ),
255 ],
256 ]);
257 expect(issueFilterScopes(held, [])).toEqual([
258 {
259 environmentId,
260 input: { projectId: api, tracker: "linear", host: "linear.app", scope: "ENG" },
261 },
262 ]);
263 });
264​
265 it("offers a bound tracker's scope the answer names when a search matches nothing", () => {
266 const scope = {
267 projectId: api,
268 tracker: "linear" as IssueTrackerKind,
269 host: "linear.app",
270 scope: "ENG",
271 };
272 const held = new Map([
273 [environmentId, hold(answer({ trackers: [tracker({})], entries: [], scopes: [scope] }))],
274 ]);
275 expect(issueFilterScopes(held, [])).toEqual([{ environmentId, input: scope }]);
276 });
277​
278 it("offers the same team key of two Linear accounts as two scopes", () => {
279 const first = {
280 projectId: api,
281 tracker: "linear" as IssueTrackerKind,
282 host: "linear.app",
283 scope: "ENG",
284 };
285 const second = hold(
286 answer({
287 trackers: [tracker({})],
288 scopes: [first, { ...first, projectId: "home" as ProjectId }],
289 }),
290 );
291 const other = hold(
292 answer({ trackers: [tracker({})], scopes: [{ ...first, projectId: "o" as ProjectId }] }),
293 );
294 // Older servers name no scopes; their rows stand in, once per team on that server.
295 const older = hold(
296 answer({ trackers: [tracker({})], entries: [linearRow, { ...linearRow, key: "ENG-2" }] }),
297 );
298 const count = (...pages: ReadonlyArray<ReturnType<typeof hold>>) =>
299 issueFilterScopes(
300 new Map(pages.map((page, index) => [`env-${index}` as EnvironmentId, page])),
301 [],
302 ).length;
303 expect([count(second), count(second, other), count(other, second)]).toEqual([2, 3, 3]);
304 expect([count(older), count(older, second), count(second, older)]).toEqual([1, 3, 3]);
305 });
306​
307 it("skips a tracker that cannot be read", () => {
308 const held = new Map([
309 [
310 environmentId,
311 hold(answer({ trackers: [tracker({ configured: false })], entries: [linearRow] })),
312 ],
313 ]);
314 expect(issueFilterScopes(held, [])).toEqual([]);
315 });
316});
317​
318describe("groupIssues", () => {
319 it("lists an issue once when two environments hold the same repository", () => {
320 const github = {
321 tracker: "github",
322 host: "github.com",
323 projectCount: 1,
324 capabilities: { labels: true, assignees: true },
325 configured: true,
326 detail: null,
327 } as IssueTrackerSummary;
328 const page = answer({
329 trackers: [github],
330 entries: [entry({ key: "1" }), entry({ key: "2" })],
331 });
332 const held = holdIssuePages(
333 [
334 [environmentId, page],
335 [
336 "env-2" as EnvironmentId,
337 answer({
338 ...page,
339 entries: page.entries.map((row) => ({ ...row, projectId: "other" as ProjectId })),
340 }),
341 ],
342 ],
343 new Map(),
344 );
345 const [group] = groupIssues(held, "", []);
346 expect(group!.rows.map((row) => row.displayKey)).toEqual(["#1", "#2"]);
347 });
348​
349 it("shows a repository's errors once when two environments hold it", () => {
350 const github = {
351 tracker: "github",
352 host: "github.com",
353 projectCount: 1,
354 capabilities: { labels: true, assignees: true },
355 configured: true,
356 detail: null,
357 } as IssueTrackerSummary;
358 const failing = (projectId: ProjectId) =>
359 answer({
360 trackers: [github],
361 // A fork fails its own and its upstream's scope separately.
362 errors: [
363 { projectId, projectTitle: "api (me/api)", message: "Repository not found." },
364 { projectId, projectTitle: "api (acme/api)", message: "Repository not found." },
365 ],
366 });
367 const other = "env-2" as EnvironmentId;
368 const repositoryIdentity = {
369 canonicalKey: "github.com/acme/api",
370 provider: "github",
371 displayName: "acme/api",
372 locator: { source: "git-remote", remoteName: "origin", remoteUrl: "git@github.com:acme/api" },
373 };
374 const held = holdIssuePages(
375 [
376 [environmentId, failing(api)],
377 [other, failing("other" as ProjectId)],
378 ],
379 new Map(),
380 );
381 const projects = [
382 { id: api, environmentId, repositoryIdentity },
383 { id: "other", environmentId: other, repositoryIdentity },
384 ] as never;
385 const [group] = groupIssues(held, "", projects);
386 expect(group!.errors.map((error) => `${error.projectTitle}: ${error.message}`)).toEqual([
387 "api (me/api): Repository not found.",
388 "api (acme/api): Repository not found.",
389 ]);
390 });
391​
392 it("keeps same-keyed issues from two Linear workspaces apart", () => {
393 const linear = {
394 tracker: "linear",
395 host: "linear.app",
396 projectCount: 1,
397 capabilities: { labels: true, assignees: true },
398 configured: true,
399 detail: null,
400 } as IssueTrackerSummary;
401 const inWorkspace = (workspace: string) =>
402 answer({
403 trackers: [linear],
404 entries: [
405 entry({
406 tracker: "linear" as IssueTrackerKind,
407 host: "linear.app",
408 scope: "ENG",
409 key: "ENG-1",
410 url: `https://linear.app/${workspace}/issue/ENG-1`,
411 }),
412 ],
413 });
414 const held = holdIssuePages(
415 [
416 [environmentId, inWorkspace("acme")],
417 ["env-2" as EnvironmentId, inWorkspace("globex")],
418 ],
419 new Map(),
420 );
421 const [group] = groupIssues(held, "", []);
422 expect(group!.rows.map((row) => row.url)).toEqual([
423 "https://linear.app/acme/issue/ENG-1",
424 "https://linear.app/globex/issue/ENG-1",
425 ]);
426 });
427});