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