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 groupIssues,
14 holdIssuePages,
15 issueCursorsForHost,
16 issueFilterScopes,
17} from "./issueList.ts";
18
19const environmentId = "env-1" as EnvironmentId;
20const api = "api" as ProjectId;
21
22function entry(overrides: Partial<IssueListEntry> & { key: string }): IssueListEntry {
23 return {
24 tracker: "github" as IssueTrackerKind,
25 host: "github.com",
26 scope: "acme/api",
27 displayKey: `#${overrides.key}`,
28 title: `Issue ${overrides.key}`,
29 url: `https://github.com/acme/api/issues/${overrides.key}`,
30 author: null,
31 state: { category: "open", name: "open" },
32 createdAt: "2026-10-01T00:00:00.000Z",
33 updatedAt: "2026-10-01T00:00:00.000Z",
34 closedAt: null,
35 assignees: [],
36 labels: [],
37 commentCount: 0,
38 projectId: api,
39 projectTitle: "api",
40 ...overrides,
41 };
42}
43
44function answer(overrides: Partial<IssueListResult>): IssueListResult {
45 return { viewers: {}, trackers: [], entries: [], errors: [], nextCursors: {}, ...overrides };
46}
47
48function hold(first: IssueListResult) {
49 return holdIssuePages([[environmentId, first]], new Map()).get(environmentId)!;
50}
51
52describe("continueIssuePage", () => {
53 const failure = { projectId: api, projectTitle: "api", message: "rate limited" };
54 const firstPageFailure = { projectId: "web" as ProjectId, projectTitle: "web", message: "boom" };
55 const cursors = { "github.com acme/api": "c1", "linear.app ENG": "l1" };
56
57 it("drops a continuation's error once a retry of the same cursors works", () => {
58 const first = hold(answer({ errors: [firstPageFailure], nextCursors: cursors }));
59 const failed = continueIssuePage(
60 first,
61 answer({ errors: [failure], nextCursors: cursors }),
62 cursors,
63 );
64 expect(failed.errors.map((error) => error.message)).toEqual(["boom", "rate limited"]);
65 const retried = continueIssuePage(failed, answer({ entries: [entry({ key: "2" })] }), cursors);
66 // The first page's failure has no cursor, so no continuation retried it.
67 expect(retried.errors.map((error) => error.message)).toEqual(["boom"]);
68 });
69
70 it("moves only the cursors asked for and keeps the other trackers' errors", () => {
71 const first = hold(answer({ nextCursors: cursors }));
72 const linear = issueCursorsForHost(cursors, "linear.app");
73 expect(linear).toEqual({ "linear.app ENG": "l1" });
74 const linearFailed = continueIssuePage(
75 first,
76 answer({ errors: [failure], nextCursors: linear }),
77 linear,
78 );
79 const github = issueCursorsForHost(linearFailed.nextCursors, "github.com");
80 const next = continueIssuePage(
81 linearFailed,
82 answer({ entries: [entry({ key: "2" })], nextCursors: { "github.com acme/api": "c2" } }),
83 github,
84 );
85 expect(next.nextCursors).toEqual({ "github.com acme/api": "c2", "linear.app ENG": "l1" });
86 expect(next.errors).toEqual([failure]);
87 const done = continueIssuePage(
88 next,
89 answer({}),
90 issueCursorsForHost(next.nextCursors, "github.com"),
91 );
92 expect(done.nextCursors).toEqual({ "linear.app ENG": "l1" });
93 });
94});
95
96describe("issueFilterScopes", () => {
97 const tracker = (overrides: Partial<IssueTrackerSummary>): IssueTrackerSummary =>
98 ({
99 tracker: "linear",
100 host: "linear.app",
101 projectCount: 1,
102 capabilities: { labels: true, assignees: true },
103 configured: true,
104 detail: null,
105 ...overrides,
106 }) as IssueTrackerSummary;
107 const linearRow = entry({
108 key: "ENG-1",
109 tracker: "linear" as IssueTrackerKind,
110 host: "linear.app",
111 scope: "ENG",
112 });
113
114 it("offers a bound standalone tracker's scope, named by the rows it listed", () => {
115 const held = new Map([
116 [
117 environmentId,
118 hold(
119 answer({
120 trackers: [tracker({})],
121 entries: [linearRow, { ...linearRow, key: "ENG-2" }],
122 }),
123 ),
124 ],
125 ]);
126 expect(issueFilterScopes(held, [])).toEqual([
127 {
128 environmentId,
129 input: { projectId: api, tracker: "linear", host: "linear.app", scope: "ENG" },
130 },
131 ]);
132 });
133
134 it("offers a bound tracker's scope the answer names when a search matches nothing", () => {
135 const scope = {
136 projectId: api,
137 tracker: "linear" as IssueTrackerKind,
138 host: "linear.app",
139 scope: "ENG",
140 };
141 const held = new Map([
142 [environmentId, hold(answer({ trackers: [tracker({})], entries: [], scopes: [scope] }))],
143 ]);
144 expect(issueFilterScopes(held, [])).toEqual([{ environmentId, input: scope }]);
145 });
146
147 it("offers the same team key of two Linear accounts as two scopes", () => {
148 const first = {
149 projectId: api,
150 tracker: "linear" as IssueTrackerKind,
151 host: "linear.app",
152 scope: "ENG",
153 };
154 const second = hold(
155 answer({
156 trackers: [tracker({})],
157 scopes: [first, { ...first, projectId: "home" as ProjectId }],
158 }),
159 );
160 const other = hold(
161 answer({ trackers: [tracker({})], scopes: [{ ...first, projectId: "o" as ProjectId }] }),
162 );
163 // Older servers name no scopes; their rows stand in, once per team on that server.
164 const older = hold(
165 answer({ trackers: [tracker({})], entries: [linearRow, { ...linearRow, key: "ENG-2" }] }),
166 );
167 const count = (...pages: ReadonlyArray<ReturnType<typeof hold>>) =>
168 issueFilterScopes(
169 new Map(pages.map((page, index) => [`env-${index}` as EnvironmentId, page])),
170 [],
171 ).length;
172 expect([count(second), count(second, other), count(other, second)]).toEqual([2, 3, 3]);
173 expect([count(older), count(older, second), count(second, older)]).toEqual([1, 3, 3]);
174 });
175
176 it("skips a tracker that cannot be read", () => {
177 const held = new Map([
178 [
179 environmentId,
180 hold(answer({ trackers: [tracker({ configured: false })], entries: [linearRow] })),
181 ],
182 ]);
183 expect(issueFilterScopes(held, [])).toEqual([]);
184 });
185});
186
187describe("groupIssues", () => {
188 it("lists an issue once when two environments hold the same repository", () => {
189 const github = {
190 tracker: "github",
191 host: "github.com",
192 projectCount: 1,
193 capabilities: { labels: true, assignees: true },
194 configured: true,
195 detail: null,
196 } as IssueTrackerSummary;
197 const page = answer({
198 trackers: [github],
199 entries: [entry({ key: "1" }), entry({ key: "2" })],
200 });
201 const held = holdIssuePages(
202 [
203 [environmentId, page],
204 [
205 "env-2" as EnvironmentId,
206 answer({
207 ...page,
208 entries: page.entries.map((row) => ({ ...row, projectId: "other" as ProjectId })),
209 }),
210 ],
211 ],
212 new Map(),
213 );
214 const [group] = groupIssues(held, "", []);
215 expect(group!.rows.map((row) => row.displayKey)).toEqual(["#1", "#2"]);
216 });
217
218 it("keeps same-keyed issues from two Linear workspaces apart", () => {
219 const linear = {
220 tracker: "linear",
221 host: "linear.app",
222 projectCount: 1,
223 capabilities: { labels: true, assignees: true },
224 configured: true,
225 detail: null,
226 } as IssueTrackerSummary;
227 const inWorkspace = (workspace: string) =>
228 answer({
229 trackers: [linear],
230 entries: [
231 entry({
232 tracker: "linear" as IssueTrackerKind,
233 host: "linear.app",
234 scope: "ENG",
235 key: "ENG-1",
236 url: `https://linear.app/${workspace}/issue/ENG-1`,
237 }),
238 ],
239 });
240 const held = holdIssuePages(
241 [
242 [environmentId, inWorkspace("acme")],
243 ["env-2" as EnvironmentId, inWorkspace("globex")],
244 ],
245 new Map(),
246 );
247 const [group] = groupIssues(held, "", []);
248 expect(group!.rows.map((row) => row.url)).toEqual([
249 "https://linear.app/acme/issue/ENG-1",
250 "https://linear.app/globex/issue/ENG-1",
251 ]);
252 });
253});