@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 366 lines 13 KB
Raw
1 import { describe, expect, it } from "@effect/vitest";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
5
6 import * as ForgejoCli from "./ForgejoCli.ts";
7 import * as ForgejoIssueTracker from "./ForgejoIssueTracker.ts";
8
9 const encode = (value: unknown) => JSON.stringify(value);
10
11 const ISSUE = {
12 number: 42,
13 title: "Composer loses focus",
14 body: "Typing drops focus.",
15 html_url: "https://forgejo.test:3000/acme/web/issues/42",
16 user: { login: "octocat", full_name: "Octo Cat", avatar_url: "" },
17 state: "open",
18 created_at: "2026-07-01T00:00:00+02:00",
19 updated_at: "2026-07-03T00:00:00Z",
20 closed_at: null,
21 assignees: [{ login: "theo" }],
22 labels: [{ id: 3, name: "bug", color: "d73a4a" }],
23 comments: 1,
24 is_locked: true,
25 };
26
27 /** Every endpoint the detail read touches, answered by path; stderr carries the `Link` header. */
28 function answer(path: string): { readonly stdout: string; readonly stderr?: string } {
29 const [route] = path.split("?");
30 switch (route) {
31 case "user":
32 return { stdout: encode({ login: "julius" }) };
33 case "repos/acme/web":
34 return {
35 stdout: encode({ full_name: "acme/web", permissions: { push: false, admin: false } }),
36 };
37 case "repos/acme/web/issues/42":
38 return { stdout: encode(ISSUE) };
39 case "repos/acme/web/issues/42/timeline":
40 // A second page, as an issue with a long history has: a reference from another repository.
41 if (path.endsWith("page=2")) {
42 return {
43 stdout: encode([
44 {
45 id: 5,
46 type: "pull_ref",
47 user: { login: "theo" },
48 created_at: "2026-07-02T03:00:00Z",
49 ref_issue: {
50 number: 50,
51 html_url: "https://forgejo.test:3000/acme/api/pulls/50",
52 repository: { full_name: "acme/api" },
53 pull_request: { merged: true },
54 },
55 },
56 ]),
57 };
58 }
59 if (!path.endsWith("page=1")) return { stdout: "[]" };
60 return {
61 stdout: encode([
62 {
63 id: 1,
64 type: "label",
65 body: "1",
66 user: { login: "theo" },
67 created_at: "2026-07-01T01:00:00Z",
68 label: { id: 3, name: "bug" },
69 },
70 {
71 id: 2,
72 type: "comment",
73 body: "Repro attached.",
74 user: { login: "julius" },
75 created_at: "2026-07-02T00:00:00Z",
76 },
77 {
78 id: 3,
79 type: "pull_ref",
80 user: { login: "theo" },
81 created_at: "2026-07-02T01:00:00Z",
82 ref_issue: {
83 number: 50,
84 html_url: "https://forgejo.test:3000/acme/web/pulls/50",
85 pull_request: { merged: false },
86 },
87 },
88 { id: 4, type: "milestone", user: { login: "theo" }, created_at: "2026-07-02T02:00:00Z" },
89 ]),
90 };
91 case "repos/acme/web/issues/42/reactions":
92 return { stdout: encode([{ content: "eyes", user: { login: "theo" } }]) };
93 case "repos/acme/web/issues/42/comments":
94 return {
95 stdout: encode([
96 {
97 id: 7,
98 body: "Repro attached.",
99 user: { login: "julius" },
100 created_at: "2026-07-02T00:00:00Z",
101 html_url: "https://forgejo.test:3000/acme/web/issues/42#issuecomment-7",
102 },
103 ]),
104 };
105 case "repos/acme/web/issues/comments/7/reactions":
106 return {
107 stdout: encode([
108 { content: "+1", user: { login: "julius" } },
109 { content: "+1", user: { login: "theo" } },
110 ]),
111 };
112 case "repos/acme/api/pulls/50":
113 return {
114 stdout: encode({
115 number: 50,
116 title: "fix(api): accept the pasted payload",
117 html_url: "https://forgejo.test:3000/acme/api/pulls/50",
118 state: "closed",
119 merged: true,
120 }),
121 };
122 case "repos/acme/web/pulls/50":
123 return {
124 stdout: encode({
125 number: 50,
126 title: "fix(web): keep composer focus",
127 html_url: "https://forgejo.test:3000/acme/web/pulls/50",
128 state: "open",
129 merged: false,
130 draft: true,
131 }),
132 };
133 default:
134 throw new Error(`unexpected path ${path}`);
135 }
136 }
137
138 function tracker(
139 onApi: (input: ForgejoCli.ForgejoApiInput) => {
140 readonly stdout: string;
141 readonly stderr?: string;
142 },
143 ) {
144 return ForgejoIssueTracker.make.pipe(
145 Effect.provide(
146 Layer.mock(ForgejoCli.ForgejoCli)({
147 api: (input) => {
148 const { stdout, stderr } = onApi(input);
149 return Effect.succeed(
150 TestSourceControlHost.processOutput(stdout, { stderr: stderr ?? "" }),
151 );
152 },
153 }),
154 ),
155 );
156 }
157
158 describe("ForgejoIssueTracker", () => {
159 it.effect("joins the issue, timeline and conversation into the neutral issue shape", () =>
160 Effect.gen(function* () {
161 const hosts = new Set<string | undefined>();
162 const api = yield* tracker((input) => {
163 hosts.add(input.host);
164 return answer(input.path);
165 });
166 const issue = yield* api.getIssue({
167 cwd: "/w",
168 host: "forgejo.test:3000",
169 scope: "acme/web",
170 key: "42",
171 });
172 // The custom host and port reach the CLI, which resolves the login for them.
173 expect([...hosts]).toEqual(["forgejo.test:3000"]);
174 expect(issue).toMatchObject({
175 key: "42",
176 displayKey: "#42",
177 state: { category: "open", name: "Open" },
178 states: [
179 { category: "open", name: "Open" },
180 { category: "closed", name: "Closed" },
181 ],
182 author: { login: "octocat", name: "Octo Cat", avatarUrl: null },
183 createdAt: "2026-06-30T22:00:00.000Z",
184 labels: [{ name: "bug", color: "d73a4a" }],
185 viewer: "julius",
186 // No push and not the author: nothing but reading. Locked, so not even a comment.
187 viewerPermissions: {
188 setState: false,
189 comment: false,
190 edit: false,
191 labels: false,
192 assignees: false,
193 },
194 reactions: [{ content: "eyes", count: 1, actors: ["theo"], viewerHasReacted: false }],
195 // `/comments` answered with everything it has, so nothing is left to page.
196 commentsCursor: null,
197 });
198 expect(issue.comments).toEqual([
199 {
200 id: "7",
201 author: { login: "julius", name: null, avatarUrl: null },
202 body: "Repro attached.",
203 createdAt: "2026-07-02T00:00:00.000Z",
204 url: "https://forgejo.test:3000/acme/web/issues/42#issuecomment-7",
205 reactions: [{ content: "thumbs-up", count: 2, actors: ["theo"], viewerHasReacted: true }],
206 },
207 ]);
208 // Comments and milestones on the timeline are not events in the shared vocabulary.
209 expect(issue.events.map((event) => [event.kind, event.detail])).toEqual([
210 ["labeled", "bug"],
211 ["referenced", "#50"],
212 ["referenced", "#50"],
213 ]);
214 // The same number in another repository is its own pull request, read from there.
215 expect(issue.linkedPullRequests).toEqual([
216 {
217 repository: "acme/web",
218 number: 50,
219 title: "fix(web): keep composer focus",
220 url: "https://forgejo.test:3000/acme/web/pulls/50",
221 state: "open",
222 isDraft: true,
223 closesIssue: false,
224 },
225 {
226 repository: "acme/api",
227 number: 50,
228 title: "fix(api): accept the pasted payload",
229 url: "https://forgejo.test:3000/acme/api/pulls/50",
230 state: "merged",
231 isDraft: false,
232 closesIssue: false,
233 },
234 ]);
235 }),
236 );
237
238 it.effect("cuts the unpaged comments into pages and resolves labels past the first page", () =>
239 Effect.gen(function* () {
240 const calls: Array<readonly [string, string, unknown]> = [];
241 const api = yield* tracker((input) => {
242 calls.push([input.method ?? "GET", input.path, input.body]);
243 const [route] = input.path.split("?");
244 if (route === "user") return { stdout: encode({ login: "julius" }) };
245 if (route === "repos/acme/web/issues/42/comments") {
246 return {
247 stdout: encode(
248 Array.from({ length: 51 }, (_, index) => ({
249 id: index + 1,
250 body: `Comment ${index + 1}`,
251 user: { login: "theo" },
252 created_at: "2026-07-02T00:00:00Z",
253 html_url: "",
254 })),
255 ),
256 };
257 }
258 if (route === "repos/acme/web/labels") {
259 return input.path.endsWith("page=1")
260 ? {
261 stdout: encode([{ id: 3, name: "bug" }]),
262 stderr: 'link: <https://forgejo.test:3000/api/v1/x?page=2>; rel="next"\n',
263 }
264 : { stdout: encode([{ id: 9, name: "urgent" }]), stderr: "link: \n" };
265 }
266 return { stdout: "[]" };
267 });
268 const target = { cwd: "/w", host: "forgejo.test:3000", scope: "acme/web", key: "42" };
269 const page = yield* api.getComments({ ...target, cursor: "2" });
270 expect(page.comments.map((comment) => comment.id)).toEqual(["51"]);
271 expect(page.nextCursor).toBeNull();
272 expect(calls[1]?.[1]).toBe("repos/acme/web/issues/42/comments");
273
274 calls.length = 0;
275 yield* api.update!({ ...target, labels: ["bug", "urgent"] });
276 expect(calls.at(-1)).toEqual(["PUT", "repos/acme/web/issues/42/labels", { labels: [3, 9] }]);
277 // A name the repository lacks refuses the save before anything is written.
278 calls.length = 0;
279 const error = yield* Effect.flip(
280 api.update!({ ...target, title: "Renamed", labels: ["bug", "missing"] }),
281 );
282 expect(error.reason).toBe("failed");
283 expect(calls.map(([method]) => method)).toEqual(["GET", "GET"]);
284 }),
285 );
286
287 it.effect("moves an issue to a state by its name and refuses any other name", () =>
288 Effect.gen(function* () {
289 const writes: unknown[] = [];
290 const api = yield* tracker((input) => {
291 writes.push([input.method, input.path, input.body]);
292 return { stdout: "{}" };
293 });
294 const target = { cwd: "/w", host: "forgejo.test:3000", scope: "acme/web", key: "42" };
295 yield* api.setState!({ ...target, state: "Closed" });
296 yield* api.setState!({ ...target, state: "Open" });
297 // The category is not a state name: nothing is sent for it.
298 const error = yield* Effect.flip(api.setState!({ ...target, state: "closed" }));
299 expect(error).toMatchObject({ operation: "setState", reason: "failed" });
300 expect(writes).toEqual([
301 ["PATCH", "repos/acme/web/issues/42", { state: "closed" }],
302 ["PATCH", "repos/acme/web/issues/42", { state: "open" }],
303 ]);
304 }),
305 );
306
307 it.effect("lists issues by page and leaves pull requests out of the rows", () =>
308 Effect.gen(function* () {
309 const paths: string[] = [];
310 const api = yield* tracker((input) => {
311 paths.push(input.path);
312 return {
313 stdout: encode([ISSUE, { ...ISSUE, number: 43, pull_request: { merged: false } }]),
314 };
315 });
316 const page = yield* api.listIssues({
317 cwd: "/w",
318 host: "forgejo.test:3000",
319 scope: "acme/web",
320 state: "open",
321 involvement: "authored",
322 viewer: "julius",
323 sort: "updated",
324 limit: 50,
325 cursor: "2",
326 });
327 // Forgejo matches any of the labels it is sent, so a row missing one is dropped here.
328 const filtered = yield* api.listIssues({
329 cwd: "/w",
330 host: "forgejo.test:3000",
331 scope: "acme/web",
332 state: "open",
333 involvement: "all",
334 labels: ["bug", "p1"],
335 assignee: "theo",
336 sort: "created",
337 limit: 50,
338 });
339 expect(filtered.items).toEqual([]);
340 // One `assigned_by` cannot name two people: "assigned to me" is kept by dropping rows here.
341 const mine = {
342 cwd: "/w",
343 host: "forgejo.test:3000",
344 scope: "acme/web",
345 state: "open",
346 involvement: "assigned",
347 assignee: "theo",
348 sort: "created",
349 limit: 50,
350 } as const;
351 expect((yield* api.listIssues({ ...mine, viewer: "julius" })).items).toEqual([]);
352 expect(
353 (yield* api.listIssues({ ...mine, viewer: "Theo" })).items.map((item) => item.key),
354 ).toEqual(["42"]);
355 expect(paths).toEqual([
356 "repos/acme/web/issues?type=issues&state=open&sort=recentupdate&created_by=julius&limit=50&page=2",
357 "repos/acme/web/issues?type=issues&state=open&sort=latest&labels=bug%2Cp1&assigned_by=theo&limit=50&page=1",
358 "repos/acme/web/issues?type=issues&state=open&sort=latest&assigned_by=theo&limit=50&page=1",
359 "repos/acme/web/issues?type=issues&state=open&sort=latest&assigned_by=theo&limit=50&page=1",
360 ]);
361 expect(page.items.map((item) => item.key)).toEqual(["42"]);
362 // No `Link` header at all means the server may still have more; a full page carries on.
363 expect(page.nextCursor).toBe("3");
364 }),
365 );
366 });