|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { parseGitHubUrl, isParseError } from "./github-url"; |
| 3 | + |
| 4 | +describe("parseGitHubUrl", () => { |
| 5 | + describe("valid full URLs", () => { |
| 6 | + it("parses https://github.com/user/repo", () => { |
| 7 | + const result = parseGitHubUrl("https://github.com/user/repo"); |
| 8 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 9 | + }); |
| 10 | + |
| 11 | + it("parses http://github.com/user/repo", () => { |
| 12 | + const result = parseGitHubUrl("http://github.com/user/repo"); |
| 13 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 14 | + }); |
| 15 | + |
| 16 | + it("parses URL with trailing slash", () => { |
| 17 | + const result = parseGitHubUrl("https://github.com/user/repo/"); |
| 18 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 19 | + }); |
| 20 | + |
| 21 | + it("parses URL with www prefix", () => { |
| 22 | + const result = parseGitHubUrl("https://www.github.com/user/repo"); |
| 23 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 24 | + }); |
| 25 | + |
| 26 | + it("strips .git suffix", () => { |
| 27 | + const result = parseGitHubUrl("https://github.com/user/repo.git"); |
| 28 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 29 | + }); |
| 30 | + |
| 31 | + it("ignores subpaths (tree/main/src)", () => { |
| 32 | + const result = parseGitHubUrl("https://github.com/fastapi/fastapi/tree/main/src"); |
| 33 | + expect(result).toEqual({ username: "fastapi", repo: "fastapi" }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("ignores query params", () => { |
| 37 | + const result = parseGitHubUrl("https://github.com/user/repo?tab=readme"); |
| 38 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("ignores fragment", () => { |
| 42 | + const result = parseGitHubUrl("https://github.com/user/repo#readme"); |
| 43 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("handles hyphens and dots in names", () => { |
| 47 | + const result = parseGitHubUrl("https://github.com/tom-draper/api-analytics"); |
| 48 | + expect(result).toEqual({ username: "tom-draper", repo: "api-analytics" }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("handles underscores and dots in repo names", () => { |
| 52 | + const result = parseGitHubUrl("https://github.com/user/my_repo.js"); |
| 53 | + expect(result).toEqual({ username: "user", repo: "my_repo.js" }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe("no-protocol URLs", () => { |
| 58 | + it("parses github.com/user/repo", () => { |
| 59 | + const result = parseGitHubUrl("github.com/user/repo"); |
| 60 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("parses www.github.com/user/repo", () => { |
| 64 | + const result = parseGitHubUrl("www.github.com/user/repo"); |
| 65 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("parses github.com/user/repo with subpath", () => { |
| 69 | + const result = parseGitHubUrl("github.com/user/repo/pulls"); |
| 70 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe("shorthand format", () => { |
| 75 | + it("parses user/repo", () => { |
| 76 | + const result = parseGitHubUrl("user/repo"); |
| 77 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("parses fastapi/fastapi", () => { |
| 81 | + const result = parseGitHubUrl("fastapi/fastapi"); |
| 82 | + expect(result).toEqual({ username: "fastapi", repo: "fastapi" }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("parses user/repo with trailing slash", () => { |
| 86 | + const result = parseGitHubUrl("user/repo/"); |
| 87 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("strips .git from shorthand", () => { |
| 91 | + const result = parseGitHubUrl("user/repo.git"); |
| 92 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("whitespace handling", () => { |
| 97 | + it("trims leading/trailing whitespace", () => { |
| 98 | + const result = parseGitHubUrl(" https://github.com/user/repo "); |
| 99 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 100 | + }); |
| 101 | + |
| 102 | + it("trims shorthand whitespace", () => { |
| 103 | + const result = parseGitHubUrl(" user/repo "); |
| 104 | + expect(result).toEqual({ username: "user", repo: "repo" }); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("error cases", () => { |
| 109 | + it("rejects empty string", () => { |
| 110 | + const result = parseGitHubUrl(""); |
| 111 | + expect(isParseError(result)).toBe(true); |
| 112 | + if (isParseError(result)) { |
| 113 | + expect(result.error).toBe("Please enter a GitHub repository URL"); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it("rejects whitespace-only string", () => { |
| 118 | + const result = parseGitHubUrl(" "); |
| 119 | + expect(isParseError(result)).toBe(true); |
| 120 | + if (isParseError(result)) { |
| 121 | + expect(result.error).toBe("Please enter a GitHub repository URL"); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it("rejects GitLab URL", () => { |
| 126 | + const result = parseGitHubUrl("https://gitlab.com/user/repo"); |
| 127 | + expect(isParseError(result)).toBe(true); |
| 128 | + if (isParseError(result)) { |
| 129 | + expect(result.error).toBe("Only GitHub repositories are supported"); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + it("rejects Bitbucket URL", () => { |
| 134 | + const result = parseGitHubUrl("https://bitbucket.org/user/repo"); |
| 135 | + expect(isParseError(result)).toBe(true); |
| 136 | + if (isParseError(result)) { |
| 137 | + expect(result.error).toBe("Only GitHub repositories are supported"); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it("rejects single word", () => { |
| 142 | + const result = parseGitHubUrl("justarepo"); |
| 143 | + expect(isParseError(result)).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it("rejects bare domain", () => { |
| 147 | + const result = parseGitHubUrl("github.com"); |
| 148 | + expect(isParseError(result)).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + it("rejects github.com with only username", () => { |
| 152 | + const result = parseGitHubUrl("https://github.com/user"); |
| 153 | + expect(isParseError(result)).toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + it("rejects random URL", () => { |
| 157 | + const result = parseGitHubUrl("https://example.com/something"); |
| 158 | + expect(isParseError(result)).toBe(true); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe("isParseError helper", () => { |
| 163 | + it("returns true for error results", () => { |
| 164 | + expect(isParseError({ error: "test" })).toBe(true); |
| 165 | + }); |
| 166 | + |
| 167 | + it("returns false for success results", () => { |
| 168 | + expect(isParseError({ username: "u", repo: "r" })).toBe(false); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments