Open History to see the last commit
1import {
2 makeSourceControlDefinitionRegistry,
3 recognizeRemoteWith,
4} from "@t3tools/source-control-core/shared/definition";
5import { describe, expect, it } from "vite-plus/test";
6
7import { definition } from "./definition.ts";
8
9const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
10
11describe("Bitbucket change request URLs", () => {
12 it("reads a pull request on Bitbucket hosts", () => {
13 expect(
14 definition.isChangeRequestReference(
15 "https://bitbucket.org/workspace/repo/pull-requests/5/diff",
16 ),
17 ).toBe(true);
18 expect(parse("https://bitbucket.org/workspace/repo/pull-requests/5/diff")).toEqual({
19 host: "bitbucket.org",
20 repository: "workspace/repo",
21 number: 5,
22 repositoryUrl: "https://bitbucket.org/workspace/repo",
23 });
24 expect(parse("https://code.bitbucket.example.test/team/repo/pull-requests/6")).toMatchObject({
25 host: "code.bitbucket.example.test",
26 number: 6,
27 });
28 });
29
30 it("leaves other hosts alone", () => {
31 expect(parse("https://scm.example.test/workspace/repo/pull-requests/5")).toBeNull();
32 expect(
33 definition.isChangeRequestReference(
34 "https://scm.example.test/workspace/repo/pull-requests/5",
35 ),
36 ).toBe(false);
37 expect(
38 definition.isChangeRequestReference("https://bitbucket.org/workspace/repo/src/main"),
39 ).toBe(false);
40 });
41
42 it("builds pull request URLs below the repository", () => {
43 expect(
44 definition.changeRequestUrl({ host: "bitbucket.org", repository: "w/repo", number: 5 }),
45 ).toBe("https://bitbucket.org/w/repo/pull-requests/5");
46 expect(
47 definition.changeRequestUrl({
48 host: "bitbucket.org",
49 repository: "w/repo",
50 number: 5,
51 webUrl: "https://bitbucket.org/w/repo",
52 }),
53 ).toBe("https://bitbucket.org/w/repo/pull-requests/5");
54 });
55});
56
57describe("Bitbucket repository pages", () => {
58 it("reads the repository from a source view on Bitbucket hosts only", () => {
59 const repositoryUrl = (url: string) => definition.repositoryUrlFromPage?.(new URL(url));
60 expect(repositoryUrl("https://bitbucket.org/workspace/repo/src/main/")).toBe(
61 "https://bitbucket.org/workspace/repo",
62 );
63 expect(repositoryUrl("https://bitbucket.org/workspace/repo")).toBeNull();
64 expect(repositoryUrl("https://scm.example.test/workspace/repo/src/main/")).toBeNull();
65 });
66});
67
68describe("Bitbucket remotes", () => {
69 const recognize = (url: string) => recognizeRemoteWith(definition, url);
70
71 it("recognises bitbucket.org and hosts with a bitbucket label", () => {
72 expect(recognize("git@bitbucket.org:workspace/repo.git")).toEqual({
73 kind: "bitbucket",
74 name: "Bitbucket",
75 baseUrl: "https://bitbucket.org",
76 });
77 expect(recognize("https://bitbucket.example.com/workspace/repo.git")?.name).toBe(
78 "Bitbucket Self-Hosted",
79 );
80 expect(recognize("https://notbitbucket.example.com/workspace/repo.git")).toBeNull();
81 });
82
83 it.each([
84 "git@ssh.bitbucket.org:workspace/repo.git",
85 "ssh://git@altssh.bitbucket.org:443/workspace/repo.git",
86 ])("recognises Bitbucket Cloud's SSH host in %s", (url) => {
87 expect(recognize(url)).toEqual({
88 kind: "bitbucket",
89 name: "Bitbucket",
90 baseUrl: "https://bitbucket.org",
91 });
92 });
93});
94
95describe("Bitbucket repository keys", () => {
96 const registry = makeSourceControlDefinitionRegistry([definition]);
97
98 it.each([
99 "git@bitbucket.org:Workspace/Repo.git",
100 "git@ssh.bitbucket.org:Workspace/Repo.git",
101 "ssh://git@altssh.bitbucket.org:443/Workspace/Repo.git",
102 "https://bitbucket.org/Workspace/Repo.git",
103 ])("names %s on bitbucket.org, where its pull requests live", (remoteUrl) => {
104 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
105 expect(canonicalKey).toBe("bitbucket.org/workspace/repo");
106 const link = parse("https://bitbucket.org/workspace/repo/pull-requests/5")!;
107 expect(
108 definition.isChangeRequestInRepository(
109 {
110 canonicalKey,
111 displayName: "workspace/repo",
112 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
113 },
114 link,
115 ),
116 ).toBe(true);
117 });
118
119 it.each(["ssh.bitbucket.org", "altssh.bitbucket.org"])(
120 "matches pull requests for a key saved under %s",
121 (host) => {
122 const canonicalKey = `${host}/workspace/repo`;
123 expect(registry.canonicalRepositoryKey(canonicalKey)).toBe("bitbucket.org/workspace/repo");
124 const identity = {
125 canonicalKey,
126 displayName: "workspace/repo",
127 locator: {
128 source: "git-remote" as const,
129 remoteName: "origin",
130 remoteUrl: `git@${host}:workspace/repo.git`,
131 },
132 };
133 const link = parse("https://bitbucket.org/workspace/repo/pull-requests/5")!;
134 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
135 expect(definition.canReadChangeRequestOnHost(identity, link)).toBe(true);
136 },
137 );
138
139 it("leaves self-hosted keys alone", () => {
140 expect(registry.repositoryKeyFromRemote("git@bitbucket.example.com:team/repo.git")).toBe(
141 "bitbucket.example.com/team/repo",
142 );
143 });
144});