"""Tests for Gitea flake.lock automation.""" from __future__ import annotations from python.gitea import PullRequest from python.gitea_flake_lock import ( PR_CHECK_WORKFLOWS, PR_LABELS, dispatch_pull_request_checks, ensure_flake_lock_pull_request, find_flake_lock_pull_request, ) def _pull_request(number=1, head_branch="automation/update-flake-lock"): return PullRequest( number=number, title="Update flake.lock", html_url=f"https://gitea.example.test/pulls/{number}", labels=(), head_branch=head_branch, base_branch="main", ) class FakeGiteaClient: def __init__(self, pull_requests=None): self.pull_requests = pull_requests or [] self.dispatch_calls = [] self.list_calls = [] self.create_calls = [] def list_open_pull_requests(self, **kwargs): self.list_calls.append(kwargs) return self.pull_requests def create_pull_request(self, **kwargs): self.create_calls.append(kwargs) return _pull_request() def dispatch_workflow(self, **kwargs): self.dispatch_calls.append(kwargs) def test_ensure_flake_lock_pull_request_finds_by_branch(): pull_request = _pull_request() client = FakeGiteaClient([pull_request]) result = ensure_flake_lock_pull_request( client, owner="Richie", repo="dotfiles", branch="automation/update-flake-lock", base="main", ) assert result == pull_request assert client.list_calls == [ {"owner": "Richie", "repo": "dotfiles", "head": "automation/update-flake-lock"}, ] assert client.create_calls == [] def test_ensure_flake_lock_pull_request_creates_with_labels(): client = FakeGiteaClient() ensure_flake_lock_pull_request( client, owner="Richie", repo="dotfiles", branch="automation/update-flake-lock", base="main", ) assert client.create_calls == [ { "owner": "Richie", "repo": "dotfiles", "title": "Update flake.lock", "body": "Automated flake.lock update.", "head": "automation/update-flake-lock", "base": "main", "labels": PR_LABELS, }, ] def test_find_flake_lock_pull_request_finds_by_label(): pull_request = _pull_request() client = FakeGiteaClient([pull_request]) result = find_flake_lock_pull_request(client, owner="Richie", repo="dotfiles") assert result == pull_request assert client.list_calls == [ {"owner": "Richie", "repo": "dotfiles", "labels": ["flake_lock_update"]}, ] def test_dispatch_pull_request_checks_runs_each_workflow(): client = FakeGiteaClient() dispatch_pull_request_checks(client, owner="Richie", repo="dotfiles", branch="automation/update-flake-lock") assert client.dispatch_calls == [ { "owner": "Richie", "repo": "dotfiles", "workflow_id": workflow, "ref": "automation/update-flake-lock", } for workflow in PR_CHECK_WORKFLOWS ]