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

ForgejoSourceControlProvider.test.ts

1,472 lines52.9 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9import * as HttpClient from "effect/http/HttpClient";
10import * as HttpClientRequest from "effect/http/HttpClientRequest";
11import * as HttpClientResponse from "effect/http/HttpClientResponse";
12import { VcsProcessSpawnError } from "@t3tools/contracts";
13import { firstNonEmptyLine } from "@t3tools/source-control-core/server/discovery";
14import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
15​
16import * as ForgejoCli from "./ForgejoCli.ts";
17import * as ForgejoPullRequestProvider from "./ForgejoPullRequestProvider.ts";
18import * as ForgejoSourceControlProvider from "./ForgejoSourceControlProvider.ts";
19​
20const processOutput = TestSourceControlHost.processOutput;
21​
22const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
23const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
24​
25const requestJson = (request: HttpClientRequest.HttpClientRequest): unknown => {
26 assert.strictEqual(request.body._tag, "Uint8Array");
27 return request.body._tag === "Uint8Array"
28 ? decodeJson(new TextDecoder().decode(request.body.body))
29 : undefined;
30};
31const encodeJsonEffect = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
32​
33it.effect("submits a Forgejo review without sending its summary in the preliminary GET", () => {
34 const methods: string[] = [];
35 const reviewClient = HttpClient.make((request) => {
36 methods.push(request.method);
37 if (request.method === "GET") {
38 assert.strictEqual(request.url, "https://forgejo.test/api/v1/repos/maria/project/pulls/42");
39 return Effect.succeed(
40 HttpClientResponse.fromWeb(
41 request,
42 new Response(
43 encodeJson({
44 number: 42,
45 title: "Review target",
46 body: "",
47 html_url: "https://forgejo.test/maria/project/pulls/42",
48 user: { login: "maria" },
49 state: "open",
50 merged: false,
51 head: { ref: "feature", sha: "head", repo: null },
52 base: { ref: "main", sha: "base", repo: null },
53 created_at: "2026-09-13T00:00:00Z",
54 updated_at: "2026-09-13T00:00:00Z",
55 closed_at: null,
56 merged_at: null,
57 labels: [],
58 }),
59 ),
60 ),
61 );
62 }
63 assert.strictEqual(request.method, "POST");
64 assert.strictEqual(
65 request.url,
66 "https://forgejo.test/api/v1/repos/maria/project/pulls/42/reviews",
67 );
68 assert.deepStrictEqual(requestJson(request), {
69 event: "COMMENT",
70 body: "Review summary",
71 commit_id: "head",
72 comments: [],
73 });
74 return Effect.succeed(
75 HttpClientResponse.fromWeb(request, new Response('{"id":1}', { status: 200 })),
76 );
77 });
78 return Effect.gen(function* () {
79 const cli = yield* ForgejoCli.make;
80 const provider = yield* ForgejoPullRequestProvider.make.pipe(
81 Effect.provideService(ForgejoCli.ForgejoCli, cli),
82 );
83 yield* provider.submitReview({
84 cwd: "/repo",
85 repository: "maria/project",
86 host: "forgejo.test",
87 number: 42,
88 verdict: "comment",
89 body: "Review summary",
90 comments: [],
91 });
92 assert.deepStrictEqual(methods, ["GET", "POST"]);
93 }).pipe(
94 Effect.provideService(HttpClient.HttpClient, reviewClient),
95 Effect.provideService(
96 FileSystem.FileSystem,
97 FileSystem.makeNoop({
98 exists: () => Effect.succeed(true),
99 readFileString: () =>
100 Effect.succeed(
101 encodeJson({ hosts: { "forgejo.test": { type: "Application", token: "test-token" } } }),
102 ),
103 }),
104 ),
105 Effect.provide(
106 TestSourceControlHost.layer({
107 process: {
108 run: (input) => {
109 if (input.command === "git") {
110 assert.deepStrictEqual(input.args, ["remote", "-v"]);
111 return Effect.succeed(
112 processOutput("origin\thttps://forgejo.test/maria/project.git (fetch)"),
113 );
114 }
115 assert.strictEqual(input.command, "fj");
116 assert.deepStrictEqual(input.args, ["--host", "https://forgejo.test", "whoami"]);
117 return Effect.succeed(processOutput(""));
118 },
119 },
120 }),
121 ),
122 );
123});
124​
125it.effect("reads the newest Forgejo checks without repository or viewer requests", () => {
126 const paths: string[] = [];
127 return Effect.gen(function* () {
128 const provider = yield* ForgejoPullRequestProvider.make;
129 const read = provider.getChangeRequestChecks;
130 if (read === undefined) return yield* Effect.die("checks read missing");
131 const result = yield* read({
132 cwd: "/repo",
133 repository: "acme/web",
134 host: "forgejo.test",
135 number: 1,
136 });
137 assert.strictEqual(result.state, "open");
138 assert.strictEqual(result.checks[0]?.status, "failure");
139 assert.deepStrictEqual(paths, [
140 "repos/acme/web/pulls/1",
141 "repos/acme/web/statuses/head?sort=recentupdate&limit=50&page=1",
142 "repos/acme/web/statuses/head?sort=recentupdate&limit=50&page=2",
143 ]);
144 }).pipe(
145 Effect.provide(
146 Layer.mock(ForgejoCli.ForgejoCli)({
147 api: (input) => {
148 paths.push(input.path);
149 assert.match(input.path, /^repos\/acme\/web\/(pulls\/1|statuses\/head)/);
150 return Effect.succeed(
151 processOutput(
152 input.path.endsWith("pulls/1")
153 ? `{"number":1,"title":"Checks","body":"","html_url":"https://forgejo.test/acme/web/pulls/1", "user":null,"state":"open","merged":false,
154 "head":{"ref":"feature","sha":"head","repo":null},"base":{"ref":"main","sha":"base","repo":null},
155 "created_at":"2026-09-16T00:00:00Z","updated_at":"2026-09-16T00:00:00Z","closed_at":null,"merged_at":null,"labels":[]}`
156 : input.path.endsWith("page=1")
157 ? `[{"context":"build","status":"failure","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00.500Z"},{"context":"build","status":"success","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00Z"}]`
158 : "[]",
159 ),
160 );
161 },
162 }),
163 ),
164 );
165});
166​
167it.effect("opens Forgejo Actions check links on the pull request's server", () =>
168 Effect.gen(function* () {
169 const provider = yield* ForgejoPullRequestProvider.make;
170 const read = provider.getChangeRequestChecks;
171 if (read === undefined) return yield* Effect.die("checks read missing");
172 const result = yield* read({
173 cwd: "/repo",
174 repository: "acme/web",
175 host: "forgejo.test",
176 number: 1,
177 });
178 assert.deepStrictEqual(
179 result.checks.map((check) => [check.name, check.url]),
180 [
181 ["actions", "https://forgejo.test/git/acme/web/actions/runs/7/jobs/2"],
182 ["external", "https://ci.example/build/9"],
183 ["none", null],
184 ],
185 );
186 }).pipe(
187 Effect.provide(
188 Layer.mock(ForgejoCli.ForgejoCli)({
189 api: (input) => {
190 const status = (context: string, target_url: string | null) => ({
191 context,
192 status: "success",
193 description: null,
194 target_url,
195 updated_at: "2026-10-11T00:00:00Z",
196 });
197 const body = input.path.endsWith("pulls/1")
198 ? {
199 number: 1,
200 title: "Checks",
201 body: "",
202 html_url: "https://forgejo.test/git/acme/web/pulls/1",
203 user: null,
204 state: "open",
205 merged: false,
206 head: { ref: "feature", sha: "head", repo: null },
207 base: { ref: "main", sha: "base", repo: null },
208 created_at: "2026-10-11T00:00:00Z",
209 updated_at: "2026-10-11T00:00:00Z",
210 closed_at: null,
211 merged_at: null,
212 labels: [],
213 }
214 : input.path.endsWith("page=1")
215 ? [
216 // Forgejo includes any sub-path mount in the relative links it reports.
217 status("actions", "/git/acme/web/actions/runs/7/jobs/2"),
218 status("external", "https://ci.example/build/9"),
219 status("none", null),
220 ]
221 : [];
222 return encodeJsonEffect(body).pipe(Effect.orDie, Effect.map(processOutput));
223 },
224 }),
225 ),
226 ),
227);
228​
229it.effect.each([
230 ["Ready", false, true, false, "mergeable"],
231 ["Draft", true, false, true, "unknown"],
232 ["Blocked", false, false, false, "unknown"],
233 ["Unchecked", false, undefined, false, "unknown"],
234 ["WIP: Legacy draft", undefined, false, true, "unknown"],
235 ["[WIP] Legacy draft", undefined, false, true, "unknown"],
236 ["WIP: Explicitly ready", false, false, false, "unknown"],
237] as const)(
238 "reads Forgejo mergeability for %s (draft=%s, mergeable=%s) across list, summary and detail",
239 ([title, draft, mergeable, isDraft, mergeability]) => {
240 const pr = {
241 number: 42,
242 title,
243 body: "",
244 html_url: "https://forgejo.test/maria/project/pulls/42",
245 user: { login: "maria" },
246 state: "open",
247 merged: false,
248 ...(draft === undefined ? {} : { draft }),
249 ...(mergeable === undefined ? {} : { mergeable }),
250 head: { ref: "feature", sha: "head", repo: null },
251 base: { ref: "main", sha: "base", repo: null },
252 created_at: "2026-09-16T00:00:00Z",
253 updated_at: "2026-09-16T00:00:00Z",
254 closed_at: null,
255 merged_at: null,
256 labels: [],
257 };
258 return Effect.gen(function* () {
259 const provider = yield* ForgejoPullRequestProvider.make;
260 const readSummary = provider.getChangeRequestSummary;
261 if (readSummary === undefined) return yield* Effect.die("summary read missing");
262 const input = { cwd: "/repo", repository: "maria/project", host: "forgejo.test", number: 42 };
263 const list = yield* provider.listChangeRequests({
264 ...input,
265 state: "open",
266 involvement: "all",
267 viewer: "maria",
268 limit: 10,
269 });
270 const summary = yield* readSummary(input);
271 const detail = yield* provider.getChangeRequest(input);
272 assert.strictEqual(list.items.length, 1);
273 assert.strictEqual(detail.headSha, "head");
274 for (const result of [list.items[0]!, summary, detail]) {
275 assert.strictEqual(result.mergeability, mergeability);
276 assert.strictEqual(result.isDraft, isDraft);
277 }
278 }).pipe(
279 Effect.provide(
280 Layer.mock(ForgejoCli.ForgejoCli)({
281 api: (input) => {
282 const [path, query] = input.path.split("?");
283 let response: unknown;
284 switch (path) {
285 case "repos/maria/project/pulls":
286 response = new URLSearchParams(query).get("page") === "1" ? [pr] : [];
287 break;
288 case "repos/maria/project/pulls/42":
289 response = pr;
290 break;
291 case "repos/maria/project":
292 response = { full_name: "maria/project", permissions: { push: true, admin: true } };
293 break;
294 case "user":
295 response = pr.user;
296 break;
297 case "repos/maria/project/statuses/head":
298 response = [];
299 break;
300 default:
301 return Effect.die(`Unexpected Forgejo request: ${input.path}`);
302 }
303 return encodeJsonEffect(response).pipe(Effect.orDie, Effect.map(processOutput));
304 },
305 }),
306 ),
307 );
308 },
309);
310​
311it.effect("gives Forgejo identities the browser URL of the login that serves them", () =>
312 Effect.gen(function* () {
313 const provider = yield* ForgejoSourceControlProvider.make;
314 const refine = provider.refineRepositoryIdentity;
315 assert.isDefined(refine);
316 const identity = (remoteUrl: string, provider?: string) => ({
317 canonicalKey: "forge.test/team/repo",
318 locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl },
319 rootPath: "/repo",
320 ...(provider === undefined ? {} : { provider }),
321 });
322 const served = (baseUrl: string) => () =>
323 Effect.succeed({
324 provider: {
325 kind: SourceControlProviderKind.make("forgejo"),
326 name: "Forgejo / Gitea",
327 baseUrl,
328 },
329 remoteName: "origin",
330 remoteUrl: "",
331 });
332​
333 assert.deepStrictEqual(
334 yield* refine!({
335 identity: identity("http://forge.test:3000/git/team/repo.git"),
336 resolveContext: served("http://forge.test:3000/git/"),
337 }),
338 {
339 ...identity("http://forge.test:3000/git/team/repo.git"),
340 provider: SourceControlProviderKind.make("forgejo"),
341 webUrl: "http://forge.test:3000/git/team/repo",
342 },
343 );
344 assert.strictEqual(
345 (yield* refine!({
346 identity: identity("ssh://git@ssh.forge.test/team/repo.git"),
347 resolveContext: served("http://forge.test:3000/git"),
348 })).webUrl,
349 "http://forge.test:3000/git/team/repo",
350 );
351 const github = identity("https://github.com/team/repo.git", "github");
352 assert.strictEqual(
353 yield* refine!({ identity: github, resolveContext: () => Effect.die("not asked") }),
354 github,
355 );
356 const unserved = identity("http://forge.test:3000/team/repo.git");
357 assert.strictEqual(
358 yield* refine!({ identity: unserved, resolveContext: () => Effect.succeed(null) }),
359 unserved,
360 );
361 }).pipe(
362 Effect.provide(
363 Layer.mergeAll(
364 Layer.succeed(FileSystem.FileSystem, FileSystem.makeNoop({})),
365 TestSourceControlHost.layer(),
366 Layer.mock(ForgejoCli.ForgejoCli)({}),
367 ),
368 ),
369 ),
370);
371​
372it.effect("loads Forgejo pull request references from files and commits views", () =>
373 Effect.gen(function* () {
374 const provider = yield* ForgejoSourceControlProvider.make;
375 for (const reference of [
376 "42",
377 "#42",
378 "https://forgejo.test/maria/project/pulls/42",
379 "https://forgejo.test/maria/project/pulls/42/",
380 "https://forgejo.test/maria/project/pulls/42/files?w=1#diff-1",
381 "http://forgejo.test:3000/git/maria/project/pulls/42/commits",
382 ]) {
383 const result = yield* provider.getChangeRequest({ cwd: "/repo", reference });
384 assert.strictEqual(result.number, 42);
385 assert.strictEqual(result.title, "Forgejo view reference");
386 }
387 const invalid = yield* provider
388 .getChangeRequest({
389 cwd: "/repo",
390 reference: "https://forgejo.test/maria/project/pulls/42invalid/files",
391 })
392 .pipe(Effect.result);
393 assert.strictEqual(invalid._tag, "Failure");
394 }).pipe(
395 Effect.provide(
396 Layer.mergeAll(
397 Layer.succeed(FileSystem.FileSystem, FileSystem.makeNoop({})),
398 TestSourceControlHost.layer({ process: {} }),
399 Layer.mock(ForgejoCli.ForgejoCli)({
400 resolveRepository: () =>
401 Effect.succeed({
402 login: "work",
403 repository: "maria/project",
404 baseUrl: "https://forgejo.test",
405 }),
406 api: (input) => {
407 assert.strictEqual(input.path, "repos/maria/project/pulls/42");
408 return encodeJsonEffect({
409 number: 42,
410 title: "Forgejo view reference",
411 html_url: "https://forgejo.test/maria/project/pulls/42",
412 state: "open",
413 merged: false,
414 base: { ref: "main", sha: "base", repo: null },
415 head: { ref: "feature", sha: "head", repo: null },
416 }).pipe(Effect.orDie, Effect.map(processOutput));
417 },
418 }),
419 ),
420 ),
421 ),
422);
423​
424it.effect(
425 "loads Forgejo reactions on comments, reviews and inline threads and resolves review mutations",
426 () => {
427 const user = { login: "maria" };
428 const review = {
429 id: 8,
430 body: "review body",
431 user,
432 state: "COMMENT",
433 submitted_at: "2026-09-12T00:00:00Z",
434 html_url: "https://forgejo.test/maria/project/pulls/2#issuecomment-37",
435 comments_count: 1,
436 };
437 const comment = { id: 12, body: "ordinary", user, created_at: review.submitted_at };
438 const responses: Record<string, unknown> = {
439 user,
440 "repos/maria/project/issues/2/comments": [comment],
441 "repos/maria/project/pulls/2/reviews": [review],
442 "repos/maria/project/pulls/2/reviews/8": review,
443 "repos/maria/project/pulls/2/reviews/9": { ...review, id: 9, html_url: "" },
444 "repos/maria/project/pulls/2/commits": [],
445 "repos/maria/project/issues/2/reactions": [],
446 "repos/maria/project/pulls/2/reviews/8/comments": [
447 {
448 ...comment,
449 id: 38,
450 body: "inline",
451 path: "file.ts",
452 position: 1,
453 original_position: 1,
454 commit_id: "head",
455 original_commit_id: "head",
456 resolver: null,
457 },
458 ],
459 "repos/maria/project/issues/comments/12/reactions": [{ content: "+1", user }],
460 "repos/maria/project/issues/comments/37/reactions": [{ content: "heart", user }],
461 "repos/maria/project/issues/comments/38/reactions": [
462 { content: "rocket", user: { login: "reviewer" } },
463 ],
464 };
465 const writes: ForgejoCli.ForgejoApiInput[] = [];
466 let reactionReads = 0;
467 return Effect.gen(function* () {
468 const provider = yield* ForgejoPullRequestProvider.make;
469 const input = { cwd: "/repo", repository: "maria/project", host: "forgejo.test", number: 2 };
470 const activity = yield* provider.getChangeRequestActivity(input);
471 assert.deepStrictEqual(
472 activity.comments.map((entry) => ({
473 id: entry.id,
474 kind: entry.kind,
475 reactions: entry.reactions,
476 })),
477 [
478 {
479 id: "12",
480 kind: "issue-comment",
481 reactions: [{ content: "thumbs-up", count: 1, actors: [], viewerHasReacted: true }],
482 },
483 {
484 id: "review:8",
485 kind: "review",
486 reactions: [{ content: "heart", count: 1, actors: [], viewerHasReacted: true }],
487 },
488 {
489 id: "38",
490 kind: "review-comment",
491 reactions: [
492 { content: "rocket", count: 1, actors: ["reviewer"], viewerHasReacted: false },
493 ],
494 },
495 ],
496 );
497 const inlineComment = activity.comments[2];
498 assert.ok(inlineComment);
499 assert.deepStrictEqual(activity.reviewThreads[0]?.comments, [inlineComment]);
500 for (const reacted of [true, false]) {
501 yield* provider.setReaction({ ...input, subjectId: "review:8", content: "heart", reacted });
502 yield* provider.setReaction({ ...input, subjectId: "38", content: "rocket", reacted });
503 }
504 assert.deepStrictEqual(
505 writes.map(({ path, method, body }) => ({ path, method, body })),
506 [
507 {
508 path: "repos/maria/project/issues/comments/37/reactions",
509 method: "POST",
510 body: { content: "heart" },
511 },
512 {
513 path: "repos/maria/project/issues/comments/38/reactions",
514 method: "POST",
515 body: { content: "rocket" },
516 },
517 {
518 path: "repos/maria/project/issues/comments/37/reactions",
519 method: "DELETE",
520 body: { content: "heart" },
521 },
522 {
523 path: "repos/maria/project/issues/comments/38/reactions",
524 method: "DELETE",
525 body: { content: "rocket" },
526 },
527 ],
528 );
529 const missing = yield* provider
530 .setReaction({ ...input, subjectId: "review:9", content: "heart", reacted: true })
531 .pipe(Effect.result);
532 assert.strictEqual(missing._tag, "Failure");
533 if (missing._tag === "Failure") assert.include(missing.failure.detail, "comment ID");
534 assert.strictEqual(writes.length, 4);
535 responses["repos/maria/project/pulls/2/reviews/8/comments"] = Array.from(
536 { length: 501 },
537 (_, index) => ({
538 ...comment,
539 id: 1000 + index,
540 path: "file.ts",
541 position: 1,
542 original_position: 1,
543 commit_id: "head",
544 original_commit_id: "head",
545 resolver: null,
546 }),
547 );
548 for (let index = 0; index < 500; index++) {
549 responses[`repos/maria/project/issues/comments/${1000 + index}/reactions`] = [];
550 }
551 reactionReads = 0;
552 const bounded = yield* provider.getChangeRequestActivity(input);
553 assert.strictEqual(bounded.reviewThreads.length, 500);
554 assert.strictEqual(
555 bounded.comments.filter((entry) => entry.kind === "review-comment").length,
556 500,
557 );
558 assert.strictEqual(bounded.commentsTruncated, true);
559 assert.strictEqual(reactionReads, 502);
560 }).pipe(
561 Effect.provide(
562 Layer.mock(ForgejoCli.ForgejoCli)({
563 api: (input) => {
564 if (input.method) {
565 writes.push(input);
566 return Effect.succeed(processOutput("{}"));
567 }
568 const path = input.path.split("?")[0]!;
569 if (/\/issues\/comments\/\d+\/reactions$/.test(path)) reactionReads++;
570 assert.ok(Object.hasOwn(responses, path), `Unexpected Forgejo request: ${path}`);
571 const page = Number(new URLSearchParams(input.path.split("?")[1]).get("page"));
572 return encodeJsonEffect(page > 1 ? [] : responses[path]).pipe(
573 Effect.orDie,
574 Effect.map(processOutput),
575 );
576 },
577 }),
578 ),
579 );
580 },
581);
582​
583it.effect("reads Forgejo pull request commits without their file lists", () => {
584 const paths: string[] = [];
585 return Effect.gen(function* () {
586 const provider = yield* ForgejoPullRequestProvider.make;
587 const activity = yield* provider.getChangeRequestActivity({
588 cwd: "/repo",
589 repository: "maria/project",
590 host: "forgejo.test",
591 number: 30,
592 });
593 assert.deepStrictEqual(
594 activity.commits.map((commit) => commit.oid),
595 ["abc"],
596 );
597 const commitReads = paths.filter((path) =>
598 path.startsWith("repos/maria/project/pulls/30/commits"),
599 );
600 assert.isNotEmpty(commitReads);
601 for (const path of commitReads) {
602 const query = new URLSearchParams(path.split("?")[1]);
603 assert.strictEqual(query.get("files"), "false");
604 assert.strictEqual(query.get("verification"), "false");
605 }
606 }).pipe(
607 Effect.provide(
608 Layer.mock(ForgejoCli.ForgejoCli)({
609 api: (input) => {
610 paths.push(input.path);
611 const [path, query] = input.path.split("?");
612 const page = Number(new URLSearchParams(query).get("page") ?? "1");
613 const body =
614 path === "user"
615 ? { login: "maria" }
616 : path === "repos/maria/project/pulls/30/commits" && page === 1
617 ? [
618 {
619 sha: "abc",
620 author: null,
621 commit: {
622 message: "Reorganize folders",
623 committer: { date: "2026-10-11T00:00:00Z" },
624 },
625 parents: [],
626 },
627 ]
628 : [];
629 return encodeJsonEffect(body).pipe(Effect.orDie, Effect.map(processOutput));
630 },
631 }),
632 ),
633 );
634});
635​
636it.effect("discovers Forgejo accounts and retains the server port", () =>
637 Effect.gen(function* () {
638 const auth = ForgejoSourceControlProvider.discovery.parseAuth(
639 processOutput(
640 yield* encodeJsonEffect([
641 {
642 name: "work",
643 url: "http://forgejo.local:3000",
644 ssh_host: "git.forgejo.local",
645 user: "maria",
646 default: "true",
647 valid: "true",
648 },
649 ]),
650 ),
651 );
652 assert.deepStrictEqual(
653 firstNonEmptyLine("\u001b[1mtea version 0.16.0\u001b[0m\n"),
654 Option.some("tea version 0.16.0"),
655 );
656 assert.strictEqual(auth.status, "authenticated");
657 assert.deepStrictEqual(auth.account, Option.some("maria"));
658 assert.deepStrictEqual(auth.host, Option.some("forgejo.local:3000"));
659 const revoked = ForgejoSourceControlProvider.discovery.parseAuth(
660 processOutput(
661 encodeJson([
662 {
663 name: "work",
664 url: "http://forgejo.local:3000",
665 user: "maria",
666 default: "true",
667 valid: "false",
668 },
669 ]),
670 ),
671 );
672 assert.strictEqual(revoked.status, "unauthenticated");
673 const refined = ForgejoSourceControlProvider.discovery.refineUnknownRemote({
674 cwd: "/repo",
675 context: {
676 provider: {
677 kind: SourceControlProviderKind.make("unknown"),
678 name: "git.forgejo.local",
679 baseUrl: "https://git.forgejo.local",
680 },
681 remoteName: "origin",
682 remoteUrl: "git@git.forgejo.local:maria/project.git",
683 },
684 auth: processOutput(
685 yield* encodeJsonEffect([
686 {
687 name: "work",
688 url: "http://forgejo.local:3000",
689 ssh_host: "git.forgejo.local",
690 user: "maria",
691 default: "true",
692 },
693 ]),
694 ),
695 });
696 assert.deepStrictEqual(refined, {
697 kind: SourceControlProviderKind.make("forgejo"),
698 name: "Forgejo / Gitea",
699 baseUrl: "http://forgejo.local:3000",
700 });
701 }),
702);
703​
704it.effect("does not choose a default Forgejo login across ambiguous SSH server ports", () =>
705 Effect.gen(function* () {
706 const logins = ForgejoCli.parseForgejoLogins(
707 yield* encodeJsonEffect([
708 {
709 name: "one",
710 url: "http://forgejo.local:3000",
711 ssh_host: "forgejo.local",
712 user: "maria",
713 default: "true",
714 },
715 {
716 name: "two",
717 url: "http://forgejo.local:4000",
718 ssh_host: "forgejo.local",
719 user: "maria",
720 default: "false",
721 },
722 ]),
723 );
724 const remote = ForgejoCli.parseForgejoRemote("git@forgejo.local:maria/project.git");
725 assert.isNotNull(remote);
726 assert.deepStrictEqual(
727 ForgejoCli.parseForgejoRemote("forgejo.local:maria/project.git"),
728 remote,
729 );
730 assert.isUndefined(ForgejoCli.matchForgejoLogin(logins, remote!));
731 assert.strictEqual(
732 ForgejoCli.matchForgejoLogin(logins, remote!, "forgejo.local:4000")?.name,
733 "two",
734 );
735 assert.isUndefined(ForgejoCli.matchForgejoLogin(logins, remote!, "other.local:4000"));
736 const alias = ForgejoCli.parseForgejoRemote("git@ssh.forgejo.local:maria/project.git");
737 assert.isNotNull(alias);
738 assert.isUndefined(ForgejoCli.matchForgejoLogin(logins, alias!, "forgejo.local:4000"));
739 const refined = ForgejoSourceControlProvider.discovery.refineUnknownRemote({
740 cwd: "/repo",
741 context: {
742 provider: {
743 kind: SourceControlProviderKind.make("unknown"),
744 name: "Forgejo",
745 baseUrl: "https://forgejo.local",
746 },
747 remoteName: "origin",
748 remoteUrl: "git@forgejo.local:maria/project.git",
749 requestedHost: "forgejo.local:4000",
750 },
751 auth: processOutput(yield* encodeJsonEffect(logins)),
752 });
753 assert.strictEqual(refined?.baseUrl, "http://forgejo.local:4000");
754 const https = ForgejoCli.parseForgejoRemote("http://forgejo.local:4000/maria/project.git");
755 assert.isNotNull(https);
756 assert.strictEqual(ForgejoCli.matchForgejoLogin(logins, https!)?.name, "two");
757 const hostOnly = ForgejoCli.parseForgejoRemote("http://forgejo.local:4000");
758 assert.strictEqual(
759 ForgejoCli.matchForgejoLogin(logins, hostOnly!, undefined, true)?.name,
760 "two",
761 );
762 const mounted = logins.map((login) => ({
763 ...login,
764 url: `http://forgejo.local:4000/${login.name}`,
765 }));
766 assert.isUndefined(ForgejoCli.matchForgejoLogin(mounted, hostOnly!, undefined, true));
767 }),
768);
769​
770it.effect("rejects HTTP failures even when tea exits successfully", () =>
771 Effect.gen(function* () {
772 const cli = yield* ForgejoCli.make;
773 const result = yield* cli
774 .api({
775 cwd: "/repo",
776 repository: "http://forgejo.local:3000/maria/project",
777 path: "repos/maria/project/pulls/42",
778 method: "PATCH",
779 body: { state: "closed" },
780 })
781 .pipe(Effect.result);
782 assert.strictEqual(result._tag, "Failure");
783 if (result._tag === "Failure")
784 assert.strictEqual(
785 result.failure.detail,
786 "Forgejo repository or pull request was not found.",
787 );
788 }).pipe(
789 Effect.provideService(
790 FileSystem.FileSystem,
791 FileSystem.makeNoop({
792 exists: () => Effect.succeed(false),
793 }),
794 ),
795 Effect.provideService(
796 HttpClient.HttpClient,
797 HttpClient.make(() => {
798 throw new Error("tea must handle its own HTTP request");
799 }),
800 ),
801 Effect.provide(
802 TestSourceControlHost.layer({
803 process: {
804 run: (input) => {
805 if (input.args[0] === "api") {
806 assert.strictEqual(input.stdin, '{"state":"closed"}');
807 assert.include(input.args, "work");
808 assert.include(
809 input.args,
810 "http://forgejo.local:3000/api/v1/repos/maria/project/pulls/42",
811 );
812 }
813 return Effect.succeed(
814 input.args[0] === "login"
815 ? processOutput(
816 encodeJson([
817 {
818 name: "work",
819 url: "http://forgejo.local:3000",
820 ssh_host: "forgejo.local",
821 user: "maria",
822 default: "true",
823 },
824 ]),
825 )
826 : processOutput('{"message":"not found"}', { stderr: "HTTP/1.1 404 Not Found\n" }),
827 );
828 },
829 },
830 }),
831 ),
832 ),
833);
834​
835it.effect("routes mounted Forgejo repositories without repeating the mount in API paths", () =>
836 Effect.gen(function* () {
837 const cli = yield* ForgejoCli.make;
838 const viewer = yield* cli.api({ cwd: "/upstream-only", host: "code.test", path: "user" });
839 assert.strictEqual(viewer.stdout, "[]");
840 const mountedRepository = yield* cli.resolveRepository({
841 cwd: "/upstream-only",
842 host: "code.test",
843 repository: "maria/project",
844 });
845 assert.strictEqual(mountedRepository.baseUrl, "https://code.test/forgejo");
846 assert.strictEqual(mountedRepository.repository, "maria/project");
847 for (const path of [
848 "repos/forgejo/maria/project/pulls?state=open",
849 "repos/forgejo/maria/project",
850 "repos/reviewer/project/contents/file.ts",
851 ]) {
852 const result = yield* cli.api({
853 cwd: "/repo",
854 repository: "forgejo/maria/project",
855 context: {
856 provider: {
857 kind: SourceControlProviderKind.make("forgejo"),
858 name: "Forgejo",
859 baseUrl: "https://code.test/forgejo",
860 },
861 remoteName: "origin",
862 remoteUrl: "https://code.test/forgejo/maria/project.git",
863 },
864 path,
865 });
866 assert.strictEqual(result.stdout, "[]");
867 }
868 const sameOwnerAsMount = yield* cli.resolveRepository({
869 cwd: "/repo",
870 repository: "forgejo/project",
871 context: {
872 provider: {
873 kind: SourceControlProviderKind.make("forgejo"),
874 name: "Forgejo",
875 baseUrl: "https://code.test/forgejo",
876 },
877 remoteName: "origin",
878 remoteUrl: "ssh://git@code.test/forgejo/project.git",
879 },
880 });
881 assert.strictEqual(sameOwnerAsMount.command, "tea");
882 assert.strictEqual(sameOwnerAsMount.repository, "forgejo/project");
883 }).pipe(
884 Effect.provideService(
885 FileSystem.FileSystem,
886 FileSystem.makeNoop({
887 exists: () => Effect.succeed(true),
888 readFileString: () =>
889 Effect.succeed(
890 encodeJson({
891 hosts: { "code.test/forgejo": { type: "Application", token: "test-token" } },
892 }),
893 ),
894 }),
895 ),
896 Effect.provideService(
897 HttpClient.HttpClient,
898 HttpClient.make(() => {
899 throw new Error("tea must handle its own HTTP request");
900 }),
901 ),
902 Effect.provide(
903 TestSourceControlHost.layer({
904 process: {
905 run: (input) => {
906 if (input.command === "git")
907 return Effect.succeed(
908 processOutput("", { exitCode: ChildProcessSpawner.ExitCode(2) }),
909 );
910 if (input.args[0] === "login")
911 return Effect.succeed(
912 processOutput(
913 encodeJson([
914 {
915 name: "mounted",
916 url: "https://code.test/forgejo",
917 ssh_host: "code.test",
918 user: "maria",
919 default: "true",
920 },
921 ]),
922 ),
923 );
924 const supported = [
925 "https://code.test/forgejo/api/v1/user",
926 "https://code.test/forgejo/api/v1/repos/maria/project/pulls?state=open",
927 "https://code.test/forgejo/api/v1/repos/maria/project",
928 "https://code.test/forgejo/api/v1/repos/reviewer/project/contents/file.ts",
929 ];
930 if (input.args.at(-1)?.endsWith("/user")) assert.notInclude(input.args, "--repo");
931 assert.strictEqual(input.command, "tea");
932 return Effect.succeed(
933 supported.includes(input.args.at(-1) ?? "")
934 ? processOutput("[]", { stderr: "HTTP/1.1 200 OK\n" })
935 : processOutput("{}", { stderr: "HTTP/1.1 404 Not Found\n" }),
936 );
937 },
938 },
939 }),
940 ),
941 ),
942);
943​
944it.effect("prefers fj for HTTP and ported SSH aliases on root servers", () => {
945 const commands: string[] = [];
946 const requests: string[] = [];
947 return Effect.gen(function* () {
948 const cli = yield* ForgejoCli.make;
949 for (const remoteUrl of [
950 "http://forgejo.local:3000/maria/project.git",
951 "ssh://git@ssh.forgejo.local:2222/maria/project.git",
952 "ssh://git@forgejo.local:2222/maria/project.git",
953 ]) {
954 const result = yield* cli.api({
955 cwd: "/repo",
956 repository: "maria/project",
957 context: {
958 provider: {
959 kind: SourceControlProviderKind.make("forgejo"),
960 name: "Forgejo",
961 baseUrl: "http://forgejo.local:3000",
962 },
963 remoteName: "origin",
964 remoteUrl,
965 requestedHost: "forgejo.local:3000",
966 },
967 path: "repos/maria/project/issues/42/comments",
968 method: "POST",
969 body: { body: "verified through fj" },
970 });
971 assert.strictEqual(result.stdout, '{"id":99}');
972 }
973 assert.deepStrictEqual(commands, ["fj"]);
974 assert.deepStrictEqual(requests, [
975 "http://forgejo.local:3000/api/v1/repos/maria/project/issues/42/comments",
976 "http://forgejo.local:3000/api/v1/repos/maria/project/issues/42/comments",
977 "http://forgejo.local:3000/api/v1/repos/maria/project/issues/42/comments",
978 ]);
979 const viewer = yield* cli.api({
980 cwd: "/no-remotes",
981 host: "forgejo.local:3000",
982 path: "user",
983 });
984 assert.strictEqual(viewer.stdout, '{"login":"maria"}');
985 assert.strictEqual(requests.at(-1), "https://forgejo.local:3000/api/v1/user");
986 const upstreamViewer = yield* cli.api({
987 cwd: "/upstream-only",
988 host: "forgejo.local:3000",
989 path: "user",
990 });
991 assert.strictEqual(upstreamViewer.stdout, '{"login":"maria"}');
992 assert.strictEqual(requests.at(-1), "http://forgejo.local:3000/api/v1/user");
993 const upstreamRepository = yield* cli.resolveRepository({
994 cwd: "/upstream-only",
995 host: "forgejo.local:3000",
996 repository: "maria/project",
997 });
998 assert.strictEqual(upstreamRepository.baseUrl, "http://forgejo.local:3000");
999 assert.strictEqual(upstreamRepository.repository, "maria/project");
1000 const httpViewer = yield* cli.api({ cwd: "/repo", host: "forgejo.local:3000", path: "user" });
1001 assert.strictEqual(httpViewer.stdout, '{"login":"maria"}');
1002 assert.strictEqual(requests.at(-1), "http://forgejo.local:3000/api/v1/user");
1003 }).pipe(
1004 Effect.provideService(
1005 FileSystem.FileSystem,
1006 FileSystem.makeNoop({
1007 exists: () => Effect.succeed(true),
1008 readFileString: () =>
1009 Effect.succeed(
1010 encodeJson({
1011 hosts: {
1012 "forgejo.local:3000": { type: "Application", token: "test-token" },
1013 "forgejo.local:4000": { type: "Application", token: "other-token" },
1014 },
1015 aliases: { "ssh.forgejo.local:2222": "forgejo.local:3000" },
1016 }),
1017 ),
1018 }),
1019 ),
1020 Effect.provideService(
1021 HttpClient.HttpClient,
1022 HttpClient.make((request) => {
1023 requests.push(request.url);
1024 if (request.url.endsWith("/user")) {
1025 assert.strictEqual(request.method, "GET");
1026 assert.strictEqual(request.headers.authorization, "token test-token");
1027 return Effect.succeed(
1028 HttpClientResponse.fromWeb(request, new Response('{"login":"maria"}')),
1029 );
1031 assert.strictEqual(request.method, "POST");
1032 assert.strictEqual(request.headers.authorization, "token test-token");
1033 assert.deepStrictEqual(requestJson(request), { body: "verified through fj" });
1034 return Effect.succeed(
1035 HttpClientResponse.fromWeb(request, new Response('{"id":99}', { status: 201 })),
1036 );
1037 }),
1038 ),
1039 Effect.provide(
1040 TestSourceControlHost.layer({
1041 process: {
1042 run: (input) => {
1043 commands.push(input.command);
1044 if (input.command === "git")
1045 return Effect.succeed(
1046 input.cwd === "/no-remotes"
1047 ? processOutput("", { exitCode: ChildProcessSpawner.ExitCode(2) })
1048 : processOutput(
1049 `${input.cwd === "/upstream-only" ? "upstream" : "origin"}\thttp://forgejo.local:3000/maria/project.git (fetch)\nother\thttp://forgejo.local:3000/maria/other.git (fetch)\nunrelated\thttp://other.local:3000/maria/project.git (fetch)`,
1050 ),
1051 );
1052 assert.strictEqual(input.command, "fj");
1053 assert.deepStrictEqual(input.args, [
1054 "--host",
1055 input.cwd === "/no-remotes"
1056 ? "https://forgejo.local:3000"
1057 : "http://forgejo.local:3000",
1058 "whoami",
1059 ]);
1060 return Effect.succeed(processOutput(""));
1061 },
1062 },
1063 }),
1064 ),
1065 );
1066});
1067​
1068it.effect("loads later fj review pages when the server caps pages below the requested size", () => {
1069 const pages: number[] = [];
1070 let issueCommentRequests = 0;
1071 return Effect.gen(function* () {
1072 const cli = yield* ForgejoCli.make;
1073 const provider = yield* ForgejoPullRequestProvider.make.pipe(
1074 Effect.provideService(ForgejoCli.ForgejoCli, cli),
1075 );
1076 const activity = yield* provider.getChangeRequestActivity({
1077 cwd: "/repo",
1078 repository: "maria/project",
1079 host: "forgejo.test",
1080 number: 42,
1081 });
1082 assert.strictEqual(activity.commentCount, 42);
1083 assert.strictEqual(activity.comments.at(-1)?.id, "review:41");
1084 assert.strictEqual(activity.commentsTruncated, false);
1085 assert.deepStrictEqual(pages, [1, 2, 3]);
1086 assert.strictEqual(issueCommentRequests, 1);
1087 }).pipe(
1088 Effect.provideService(
1089 FileSystem.FileSystem,
1090 FileSystem.makeNoop({
1091 exists: () => Effect.succeed(true),
1092 readFileString: () =>
1093 Effect.succeed(
1094 encodeJson({ hosts: { "forgejo.test": { type: "Application", token: "test-token" } } }),
1095 ),
1096 }),
1097 ),
1098 Effect.provideService(
1099 HttpClient.HttpClient,
1100 HttpClient.make((request) => {
1101 const url = new URL(request.url);
1102 if (url.pathname === "/api/v1/repos/maria/project/issues/42/comments") {
1103 issueCommentRequests++;
1104 return Effect.succeed(
1105 HttpClientResponse.fromWeb(
1106 request,
1107 new Response(
1108 encodeJson([
1110 id: 100,
1111 body: "Unpaginated issue comment",
1112 user: { login: "maria" },
1113 created_at: "2026-09-13T00:00:00Z",
1114 },
1115 ]),
1116 ),
1117 ),
1118 );
1120 if (url.pathname.endsWith("/reviews")) {
1121 const page = Number(url.searchParams.get("page"));
1122 pages.push(page);
1123 assert.ok(page >= 1 && page <= 3);
1124 const reviews = Array.from({ length: page < 3 ? 20 : 1 }, (_, index) => ({
1125 id: (page - 1) * 20 + index + 1,
1126 body: "Review from a capped page",
1127 user: { login: "maria" },
1128 state: "COMMENT",
1129 submitted_at: "2026-09-13T00:00:00Z",
1130 comments_count: 0,
1131 }));
1132 return Effect.succeed(
1133 HttpClientResponse.fromWeb(
1134 request,
1135 new Response(encodeJson(reviews), {
1136 headers:
1137 page === 2
1138 ? {}
1139 : {
1140 Link:
1141 page === 1
1142 ? `<${url.origin}${url.pathname}?limit=50&page=2>; rel="next"`
1143 : `<${url.origin}${url.pathname}?limit=50&page=1>; rel="prev"`,
1144 },
1145 }),
1146 ),
1147 );
1149 return Effect.succeed(
1150 HttpClientResponse.fromWeb(
1151 request,
1152 new Response(encodeJson(url.pathname === "/api/v1/user" ? { login: "maria" } : [])),
1153 ),
1154 );
1155 }),
1156 ),
1157 Effect.provide(
1158 TestSourceControlHost.layer({
1159 process: {
1160 run: (input) => {
1161 if (input.command === "git") {
1162 assert.deepStrictEqual(input.args, ["remote", "-v"]);
1163 return Effect.succeed(
1164 processOutput("origin\thttps://forgejo.test/maria/project.git (fetch)"),
1165 );
1167 assert.strictEqual(input.command, "fj");
1168 assert.deepStrictEqual(input.args, ["--host", "https://forgejo.test", "whoami"]);
1169 return Effect.succeed(processOutput(""));
1170 },
1171 },
1172 }),
1173 ),
1174 );
1175});
1176​
1177it.effect("falls back to tea when fj is missing or has no account for this server", () =>
1178 Effect.gen(function* () {
1179 for (const scenario of ["missing-cli", "missing-account", "stale-invalid-storage"] as const) {
1180 const commands: string[] = [];
1181 yield* Effect.gen(function* () {
1182 const cli = yield* ForgejoCli.make;
1183 const result = yield* cli.api({
1184 cwd: "/repo",
1185 repository: "https://forgejo.local:3000/maria/project",
1186 path: "repos/maria/project/pulls",
1187 });
1188 assert.strictEqual(result.stdout, "[]");
1189 assert.deepStrictEqual(
1190 commands,
1191 scenario === "missing-account" ? ["tea", "tea"] : ["fj", "tea", "tea"],
1192 );
1193 const viewer = yield* cli.api({
1194 cwd: "/upstream-only",
1195 host: "forgejo.local:3000",
1196 path: "user",
1197 });
1198 assert.strictEqual(viewer.stdout, "[]");
1199 }).pipe(
1200 Effect.provideService(
1201 FileSystem.FileSystem,
1202 FileSystem.makeNoop({
1203 exists: () => Effect.succeed(true),
1204 readFileString: () =>
1205 Effect.succeed(
1206 scenario === "stale-invalid-storage"
1207 ? "invalid json"
1208 : encodeJson({
1209 hosts: {
1210 [scenario === "missing-cli" ? "forgejo.local:3000" : "other.local"]: {
1211 type: "Application",
1212 token: "test-token",
1213 },
1214 },
1215 }),
1216 ),
1217 }),
1218 ),
1219 Effect.provideService(
1220 HttpClient.HttpClient,
1221 HttpClient.make(() => {
1222 throw new Error("tea must handle its own HTTP request");
1223 }),
1224 ),
1225 Effect.provide(
1226 TestSourceControlHost.layer({
1227 process: {
1228 run: (input) => {
1229 commands.push(input.command);
1230 if (input.command === "git")
1231 return Effect.succeed(
1232 processOutput("", { exitCode: ChildProcessSpawner.ExitCode(2) }),
1233 );
1234 if (input.command === "fj")
1235 return Effect.fail(
1236 new VcsProcessSpawnError({
1237 operation: input.operation,
1238 command: input.command,
1239 cwd: input.cwd,
1240 cause: new Error("fj not found"),
1241 }),
1242 );
1243 assert.strictEqual(input.command, "tea");
1244 if (input.args.at(-1)?.endsWith("/user")) assert.notInclude(input.args, "--repo");
1245 return Effect.succeed(
1246 input.args[0] === "login"
1247 ? processOutput(
1248 encodeJson([
1250 name: "work",
1251 url: "https://forgejo.local:3000",
1252 user: "maria",
1253 default: "true",
1254 valid: "true",
1255 },
1256 ]),
1258 : processOutput("[]", { stderr: "HTTP/1.1 200 OK\n" }),
1259 );
1260 },
1261 },
1262 }),
1263 ),
1264 );
1266 }),
1268​
1269it.effect("handles fj mutation statuses without retrying failures or reading absent bodies", () =>
1270 Effect.gen(function* () {
1271 for (const status of [204, 205, 302, 401, 403, 404, 429, 500]) {
1272 let writes = 0;
1273 yield* Effect.gen(function* () {
1274 const cli = yield* ForgejoCli.make;
1275 const result = yield* cli
1276 .api({
1277 cwd: "/repo",
1278 repository: "https://forgejo.local/maria/project",
1279 path: "repos/maria/project/issues/42/comments",
1280 method: "POST",
1281 body: { body: "only once" },
1282 })
1283 .pipe(Effect.result);
1284 assert.strictEqual(result._tag, status < 300 ? "Success" : "Failure");
1285 if (result._tag === "Success") assert.strictEqual(result.success.stdout, "");
1286 if (result._tag === "Failure") {
1287 assert.strictEqual(result.failure.command, "fj");
1288 assert.strictEqual(result.failure.httpStatus, status);
1290 assert.strictEqual(writes, 1);
1291 }).pipe(
1292 Effect.provideService(
1293 FileSystem.FileSystem,
1294 FileSystem.makeNoop({
1295 exists: () => Effect.succeed(true),
1296 readFileString: () =>
1297 Effect.succeed(
1298 encodeJson({
1299 hosts: {
1300 "forgejo.local": { type: "Application", token: "test-token" },
1301 },
1302 }),
1303 ),
1304 }),
1305 ),
1306 Effect.provideService(
1307 HttpClient.HttpClient,
1308 HttpClient.make((request) => {
1309 writes++;
1310 assert.strictEqual(
1311 request.url,
1312 "https://forgejo.local/api/v1/repos/maria/project/issues/42/comments",
1313 );
1314 return Effect.succeed(
1315 HttpClientResponse.fromWeb(
1316 request,
1317 new Response(status < 300 ? null : "", {
1318 status,
1319 headers: { location: "https://other.local/" },
1320 }),
1321 ),
1322 );
1323 }),
1324 ),
1325 Effect.provide(
1326 TestSourceControlHost.layer({
1327 process: {
1328 run: (input) => {
1329 assert.strictEqual(
1330 input.command,
1331 "fj",
1332 "a failed mutation must never switch accounts or CLI",
1333 );
1334 return Effect.succeed(processOutput(""));
1335 },
1336 },
1337 }),
1338 ),
1339 );
1341 }),
1343​
1344it.effect(
1345 "discovers fj first and retains configured authentication failures instead of switching accounts",
1346 () =>
1347 Effect.gen(function* () {
1348 for (const scenario of ["authenticated", "revoked", "missing", "invalid-storage"] as const) {
1349 const commands: string[] = [];
1350 yield* Effect.gen(function* () {
1351 const spec = yield* ForgejoSourceControlProvider.makeDiscovery;
1352 assert.strictEqual(spec.type, "managed-cli");
1353 if (spec.type !== "managed-cli") return;
1354 const result = yield* spec.probe("/repo");
1355 assert.strictEqual(result.executable, scenario === "missing" ? "tea" : "fj");
1356 assert.strictEqual(
1357 result.auth.status,
1358 scenario === "revoked"
1359 ? "unauthenticated"
1360 : scenario === "invalid-storage"
1361 ? "unknown"
1362 : "authenticated",
1363 );
1364 if (scenario !== "revoked" && scenario !== "invalid-storage")
1365 assert.deepStrictEqual(result.auth.host, Option.some("forgejo.local:3000"));
1366 assert.deepStrictEqual(
1367 result.auth.account,
1368 scenario === "authenticated" || scenario === "missing"
1369 ? Option.some("maria")
1370 : Option.none(),
1371 );
1372 assert.strictEqual(
1373 commands.some((command) => command.startsWith("tea ")),
1374 scenario === "missing",
1375 );
1376 assert.include(commands, "fj version");
1377 }).pipe(
1378 Effect.provide(
1379 Layer.mergeAll(
1380 Layer.mock(ForgejoCli.ForgejoCli)({
1381 getAccount: (input) => {
1382 assert.strictEqual(scenario, "authenticated");
1383 assert.deepStrictEqual(input, {
1384 cwd: "/repo",
1385 baseUrl: "http://forgejo.local:3000",
1386 });
1387 return Effect.succeed("maria");
1388 },
1389 listLogins: (input) => {
1390 assert.strictEqual(
1391 input.remoteUrl,
1392 "http://forgejo.local:3000/maria/project.git",
1393 );
1394 if (scenario === "invalid-storage")
1395 return Effect.fail(
1396 new ForgejoCli.ForgejoCliError({
1397 command: "fj",
1398 cwd: input.cwd,
1399 reason: "authentication",
1400 detail: "fj authentication storage is invalid.",
1401 }),
1402 );
1403 return Effect.succeed([
1405 name: "forgejo.local:3000",
1406 url: "http://forgejo.local:3000",
1407 user: "",
1408 default: "false",
1409 },
1410 ]);
1411 },
1412 }),
1413 TestSourceControlHost.layer({
1414 process: {
1415 run: (input) => {
1416 commands.push(`${input.command} ${input.args.join(" ")}`);
1417 if (input.command === "git")
1418 return Effect.succeed(
1419 processOutput("http://forgejo.local:3000/maria/project.git\n"),
1420 );
1421 if (input.command === "fj") {
1422 if (scenario === "missing")
1423 return Effect.fail(
1424 new VcsProcessSpawnError({
1425 operation: input.operation,
1426 command: input.command,
1427 cwd: input.cwd,
1428 cause: new Error("fj not found"),
1429 }),
1430 );
1431 if (input.args[0] === "version")
1432 return Effect.succeed(processOutput("fj 0.10.0"));
1433 if (scenario === "invalid-storage") {
1434 assert.deepStrictEqual(input.args, ["auth", "list"]);
1435 return Effect.succeed(processOutput(""));
1437 assert.deepStrictEqual(input.args, [
1438 "--host",
1439 "http://forgejo.local:3000",
1440 "whoami",
1441 ]);
1442 return Effect.succeed(
1443 processOutput("", {
1444 exitCode: ChildProcessSpawner.ExitCode(scenario === "revoked" ? 1 : 0),
1445 }),
1446 );
1448 assert.strictEqual(input.command, "tea");
1449 return Effect.succeed(
1450 input.args[0] === "--version"
1451 ? processOutput("tea version 0.16.0")
1452 : processOutput(
1453 encodeJson([
1455 name: "work",
1456 url: "http://forgejo.local:3000",
1457 user: "maria",
1458 default: "true",
1459 valid: "true",
1460 },
1461 ]),
1462 ),
1463 );
1464 },
1465 },
1466 }),
1467 ),
1468 ),
1469 );
1471 }),