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

BitbucketApi.test.ts

758 lines23.4 KB

1import { assert, it, vi } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as ConfigProvider from "effect/ConfigProvider";
4import * as DateTime from "effect/DateTime";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Option from "effect/Option";
9import {
10 HttpClient,
11 HttpClientError,
12 HttpClientRequest,
13 HttpClientResponse,
14} from "effect/unstable/http";
15​
16import { GitCommandError } from "@t3tools/contracts";
17import * as BitbucketApi from "./BitbucketApi.ts";
18import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
19import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
20import type * as VcsDriver from "../vcs/VcsDriver.ts";
21​
22const bitbucketPullRequest = {
23 id: 42,
24 title: "Add Bitbucket provider",
25 state: "OPEN",
26 updated_on: "2026-01-02T00:00:00.000Z",
27 links: {
28 html: {
29 href: "https://bitbucket.org/pingdotgg/t3code/pull-requests/42",
30 },
31 },
32 source: {
33 branch: { name: "feature/source-control" },
34 repository: {
35 full_name: "octocat/t3code",
36 workspace: { slug: "octocat" },
37 },
38 },
39 destination: {
40 branch: { name: "main" },
41 repository: {
42 full_name: "pingdotgg/t3code",
43 workspace: { slug: "pingdotgg" },
44 },
45 },
46};
47​
48const repositoryJson = {
49 full_name: "pingdotgg/t3code",
50 links: {
51 html: { href: "https://bitbucket.org/pingdotgg/t3code" },
52 clone: [
53 { name: "https", href: "https://bitbucket.org/pingdotgg/t3code.git" },
54 { name: "ssh", href: "git@bitbucket.org:pingdotgg/t3code.git" },
55 ],
56 },
57 mainbranch: { name: "main" },
58};
59​
60function makeLayer(input: {
61 readonly response: (request: HttpClientRequest.HttpClientRequest) => Response;
62 readonly requestFailure?: (
63 request: HttpClientRequest.HttpClientRequest,
64 ) => HttpClientError.HttpClientError;
65 readonly git?: Partial<GitVcsDriver.GitVcsDriver["Service"]>;
66}) {
67 const execute = vi.fn((request: HttpClientRequest.HttpClientRequest) =>
68 input.requestFailure
69 ? Effect.fail(input.requestFailure(request))
70 : Effect.succeed(HttpClientResponse.fromWeb(request, input.response(request))),
71 );
72 const gitMock = {
73 readConfigValue: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["readConfigValue"]>(() =>
74 Effect.succeed<string | null>("git@bitbucket.org:pingdotgg/t3code.git"),
75 ),
76 resolvePrimaryRemoteName: vi.fn<
77 GitVcsDriver.GitVcsDriver["Service"]["resolvePrimaryRemoteName"]
78 >(() => Effect.succeed("origin")),
79 ensureRemote: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["ensureRemote"]>(() =>
80 Effect.succeed("octocat"),
81 ),
82 fetchRemoteBranch: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["fetchRemoteBranch"]>(
83 () => Effect.void,
84 ),
85 fetchRemoteTrackingBranch: vi.fn<
86 GitVcsDriver.GitVcsDriver["Service"]["fetchRemoteTrackingBranch"]
87 >(() => Effect.void),
88 setBranchUpstream: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["setBranchUpstream"]>(
89 () => Effect.void,
90 ),
91 switchRef: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["switchRef"]>((request) =>
92 Effect.succeed({ refName: request.refName }),
93 ),
94 listLocalBranchNames: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["listLocalBranchNames"]>(() =>
95 Effect.succeed([]),
96 ),
97 };
98 const git = {
99 ...gitMock,
100 ...input.git,
101 } satisfies Partial<GitVcsDriver.GitVcsDriver["Service"]>;
102​
103 const driver = {
104 listRemotes: () =>
105 Effect.succeed({
106 remotes: [
107 {
108 name: "origin",
109 url: "git@bitbucket.org:pingdotgg/t3code.git",
110 pushUrl: Option.none(),
111 isPrimary: true,
112 },
113 ],
114 freshness: {
115 source: "live-local" as const,
116 observedAt: DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"),
117 expiresAt: Option.none(),
118 },
119 }),
120 } satisfies Partial<VcsDriver.VcsDriver["Service"]>;
121​
122 const layer = BitbucketApi.layer.pipe(
123 Layer.provide(
124 Layer.succeed(
125 HttpClient.HttpClient,
126 HttpClient.make((request) => execute(request)),
127 ),
128 ),
129 Layer.provide(
130 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
131 resolve: () =>
132 Effect.succeed({
133 kind: "git",
134 repository: {
135 kind: "git",
136 rootPath: "/repo",
137 metadataPath: null,
138 freshness: {
139 source: "live-local" as const,
140 observedAt: DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"),
141 expiresAt: Option.none(),
142 },
143 },
144 driver: driver as unknown as VcsDriver.VcsDriver["Service"],
145 }),
146 }),
147 ),
148 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)(git)),
149 Layer.provide(
150 ConfigProvider.layer(
151 ConfigProvider.fromEnv({
152 env: {
153 T3CODE_BITBUCKET_API_BASE_URL: "https://api.test.local/2.0",
154 T3CODE_BITBUCKET_EMAIL: "user@example.com",
155 T3CODE_BITBUCKET_API_TOKEN: "token",
156 },
157 }),
158 ),
159 ),
160 Layer.provideMerge(NodeServices.layer),
161 );
162​
163 return { execute, git: gitMock, layer };
165​
166it.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​
200it.effect("lists pull requests with Bitbucket state and source branch query params", () => {
201 const { execute, layer } = makeLayer({
202 response: () =>
203 Response.json({
204 values: [
205 {
206 ...bitbucketPullRequest,
207 id: 7,
208 state: "MERGED",
209 source: {
210 branch: { name: "feature/merged" },
211 repository: { full_name: "pingdotgg/t3code" },
212 },
213 },
214 ],
215 }),
216 });
217​
218 return Effect.gen(function* () {
219 const bitbucket = yield* BitbucketApi.BitbucketApi;
220 const result = yield* bitbucket.listPullRequests({
221 cwd: "/repo",
222 headSelector: "origin:feature/merged",
223 state: "merged",
224 limit: 10,
225 });
226​
227 assert.strictEqual(result[0]?.state, "merged");
228 const request = execute.mock.calls[0]?.[0];
229 assert.strictEqual(
230 request?.url,
231 "https://api.test.local/2.0/repositories/pingdotgg/t3code/pullrequests",
232 );
233 assert.deepStrictEqual(request?.urlParams.params, [
234 ["pagelen", "10"],
235 ["sort", "-updated_on"],
236 ["q", 'source.branch.name = "feature/merged" AND state = "MERGED"'],
237 ["state", "MERGED"],
238 ]);
239 }).pipe(Effect.provide(layer));
240});
241​
242it.effect("lists closed pull requests with both closed Bitbucket states", () => {
243 const { execute, layer } = makeLayer({
244 response: () =>
245 Response.json({
246 values: [],
247 }),
248 });
249​
250 return Effect.gen(function* () {
251 const bitbucket = yield* BitbucketApi.BitbucketApi;
252 yield* bitbucket.listPullRequests({
253 cwd: "/repo",
254 headSelector: "feature/closed",
255 state: "closed",
256 limit: 10,
257 });
258​
259 assert.deepStrictEqual(execute.mock.calls[0]?.[0].urlParams.params, [
260 ["pagelen", "10"],
261 ["sort", "-updated_on"],
262 [
263 "q",
264 'source.branch.name = "feature/closed" AND (state = "DECLINED" OR state = "SUPERSEDED")',
265 ],
266 ["state", "DECLINED"],
267 ["state", "SUPERSEDED"],
268 ]);
269 }).pipe(Effect.provide(layer));
270});
271​
272it.effect("expands all-state pull request listing instead of relying on Bitbucket defaults", () => {
273 const { execute, layer } = makeLayer({
274 response: () =>
275 Response.json({
276 values: [],
277 }),
278 });
279​
280 return Effect.gen(function* () {
281 const bitbucket = yield* BitbucketApi.BitbucketApi;
282 yield* bitbucket.listPullRequests({
283 cwd: "/repo",
284 headSelector: "feature/all",
285 state: "all",
286 limit: 10,
287 });
288​
289 assert.deepStrictEqual(execute.mock.calls[0]?.[0].urlParams.params, [
290 ["pagelen", "10"],
291 ["sort", "-updated_on"],
292 [
293 "q",
294 'source.branch.name = "feature/all" AND (state = "OPEN" OR state = "MERGED" OR state = "DECLINED" OR state = "SUPERSEDED")',
295 ],
296 ["state", "OPEN"],
297 ["state", "MERGED"],
298 ["state", "DECLINED"],
299 ["state", "SUPERSEDED"],
300 ]);
301 }).pipe(Effect.provide(layer));
302});
303​
304it.effect("reads repository clone URLs and default branch", () => {
305 const { layer } = makeLayer({
306 response: (request) =>
307 Response.json(
308 request.url.endsWith("/branching-model")
309 ? {
310 development: {
311 branch: { name: "main" },
312 name: "main",
313 use_mainbranch: true,
314 },
315 }
316 : repositoryJson,
317 ),
318 });
319​
320 return Effect.gen(function* () {
321 const bitbucket = yield* BitbucketApi.BitbucketApi;
322 const cloneUrls = yield* bitbucket.getRepositoryCloneUrls({
323 cwd: "/repo",
324 repository: "pingdotgg/t3code",
325 });
326 const defaultBranch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" });
327​
328 assert.deepStrictEqual(cloneUrls, {
329 nameWithOwner: "pingdotgg/t3code",
330 url: "https://bitbucket.org/pingdotgg/t3code.git",
331 sshUrl: "git@bitbucket.org:pingdotgg/t3code.git",
332 });
333 assert.strictEqual(defaultBranch, "main");
334 }).pipe(Effect.provide(layer));
335});
336​
337it.effect(
338 "prefers the Bitbucket branching model development branch as the default PR target",
339 () => {
340 const { execute, layer } = makeLayer({
341 response: (request) =>
342 Response.json(
343 request.url.endsWith("/branching-model")
344 ? {
345 development: {
346 branch: { name: "develop" },
347 name: "develop",
348 use_mainbranch: false,
349 },
350 }
351 : repositoryJson,
352 ),
353 });
354​
355 return Effect.gen(function* () {
356 const bitbucket = yield* BitbucketApi.BitbucketApi;
357 const defaultBranch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" });
358​
359 assert.strictEqual(defaultBranch, "develop");
360 assert.deepStrictEqual(
361 execute.mock.calls.map((call) => call[0].url).toSorted(),
362 [
363 "https://api.test.local/2.0/repositories/pingdotgg/t3code",
364 "https://api.test.local/2.0/repositories/pingdotgg/t3code/branching-model",
365 ].toSorted(),
366 );
367 }).pipe(Effect.provide(layer));
368 },
369);
370​
371it.effect(
372 "falls back to the repository main branch when the Bitbucket development branch is invalid",
373 () => {
374 const { layer } = makeLayer({
375 response: (request) =>
376 Response.json(
377 request.url.endsWith("/branching-model")
378 ? {
379 development: {
380 name: "develop",
381 use_mainbranch: false,
382 is_valid: false,
383 },
384 }
385 : repositoryJson,
386 ),
387 });
388​
389 return Effect.gen(function* () {
390 const bitbucket = yield* BitbucketApi.BitbucketApi;
391 const defaultBranch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" });
392​
393 assert.strictEqual(defaultBranch, "main");
394 }).pipe(Effect.provide(layer));
395 },
396);
397​
398it.effect(
399 "falls back to the repository main branch when the Bitbucket branching model is unavailable",
400 () => {
401 const { layer } = makeLayer({
402 response: (request) =>
403 request.url.endsWith("/branching-model")
404 ? Response.json({ error: { message: "Not found" } }, { status: 404 })
405 : Response.json(repositoryJson),
406 });
407​
408 return Effect.gen(function* () {
409 const bitbucket = yield* BitbucketApi.BitbucketApi;
410 const defaultBranch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" });
411​
412 assert.strictEqual(defaultBranch, "main");
413 }).pipe(Effect.provide(layer));
414 },
415);
416​
417it.effect("creates repositories through the Bitbucket REST API", () => {
418 const { execute, layer } = makeLayer({
419 response: () => Response.json(repositoryJson),
420 });
421​
422 return Effect.gen(function* () {
423 const bitbucket = yield* BitbucketApi.BitbucketApi;
424 const cloneUrls = yield* bitbucket.createRepository({
425 cwd: "/repo",
426 repository: "pingdotgg/t3code",
427 visibility: "private",
428 });
429​
430 assert.deepStrictEqual(cloneUrls, {
431 nameWithOwner: "pingdotgg/t3code",
432 url: "https://bitbucket.org/pingdotgg/t3code.git",
433 sshUrl: "git@bitbucket.org:pingdotgg/t3code.git",
434 });
435​
436 const request = execute.mock.calls[0]?.[0];
437 assert.strictEqual(request?.url, "https://api.test.local/2.0/repositories/pingdotgg/t3code");
438 assert.strictEqual(request?.method, "POST");
439 assert.ok(request);
440 const rawBody = (request.body as { readonly body?: Uint8Array }).body;
441 assert.ok(rawBody);
442 // @effect-diagnostics-next-line preferSchemaOverJson:off
443 assert.deepStrictEqual(JSON.parse(new TextDecoder().decode(rawBody)), {
444 scm: "git",
445 is_private: true,
446 });
447 }).pipe(Effect.provide(layer));
448});
449​
450it.effect("creates pull requests using the official REST payload shape", () => {
451 const { execute, layer } = makeLayer({
452 response: () => Response.json(bitbucketPullRequest),
453 });
454​
455 return Effect.gen(function* () {
456 const fileSystem = yield* FileSystem.FileSystem;
457 const bodyFile = yield* fileSystem.makeTempFileScoped({ prefix: "bitbucket-pr-body-" });
458 yield* fileSystem.writeFileString(bodyFile, "PR body");
459​
460 const bitbucket = yield* BitbucketApi.BitbucketApi;
461 yield* bitbucket.createPullRequest({
462 cwd: "/repo",
463 baseBranch: "main",
464 headSelector: "owner:feature/provider",
465 title: "Provider PR",
466 bodyFile,
467 });
468​
469 const request = execute.mock.calls[0]?.[0];
470 assert.strictEqual(
471 request?.url,
472 "https://api.test.local/2.0/repositories/pingdotgg/t3code/pullrequests",
473 );
474 assert.strictEqual(request?.method, "POST");
475 assert.ok(request);
476 const rawBody = (request.body as { readonly body?: Uint8Array }).body;
477 assert.ok(rawBody);
478 // @effect-diagnostics-next-line preferSchemaOverJson:off
479 assert.deepStrictEqual(JSON.parse(new TextDecoder().decode(rawBody)), {
480 title: "Provider PR",
481 description: "PR body",
482 source: {
483 branch: { name: "feature/provider" },
484 repository: { full_name: "owner/t3code" },
485 },
486 destination: {
487 branch: { name: "main" },
488 },
489 });
490 }).pipe(Effect.provide(layer), Effect.scoped);
491});
492​
493it.effect("reports auth status through the Bitbucket REST /user endpoint", () => {
494 const { layer } = makeLayer({
495 response: () => Response.json({ username: "bitbucket-user" }),
496 });
497​
498 return Effect.gen(function* () {
499 const bitbucket = yield* BitbucketApi.BitbucketApi;
500 const auth = yield* bitbucket.probeAuth;
501​
502 assert.deepStrictEqual(auth, {
503 status: "authenticated",
504 account: Option.some("bitbucket-user"),
505 host: Option.some("bitbucket.org"),
506 detail: Option.none(),
507 });
508 }).pipe(Effect.provide(layer));
509});
510​
511it.effect("preserves the HTTP client failure without deriving the domain message from it", () => {
512 const transportCause = new Error("socket reset by peer");
513 let requestFailure: HttpClientError.HttpClientError | undefined;
514 const { layer } = makeLayer({
515 response: () => Response.json({}),
516 requestFailure: (request) => {
517 requestFailure = new HttpClientError.HttpClientError({
518 reason: new HttpClientError.TransportError({
519 request,
520 cause: transportCause,
521 }),
522 });
523 return requestFailure;
524 },
525 });
526​
527 return Effect.gen(function* () {
528 const bitbucket = yield* BitbucketApi.BitbucketApi;
529 const error = yield* Effect.flip(
530 bitbucket.getPullRequest({
531 cwd: "/repo",
532 reference: "42",
533 }),
534 );
535​
536 assert.instanceOf(error, BitbucketApi.BitbucketRequestError);
537 assert.strictEqual(error.operation, "getPullRequest");
538 assert.strictEqual(
539 error.message,
540 "Bitbucket API failed in getPullRequest: Failed to send the Bitbucket request.",
541 );
542 assert.strictEqual(error.cause, requestFailure);
543 assert.strictEqual(requestFailure?.cause, transportCause);
544 }).pipe(Effect.provide(layer));
545});
546​
547it.effect("keeps Bitbucket response bodies out of checkout diagnostics", () => {
548 const responseBody = '{"error":{"message":"credential=secret-value"}}';
549 const { layer } = makeLayer({
550 response: () => new Response(responseBody, { status: 403 }),
551 });
552​
553 return Effect.gen(function* () {
554 const bitbucket = yield* BitbucketApi.BitbucketApi;
555 const error = yield* bitbucket
556 .checkoutPullRequest({ cwd: "/repo", reference: "42" })
557 .pipe(Effect.flip);
558​
559 assert.instanceOf(error, BitbucketApi.BitbucketResponseError);
560 assert.strictEqual(error.operation, "getPullRequest");
561 assert.strictEqual(error.status, 403);
562 assert.strictEqual(error.responseBodyLength, responseBody.length);
563 assert.notProperty(error, "responseBody");
564 assert.strictEqual(
565 error.message,
566 "Bitbucket API failed in getPullRequest: Bitbucket returned HTTP 403.",
567 );
568 assert.notInclude(error.message, "secret-value");
569 }).pipe(Effect.provide(layer));
570});
571​
572it.effect("preserves Bitbucket response body read failures as their immediate cause", () => {
573 const cause = new Error("response stream failed");
574 const { layer } = makeLayer({
575 response: () =>
576 new Response(
577 new ReadableStream<Uint8Array>({
578 start: (controller) => controller.error(cause),
579 }),
580 { status: 502 },
581 ),
582 });
583​
584 return Effect.gen(function* () {
585 const bitbucket = yield* BitbucketApi.BitbucketApi;
586 const error = yield* bitbucket
587 .getPullRequest({ cwd: "/repo", reference: "42" })
588 .pipe(Effect.flip);
589​
590 assert.instanceOf(error, BitbucketApi.BitbucketResponseBodyReadError);
591 assert.strictEqual(error.operation, "getPullRequest");
592 assert.strictEqual(error.status, 502);
593 assert.instanceOf(error.cause, HttpClientError.HttpClientError);
594 assert.strictEqual(error.cause.cause, cause);
595 assert.strictEqual(
596 error.message,
597 "Bitbucket API failed in getPullRequest: Bitbucket returned HTTP 502.",
598 );
599 }).pipe(Effect.provide(layer));
600});
601​
602it.effect("checks out same-repository pull requests with the existing Bitbucket remote", () => {
603 const { git, layer } = makeLayer({
604 response: () =>
605 Response.json({
606 ...bitbucketPullRequest,
607 source: {
608 branch: { name: "feature/source-control" },
609 repository: {
610 full_name: "pingdotgg/t3code",
611 workspace: { slug: "pingdotgg" },
612 },
613 },
614 }),
615 });
616​
617 return Effect.gen(function* () {
618 const bitbucket = yield* BitbucketApi.BitbucketApi;
619 yield* bitbucket.checkoutPullRequest({
620 cwd: "/repo",
621 context: {
622 provider: {
623 kind: "bitbucket",
624 name: "Bitbucket",
625 baseUrl: "https://bitbucket.org",
626 },
627 remoteName: "origin",
628 remoteUrl: "git@bitbucket.org:pingdotgg/t3code.git",
629 },
630 reference: "42",
631 force: true,
632 });
633​
634 assert.strictEqual(git.ensureRemote.mock.calls.length, 0);
635 assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], {
636 cwd: "/repo",
637 remoteName: "origin",
638 remoteBranch: "feature/source-control",
639 localBranch: "feature/source-control",
640 });
641 assert.deepStrictEqual(git.setBranchUpstream.mock.calls[0]?.[0], {
642 cwd: "/repo",
643 branch: "feature/source-control",
644 remoteName: "origin",
645 remoteBranch: "feature/source-control",
646 });
647 assert.deepStrictEqual(git.switchRef.mock.calls[0]?.[0], {
648 cwd: "/repo",
649 refName: "feature/source-control",
650 });
651 }).pipe(Effect.provide(layer));
652});
653​
654it.effect("preserves Git checkout failures without deriving the domain message from them", () => {
655 const gitCause = new GitCommandError({
656 operation: "fetchRemoteBranch",
657 command: "git fetch origin feature/source-control",
658 cwd: "/repo",
659 detail: "remote rejected the request",
660 });
661 const { layer } = makeLayer({
662 response: () =>
663 Response.json({
664 ...bitbucketPullRequest,
665 source: {
666 branch: { name: "feature/source-control" },
667 repository: {
668 full_name: "pingdotgg/t3code",
669 workspace: { slug: "pingdotgg" },
670 },
671 },
672 }),
673 git: {
674 fetchRemoteBranch: () => Effect.fail(gitCause),
675 },
676 });
677​
678 return Effect.gen(function* () {
679 const bitbucket = yield* BitbucketApi.BitbucketApi;
680 const error = yield* Effect.flip(
681 bitbucket.checkoutPullRequest({
682 cwd: "/repo",
683 reference: "42",
684 force: true,
685 }),
686 );
687​
688 assert.instanceOf(error, BitbucketApi.BitbucketCheckoutError);
689 assert.strictEqual(error.cwd, "/repo");
690 assert.strictEqual(error.reference, "42");
691 assert.strictEqual(
692 error.message,
693 "Bitbucket API failed in checkoutPullRequest: Failed to check out the Bitbucket pull request.",
694 );
695 assert.strictEqual(error.cause, gitCause);
696 }).pipe(Effect.provide(layer));
697});
698​
699it.effect("checks out fork pull requests through an ensured fork remote", () => {
700 const { git, layer } = makeLayer({
701 response: (request) => {
702 if (request.url.endsWith("/repositories/octocat/t3code")) {
703 return Response.json({
704 ...repositoryJson,
705 full_name: "octocat/t3code",
706 links: {
707 html: { href: "https://bitbucket.org/octocat/t3code" },
708 clone: [
709 { name: "https", href: "https://bitbucket.org/octocat/t3code.git" },
710 { name: "ssh", href: "git@bitbucket.org:octocat/t3code.git" },
711 ],
712 },
713 });
714 }
715 return Response.json({
716 ...bitbucketPullRequest,
717 source: {
718 branch: { name: "main" },
719 repository: {
720 full_name: "octocat/t3code",
721 workspace: { slug: "octocat" },
722 },
723 },
724 });
725 },
726 });
727​
728 return Effect.gen(function* () {
729 const bitbucket = yield* BitbucketApi.BitbucketApi;
730 yield* bitbucket.checkoutPullRequest({
731 cwd: "/repo",
732 reference: "42",
733 force: true,
734 });
735​
736 assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], {
737 cwd: "/repo",
738 preferredName: "octocat",
739 url: "git@bitbucket.org:octocat/t3code.git",
740 });
741 assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], {
742 cwd: "/repo",
743 remoteName: "octocat",
744 remoteBranch: "main",
745 localBranch: "t3code/pr-42/main",
746 });
747 assert.deepStrictEqual(git.setBranchUpstream.mock.calls[0]?.[0], {
748 cwd: "/repo",
749 branch: "t3code/pr-42/main",
750 remoteName: "octocat",
751 remoteBranch: "main",
752 });
753 assert.deepStrictEqual(git.switchRef.mock.calls[0]?.[0], {
754 cwd: "/repo",
755 refName: "t3code/pr-42/main",
756 });
757 }).pipe(Effect.provide(layer));
758});