"""Tests for Gitea flake.lock automation.""" from __future__ import annotations from python.gitea import PullRequest from python.gitea_flake_lock import 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.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 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_without_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", }, ] def test_find_flake_lock_pull_request_finds_by_branch(): 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", "head": "automation/update-flake-lock"}, ]