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