-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathurl-parser.test.ts
More file actions
220 lines (192 loc) · 8.06 KB
/
url-parser.test.ts
File metadata and controls
220 lines (192 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { describe, it, expect } from "vitest";
import { parseSourceUrl } from "./url-parser.js";
describe("parseSourceUrl", () => {
describe("GitHub URLs", () => {
it("parses basic github.com URL", () => {
const result = parseSourceUrl("https://github.com/owner/repo");
expect(result.type).toBe("github");
expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "HEAD" });
expect(result.defaultIndexName).toBe("repo");
});
it("parses GitHub URL with tree/branch", () => {
const result = parseSourceUrl("https://github.com/owner/repo/tree/main");
expect(result.type).toBe("github");
expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "main" });
expect(result.defaultIndexName).toBe("repo");
});
it("parses GitHub URL with tree/feature/branch (slashes in branch name)", () => {
const result = parseSourceUrl("https://github.com/owner/repo/tree/feature/branch");
expect(result.type).toBe("github");
expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "feature/branch" });
expect(result.defaultIndexName).toBe("repo");
});
it("parses GitHub URL with commit SHA", () => {
const result = parseSourceUrl("https://github.com/owner/repo/commit/abc123def456");
expect(result.type).toBe("github");
expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "abc123def456" });
expect(result.defaultIndexName).toBe("repo");
});
it("throws on invalid GitHub URL without repo", () => {
expect(() => parseSourceUrl("https://github.com/owner")).toThrow("Invalid GitHub URL");
});
});
describe("GitLab URLs", () => {
it("parses basic gitlab.com URL", () => {
const result = parseSourceUrl("https://gitlab.com/group/project");
expect(result.type).toBe("gitlab");
expect(result.config).toEqual({ projectId: "group/project", ref: "HEAD", baseUrl: undefined });
expect(result.defaultIndexName).toBe("project");
});
it("parses GitLab URL with subgroups", () => {
const result = parseSourceUrl("https://gitlab.com/group/subgroup/project");
expect(result.type).toBe("gitlab");
expect(result.config).toEqual({
projectId: "group/subgroup/project",
ref: "HEAD",
baseUrl: undefined,
});
expect(result.defaultIndexName).toBe("project");
});
it("parses GitLab URL with /-/tree/branch", () => {
const result = parseSourceUrl("https://gitlab.com/group/project/-/tree/main");
expect(result.type).toBe("gitlab");
expect(result.config).toEqual({ projectId: "group/project", ref: "main", baseUrl: undefined });
expect(result.defaultIndexName).toBe("project");
});
it("parses GitLab URL with /-/tree/feature/branch", () => {
const result = parseSourceUrl("https://gitlab.com/group/project/-/tree/feature/branch");
expect(result.type).toBe("gitlab");
expect(result.config).toEqual({
projectId: "group/project",
ref: "feature/branch",
baseUrl: undefined,
});
});
it("parses self-hosted GitLab URL", () => {
const result = parseSourceUrl("https://gitlab.mycompany.com/team/project");
expect(result.type).toBe("gitlab");
expect(result.config).toEqual({
projectId: "team/project",
ref: "HEAD",
baseUrl: "https://gitlab.mycompany.com",
});
expect(result.defaultIndexName).toBe("project");
});
it("throws on invalid GitLab URL", () => {
expect(() => parseSourceUrl("https://gitlab.com/group")).toThrow("Invalid GitLab URL");
});
});
describe("Bitbucket URLs", () => {
it("parses basic bitbucket.org URL", () => {
const result = parseSourceUrl("https://bitbucket.org/workspace/repo");
expect(result.type).toBe("bitbucket");
expect(result.config).toEqual({
workspace: "workspace",
repo: "repo",
ref: "HEAD",
baseUrl: undefined,
});
expect(result.defaultIndexName).toBe("repo");
});
it("parses Bitbucket URL with /src/branch", () => {
const result = parseSourceUrl("https://bitbucket.org/workspace/repo/src/main");
expect(result.type).toBe("bitbucket");
expect(result.config).toEqual({
workspace: "workspace",
repo: "repo",
ref: "main",
baseUrl: undefined,
});
});
it("parses Bitbucket URL with /branch/feature", () => {
const result = parseSourceUrl("https://bitbucket.org/workspace/repo/branch/feature");
expect(result.type).toBe("bitbucket");
expect(result.config).toEqual({
workspace: "workspace",
repo: "repo",
ref: "feature",
baseUrl: undefined,
});
});
it("parses self-hosted Bitbucket URL", () => {
const result = parseSourceUrl("https://bitbucket.mycompany.com/workspace/repo");
expect(result.type).toBe("bitbucket");
expect(result.config).toEqual({
workspace: "workspace",
repo: "repo",
ref: "HEAD",
baseUrl: "https://bitbucket.mycompany.com",
});
});
it("throws on invalid Bitbucket URL", () => {
expect(() => parseSourceUrl("https://bitbucket.org/workspace")).toThrow("Invalid Bitbucket URL");
});
});
describe("Website URLs (fallback)", () => {
it("parses unknown URL as website", () => {
const result = parseSourceUrl("https://docs.example.com/api/v2");
expect(result.type).toBe("website");
expect(result.config).toEqual({ url: "https://docs.example.com/api/v2" });
expect(result.defaultIndexName).toBe("docs.example.com");
});
it("uses hostname as default index name for website", () => {
const result = parseSourceUrl("https://react.dev/learn/thinking-in-react");
expect(result.type).toBe("website");
expect(result.defaultIndexName).toBe("react.dev");
});
});
describe("Invalid URLs", () => {
it("throws on invalid URL format", () => {
expect(() => parseSourceUrl("not-a-url")).toThrow();
});
});
});
describe("Edge cases", () => {
describe(".git suffix handling", () => {
it("strips .git suffix from GitHub URLs", () => {
const result = parseSourceUrl("https://github.com/owner/repo.git");
expect(result.type).toBe("github");
expect(result.config).toEqual({ owner: "owner", repo: "repo", ref: "HEAD" });
expect(result.defaultIndexName).toBe("repo");
});
it("strips .git suffix from GitLab URLs", () => {
const result = parseSourceUrl("https://gitlab.com/group/project.git");
expect(result.type).toBe("gitlab");
expect(result.config).toEqual({ projectId: "group/project", ref: "HEAD", baseUrl: undefined });
expect(result.defaultIndexName).toBe("project");
});
it("strips .git suffix from Bitbucket URLs", () => {
const result = parseSourceUrl("https://bitbucket.org/workspace/repo.git");
expect(result.type).toBe("bitbucket");
expect(result.config).toEqual({
workspace: "workspace",
repo: "repo",
ref: "HEAD",
baseUrl: undefined,
});
expect(result.defaultIndexName).toBe("repo");
});
});
describe("Conservative self-hosted detection", () => {
it("detects gitlab.company.com as GitLab", () => {
const result = parseSourceUrl("https://gitlab.company.com/team/project");
expect(result.type).toBe("gitlab");
});
it("does NOT match notgitlab.com as GitLab", () => {
const result = parseSourceUrl("https://notgitlab.com/some/path");
expect(result.type).toBe("website");
});
it("does NOT match mygitlabserver.com as GitLab", () => {
const result = parseSourceUrl("https://mygitlabserver.com/some/path");
expect(result.type).toBe("website");
});
it("detects bitbucket.company.com as Bitbucket", () => {
const result = parseSourceUrl("https://bitbucket.company.com/workspace/repo");
expect(result.type).toBe("bitbucket");
});
it("does NOT match notbitbucket.org as Bitbucket", () => {
const result = parseSourceUrl("https://notbitbucket.org/some/path");
expect(result.type).toBe("website");
});
});
});