The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | SourceControlProviderKind, |
| 3 | type PullRequestReviewCapabilities, |
| 4 | type PullRequestReviewerCapabilities, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { |
| 7 | providerAuth, |
| 8 | type SourceControlProviderDiscoverySpec, |
| 9 | } from "@t3tools/source-control-core/server/discovery"; |
| 10 | import type { SourceControlDriverInstance } from "@t3tools/source-control-core/server/driver"; |
| 11 | import type { PullRequestProviderApi } from "@t3tools/source-control-core/server/PullRequestProvider"; |
| 12 | import type * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | |
| 15 | const FULL_REVIEW: PullRequestReviewCapabilities = { |
| 16 | inlineComment: true, |
| 17 | reply: true, |
| 18 | resolve: true, |
| 19 | verdicts: ["comment", "approve", "request-changes"], |
| 20 | }; |
| 21 | |
| 22 | const FULL_REVIEWERS: PullRequestReviewerCapabilities = { request: true, listCandidates: true }; |
| 23 | |
| 24 | const kindOf = (kind: string) => SourceControlProviderKind.make(kind); |
| 25 | |
| 26 | /** |
| 27 | * A pull request provider that can do everything and holds nothing: lists are empty, writes |
| 28 | * succeed, and reads a test did not supply die. |
| 29 | */ |
| 30 | export function fakePullRequestProvider( |
| 31 | kind: string, |
| 32 | overrides: Partial<PullRequestProviderApi> = {}, |
| 33 | ): PullRequestProviderApi { |
| 34 | return { |
| 35 | kind: kindOf(kind), |
| 36 | capabilities: { |
| 37 | diff: true, |
| 38 | comment: true, |
| 39 | actions: ["merge", "ready", "draft", "close", "reopen"], |
| 40 | mergeMethods: ["merge"], |
| 41 | search: true, |
| 42 | reactions: true, |
| 43 | review: FULL_REVIEW, |
| 44 | reviewers: FULL_REVIEWERS, |
| 45 | edit: { changeRequest: true, comment: true }, |
| 46 | withheldFileDiff: true, |
| 47 | }, |
| 48 | getViewer: () => Effect.succeed("bilal"), |
| 49 | getViewerPermissions: () => |
| 50 | Effect.succeed({ |
| 51 | actions: ["merge", "ready", "draft", "close", "reopen"], |
| 52 | comment: true, |
| 53 | resolve: true, |
| 54 | verdicts: ["comment", "approve", "request-changes"], |
| 55 | requestReviewers: true, |
| 56 | }), |
| 57 | listChangeRequests: () => Effect.succeed({ items: [], truncated: false, continues: true }), |
| 58 | getChangeRequest: () => Effect.die("unused"), |
| 59 | getChangeRequestActivity: () => Effect.die("unused"), |
| 60 | getDiff: () => Effect.die("unused"), |
| 61 | runAction: () => Effect.void, |
| 62 | updateChangeRequest: () => Effect.void, |
| 63 | comment: () => Effect.void, |
| 64 | updateComment: () => Effect.void, |
| 65 | submitReview: () => Effect.void, |
| 66 | replyToThread: () => Effect.void, |
| 67 | setThreadResolution: () => Effect.void, |
| 68 | setReaction: () => Effect.void, |
| 69 | listReviewerCandidates: () => Effect.succeed({ candidates: [], truncated: false }), |
| 70 | setReviewerRequest: () => Effect.void, |
| 71 | ...overrides, |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | type SourceControlProviderService = SourceControlProvider.SourceControlProvider["Service"]; |
| 76 | |
| 77 | const unexpected = (kind: string, operation: string) => () => |
| 78 | Effect.die(`Unexpected ${kind} ${operation} in a test that supplied none.`); |
| 79 | |
| 80 | /** A source control provider whose every operation dies unless the test supplies it. */ |
| 81 | export function fakeSourceControlProvider( |
| 82 | kind: string, |
| 83 | overrides: Partial<SourceControlProviderService> = {}, |
| 84 | ): SourceControlProviderService { |
| 85 | return { |
| 86 | kind: kindOf(kind), |
| 87 | listChangeRequests: unexpected(kind, "listChangeRequests"), |
| 88 | getChangeRequest: unexpected(kind, "getChangeRequest"), |
| 89 | createChangeRequest: unexpected(kind, "createChangeRequest"), |
| 90 | getRepositoryCloneUrls: unexpected(kind, "getRepositoryCloneUrls"), |
| 91 | createRepository: unexpected(kind, "createRepository"), |
| 92 | getDefaultBranch: unexpected(kind, "getDefaultBranch"), |
| 93 | checkoutChangeRequest: unexpected(kind, "checkoutChangeRequest"), |
| 94 | ...overrides, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | /** Discovery over an API integration that reports an unknown sign-in. */ |
| 99 | export function fakeDiscovery( |
| 100 | kind: string, |
| 101 | overrides: Partial<Extract<SourceControlProviderDiscoverySpec, { readonly type: "api" }>> = {}, |
| 102 | ): SourceControlProviderDiscoverySpec { |
| 103 | return { |
| 104 | type: "api", |
| 105 | kind: kindOf(kind), |
| 106 | label: kind, |
| 107 | installHint: `Install ${kind}.`, |
| 108 | probeAuth: Effect.succeed(providerAuth({ status: "unknown" })), |
| 109 | ...overrides, |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | export interface FakeSourceControlDriverOptions { |
| 114 | readonly sourceControl?: Partial<SourceControlProviderService>; |
| 115 | /** Null for a host whose pull requests this build cannot read. */ |
| 116 | readonly pullRequests?: Partial<PullRequestProviderApi> | null; |
| 117 | readonly discovery?: SourceControlProviderDiscoverySpec; |
| 118 | readonly fetchMedia?: SourceControlDriverInstance["fetchMedia"]; |
| 119 | } |
| 120 | |
| 121 | /** A driver instance for a host named `kind` that needs none of the server's services. */ |
| 122 | export function fakeSourceControlDriverInstance( |
| 123 | kind: string, |
| 124 | options: FakeSourceControlDriverOptions = {}, |
| 125 | ): SourceControlDriverInstance { |
| 126 | return { |
| 127 | sourceControl: fakeSourceControlProvider(kind, options.sourceControl), |
| 128 | discovery: options.discovery ?? fakeDiscovery(kind), |
| 129 | pullRequests: |
| 130 | options.pullRequests === null ? null : fakePullRequestProvider(kind, options.pullRequests), |
| 131 | ...(options.fetchMedia === undefined ? {} : { fetchMedia: options.fetchMedia }), |
| 132 | }; |
| 133 | } |