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
| 1 | import { afterEach, expect, it, vi } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import { ChildProcessSpawner } from "effect/process"; |
| 6 | import { VcsProcessExitError } from "@t3tools/contracts"; |
| 7 | |
| 8 | import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 9 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 10 | import * as GitLabCli from "./GitLabCli.ts"; |
| 11 | import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts"; |
| 12 | |
| 13 | const run = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"]["run"]>(); |
| 14 | const layer = it.layer( |
| 15 | GitLabPullRequestCli.layer.pipe( |
| 16 | Layer.provide(GitLabCli.layer), |
| 17 | Layer.provide(TestSourceControlHost.layer({ process: { run } })), |
| 18 | Layer.provide(FileSystem.layerNoop({})), |
| 19 | ), |
| 20 | ); |
| 21 | const input = { cwd: "/repo", repository: "acme/nested/web", number: 7 }; |
| 22 | const file = { |
| 23 | old_path: "src/old.ts", |
| 24 | new_path: "src/new.ts", |
| 25 | renamed_file: true, |
| 26 | new_file: false, |
| 27 | deleted_file: false, |
| 28 | a_mode: "100644", |
| 29 | b_mode: "100644", |
| 30 | diff: "@@ -1 +1 @@\n-before\n+after\n", |
| 31 | }; |
| 32 | function output(body: unknown, stdoutTruncated = false) { |
| 33 | return Effect.succeed({ |
| 34 | exitCode: ChildProcessSpawner.ExitCode(0), |
| 35 | stdout: JSON.stringify(body), |
| 36 | stderr: "", |
| 37 | stdoutTruncated, |
| 38 | stderrTruncated: false, |
| 39 | }); |
| 40 | } |
| 41 | function failure(failureKind: "not-found" | "authentication" | "rate-limited" | "command-failed") { |
| 42 | return Effect.fail( |
| 43 | new VcsProcessExitError({ |
| 44 | operation: "GitLabCli.execute", |
| 45 | command: "glab", |
| 46 | cwd: input.cwd, |
| 47 | exitCode: 1, |
| 48 | detail: "GitLab HTTP failure", |
| 49 | failureKind, |
| 50 | }), |
| 51 | ); |
| 52 | } |
| 53 | afterEach(() => run.mockReset()); |
| 54 | |
| 55 | layer("legacy GitLab diff requests through GitLabCli", (it) => { |
| 56 | it.effect.each([false, true])("accepts an unavailable legacy count (overflow: %s)", (overflow) => |
| 57 | Effect.gen(function* () { |
| 58 | run.mockReturnValueOnce(failure("not-found")); |
| 59 | run.mockReturnValueOnce(output({ changes: [file], changes_count: null, overflow })); |
| 60 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 61 | const result = yield* cli.getMergeRequestDiff(input); |
| 62 | expect(result.truncated).toBe(overflow); |
| 63 | expect(result.nextCursor).toBeNull(); |
| 64 | expect(result.patch).toContain("@@ -1 +1 @@\n-before\n+after\n"); |
| 65 | }), |
| 66 | ); |
| 67 | it.effect.each([{ overflow: true }, { changes_count: "247+" }])( |
| 68 | "marks legacy limits %j truncated", |
| 69 | (metadata) => |
| 70 | Effect.gen(function* () { |
| 71 | run.mockReturnValueOnce(failure("not-found")); |
| 72 | run.mockReturnValueOnce(output({ changes: [file], ...metadata })); |
| 73 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 74 | expect(yield* cli.getMergeRequestDiff(input)).toMatchObject({ |
| 75 | truncated: true, |
| 76 | nextCursor: null, |
| 77 | }); |
| 78 | }), |
| 79 | ); |
| 80 | it.effect.each([false, true])("marks skipped file entries truncated (legacy: %s)", (legacy) => |
| 81 | Effect.gen(function* () { |
| 82 | const entries = [file, { new_path: "missing-old-path.ts" }]; |
| 83 | if (legacy) run.mockReturnValueOnce(failure("not-found")); |
| 84 | run.mockReturnValueOnce(output(legacy ? { changes: entries } : entries)); |
| 85 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 86 | const result = yield* cli.getMergeRequestDiff(input); |
| 87 | expect(result.truncated).toBe(true); |
| 88 | expect(result.nextCursor).toBeNull(); |
| 89 | expect(result.patch.match(/^diff --git /gm)).toHaveLength(1); |
| 90 | expect(result.patch).toContain("@@ -1 +1 @@\n-before\n+after\n"); |
| 91 | }), |
| 92 | ); |
| 93 | it.effect("rejects a byte-truncated legacy response", () => |
| 94 | Effect.gen(function* () { |
| 95 | run.mockReturnValueOnce(failure("not-found")); |
| 96 | run.mockReturnValueOnce(output({ changes: [file] }, true)); |
| 97 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 98 | expect((yield* cli.getMergeRequestDiff(input).pipe(Effect.flip))._tag).toBe( |
| 99 | "GitLabMergeRequestReadError", |
| 100 | ); |
| 101 | }), |
| 102 | ); |
| 103 | it.effect.each(["authentication", "rate-limited", "command-failed"] as const)( |
| 104 | "does not fall back on %s", |
| 105 | (kind) => |
| 106 | Effect.gen(function* () { |
| 107 | run.mockReturnValueOnce(failure(kind)); |
| 108 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 109 | yield* cli.getMergeRequestDiff(input).pipe(Effect.flip); |
| 110 | expect(run).toHaveBeenCalledTimes(1); |
| 111 | }), |
| 112 | ); |
| 113 | it.effect("does not fall back for a missing commit or later MR page", () => |
| 114 | Effect.gen(function* () { |
| 115 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 116 | for (const target of [ |
| 117 | { ...input, commit: "abc1234" }, |
| 118 | { ...input, cursor: "2" }, |
| 119 | ]) { |
| 120 | run.mockReturnValueOnce(failure("not-found")); |
| 121 | yield* cli.getMergeRequestDiff(target).pipe(Effect.flip); |
| 122 | } |
| 123 | expect(run).toHaveBeenCalledTimes(2); |
| 124 | }), |
| 125 | ); |
| 126 | // regression: #15886 the older-GitLab fallback still surfaces its own failure |
| 127 | it.effect("propagates failure of the legacy endpoint", () => |
| 128 | Effect.gen(function* () { |
| 129 | run.mockReturnValueOnce(failure("not-found")); |
| 130 | run.mockReturnValueOnce(failure("not-found")); |
| 131 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 132 | expect((yield* cli.getMergeRequestDiff(input).pipe(Effect.flip))._tag).toBe( |
| 133 | "GitLabCliCommandError", |
| 134 | ); |
| 135 | expect(run).toHaveBeenCalledTimes(2); |
| 136 | }), |
| 137 | ); |
| 138 | it.effect("does not fall back on an unreadable successful modern response", () => |
| 139 | Effect.gen(function* () { |
| 140 | run.mockReturnValueOnce(output({ changes: [file] })); |
| 141 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 142 | expect((yield* cli.getMergeRequestDiff(input).pipe(Effect.flip))._tag).toBe( |
| 143 | "GitLabMergeRequestReadError", |
| 144 | ); |
| 145 | expect(run).toHaveBeenCalledTimes(1); |
| 146 | }), |
| 147 | ); |
| 148 | it.effect("keeps modern pagination without requesting legacy changes", () => |
| 149 | Effect.gen(function* () { |
| 150 | run.mockReturnValueOnce(output(Array.from({ length: 30 }, () => file))); |
| 151 | run.mockReturnValueOnce(output([file])); |
| 152 | const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; |
| 153 | const first = yield* cli.getMergeRequestDiff(input); |
| 154 | expect(first.nextCursor).toBe("2"); |
| 155 | expect( |
| 156 | (yield* cli.getMergeRequestDiff({ ...input, cursor: first.nextCursor! })).nextCursor, |
| 157 | ).toBeNull(); |
| 158 | expect(run).toHaveBeenCalledTimes(2); |
| 159 | expect(run.mock.calls[1]?.[0].args).toContain( |
| 160 | "projects/acme%2Fnested%2Fweb/merge_requests/7/diffs?per_page=30&page=2", |
| 161 | ); |
| 162 | }), |
| 163 | ); |
| 164 | }); |