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

describe("Lua version compatibility", function()
	describe("os.execute return value", function()
		it("returns true for successful commands", function()
			local success = os.execute("true")
			assert.is_true(success == true or success == 0)
		end)

		it("returns non-true for failed commands", function()
			local success = os.execute("false")
			assert.is_not_true(success == true)
		end)
	end)

	describe("load() with 4 arguments", function()
		it("works with mode and env parameters", function()
			local chunk = load("return 42", "test", "t", {})
			assert.is_function(chunk)
			assert.are.equal(42, chunk())
		end)

		it("respects restricted environment", function()
			local chunk = load("return { x = 1, y = 2 }", "test", "t", {})
			assert.is_function(chunk)
			local result = chunk()
			assert.are.equal(1, result.x)
			assert.are.equal(2, result.y)
		end)
	end)

	describe("string patterns used in wt", function()
		it("matches git URL project names", function()
			local url = "git@github.com:user/project.git"
			local name = url:match("([^/]+)%.git$") or url:match("([^/:]+)$")
			assert.are.equal("project", name)
		end)

		it("matches https URL project names", function()
			local url = "https://github.com/user/project.git"
			local name = url:match("([^/]+)%.git$") or url:match("([^/:]+)$")
			assert.are.equal("project", name)
		end)

		it("handles gitdir patterns", function()
			local content = "gitdir: .bare"
			assert.is_truthy(content:match("gitdir:%s*%.?/?%.bare"))
		end)
	end)

	describe("io.popen", function()
		it("captures command output", function()
			local handle = io.popen("echo hello")
			assert.is_not_nil(handle)
			local output = handle:read("*a")
			handle:close()
			assert.are.equal("hello\n", output)
		end)
	end)
end)
