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

definition.test.ts

79 lines2.9 KB

1import { recognizeRemoteWith } from "@t3tools/source-control-core/shared/definition";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { definition } from "./definition.ts";
5​
6const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
7​
8describe("Bitbucket change request URLs", () => {
9 it("reads a pull request on Bitbucket hosts", () => {
10 expect(
11 definition.isChangeRequestReference(
12 "https://bitbucket.org/workspace/repo/pull-requests/5/diff",
13 ),
14 ).toBe(true);
15 expect(parse("https://bitbucket.org/workspace/repo/pull-requests/5/diff")).toEqual({
16 host: "bitbucket.org",
17 repository: "workspace/repo",
18 number: 5,
19 repositoryUrl: "https://bitbucket.org/workspace/repo",
20 });
21 expect(parse("https://code.bitbucket.example.test/team/repo/pull-requests/6")).toMatchObject({
22 host: "code.bitbucket.example.test",
23 number: 6,
24 });
25 });
26​
27 it("leaves other hosts alone", () => {
28 expect(parse("https://scm.example.test/workspace/repo/pull-requests/5")).toBeNull();
29 expect(
30 definition.isChangeRequestReference(
31 "https://scm.example.test/workspace/repo/pull-requests/5",
32 ),
33 ).toBe(false);
34 expect(
35 definition.isChangeRequestReference("https://bitbucket.org/workspace/repo/src/main"),
36 ).toBe(false);
37 });
38​
39 it("builds pull request URLs below the repository", () => {
40 expect(
41 definition.changeRequestUrl({ host: "bitbucket.org", repository: "w/repo", number: 5 }),
42 ).toBe("https://bitbucket.org/w/repo/pull-requests/5");
43 expect(
44 definition.changeRequestUrl({
45 host: "bitbucket.org",
46 repository: "w/repo",
47 number: 5,
48 webUrl: "https://bitbucket.org/w/repo",
49 }),
50 ).toBe("https://bitbucket.org/w/repo/pull-requests/5");
51 });
52});
53​
54describe("Bitbucket repository pages", () => {
55 it("reads the repository from a source view on Bitbucket hosts only", () => {
56 const repositoryUrl = (url: string) => definition.repositoryUrlFromPage?.(new URL(url));
57 expect(repositoryUrl("https://bitbucket.org/workspace/repo/src/main/")).toBe(
58 "https://bitbucket.org/workspace/repo",
59 );
60 expect(repositoryUrl("https://bitbucket.org/workspace/repo")).toBeNull();
61 expect(repositoryUrl("https://scm.example.test/workspace/repo/src/main/")).toBeNull();
62 });
63});
64​
65describe("Bitbucket remotes", () => {
66 const recognize = (url: string) => recognizeRemoteWith(definition, url);
67​
68 it("recognises bitbucket.org and hosts with a bitbucket label", () => {
69 expect(recognize("git@bitbucket.org:workspace/repo.git")).toEqual({
70 kind: "bitbucket",
71 name: "Bitbucket",
72 baseUrl: "https://bitbucket.org",
73 });
74 expect(recognize("https://bitbucket.example.com/workspace/repo.git")?.name).toBe(
75 "Bitbucket Self-Hosted",
76 );
77 expect(recognize("https://notbitbucket.example.com/workspace/repo.git")).toBeNull();
78 });
79});