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

LinkPullRequestDialog.logic.test.ts

136 lines4.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { changeRequestWebUrl, resolveLinkPullRequestInput } from "./LinkPullRequestDialog";
4​
5const project = {
6 host: "github.com",
7 repository: "acme/web",
8 webUrl: (number: number) => changeRequestWebUrl("github", "github.com", "acme/web", number),
9};
10​
11describe("resolveLinkPullRequestInput", () => {
12 it.each([
13 ["https://bitbucket.org/acme/web/pull-requests/42", "bitbucket.org"],
14 ["https://github.acme.test/acme/web/pull/42", "github.acme.test"],
15 ["https://git.acme.test/acme/web/-/merge_requests/42", "git.acme.test"],
16 ])("links supported host URL %s without a thread project", (url, host) => {
17 expect(
18 resolveLinkPullRequestInput({
19 reference: ` ${url} `,
20 project: null,
21 hasProject: (candidate) => candidate.host === host,
22 }),
23 ).toEqual({ link: { host, repository: "acme/web", number: 42, url } });
24 });
25​
26 it("validates the full Azure repository when resolving a browser URL", () => {
27 const hasProject = (reference: { host: string; repository: string }) =>
28 reference.host === "dev.azure.com" && reference.repository === "org-a/project/_git/web";
29 expect(
30 resolveLinkPullRequestInput({
31 reference: "https://dev.azure.com/org-a/project/_git/web/pullrequest/42",
32 project: null,
33 hasProject,
34 }),
35 ).toMatchObject({ link: { repository: "org-a/project/_git/web", number: 42 } });
36 expect(
37 resolveLinkPullRequestInput({
38 reference: "https://dev.azure.com/org-b/project/_git/web/pullrequest/42",
39 project: null,
40 hasProject,
41 }),
42 ).toMatchObject({ error: expect.stringContaining("org-b/project/_git/web") });
43 });
44​
45 it("resolves bare Azure numbers into canonical browser URLs", () => {
46 expect(
47 resolveLinkPullRequestInput({
48 reference: "#42",
49 project: {
50 host: "ssh.dev.azure.com",
51 repository: "v3/org/project/web",
52 webUrl: (number) =>
53 changeRequestWebUrl("azure-devops", "ssh.dev.azure.com", "v3/org/project/web", number),
54 },
55 hasProject: () => true,
56 }),
57 ).toMatchObject({
58 link: {
59 host: "dev.azure.com",
60 repository: "org/project/_git/web",
61 number: 42,
62 url: "https://dev.azure.com/org/project/_git/web/pullrequest/42",
63 },
64 });
65 });
66​
67 it("returns null for input that is not a reference", () => {
68 expect(
69 resolveLinkPullRequestInput({ reference: "hello", project, hasProject: () => true }),
70 ).toBeNull();
71 });
72​
73 it("resolves a bare number against the thread's own repository", () => {
74 expect(
75 resolveLinkPullRequestInput({ reference: "#42", project, hasProject: () => true }),
76 ).toEqual({
77 link: {
78 host: "github.com",
79 repository: "acme/web",
80 number: 42,
81 url: "https://github.com/acme/web/pull/42",
82 },
83 });
84 });
85​
86 it("links a URL from another repository on a host with a project", () => {
87 expect(
88 resolveLinkPullRequestInput({
89 reference: "https://github.com/acme/api/pull/7",
90 project,
91 hasProject: (reference) => reference.host === "github.com",
92 }),
93 ).toEqual({
94 link: {
95 host: "github.com",
96 repository: "acme/api",
97 number: 7,
98 url: "https://github.com/acme/api/pull/7",
99 },
100 });
101 });
102​
103 it("refuses a URL on a host nothing is checked out from", () => {
104 const result = resolveLinkPullRequestInput({
105 reference: "https://gitlab.com/acme/api/-/merge_requests/7",
106 project,
107 hasProject: () => false,
108 });
109 expect(result).toMatchObject({ error: expect.stringContaining("gitlab.com") });
110 });
111​
112 it("asks for a URL when a bare number has no project to resolve against", () => {
113 expect(
114 resolveLinkPullRequestInput({ reference: "12", project: null, hasProject: () => true }),
115 ).toMatchObject({ error: expect.stringContaining("full URL") });
116 });
117​
118 it("accepts a checkout command as a reference", () => {
119 expect(
120 resolveLinkPullRequestInput({
121 reference: "gh pr checkout https://github.com/acme/web/pull/3",
122 project,
123 hasProject: () => true,
124 }),
125 ).toMatchObject({ link: { number: 3, repository: "acme/web" } });
126 });
127});
128​
129describe("changeRequestWebUrl", () => {
130 it("knows the four hosts and nothing else", () => {
131 expect(changeRequestWebUrl("gitlab", "gitlab.com", "g/sub/repo", 5)).toBe(
132 "https://gitlab.com/g/sub/repo/-/merge_requests/5",
133 );
134 expect(changeRequestWebUrl("unknown", "x", "a/b", 1)).toBeNull();
135 });
136});