1-- Load main.lua as a module (it exports functions when required)
2package.path = package.path .. ";./?.lua"
3local wt = dofile("src/main.lua")
4
5describe("extract_project_name", function()
6 describe("standard git URLs", function()
7 it("extracts from SSH URL with .git suffix", function()
8 assert.are.equal("project", wt.extract_project_name("git@github.com:user/project.git"))
9 end)
10
11 it("extracts from HTTPS URL with .git suffix", function()
12 assert.are.equal("project", wt.extract_project_name("https://github.com/user/project.git"))
13 end)
14
15 it("extracts from HTTPS URL without .git suffix", function()
16 assert.are.equal("project", wt.extract_project_name("https://github.com/user/project"))
17 end)
18
19 it("extracts from SSH URL without .git suffix", function()
20 assert.are.equal("project", wt.extract_project_name("git@github.com:user/project"))
21 end)
22
23 it("extracts from ssh:// protocol URL", function()
24 assert.are.equal("repo", wt.extract_project_name("ssh://git@host:2222/user/repo.git"))
25 end)
26
27 it("extracts from gitlab-style nested groups", function()
28 assert.are.equal("repo", wt.extract_project_name("git@gitlab.com:group/subgroup/repo.git"))
29 end)
30 end)
31
32 describe("SCP-style URLs", function()
33 it("extracts from SCP URL with path", function()
34 assert.are.equal("project", wt.extract_project_name("git@host:user/project.git"))
35 end)
36
37 it("extracts from SCP URL without path slash", function()
38 assert.are.equal("repo", wt.extract_project_name("git@host:repo.git"))
39 end)
40 end)
41
42 describe("edge cases", function()
43 it("returns nil for empty string", function()
44 assert.is_nil(wt.extract_project_name(""))
45 end)
46
47 it("returns nil for just a slash", function()
48 assert.is_nil(wt.extract_project_name("/"))
49 end)
50
51 it("extracts bare project name with .git suffix", function()
52 assert.are.equal("repo", wt.extract_project_name("repo.git"))
53 end)
54
55 it("extracts bare project name without suffix", function()
56 assert.are.equal("repo", wt.extract_project_name("repo"))
57 end)
58
59 it("extracts project from URL with trailing slash", function()
60 assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo.git/"))
61 end)
62
63 it("extracts project from URL with query string", function()
64 assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo.git?ref=main"))
65 end)
66
67 it("extracts project from URL with fragment", function()
68 assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo#readme"))
69 end)
70 end)
71
72 describe("special characters in project names", function()
73 it("extracts project with dots", function()
74 assert.are.equal("my.project", wt.extract_project_name("git@github.com:user/my.project.git"))
75 end)
76
77 it("extracts project with hyphens", function()
78 assert.are.equal("my-project", wt.extract_project_name("git@github.com:user/my-project.git"))
79 end)
80
81 it("extracts project with underscores", function()
82 assert.are.equal("my_project", wt.extract_project_name("git@github.com:user/my_project.git"))
83 end)
84
85 it("extracts project with numbers", function()
86 assert.are.equal("project123", wt.extract_project_name("git@github.com:user/project123.git"))
87 end)
88 end)
89end)
90
91describe("resolve_url_template", function()
92 describe("basic substitution", function()
93 it("substitutes ${project} in SSH template", function()
94 local result = wt.resolve_url_template("git@github.com:myuser/${project}.git", "myrepo")
95 assert.are.equal("git@github.com:myuser/myrepo.git", result)
96 end)
97
98 it("substitutes ${project} in HTTPS template", function()
99 local result = wt.resolve_url_template("https://github.com/myuser/${project}.git", "myrepo")
100 assert.are.equal("https://github.com/myuser/myrepo.git", result)
101 end)
102
103 it("handles multiple ${project} occurrences", function()
104 local result = wt.resolve_url_template("${project}/${project}", "x")
105 assert.are.equal("x/x", result)
106 end)
107
108 it("returns unchanged template with no placeholder", function()
109 local result = wt.resolve_url_template("git@host:static.git", "ignored")
110 assert.are.equal("git@host:static.git", result)
111 end)
112 end)
113
114 describe("special characters in project name", function()
115 it("handles hyphens in project name", function()
116 local result = wt.resolve_url_template("git@host:${project}.git", "my-repo")
117 assert.are.equal("git@host:my-repo.git", result)
118 end)
119
120 it("handles dots in project name", function()
121 local result = wt.resolve_url_template("git@host:${project}.git", "my.repo")
122 assert.are.equal("git@host:my.repo.git", result)
123 end)
124
125 it("handles percent in project name", function()
126 assert.are.equal("git@host:weird%name.git", wt.resolve_url_template("git@host:${project}.git", "weird%name"))
127 end)
128 end)
129end)