import os
import pathlib
import subprocess
import tempfile
import unittest


class InstallDepsLockBoundaryTest(unittest.TestCase):
    def test_system_tool_install_does_not_require_lock_entry(self):
        repo = pathlib.Path(__file__).resolve().parents[1]
        script = repo / "scripts" / "install-deps"

        with tempfile.TemporaryDirectory() as tmp:
            tmp_path = pathlib.Path(tmp)
            bin_dir = tmp_path / "bin"
            bin_dir.mkdir()

            git_cmd = bin_dir / "git"
            git_cmd.write_text("#!/usr/bin/env bash\necho 'git version 2.50.1'\n", encoding="utf-8")
            git_cmd.chmod(0o755)

            manifest = tmp_path / "deps.toml"
            manifest.write_text(
                """
[deps.git]
command = "git"
target = "latest"

[deps.git.mac]
installer = "system"
source = "https://git-scm.com/downloads"
""".strip(),
                encoding="utf-8",
            )

            lock = tmp_path / "deps.lock"
            lock.write_text("[meta]\nmanifest_commit = \"test\"\n", encoding="utf-8")

            env = os.environ.copy()
            env["PATH"] = f"{bin_dir}:{env['PATH']}"
            env["DEPS_FILE"] = str(manifest)
            env["LOCK_FILE"] = str(lock)

            result = subprocess.run(
                [str(script), "install", "--only", "git"],
                env=env,
                capture_output=True,
                text=True,
                check=False,
            )

            self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
            self.assertIn("git target=latest installed=git version 2.50.1", result.stdout)

    def test_downloaded_tool_install_still_requires_lock_entry(self):
        repo = pathlib.Path(__file__).resolve().parents[1]
        script = repo / "scripts" / "install-deps"

        with tempfile.TemporaryDirectory() as tmp:
            tmp_path = pathlib.Path(tmp)
            bin_dir = tmp_path / "bin"
            bin_dir.mkdir()

            nvim_cmd = bin_dir / "nvim"
            nvim_cmd.write_text("#!/usr/bin/env bash\necho 'NVIM v0.12.2'\n", encoding="utf-8")
            nvim_cmd.chmod(0o755)

            manifest = tmp_path / "deps.toml"
            manifest.write_text(
                """
[deps.nvim]
command = "nvim"
target = "v0.12.2"

[deps.nvim.mac]
installer = "official_cask"
source = "https://neovim.io/doc/install/"
""".strip(),
                encoding="utf-8",
            )

            lock = tmp_path / "deps.lock"
            lock.write_text("[meta]\nmanifest_commit = \"test\"\n", encoding="utf-8")

            env = os.environ.copy()
            env["PATH"] = f"{bin_dir}:{env['PATH']}"
            env["DEPS_FILE"] = str(manifest)
            env["LOCK_FILE"] = str(lock)

            result = subprocess.run(
                [str(script), "install", "--only", "nvim"],
                env=env,
                capture_output=True,
                text=True,
                check=False,
            )

            self.assertNotEqual(result.returncode, 0)
            self.assertIn("missing from deps.lock", result.stdout + result.stderr)


if __name__ == "__main__":
    unittest.main()
