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 { SecretProviderId, SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { assert, it, vi } from "@effect/vitest"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 5 | import * as DateTime from "effect/DateTime"; |
| 6 | import * as Deferred from "effect/Deferred"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Fiber from "effect/Fiber"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 13 | import * as Queue from "effect/Queue"; |
| 14 | import * as Result from "effect/Result"; |
| 15 | import * as Schema from "effect/Schema"; |
| 16 | import * as TestClock from "effect/testing/TestClock"; |
| 17 | import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 18 | |
| 19 | import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; |
| 20 | import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 21 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 22 | |
| 23 | import * as BitbucketApi from "./BitbucketApi.ts"; |
| 24 | |
| 25 | type HostGit = SourceControlHost.SourceControlHost["Service"]["git"]; |
| 26 | |
| 27 | const isBitbucketRepositoryLocatorError = Schema.is(BitbucketApi.BitbucketRepositoryLocatorError); |
| 28 | |
| 29 | const bitbucketPullRequest = { |
| 30 | id: 42, |
| 31 | title: "Add Bitbucket provider", |
| 32 | state: "OPEN", |
| 33 | updated_on: "2026-01-02T00:00:00.000Z", |
| 34 | links: { |
| 35 | html: { |
| 36 | href: "https://bitbucket.org/pingdotgg/t3code/pull-requests/42", |
| 37 | }, |
| 38 | }, |
| 39 | source: { |
| 40 | branch: { name: "feature/source-control" }, |
| 41 | repository: { |
| 42 | full_name: "octocat/t3code", |
| 43 | workspace: { slug: "octocat" }, |
| 44 | }, |
| 45 | }, |
| 46 | destination: { |
| 47 | branch: { name: "main" }, |
| 48 | repository: { |
| 49 | full_name: "pingdotgg/t3code", |
| 50 | workspace: { slug: "pingdotgg" }, |
| 51 | }, |
| 52 | }, |
| 53 | }; |
| 54 | |
| 55 | const repositoryJson = { |
| 56 | full_name: "pingdotgg/t3code", |
| 57 | links: { |
| 58 | html: { href: "https://bitbucket.org/pingdotgg/t3code" }, |
| 59 | clone: [ |
| 60 | { name: "https", href: "https://bitbucket.org/pingdotgg/t3code.git" }, |
| 61 | { name: "ssh", href: "git@bitbucket.org:pingdotgg/t3code.git" }, |
| 62 | ], |
| 63 | }, |
| 64 | mainbranch: { name: "main" }, |
| 65 | }; |
| 66 | |
| 67 | const gitOutput = (stdout: string) => ({ |
| 68 | exitCode: ChildProcessSpawner.ExitCode(0), |
| 69 | stdout, |
| 70 | stderr: "", |
| 71 | stdoutTruncated: false, |
| 72 | stderrTruncated: false, |
| 73 | }); |
| 74 | |
| 75 | function makeLayer(input: { |
| 76 | readonly response: (request: HttpClientRequest.HttpClientRequest) => Response; |
| 77 | /** Holds the answer until it resolves, in place of answering at once. */ |
| 78 | readonly answer?: (response: Response) => Effect.Effect<Response>; |
| 79 | readonly requestFailure?: ( |
| 80 | request: HttpClientRequest.HttpClientRequest, |
| 81 | ) => HttpClientError.HttpClientError; |
| 82 | readonly stall?: boolean; |
| 83 | readonly git?: Partial<HostGit>; |
| 84 | readonly env?: Record<string, string>; |
| 85 | readonly remoteUrl?: string; |
| 86 | }) { |
| 87 | const execute = vi.fn((request: HttpClientRequest.HttpClientRequest) => |
| 88 | input.stall |
| 89 | ? Effect.never |
| 90 | : input.requestFailure |
| 91 | ? Effect.fail(input.requestFailure(request)) |
| 92 | : (input.answer ?? Effect.succeed)(input.response(request)).pipe( |
| 93 | Effect.map((response) => HttpClientResponse.fromWeb(request, response)), |
| 94 | ), |
| 95 | ); |
| 96 | const gitMock = { |
| 97 | execute: vi.fn<HostGit["execute"]>(() => |
| 98 | Effect.succeed(gitOutput("git@bitbucket.org:pingdotgg/t3code.git\n")), |
| 99 | ), |
| 100 | readConfigValue: vi.fn<HostGit["readConfigValue"]>(() => |
| 101 | Effect.succeed<string | null>("git@bitbucket.org:pingdotgg/t3code.git"), |
| 102 | ), |
| 103 | resolvePrimaryRemoteName: vi.fn<HostGit["resolvePrimaryRemoteName"]>(() => |
| 104 | Effect.succeed("origin"), |
| 105 | ), |
| 106 | ensureRemote: vi.fn<HostGit["ensureRemote"]>(() => Effect.succeed("octocat")), |
| 107 | fetchRemoteBranch: vi.fn<HostGit["fetchRemoteBranch"]>(() => Effect.void), |
| 108 | fetchRemoteTrackingBranch: vi.fn<HostGit["fetchRemoteTrackingBranch"]>(() => Effect.void), |
| 109 | setBranchUpstream: vi.fn<HostGit["setBranchUpstream"]>(() => Effect.void), |
| 110 | switchRef: vi.fn<HostGit["switchRef"]>((request) => |
| 111 | Effect.succeed({ refName: request.refName }), |
| 112 | ), |
| 113 | listLocalBranchNames: vi.fn<HostGit["listLocalBranchNames"]>(() => Effect.succeed([])), |
| 114 | }; |
| 115 | const git = { |
| 116 | ...gitMock, |
| 117 | ...input.git, |
| 118 | } satisfies Partial<HostGit>; |
| 119 | |
| 120 | const listRemotes = vi.fn(() => |
| 121 | Effect.succeed({ |
| 122 | remotes: [ |
| 123 | { |
| 124 | name: "origin", |
| 125 | url: input.remoteUrl ?? "git@bitbucket.org:pingdotgg/t3code.git", |
| 126 | pushUrl: Option.none(), |
| 127 | isPrimary: true, |
| 128 | }, |
| 129 | ], |
| 130 | freshness: { |
| 131 | source: "live-local" as const, |
| 132 | observedAt: DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"), |
| 133 | expiresAt: Option.none(), |
| 134 | }, |
| 135 | }), |
| 136 | ); |
| 137 | // The outer effect resolves the repository; the inner one lists its remotes. |
| 138 | const resolve = vi.fn((_cwd: string) => Effect.succeed(Effect.suspend(listRemotes))); |
| 139 | |
| 140 | const layer = BitbucketApi.layer.pipe( |
| 141 | Layer.provide(SourceControlRateLimit.layer), |
| 142 | Layer.provide( |
| 143 | Layer.succeed( |
| 144 | HttpClient.HttpClient, |
| 145 | HttpClient.make((request) => execute(request)), |
| 146 | ), |
| 147 | ), |
| 148 | Layer.provide( |
| 149 | ConfigProvider.layer( |
| 150 | ConfigProvider.fromEnv({ |
| 151 | env: input.env ?? { |
| 152 | T3CODE_BITBUCKET_API_BASE_URL: "https://api.test.local/2.0", |
| 153 | T3CODE_BITBUCKET_EMAIL: "user@example.com", |
| 154 | T3CODE_BITBUCKET_API_TOKEN: "token", |
| 155 | }, |
| 156 | }), |
| 157 | ), |
| 158 | ), |
| 159 | Layer.provideMerge(TestSourceControlHost.layer({ git: { ...git, remotes: resolve } })), |
| 160 | Layer.provideMerge(NodeServices.layer), |
| 161 | ); |
| 162 | |
| 163 | return { execute, git: gitMock, resolve, listRemotes, layer }; |
| 164 | } |
| 165 | |
| 166 | it.effect("parses pull request responses from the Bitbucket REST API", () => { |
| 167 | const { execute, layer } = makeLayer({ |
| 168 | response: () => |
| 169 | Response.json({ |
| 170 | ...bitbucketPullRequest, |
| 171 | }), |
| 172 | }); |
| 173 | |
| 174 | return Effect.gen(function* () { |
| 175 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 176 | const result = yield* bitbucket.getPullRequest({ |
| 177 | cwd: "/repo", |
| 178 | reference: "#42", |
| 179 | }); |
| 180 | |
| 181 | assert.deepStrictEqual(result, { |
| 182 | number: 42, |
| 183 | title: "Add Bitbucket provider", |
| 184 | url: "https://bitbucket.org/pingdotgg/t3code/pull-requests/42", |
| 185 | baseRefName: "main", |
| 186 | headRefName: "feature/source-control", |
| 187 | state: "open", |
| 188 | updatedAt: Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")), |
| 189 | isCrossRepository: true, |
| 190 | headRepositoryNameWithOwner: "octocat/t3code", |
| 191 | headRepositoryOwnerLogin: "octocat", |
| 192 | }); |
| 193 | assert.strictEqual( |
| 194 | execute.mock.calls[0]?.[0].url, |
| 195 | "https://api.test.local/2.0/repositories/pingdotgg/t3code/pullrequests/42", |
| 196 | ); |
| 197 | }).pipe(Effect.provide(layer)); |
| 198 | }); |
| 199 | |
| 200 | it.effect.each([false, true])("honors PR URL repositories, context=%s", (withContext) => { |
| 201 | const { execute, git, layer } = makeLayer({ |
| 202 | response: (request) => |
| 203 | Response.json( |
| 204 | request.url.endsWith("/pullrequests/42") |
| 205 | ? { |
| 206 | ...bitbucketPullRequest, |
| 207 | source: { |
| 208 | branch: { name: "feature/source-control" }, |
| 209 | repository: { full_name: "another/app" }, |
| 210 | }, |
| 211 | destination: { branch: { name: "main" }, repository: { full_name: "another/app" } }, |
| 212 | } |
| 213 | : { |
| 214 | ...repositoryJson, |
| 215 | full_name: "another/app", |
| 216 | links: { clone: [{ name: "ssh", href: "git@bitbucket.org:another/app.git" }] }, |
| 217 | }, |
| 218 | ), |
| 219 | }); |
| 220 | return Effect.gen(function* () { |
| 221 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 222 | const input = { |
| 223 | cwd: "/repo", |
| 224 | reference: "https://bitbucket.org/another/app/pull-requests/42", |
| 225 | ...(withContext |
| 226 | ? { |
| 227 | context: { |
| 228 | provider: { |
| 229 | kind: SourceControlProviderKind.make("bitbucket"), |
| 230 | name: "Bitbucket", |
| 231 | baseUrl: "https://bitbucket.org", |
| 232 | }, |
| 233 | remoteName: "origin", |
| 234 | remoteUrl: "git@bitbucket.org:pingdotgg/t3code.git", |
| 235 | }, |
| 236 | } |
| 237 | : {}), |
| 238 | }; |
| 239 | yield* bitbucket.getPullRequest(input); |
| 240 | yield* bitbucket.checkoutPullRequest(input); |
| 241 | assert.deepStrictEqual( |
| 242 | execute.mock.calls.slice(0, 2).map(([request]) => request.url), |
| 243 | [ |
| 244 | "https://api.test.local/2.0/repositories/another/app/pullrequests/42", |
| 245 | "https://api.test.local/2.0/repositories/another/app/pullrequests/42", |
| 246 | ], |
| 247 | ); |
| 248 | assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], { |
| 249 | cwd: "/repo", |
| 250 | preferredName: "another", |
| 251 | url: "git@bitbucket.org:another/app.git", |
| 252 | }); |
| 253 | assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], { |
| 254 | cwd: "/repo", |
| 255 | remoteName: "octocat", |
| 256 | remoteBranch: "feature/source-control", |
| 257 | localBranch: "feature/source-control", |
| 258 | }); |
| 259 | assert.deepStrictEqual(git.setBranchUpstream.mock.calls[0]?.[0], { |
| 260 | cwd: "/repo", |
| 261 | branch: "feature/source-control", |
| 262 | remoteName: "octocat", |
| 263 | remoteBranch: "feature/source-control", |
| 264 | }); |
| 265 | }).pipe(Effect.provide(layer)); |
| 266 | }); |
| 267 | |
| 268 | it.effect("adds a fork remote with the protocol of the URL origin fetches from", () => { |
| 269 | // `git remote -v` shows origin's SSH `insteadOf` rewrite; config holds HTTPS, then an SSH push URL. |
| 270 | const { git, layer } = makeLayer({ |
| 271 | git: { |
| 272 | execute: () => |
| 273 | Effect.succeed( |
| 274 | gitOutput( |
| 275 | "https://bitbucket.org/pingdotgg/t3code.git\ngit@bitbucket.org:pingdotgg/t3code.git\n", |
| 276 | ), |
| 277 | ), |
| 278 | }, |
| 279 | response: (request) => |
| 280 | Response.json( |
| 281 | request.url.endsWith("/pullrequests/42") |
| 282 | ? bitbucketPullRequest |
| 283 | : { |
| 284 | ...repositoryJson, |
| 285 | full_name: "octocat/t3code", |
| 286 | links: { |
| 287 | clone: [ |
| 288 | { name: "https", href: "https://bitbucket.org/octocat/t3code.git" }, |
| 289 | { name: "ssh", href: "git@bitbucket.org:octocat/t3code.git" }, |
| 290 | ], |
| 291 | }, |
| 292 | }, |
| 293 | ), |
| 294 | }); |
| 295 | return Effect.gen(function* () { |
| 296 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 297 | yield* bitbucket.checkoutPullRequest({ cwd: "/repo", reference: "#42" }); |
| 298 | assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], { |
| 299 | cwd: "/repo", |
| 300 | preferredName: "octocat", |
| 301 | url: "https://bitbucket.org/octocat/t3code.git", |
| 302 | }); |
| 303 | }).pipe(Effect.provide(layer)); |
| 304 | }); |
| 305 | |
| 306 | it.effect.each([false, true])( |
| 307 | "reuses matching checkout remotes for numeric references, context=%s", |
| 308 | (withContext) => { |
| 309 | const { git, layer } = makeLayer({ |
| 310 | response: () => |
| 311 | Response.json({ |
| 312 | ...bitbucketPullRequest, |
| 313 | source: { ...bitbucketPullRequest.source, repository: { full_name: "pingdotgg/t3code" } }, |
| 314 | }), |
| 315 | }); |
| 316 | return Effect.gen(function* () { |
| 317 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 318 | yield* bitbucket.checkoutPullRequest({ |
| 319 | cwd: "/repo", |
| 320 | reference: "#42", |
| 321 | ...(withContext |
| 322 | ? { |
| 323 | context: { |
| 324 | provider: { |
| 325 | kind: SourceControlProviderKind.make("bitbucket"), |
| 326 | name: "Bitbucket", |
| 327 | baseUrl: "https://bitbucket.org", |
| 328 | }, |
| 329 | remoteName: "upstream", |
| 330 | remoteUrl: "git@bitbucket.org:PingDotGG/T3Code.git", |
| 331 | }, |
| 332 | } |
| 333 | : {}), |
| 334 | }); |
| 335 | assert.strictEqual(git.ensureRemote.mock.calls.length, 0); |
| 336 | assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], { |
| 337 | cwd: "/repo", |
| 338 | remoteName: withContext ? "upstream" : "origin", |
| 339 | remoteBranch: "feature/source-control", |
| 340 | localBranch: "feature/source-control", |
| 341 | }); |
| 342 | }).pipe(Effect.provide(layer)); |
| 343 | }, |
| 344 | ); |
| 345 | |
| 346 | it.effect("lists pull requests with Bitbucket state and source branch query params", () => { |
| 347 | const { execute, layer } = makeLayer({ |
| 348 | response: () => |
| 349 | Response.json({ |
| 350 | values: [ |
| 351 | { |
| 352 | ...bitbucketPullRequest, |
| 353 | id: 7, |
| 354 | state: "MERGED", |
| 355 | source: { |
| 356 | branch: { name: "feature/merged" }, |
| 357 | repository: { full_name: "pingdotgg/t3code" }, |
| 358 | }, |
| 359 | }, |
| 360 | ], |
| 361 | }), |
| 362 | }); |
| 363 | |
| 364 | return Effect.gen(function* () { |
| 365 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 366 | const result = yield* bitbucket.listPullRequests({ |
| 367 | cwd: "/repo", |
| 368 | headSelector: "origin:feature/merged", |
| 369 | state: "merged", |
| 370 | limit: 10, |
| 371 | }); |
| 372 | |
| 373 | assert.strictEqual(result[0]?.state, "merged"); |
| 374 | const request = execute.mock.calls[0]?.[0]; |
| 375 | assert.strictEqual( |
| 376 | request?.url, |
| 377 | "https://api.test.local/2.0/repositories/pingdotgg/t3code/pullrequests", |
| 378 | ); |
| 379 | assert.deepStrictEqual(request?.urlParams.params, [ |
| 380 | ["pagelen", "10"], |
| 381 | ["sort", "-updated_on"], |
| 382 | ["q", 'source.branch.name = "feature/merged" AND state = "MERGED"'], |
| 383 | ["state", "MERGED"], |
| 384 | ]); |
| 385 | }).pipe(Effect.provide(layer)); |
| 386 | }); |
| 387 | |
| 388 | it.effect("reads repository clone URLs and default branch", () => { |
| 389 | const { layer } = makeLayer({ |
| 390 | response: (request) => |
| 391 | Response.json( |
| 392 | request.url.endsWith("/branching-model") |
| 393 | ? { |
| 394 | development: { |
| 395 | branch: { name: "main" }, |
| 396 | name: "main", |
| 397 | use_mainbranch: true, |
| 398 | }, |
| 399 | } |
| 400 | : repositoryJson, |
| 401 | ), |
| 402 | }); |
| 403 | |
| 404 | return Effect.gen(function* () { |
| 405 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 406 | const cloneUrls = yield* bitbucket.getRepositoryCloneUrls({ |
| 407 | cwd: "/repo", |
| 408 | repository: "pingdotgg/t3code", |
| 409 | }); |
| 410 | const defaultBranch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" }); |
| 411 | |
| 412 | assert.deepStrictEqual(cloneUrls, { |
| 413 | nameWithOwner: "pingdotgg/t3code", |
| 414 | url: "https://bitbucket.org/pingdotgg/t3code.git", |
| 415 | sshUrl: "git@bitbucket.org:pingdotgg/t3code.git", |
| 416 | }); |
| 417 | assert.strictEqual(defaultBranch, "main"); |
| 418 | }).pipe(Effect.provide(layer)); |
| 419 | }); |
| 420 | |
| 421 | it.effect.each([false, true])( |
| 422 | "rejects invalid explicit repositories before fallback, context=%s", |
| 423 | (withContext) => { |
| 424 | const { execute, resolve, listRemotes, layer } = makeLayer({ |
| 425 | response: () => Response.json(repositoryJson), |
| 426 | }); |
| 427 | |
| 428 | return Effect.gen(function* () { |
| 429 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 430 | for (const repository of ["t3code", "", " \t "]) { |
| 431 | const error = yield* bitbucket |
| 432 | .getRepositoryCloneUrls({ |
| 433 | cwd: "/repo", |
| 434 | repository, |
| 435 | ...(withContext |
| 436 | ? { |
| 437 | context: { |
| 438 | provider: { |
| 439 | kind: SourceControlProviderKind.make("bitbucket"), |
| 440 | name: "Bitbucket", |
| 441 | baseUrl: "https://bitbucket.org", |
| 442 | }, |
| 443 | remoteName: "origin", |
| 444 | remoteUrl: "git@bitbucket.org:pingdotgg/t3code.git", |
| 445 | }, |
| 446 | } |
| 447 | : {}), |
| 448 | }) |
| 449 | .pipe(Effect.match({ onFailure: (error) => error, onSuccess: () => null })); |
| 450 | |
| 451 | assert.instanceOf(error, BitbucketApi.BitbucketRepositoryLocatorError); |
| 452 | assert.strictEqual( |
| 453 | isBitbucketRepositoryLocatorError(error) ? error.repository : null, |
| 454 | repository, |
| 455 | ); |
| 456 | assert.strictEqual(resolve.mock.calls.length, 0); |
| 457 | assert.strictEqual(listRemotes.mock.calls.length, 0); |
| 458 | assert.strictEqual(execute.mock.calls.length, 0); |
| 459 | } |
| 460 | }).pipe(Effect.provide(layer)); |
| 461 | }, |
| 462 | ); |
| 463 | |
| 464 | it.effect("prefers an explicit repository and uses context when the repository is omitted", () => { |
| 465 | const { execute, resolve, listRemotes, layer } = makeLayer({ |
| 466 | response: (request) => |
| 467 | Response.json(request.url.endsWith("/branching-model") ? {} : repositoryJson), |
| 468 | }); |
| 469 | |
| 470 | return Effect.gen(function* () { |
| 471 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 472 | const context = { |
| 473 | provider: { |
| 474 | kind: SourceControlProviderKind.make("bitbucket"), |
| 475 | name: "Bitbucket", |
| 476 | baseUrl: "https://bitbucket.org", |
| 477 | }, |
| 478 | remoteName: "origin", |
| 479 | remoteUrl: "git@bitbucket.org:another/context.git", |
| 480 | }; |
| 481 | for (const repository of ["pingdotgg/t3code", "https://bitbucket.org/pingdotgg/t3code.git"]) { |
| 482 | yield* bitbucket.getRepositoryCloneUrls({ cwd: "/repo", context, repository }); |
| 483 | } |
| 484 | yield* bitbucket.getDefaultBranch({ cwd: "/repo", context }); |
| 485 | |
| 486 | assert.deepStrictEqual( |
| 487 | execute.mock.calls.map(([request]) => request.url).toSorted(), |
| 488 | [ |
| 489 | "https://api.test.local/2.0/repositories/pingdotgg/t3code", |
| 490 | "https://api.test.local/2.0/repositories/pingdotgg/t3code", |
| 491 | "https://api.test.local/2.0/repositories/another/context", |
| 492 | "https://api.test.local/2.0/repositories/another/context/branching-model", |
| 493 | ].toSorted(), |
| 494 | ); |
| 495 | assert.strictEqual(resolve.mock.calls.length, 0); |
| 496 | assert.strictEqual(listRemotes.mock.calls.length, 0); |
| 497 | }).pipe(Effect.provide(layer)); |
| 498 | }); |
| 499 | |
| 500 | it.effect("sends a self-hosted Bitbucket remote nowhere unless an API base url is configured", () => |
| 501 | Effect.gen(function* () { |
| 502 | const remoteUrl = "https://bitbucket.example.com/scm/proj/repo.git"; |
| 503 | const context = { |
| 504 | provider: { |
| 505 | kind: SourceControlProviderKind.make("bitbucket"), |
| 506 | name: "Bitbucket Self-Hosted", |
| 507 | baseUrl: "https://bitbucket.example.com", |
| 508 | }, |
| 509 | remoteName: "origin", |
| 510 | remoteUrl, |
| 511 | }; |
| 512 | const cloudEnv = { |
| 513 | T3CODE_BITBUCKET_EMAIL: "user@example.com", |
| 514 | T3CODE_BITBUCKET_API_TOKEN: "t", |
| 515 | }; |
| 516 | const response = (request: HttpClientRequest.HttpClientRequest) => |
| 517 | Response.json(request.url.endsWith("/branching-model") ? {} : repositoryJson); |
| 518 | const defaultBranch = (setup: ReturnType<typeof makeLayer>, withContext: boolean) => |
| 519 | BitbucketApi.BitbucketApi.pipe( |
| 520 | Effect.flatMap((bitbucket) => |
| 521 | bitbucket.getDefaultBranch({ cwd: "/repo", ...(withContext ? { context } : {}) }), |
| 522 | ), |
| 523 | Effect.provide(setup.layer), |
| 524 | ); |
| 525 | const requestedUrls = (setup: ReturnType<typeof makeLayer>) => |
| 526 | setup.execute.mock.calls.map(([request]) => request.url).toSorted(); |
| 527 | |
| 528 | // A self-hosted remote in the checkout, then a self-hosted context beside a Cloud remote. |
| 529 | for (const [setup, withContext] of [ |
| 530 | [makeLayer({ response, env: cloudEnv, remoteUrl }), false], |
| 531 | [makeLayer({ response, env: cloudEnv }), true], |
| 532 | ] as const) { |
| 533 | const result = yield* defaultBranch(setup, withContext).pipe(Effect.result); |
| 534 | assert.deepStrictEqual(requestedUrls(setup), []); |
| 535 | assert.strictEqual( |
| 536 | Result.isFailure(result) && result.failure._tag, |
| 537 | "BitbucketRepositoryRemoteNotFoundError", |
| 538 | ); |
| 539 | } |
| 540 | |
| 541 | // An explicit repository does not let a self-hosted context reach the Cloud API. |
| 542 | const explicit = makeLayer({ response, env: cloudEnv }); |
| 543 | const cloneUrls = yield* BitbucketApi.BitbucketApi.pipe( |
| 544 | Effect.flatMap((bitbucket) => |
| 545 | bitbucket.getRepositoryCloneUrls({ cwd: "/repo", context, repository: "proj/repo" }), |
| 546 | ), |
| 547 | Effect.provide(explicit.layer), |
| 548 | Effect.result, |
| 549 | ); |
| 550 | assert.deepStrictEqual(requestedUrls(explicit), []); |
| 551 | assert.strictEqual( |
| 552 | Result.isFailure(cloneUrls) && cloneUrls.failure._tag, |
| 553 | "BitbucketRepositoryRemoteNotFoundError", |
| 554 | ); |
| 555 | |
| 556 | const cloud = makeLayer({ response, env: cloudEnv }); |
| 557 | yield* defaultBranch(cloud, false); |
| 558 | assert.deepStrictEqual(requestedUrls(cloud), [ |
| 559 | "https://api.bitbucket.org/2.0/repositories/pingdotgg/t3code", |
| 560 | "https://api.bitbucket.org/2.0/repositories/pingdotgg/t3code/branching-model", |
| 561 | ]); |
| 562 | |
| 563 | const configured = makeLayer({ response, remoteUrl }); |
| 564 | yield* defaultBranch(configured, true); |
| 565 | assert.deepStrictEqual(requestedUrls(configured), [ |
| 566 | "https://api.test.local/2.0/repositories/proj/repo", |
| 567 | "https://api.test.local/2.0/repositories/proj/repo/branching-model", |
| 568 | ]); |
| 569 | }), |
| 570 | ); |
| 571 | |
| 572 | it.effect( |
| 573 | "prefers the Bitbucket branching model development branch as the default PR target", |
| 574 | () => { |
| 575 | const { execute, layer } = makeLayer({ |
| 576 | response: (request) => |
| 577 | Response.json( |
| 578 | request.url.endsWith("/branching-model") |
| 579 | ? { |
| 580 | development: { |
| 581 | branch: { name: "develop" }, |
| 582 | name: "develop", |
| 583 | use_mainbranch: false, |
| 584 | }, |
| 585 | } |
| 586 | : repositoryJson, |
| 587 | ), |
| 588 | }); |
| 589 | |
| 590 | return Effect.gen(function* () { |
| 591 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 592 | const defaultBranch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" }); |
| 593 | |
| 594 | assert.strictEqual(defaultBranch, "develop"); |
| 595 | assert.deepStrictEqual( |
| 596 | execute.mock.calls.map((call) => call[0].url).toSorted(), |
| 597 | [ |
| 598 | "https://api.test.local/2.0/repositories/pingdotgg/t3code", |
| 599 | "https://api.test.local/2.0/repositories/pingdotgg/t3code/branching-model", |
| 600 | ].toSorted(), |
| 601 | ); |
| 602 | }).pipe(Effect.provide(layer)); |
| 603 | }, |
| 604 | ); |
| 605 | |
| 606 | it.effect( |
| 607 | "falls back to the repository main branch when the Bitbucket development branch is invalid", |
| 608 | () => { |
| 609 | const { layer } = makeLayer({ |
| 610 | response: (request) => |
| 611 | Response.json( |
| 612 | request.url.endsWith("/branching-model") |
| 613 | ? { |
| 614 | development: { |
| 615 | name: "develop", |
| 616 | use_mainbranch: false, |
| 617 | is_valid: false, |
| 618 | }, |
| 619 | } |
| 620 | : repositoryJson, |
| 621 | ), |
| 622 | }); |
| 623 | |
| 624 | return Effect.gen(function* () { |
| 625 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 626 | const defaultBranch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" }); |
| 627 | |
| 628 | assert.strictEqual(defaultBranch, "main"); |
| 629 | }).pipe(Effect.provide(layer)); |
| 630 | }, |
| 631 | ); |
| 632 | |
| 633 | it.effect("creates pull requests using the official REST payload shape", () => { |
| 634 | const { execute, layer } = makeLayer({ |
| 635 | response: () => Response.json(bitbucketPullRequest), |
| 636 | }); |
| 637 | |
| 638 | return Effect.gen(function* () { |
| 639 | const fileSystem = yield* FileSystem.FileSystem; |
| 640 | const bodyFile = yield* fileSystem.makeTempFileScoped({ prefix: "bitbucket-pr-body-" }); |
| 641 | yield* fileSystem.writeFileString(bodyFile, "PR body"); |
| 642 | |
| 643 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 644 | yield* bitbucket.createPullRequest({ |
| 645 | cwd: "/repo", |
| 646 | baseBranch: "main", |
| 647 | headSelector: "owner:feature/provider", |
| 648 | title: "Provider PR", |
| 649 | bodyFile, |
| 650 | }); |
| 651 | |
| 652 | const request = execute.mock.calls[0]?.[0]; |
| 653 | assert.strictEqual( |
| 654 | request?.url, |
| 655 | "https://api.test.local/2.0/repositories/pingdotgg/t3code/pullrequests", |
| 656 | ); |
| 657 | assert.strictEqual(request?.method, "POST"); |
| 658 | assert.ok(request); |
| 659 | const rawBody = (request.body as { readonly body?: Uint8Array }).body; |
| 660 | assert.ok(rawBody); |
| 661 | assert.deepStrictEqual(JSON.parse(new TextDecoder().decode(rawBody)), { |
| 662 | title: "Provider PR", |
| 663 | description: "PR body", |
| 664 | source: { |
| 665 | branch: { name: "feature/provider" }, |
| 666 | repository: { full_name: "owner/t3code" }, |
| 667 | }, |
| 668 | destination: { |
| 669 | branch: { name: "main" }, |
| 670 | }, |
| 671 | }); |
| 672 | }).pipe(Effect.provide(layer)); |
| 673 | }); |
| 674 | |
| 675 | it.effect("reports the account Bitbucket confirms for the configured credential", () => { |
| 676 | const { layer } = makeLayer({ |
| 677 | response: () => Response.json({ username: "bitbucket-user" }), |
| 678 | }); |
| 679 | |
| 680 | return Effect.gen(function* () { |
| 681 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 682 | const auth = yield* bitbucket.probeAuth; |
| 683 | assert.strictEqual(auth.status, "authenticated"); |
| 684 | assert.deepStrictEqual(auth.account, Option.some("bitbucket-user")); |
| 685 | assert.deepStrictEqual(auth.host, Option.some("bitbucket.org")); |
| 686 | }).pipe(Effect.provide(layer)); |
| 687 | }); |
| 688 | |
| 689 | it.effect("prefers credentials saved in settings over the environment, without a restart", () => { |
| 690 | const { execute, layer } = makeLayer({ |
| 691 | response: () => Response.json({ username: "bitbucket-user" }), |
| 692 | }); |
| 693 | const lastAuthorization = () => execute.mock.calls.at(-1)?.[0].headers.authorization; |
| 694 | const basic = (user: string, password: string) => `Basic ${btoa(`${user}:${password}`)}`; |
| 695 | |
| 696 | return Effect.gen(function* () { |
| 697 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 698 | const settings = yield* TestSourceControlHost.TestSourceControlHostSettings; |
| 699 | |
| 700 | yield* bitbucket.probeAuth; |
| 701 | assert.strictEqual(lastAuthorization(), basic("user@example.com", "token")); |
| 702 | |
| 703 | yield* settings.update({ |
| 704 | sourceControlHosts: { |
| 705 | bitbucket: { email: "saved@example.com", apiToken: "saved-api-token" }, |
| 706 | }, |
| 707 | }); |
| 708 | yield* bitbucket.probeAuth; |
| 709 | assert.strictEqual(lastAuthorization(), basic("saved@example.com", "saved-api-token")); |
| 710 | |
| 711 | yield* settings.update({ |
| 712 | sourceControlHosts: { bitbucket: { accessToken: "saved-access-token" } }, |
| 713 | }); |
| 714 | yield* bitbucket.probeAuth; |
| 715 | assert.strictEqual(lastAuthorization(), "Bearer saved-access-token"); |
| 716 | |
| 717 | yield* settings.update({ |
| 718 | sourceControlHosts: { bitbucket: { accessToken: "", apiToken: "" } }, |
| 719 | }); |
| 720 | yield* bitbucket.probeAuth; |
| 721 | assert.strictEqual(lastAuthorization(), basic("user@example.com", "token")); |
| 722 | |
| 723 | // A reference left in place by getWithSecrets is one its secret provider could not read. |
| 724 | const requests = execute.mock.calls.length; |
| 725 | yield* settings.update({ |
| 726 | sourceControlHosts: { |
| 727 | bitbucket: { |
| 728 | accessToken: { provider: SecretProviderId.make("vault"), reference: "bb#token" }, |
| 729 | }, |
| 730 | }, |
| 731 | }); |
| 732 | const auth = yield* bitbucket.probeAuth; |
| 733 | assert.strictEqual(auth.status, "unauthenticated"); |
| 734 | assert.include(Option.getOrThrow(auth.detail), '"vault"'); |
| 735 | const error = yield* Effect.flip( |
| 736 | bitbucket.listPullRequests({ cwd: "/repo", headSelector: "feature", state: "open" }), |
| 737 | ); |
| 738 | assert.strictEqual(error._tag, "SettingsSecretUnresolvedError"); |
| 739 | assert.strictEqual(execute.mock.calls.length, requests); |
| 740 | }).pipe(Effect.provide(layer)); |
| 741 | }); |
| 742 | |
| 743 | it.effect("never puts a saved token that is unsafe for an HTTP header on the wire", () => { |
| 744 | const { execute, layer } = makeLayer({ |
| 745 | response: () => Response.json({ username: "bitbucket-user" }), |
| 746 | }); |
| 747 | |
| 748 | return Effect.gen(function* () { |
| 749 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 750 | const settings = yield* TestSourceControlHost.TestSourceControlHostSettings; |
| 751 | |
| 752 | // Fetch would reject this header with an error quoting the token, and that error reaches |
| 753 | // clients. The unusable token is ignored, so the environment credential is used instead. |
| 754 | yield* settings.update({ |
| 755 | sourceControlHosts: { bitbucket: { accessToken: "saved\ntoken" } }, |
| 756 | }); |
| 757 | yield* bitbucket.probeAuth; |
| 758 | assert.strictEqual( |
| 759 | execute.mock.calls.at(-1)?.[0].headers.authorization, |
| 760 | `Basic ${btoa("user@example.com:token")}`, |
| 761 | ); |
| 762 | }).pipe(Effect.provide(layer)); |
| 763 | }); |
| 764 | |
| 765 | it.effect("reports saved credentials as configured when Bitbucket cannot confirm them", () => { |
| 766 | const { execute, layer } = makeLayer({ |
| 767 | response: () => new Response(null, { status: 401 }), |
| 768 | env: { T3CODE_BITBUCKET_API_BASE_URL: "https://api.test.local/2.0" }, |
| 769 | }); |
| 770 | |
| 771 | return Effect.gen(function* () { |
| 772 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 773 | const settings = yield* TestSourceControlHost.TestSourceControlHostSettings; |
| 774 | |
| 775 | assert.strictEqual((yield* bitbucket.probeAuth).status, "unauthenticated"); |
| 776 | assert.strictEqual(execute.mock.calls.length, 0); |
| 777 | |
| 778 | yield* settings.update({ |
| 779 | sourceControlHosts: { bitbucket: { accessToken: "saved-access-token" } }, |
| 780 | }); |
| 781 | assert.deepStrictEqual(yield* bitbucket.probeAuth, { |
| 782 | status: "unknown", |
| 783 | account: Option.none(), |
| 784 | host: Option.some("bitbucket.org"), |
| 785 | detail: Option.some("An access token is configured."), |
| 786 | }); |
| 787 | }).pipe(Effect.provide(layer)); |
| 788 | }); |
| 789 | |
| 790 | it.effect("keeps Bitbucket response bodies out of checkout diagnostics", () => { |
| 791 | const responseBody = '{"error":{"message":"credential=secret-value"}}'; |
| 792 | const { layer } = makeLayer({ |
| 793 | response: () => new Response(responseBody, { status: 403 }), |
| 794 | }); |
| 795 | |
| 796 | return Effect.gen(function* () { |
| 797 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 798 | const error = yield* bitbucket |
| 799 | .checkoutPullRequest({ cwd: "/repo", reference: "42" }) |
| 800 | .pipe(Effect.flip); |
| 801 | |
| 802 | assert.instanceOf(error, BitbucketApi.BitbucketResponseError); |
| 803 | assert.strictEqual(error.operation, "getPullRequest"); |
| 804 | assert.strictEqual(error.status, 403); |
| 805 | assert.strictEqual(error.responseBodyLength, responseBody.length); |
| 806 | assert.notProperty(error, "responseBody"); |
| 807 | assert.strictEqual( |
| 808 | error.message, |
| 809 | "Bitbucket API failed in getPullRequest: Bitbucket returned HTTP 403.", |
| 810 | ); |
| 811 | assert.notInclude(error.message, "secret-value"); |
| 812 | }).pipe(Effect.provide(layer)); |
| 813 | }); |
| 814 | |
| 815 | it.effect("checks out fork pull requests through an ensured fork remote", () => { |
| 816 | const { git, layer } = makeLayer({ |
| 817 | response: (request) => { |
| 818 | if (request.url.endsWith("/repositories/octocat/t3code")) { |
| 819 | return Response.json({ |
| 820 | ...repositoryJson, |
| 821 | full_name: "octocat/t3code", |
| 822 | links: { |
| 823 | html: { href: "https://bitbucket.org/octocat/t3code" }, |
| 824 | clone: [ |
| 825 | { name: "https", href: "https://bitbucket.org/octocat/t3code.git" }, |
| 826 | { name: "ssh", href: "git@bitbucket.org:octocat/t3code.git" }, |
| 827 | ], |
| 828 | }, |
| 829 | }); |
| 830 | } |
| 831 | return Response.json({ |
| 832 | ...bitbucketPullRequest, |
| 833 | source: { |
| 834 | branch: { name: "main" }, |
| 835 | repository: { |
| 836 | full_name: "octocat/t3code", |
| 837 | workspace: { slug: "octocat" }, |
| 838 | }, |
| 839 | }, |
| 840 | }); |
| 841 | }, |
| 842 | }); |
| 843 | |
| 844 | return Effect.gen(function* () { |
| 845 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 846 | yield* bitbucket.checkoutPullRequest({ |
| 847 | cwd: "/repo", |
| 848 | reference: "42", |
| 849 | force: true, |
| 850 | }); |
| 851 | |
| 852 | assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], { |
| 853 | cwd: "/repo", |
| 854 | preferredName: "octocat", |
| 855 | url: "git@bitbucket.org:octocat/t3code.git", |
| 856 | }); |
| 857 | assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], { |
| 858 | cwd: "/repo", |
| 859 | remoteName: "octocat", |
| 860 | remoteBranch: "main", |
| 861 | localBranch: "t3code/pr-42/main", |
| 862 | }); |
| 863 | assert.deepStrictEqual(git.setBranchUpstream.mock.calls[0]?.[0], { |
| 864 | cwd: "/repo", |
| 865 | branch: "t3code/pr-42/main", |
| 866 | remoteName: "octocat", |
| 867 | remoteBranch: "main", |
| 868 | }); |
| 869 | assert.deepStrictEqual(git.switchRef.mock.calls[0]?.[0], { |
| 870 | cwd: "/repo", |
| 871 | refName: "t3code/pr-42/main", |
| 872 | }); |
| 873 | }).pipe(Effect.provide(layer)); |
| 874 | }); |
| 875 | |
| 876 | it.effect("refuses a url that points away from the configured Bitbucket", () => { |
| 877 | const { layer, execute } = makeLayer({ response: () => new Response("{}", { status: 200 }) }); |
| 878 | return Effect.gen(function* () { |
| 879 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 880 | |
| 881 | const error = yield* Effect.flip( |
| 882 | bitbucket.request({ method: "GET", url: "https://attacker.example/2.0/repositories" }), |
| 883 | ); |
| 884 | |
| 885 | assert.strictEqual(error._tag, "BitbucketUntrustedUrlError"); |
| 886 | assert.strictEqual(execute.mock.calls.length, 0); |
| 887 | }).pipe(Effect.provide(layer)); |
| 888 | }); |
| 889 | |
| 890 | it.effect("keeps only the host of a url it refuses, never its query", () => |
| 891 | Effect.gen(function* () { |
| 892 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 893 | |
| 894 | const error = yield* Effect.flip( |
| 895 | bitbucket.request({ |
| 896 | method: "GET", |
| 897 | url: "https://attacker.example/asset?signature=secret-token", |
| 898 | }), |
| 899 | ); |
| 900 | |
| 901 | assert.strictEqual(error._tag, "BitbucketUntrustedUrlError"); |
| 902 | assert.strictEqual( |
| 903 | error._tag === "BitbucketUntrustedUrlError" ? error.host : "", |
| 904 | "https://attacker.example", |
| 905 | ); |
| 906 | assert.notInclude(error.message, "secret-token"); |
| 907 | }).pipe(Effect.provide(makeLayer({ response: () => new Response("{}", { status: 200 }) }).layer)), |
| 908 | ); |
| 909 | |
| 910 | it.effect("does not follow a redirect off the configured Bitbucket", () => |
| 911 | Effect.gen(function* () { |
| 912 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 913 | |
| 914 | const error = yield* Effect.flip( |
| 915 | bitbucket.request({ method: "GET", url: "/repositories/acme/web/pullrequests/1/diff" }), |
| 916 | ); |
| 917 | |
| 918 | assert.strictEqual(error._tag, "BitbucketUntrustedUrlError"); |
| 919 | }).pipe( |
| 920 | Effect.provide( |
| 921 | makeLayer({ |
| 922 | response: () => |
| 923 | new Response(null, { |
| 924 | status: 302, |
| 925 | headers: { location: "https://attacker.example/stolen" }, |
| 926 | }), |
| 927 | }).layer, |
| 928 | ), |
| 929 | ), |
| 930 | ); |
| 931 | |
| 932 | it.effect("follows a redirect that stays on the configured Bitbucket", () => |
| 933 | Effect.gen(function* () { |
| 934 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 935 | |
| 936 | const result = yield* bitbucket.request({ |
| 937 | method: "GET", |
| 938 | url: "/repositories/acme/web/pullrequests/1/diff", |
| 939 | }); |
| 940 | |
| 941 | assert.strictEqual(result.body, "diff --git a/a.ts b/a.ts"); |
| 942 | assert.isFalse(result.truncated); |
| 943 | }).pipe( |
| 944 | Effect.provide( |
| 945 | makeLayer({ |
| 946 | response: (request) => |
| 947 | request.url.endsWith("/pullrequests/1/diff") |
| 948 | ? new Response(null, { |
| 949 | status: 302, |
| 950 | headers: { location: "https://api.test.local/2.0/repositories/acme/web/diff/abc" }, |
| 951 | }) |
| 952 | : new Response("diff --git a/a.ts b/a.ts", { status: 200 }), |
| 953 | }).layer, |
| 954 | ), |
| 955 | ), |
| 956 | ); |
| 957 | |
| 958 | it.effect("cuts a response short rather than reading an unbounded diff into memory", () => |
| 959 | Effect.gen(function* () { |
| 960 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 961 | |
| 962 | const result = yield* bitbucket.request({ |
| 963 | method: "GET", |
| 964 | url: "/repositories/acme/web/pullrequests/1/diff", |
| 965 | maxBytes: 8, |
| 966 | }); |
| 967 | |
| 968 | assert.strictEqual(result.body, "12345678"); |
| 969 | assert.isTrue(result.truncated); |
| 970 | }).pipe( |
| 971 | Effect.provide( |
| 972 | makeLayer({ response: () => new Response("1234567890", { status: 200 }) }).layer, |
| 973 | ), |
| 974 | ), |
| 975 | ); |
| 976 | |
| 977 | it.effect("pauses background reads after a 429 until Bitbucket's retry-after passes", () => { |
| 978 | let answered = 0; |
| 979 | const { execute, layer } = makeLayer({ |
| 980 | response: () => { |
| 981 | answered += 1; |
| 982 | return answered === 1 |
| 983 | ? new Response("Rate limit exceeded", { status: 429, headers: { "retry-after": "60" } }) |
| 984 | : Response.json({ values: [] }); |
| 985 | }, |
| 986 | }); |
| 987 | |
| 988 | return Effect.gen(function* () { |
| 989 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 990 | const lookup = bitbucket.listPullRequests({ |
| 991 | cwd: "/repo", |
| 992 | headSelector: "feature/a", |
| 993 | state: "all", |
| 994 | }); |
| 995 | |
| 996 | yield* Effect.flip(lookup); |
| 997 | const paused = yield* Effect.flip(lookup); |
| 998 | // Keyed by the repository host, where PullRequestService records its own backoff. |
| 999 | assert.deepInclude(paused, { |
| 1000 | _tag: "SourceControlRateLimitPausedError", |
| 1001 | host: "bitbucket.org", |
| 1002 | retryAt: 60_000, |
| 1003 | }); |
| 1004 | assert.strictEqual(execute.mock.calls.length, 1); |
| 1005 | |
| 1006 | // A user waiting on an answer is not held back by the background pause, and their success |
| 1007 | // does not end it early. |
| 1008 | yield* lookup.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true)); |
| 1009 | yield* bitbucket.request({ |
| 1010 | method: "POST", |
| 1011 | url: "/repositories/acme/web/pullrequests/1/approve", |
| 1012 | }); |
| 1013 | |
| 1014 | yield* TestClock.adjust("59 seconds"); |
| 1015 | assert.strictEqual((yield* Effect.flip(lookup))._tag, "SourceControlRateLimitPausedError"); |
| 1016 | |
| 1017 | yield* TestClock.adjust("1 second"); |
| 1018 | yield* lookup; |
| 1019 | }).pipe(Effect.provide(layer)); |
| 1020 | }); |
| 1021 | |
| 1022 | it.effect("keeps a sweep over many branches to a few Bitbucket requests at a time", () => |
| 1023 | Effect.gen(function* () { |
| 1024 | const started = yield* Queue.unbounded<void>(); |
| 1025 | const release = yield* Deferred.make<void>(); |
| 1026 | let inFlight = 0; |
| 1027 | let peak = 0; |
| 1028 | const { layer } = makeLayer({ |
| 1029 | response: () => Response.json({ values: [] }), |
| 1030 | answer: (response) => |
| 1031 | Effect.gen(function* () { |
| 1032 | inFlight += 1; |
| 1033 | peak = Math.max(peak, inFlight); |
| 1034 | yield* Queue.offer(started, undefined); |
| 1035 | yield* Deferred.await(release); |
| 1036 | inFlight -= 1; |
| 1037 | return response; |
| 1038 | }), |
| 1039 | }); |
| 1040 | |
| 1041 | yield* Effect.gen(function* () { |
| 1042 | const bitbucket = yield* BitbucketApi.BitbucketApi; |
| 1043 | const sweep = yield* Effect.forEach( |
| 1044 | Array.from({ length: 12 }, (_, index) => `feature/${index}`), |
| 1045 | (branch) => |
| 1046 | bitbucket.listPullRequests({ cwd: "/repo", headSelector: branch, state: "all" }), |
| 1047 | { concurrency: "unbounded", discard: true }, |
| 1048 | ).pipe(Effect.forkChild); |
| 1049 | for (let index = 0; index < 4; index += 1) yield* Queue.take(started); |
| 1050 | yield* Deferred.succeed(release, undefined); |
| 1051 | yield* Fiber.join(sweep); |
| 1052 | }).pipe(Effect.provide(layer)); |
| 1053 | |
| 1054 | assert.strictEqual(peak, 4); |
| 1055 | }), |
| 1056 | ); |