from __future__ import annotations import os import subprocess from pathlib import Path import pytest from airouter import update @pytest.fixture(autouse=True) def isolate_home(tmp_path: Path, monkeypatch): home = tmp_path / "home" home.mkdir() monkeypatch.setenv("HOME", str(home)) monkeypatch.setenv("XDG_CONFIG_HOME", str(home / ".config")) monkeypatch.setattr(update, "UPDATE_CACHE", tmp_path / "update-cache.json") monkeypatch.setattr(update, "__version__", "0.3.0") def git(repo: Path, *args: str) -> str: env = os.environ.copy() env.update( { "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "GIT_TERMINAL_PROMPT": "0", } ) result = subprocess.run( ["git", *args], cwd=repo, env=env, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, check=True, ) return result.stdout.strip() def init_repo(path: Path) -> None: path.mkdir(parents=True) git(path, "init") git(path, "config", "user.email", "tests@example.invalid") git(path, "config", "user.name", "AiRouter Tests") (path / "tracked.txt").write_text("one\n", encoding="utf-8") git(path, "add", "tracked.txt") git(path, "commit", "-m", "initial") def test_parse_version_is_numeric_and_strict(): assert update.parse_version("v1.2.3") == (1, 2, 3) assert update.parse_version("10.20.30") == (10, 20, 30) assert update.parse_version("v1.2.3-rc1") is None assert update.parse_version("1.2") is None assert update.parse_version("v1.x.3") is None def test_check_for_updates_parses_tags_and_uses_cache(tmp_path: Path, monkeypatch): calls = [] def fake_run(command, **kwargs): calls.append((command, kwargs)) output = ( "aaa\trefs/tags/v0.9.0\n" "bbb\trefs/tags/v0.3.1\n" "ccc\trefs/tags/v0.3.1^{}\n" "ddd\trefs/tags/not-a-version\n" "eee\trefs/tags/v0.10.0\n" "fff\trefs/tags/v0.10.0^{}" ) return subprocess.CompletedProcess(command, 0, stdout=output, stderr="") monkeypatch.setattr(update.subprocess, "run", fake_run) first = update.check_for_updates(repo_url="https://example.invalid/org/repo.git") assert first.current == "0.3.0" assert first.latest == "v0.10.0" assert first.available is True assert first.error == "" assert first.url.endswith("/-/tags/v0.10.0") assert len(calls) == 1 assert calls[0][0][-1] == "https://example.invalid/org/repo.git" second = update.check_for_updates(repo_url="https://example.invalid/org/repo.git") assert second == first assert len(calls) == 1 update.check_for_updates( force=True, repo_url="https://example.invalid/org/repo.git" ) assert len(calls) == 2 assert update.UPDATE_CACHE.is_file() def test_update_check_timeout_is_unavailable_and_not_cached(tmp_path: Path, monkeypatch): def fake_run(command, **kwargs): raise subprocess.TimeoutExpired(command, kwargs.get("timeout")) monkeypatch.setattr(update.subprocess, "run", fake_run) info = update.check_for_updates(force=True) assert info.available is False assert info.latest == info.current assert "timed out" in info.error assert not update.UPDATE_CACHE.exists() def test_update_urls_never_include_credentials(monkeypatch): monkeypatch.setattr( update.subprocess, "run", lambda command, **kwargs: subprocess.CompletedProcess( command, 0, stdout="aaa\trefs/tags/v0.3.1\n", stderr="" ), ) info = update.check_for_updates( force=True, repo_url="https://user:password@example.invalid/org/repo.git?secret=yes", ) assert "user" not in info.url assert "password" not in info.url assert "secret" not in info.url assert "password" not in update.UPDATE_CACHE.read_text(encoding="utf-8") def test_install_update_refuses_dirty_tree_without_fetching( tmp_path: Path, monkeypatch ): repo = tmp_path / "repo" init_repo(repo) monkeypatch.setattr(update, "REPO_ROOT", repo) before = git(repo, "rev-parse", "HEAD") (repo / "untracked.txt").write_text("keep me\n", encoding="utf-8") result = update.install_update(repo_url=str(tmp_path / "missing-remote")) assert result.updated is False assert result.restart_required is False assert "clean" in result.message.lower() assert git(repo, "rev-parse", "HEAD") == before assert (repo / "untracked.txt").read_text(encoding="utf-8") == "keep me\n" def test_install_update_clean_checkout_with_no_changes(tmp_path: Path, monkeypatch): repo = tmp_path / "repo" init_repo(repo) monkeypatch.setattr(update, "REPO_ROOT", repo) result = update.install_update(repo_url=str(repo)) assert result.updated is False assert result.restart_required is False assert "up to date" in result.message.lower() def test_install_update_fast_forwards_clean_checkout(tmp_path: Path, monkeypatch): remote = tmp_path / "remote.git" remote.mkdir() git(remote, "init", "--bare") seed = tmp_path / "seed" init_repo(seed) (seed / "pyproject.toml").write_text( '[project]\nname = "airouter"\nversion = "0.3.0"\n', encoding="utf-8" ) git(seed, "add", "pyproject.toml") git(seed, "commit", "-m", "version") branch = git(seed, "branch", "--show-current") git(seed, "remote", "add", "origin", str(remote)) git(seed, "push", "-u", "origin", branch) work = tmp_path / "work" git(tmp_path, "clone", str(remote), str(work)) git(work, "config", "user.email", "tests@example.invalid") git(work, "config", "user.name", "AiRouter Tests") before = git(work, "rev-parse", "HEAD") (seed / "pyproject.toml").write_text( '[project]\nname = "airouter"\nversion = "0.3.1"\n', encoding="utf-8" ) git(seed, "add", "pyproject.toml") git(seed, "commit", "-m", "new version") git(seed, "push", "origin", branch) monkeypatch.setattr(update, "REPO_ROOT", work) result = update.install_update(repo_url=str(remote)) assert result.updated is True assert result.version == "0.3.1" assert result.restart_required is True assert git(work, "rev-parse", "HEAD") != before assert 'version = "0.3.1"' in (work / "pyproject.toml").read_text(encoding="utf-8")