-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
--
-- SPDX-License-Identifier: GPL-3.0-or-later

-- Load main.lua as a module (it exports functions when required)
package.path = package.path .. ";./?.lua"
local wt = dofile("src/main.lua")

describe("extract_project_name", function()
	describe("standard git URLs", function()
		it("extracts from SSH URL with .git suffix", function()
			assert.are.equal("project", wt.extract_project_name("git@github.com:user/project.git"))
		end)

		it("extracts from HTTPS URL with .git suffix", function()
			assert.are.equal("project", wt.extract_project_name("https://github.com/user/project.git"))
		end)

		it("extracts from HTTPS URL without .git suffix", function()
			assert.are.equal("project", wt.extract_project_name("https://github.com/user/project"))
		end)

		it("extracts from SSH URL without .git suffix", function()
			assert.are.equal("project", wt.extract_project_name("git@github.com:user/project"))
		end)

		it("extracts from ssh:// protocol URL", function()
			assert.are.equal("repo", wt.extract_project_name("ssh://git@host:2222/user/repo.git"))
		end)

		it("extracts from gitlab-style nested groups", function()
			assert.are.equal("repo", wt.extract_project_name("git@gitlab.com:group/subgroup/repo.git"))
		end)
	end)

	describe("SCP-style URLs", function()
		it("extracts from SCP URL with path", function()
			assert.are.equal("project", wt.extract_project_name("git@host:user/project.git"))
		end)

		it("extracts from SCP URL without path slash", function()
			assert.are.equal("repo", wt.extract_project_name("git@host:repo.git"))
		end)
	end)

	describe("edge cases", function()
		it("returns nil for empty string", function()
			assert.is_nil(wt.extract_project_name(""))
		end)

		it("returns nil for just a slash", function()
			assert.is_nil(wt.extract_project_name("/"))
		end)

		it("extracts bare project name with .git suffix", function()
			assert.are.equal("repo", wt.extract_project_name("repo.git"))
		end)

		it("extracts bare project name without suffix", function()
			assert.are.equal("repo", wt.extract_project_name("repo"))
		end)

		it("extracts project from URL with trailing slash", function()
			assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo.git/"))
		end)

		it("extracts project from URL with query string", function()
			assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo.git?ref=main"))
		end)

		it("extracts project from URL with fragment", function()
			assert.are.equal("repo", wt.extract_project_name("https://github.com/user/repo#readme"))
		end)
	end)

	describe("special characters in project names", function()
		it("extracts project with dots", function()
			assert.are.equal("my.project", wt.extract_project_name("git@github.com:user/my.project.git"))
		end)

		it("extracts project with hyphens", function()
			assert.are.equal("my-project", wt.extract_project_name("git@github.com:user/my-project.git"))
		end)

		it("extracts project with underscores", function()
			assert.are.equal("my_project", wt.extract_project_name("git@github.com:user/my_project.git"))
		end)

		it("extracts project with numbers", function()
			assert.are.equal("project123", wt.extract_project_name("git@github.com:user/project123.git"))
		end)
	end)
end)

describe("resolve_url_template", function()
	describe("basic substitution", function()
		it("substitutes ${project} in SSH template", function()
			local result = wt.resolve_url_template("git@github.com:myuser/${project}.git", "myrepo")
			assert.are.equal("git@github.com:myuser/myrepo.git", result)
		end)

		it("substitutes ${project} in HTTPS template", function()
			local result = wt.resolve_url_template("https://github.com/myuser/${project}.git", "myrepo")
			assert.are.equal("https://github.com/myuser/myrepo.git", result)
		end)

		it("handles multiple ${project} occurrences", function()
			local result = wt.resolve_url_template("${project}/${project}", "x")
			assert.are.equal("x/x", result)
		end)

		it("returns unchanged template with no placeholder", function()
			local result = wt.resolve_url_template("git@host:static.git", "ignored")
			assert.are.equal("git@host:static.git", result)
		end)
	end)

	describe("special characters in project name", function()
		it("handles hyphens in project name", function()
			local result = wt.resolve_url_template("git@host:${project}.git", "my-repo")
			assert.are.equal("git@host:my-repo.git", result)
		end)

		it("handles dots in project name", function()
			local result = wt.resolve_url_template("git@host:${project}.git", "my.repo")
			assert.are.equal("git@host:my.repo.git", result)
		end)

		it("handles percent in project name", function()
			assert.are.equal("git@host:weird%name.git", wt.resolve_url_template("git@host:${project}.git", "weird%name"))
		end)
	end)
end)
