|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { parseSourceUrl } from "./url-parser.js"; |
| 3 | + |
| 4 | +describe("parseSourceUrl", () => { |
| 5 | + describe("GitHub URLs", () => { |
| 6 | + it("parses basic github.com URL", () => { |
| 7 | + const result = parseSourceUrl("https://github.com/owner/repo"); |
| 8 | + expect(result.type).toBe("github"); |
| 9 | + expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "HEAD" }); |
| 10 | + expect(result.defaultIndexName).toBe("repo"); |
| 11 | + }); |
| 12 | + |
| 13 | + it("parses GitHub URL with tree/branch", () => { |
| 14 | + const result = parseSourceUrl("https://github.com/owner/repo/tree/main"); |
| 15 | + expect(result.type).toBe("github"); |
| 16 | + expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "main" }); |
| 17 | + expect(result.defaultIndexName).toBe("repo"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("parses GitHub URL with tree/feature/branch (slashes in branch name)", () => { |
| 21 | + const result = parseSourceUrl("https://github.com/owner/repo/tree/feature/branch"); |
| 22 | + expect(result.type).toBe("github"); |
| 23 | + expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "feature/branch" }); |
| 24 | + expect(result.defaultIndexName).toBe("repo"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("parses GitHub URL with commit SHA", () => { |
| 28 | + const result = parseSourceUrl("https://github.com/owner/repo/commit/abc123def456"); |
| 29 | + expect(result.type).toBe("github"); |
| 30 | + expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "abc123def456" }); |
| 31 | + expect(result.defaultIndexName).toBe("repo"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("throws on invalid GitHub URL without repo", () => { |
| 35 | + expect(() => parseSourceUrl("https://github.com/owner")).toThrow("Invalid GitHub URL"); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("GitLab URLs", () => { |
| 40 | + it("parses basic gitlab.com URL", () => { |
| 41 | + const result = parseSourceUrl("https://gitlab.com/group/project"); |
| 42 | + expect(result.type).toBe("gitlab"); |
| 43 | + expect(result.config).toEqual({ projectId: "group/project", ref: "HEAD", baseUrl: undefined }); |
| 44 | + expect(result.defaultIndexName).toBe("project"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("parses GitLab URL with subgroups", () => { |
| 48 | + const result = parseSourceUrl("https://gitlab.com/group/subgroup/project"); |
| 49 | + expect(result.type).toBe("gitlab"); |
| 50 | + expect(result.config).toEqual({ |
| 51 | + projectId: "group/subgroup/project", |
| 52 | + ref: "HEAD", |
| 53 | + baseUrl: undefined, |
| 54 | + }); |
| 55 | + expect(result.defaultIndexName).toBe("project"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("parses GitLab URL with /-/tree/branch", () => { |
| 59 | + const result = parseSourceUrl("https://gitlab.com/group/project/-/tree/main"); |
| 60 | + expect(result.type).toBe("gitlab"); |
| 61 | + expect(result.config).toEqual({ projectId: "group/project", ref: "main", baseUrl: undefined }); |
| 62 | + expect(result.defaultIndexName).toBe("project"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("parses GitLab URL with /-/tree/feature/branch", () => { |
| 66 | + const result = parseSourceUrl("https://gitlab.com/group/project/-/tree/feature/branch"); |
| 67 | + expect(result.type).toBe("gitlab"); |
| 68 | + expect(result.config).toEqual({ |
| 69 | + projectId: "group/project", |
| 70 | + ref: "feature/branch", |
| 71 | + baseUrl: undefined, |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it("parses self-hosted GitLab URL", () => { |
| 76 | + const result = parseSourceUrl("https://gitlab.mycompany.com/team/project"); |
| 77 | + expect(result.type).toBe("gitlab"); |
| 78 | + expect(result.config).toEqual({ |
| 79 | + projectId: "team/project", |
| 80 | + ref: "HEAD", |
| 81 | + baseUrl: "https://gitlab.mycompany.com", |
| 82 | + }); |
| 83 | + expect(result.defaultIndexName).toBe("project"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("throws on invalid GitLab URL", () => { |
| 87 | + expect(() => parseSourceUrl("https://gitlab.com/group")).toThrow("Invalid GitLab URL"); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("Bitbucket URLs", () => { |
| 92 | + it("parses basic bitbucket.org URL", () => { |
| 93 | + const result = parseSourceUrl("https://bitbucket.org/workspace/repo"); |
| 94 | + expect(result.type).toBe("bitbucket"); |
| 95 | + expect(result.config).toEqual({ |
| 96 | + workspace: "workspace", |
| 97 | + repo: "repo", |
| 98 | + ref: "HEAD", |
| 99 | + baseUrl: undefined, |
| 100 | + }); |
| 101 | + expect(result.defaultIndexName).toBe("repo"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("parses Bitbucket URL with /src/branch", () => { |
| 105 | + const result = parseSourceUrl("https://bitbucket.org/workspace/repo/src/main"); |
| 106 | + expect(result.type).toBe("bitbucket"); |
| 107 | + expect(result.config).toEqual({ |
| 108 | + workspace: "workspace", |
| 109 | + repo: "repo", |
| 110 | + ref: "main", |
| 111 | + baseUrl: undefined, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it("parses Bitbucket URL with /branch/feature", () => { |
| 116 | + const result = parseSourceUrl("https://bitbucket.org/workspace/repo/branch/feature"); |
| 117 | + expect(result.type).toBe("bitbucket"); |
| 118 | + expect(result.config).toEqual({ |
| 119 | + workspace: "workspace", |
| 120 | + repo: "repo", |
| 121 | + ref: "feature", |
| 122 | + baseUrl: undefined, |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it("parses self-hosted Bitbucket URL", () => { |
| 127 | + const result = parseSourceUrl("https://bitbucket.mycompany.com/workspace/repo"); |
| 128 | + expect(result.type).toBe("bitbucket"); |
| 129 | + expect(result.config).toEqual({ |
| 130 | + workspace: "workspace", |
| 131 | + repo: "repo", |
| 132 | + ref: "HEAD", |
| 133 | + baseUrl: "https://bitbucket.mycompany.com", |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("throws on invalid Bitbucket URL", () => { |
| 138 | + expect(() => parseSourceUrl("https://bitbucket.org/workspace")).toThrow("Invalid Bitbucket URL"); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe("Website URLs (fallback)", () => { |
| 143 | + it("parses unknown URL as website", () => { |
| 144 | + const result = parseSourceUrl("https://docs.example.com/api/v2"); |
| 145 | + expect(result.type).toBe("website"); |
| 146 | + expect(result.config).toEqual({ url: "https://docs.example.com/api/v2" }); |
| 147 | + expect(result.defaultIndexName).toBe("docs.example.com"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("uses hostname as default index name for website", () => { |
| 151 | + const result = parseSourceUrl("https://react.dev/learn/thinking-in-react"); |
| 152 | + expect(result.type).toBe("website"); |
| 153 | + expect(result.defaultIndexName).toBe("react.dev"); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe("Invalid URLs", () => { |
| 158 | + it("throws on invalid URL format", () => { |
| 159 | + expect(() => parseSourceUrl("not-a-url")).toThrow(); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
0 commit comments