|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { |
| 3 | + getCliInstallUrl, |
| 4 | + getCliName, |
| 5 | + getDisplayRepo, |
| 6 | + getMRLabel, |
| 7 | + getMRNumberLabel, |
| 8 | + getPlatformLabel, |
| 9 | + parsePRUrl, |
| 10 | + prRefFromMetadata, |
| 11 | + type PRMetadata, |
| 12 | +} from "./pr-provider"; |
| 13 | + |
| 14 | +describe("pr-provider platform helpers", () => { |
| 15 | + test("parses GitHub PR URLs including nested suffixes", () => { |
| 16 | + const ref = parsePRUrl("https://github.com/backnotprop/plannotator/pull/364/files"); |
| 17 | + |
| 18 | + expect(ref).toEqual({ |
| 19 | + platform: "github", |
| 20 | + owner: "backnotprop", |
| 21 | + repo: "plannotator", |
| 22 | + number: 364, |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + test("parses GitLab.com MR URLs", () => { |
| 27 | + const ref = parsePRUrl("https://gitlab.com/group/project/-/merge_requests/42/diffs"); |
| 28 | + |
| 29 | + expect(ref).toEqual({ |
| 30 | + platform: "gitlab", |
| 31 | + host: "gitlab.com", |
| 32 | + projectPath: "group/project", |
| 33 | + iid: 42, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + test("parses self-hosted GitLab MR URLs with nested groups", () => { |
| 38 | + const ref = parsePRUrl("https://gitlab.example.com/group/subgroup/project/-/merge_requests/7"); |
| 39 | + |
| 40 | + expect(ref).toEqual({ |
| 41 | + platform: "gitlab", |
| 42 | + host: "gitlab.example.com", |
| 43 | + projectPath: "group/subgroup/project", |
| 44 | + iid: 7, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + test("returns null for unsupported URLs", () => { |
| 49 | + expect(parsePRUrl("https://example.com/not-a-pr/123")).toBeNull(); |
| 50 | + expect(parsePRUrl("")).toBeNull(); |
| 51 | + }); |
| 52 | + |
| 53 | + test("formats platform-aware labels for GitHub and GitLab", () => { |
| 54 | + const githubMeta: PRMetadata = { |
| 55 | + platform: "github", |
| 56 | + owner: "backnotprop", |
| 57 | + repo: "plannotator", |
| 58 | + number: 364, |
| 59 | + title: "GitHub PR", |
| 60 | + author: "backnotprop", |
| 61 | + baseBranch: "main", |
| 62 | + headBranch: "feature/github", |
| 63 | + baseSha: "base", |
| 64 | + headSha: "head", |
| 65 | + url: "https://github.com/backnotprop/plannotator/pull/364", |
| 66 | + }; |
| 67 | + |
| 68 | + const gitlabMeta: PRMetadata = { |
| 69 | + platform: "gitlab", |
| 70 | + host: "gitlab.example.com", |
| 71 | + projectPath: "group/project", |
| 72 | + iid: 42, |
| 73 | + title: "GitLab MR", |
| 74 | + author: "alice", |
| 75 | + baseBranch: "main", |
| 76 | + headBranch: "feature/gitlab", |
| 77 | + baseSha: "base", |
| 78 | + headSha: "head", |
| 79 | + url: "https://gitlab.example.com/group/project/-/merge_requests/42", |
| 80 | + }; |
| 81 | + |
| 82 | + expect(getPlatformLabel(githubMeta)).toBe("GitHub"); |
| 83 | + expect(getMRLabel(githubMeta)).toBe("PR"); |
| 84 | + expect(getMRNumberLabel(githubMeta)).toBe("#364"); |
| 85 | + expect(getDisplayRepo(githubMeta)).toBe("backnotprop/plannotator"); |
| 86 | + |
| 87 | + expect(getPlatformLabel(gitlabMeta)).toBe("GitLab"); |
| 88 | + expect(getMRLabel(gitlabMeta)).toBe("MR"); |
| 89 | + expect(getMRNumberLabel(gitlabMeta)).toBe("!42"); |
| 90 | + expect(getDisplayRepo(gitlabMeta)).toBe("group/project"); |
| 91 | + }); |
| 92 | + |
| 93 | + test("reconstructs refs and CLI metadata for each platform", () => { |
| 94 | + const githubMeta: PRMetadata = { |
| 95 | + platform: "github", |
| 96 | + owner: "backnotprop", |
| 97 | + repo: "plannotator", |
| 98 | + number: 1, |
| 99 | + title: "GitHub PR", |
| 100 | + author: "backnotprop", |
| 101 | + baseBranch: "main", |
| 102 | + headBranch: "feature/github", |
| 103 | + baseSha: "base", |
| 104 | + headSha: "head", |
| 105 | + url: "https://github.com/backnotprop/plannotator/pull/1", |
| 106 | + }; |
| 107 | + |
| 108 | + const gitlabMeta: PRMetadata = { |
| 109 | + platform: "gitlab", |
| 110 | + host: "gitlab.example.com", |
| 111 | + projectPath: "group/project", |
| 112 | + iid: 2, |
| 113 | + title: "GitLab MR", |
| 114 | + author: "alice", |
| 115 | + baseBranch: "main", |
| 116 | + headBranch: "feature/gitlab", |
| 117 | + baseSha: "base", |
| 118 | + headSha: "head", |
| 119 | + url: "https://gitlab.example.com/group/project/-/merge_requests/2", |
| 120 | + }; |
| 121 | + |
| 122 | + const githubRef = prRefFromMetadata(githubMeta); |
| 123 | + const gitlabRef = prRefFromMetadata(gitlabMeta); |
| 124 | + |
| 125 | + expect(githubRef).toEqual({ |
| 126 | + platform: "github", |
| 127 | + owner: "backnotprop", |
| 128 | + repo: "plannotator", |
| 129 | + number: 1, |
| 130 | + }); |
| 131 | + expect(gitlabRef).toEqual({ |
| 132 | + platform: "gitlab", |
| 133 | + host: "gitlab.example.com", |
| 134 | + projectPath: "group/project", |
| 135 | + iid: 2, |
| 136 | + }); |
| 137 | + |
| 138 | + expect(getCliName(githubRef)).toBe("gh"); |
| 139 | + expect(getCliInstallUrl(githubRef)).toBe("https://cli.github.com"); |
| 140 | + expect(getCliName(gitlabRef)).toBe("glab"); |
| 141 | + expect(getCliInstallUrl(gitlabRef)).toBe("https://gitlab.com/gitlab-org/cli"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments