68 lines
2.0 KiB
Python
68 lines
2.0 KiB
Python
"""Forge client: config-driven, protocol-shaped, adapters for Gitea/Forgejo/none."""
|
|
|
|
import json
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from rune.forge_client import (
|
|
ForgeClient,
|
|
GiteaForgejoAdapter,
|
|
NullAdapter,
|
|
create_forge_client,
|
|
)
|
|
|
|
|
|
def test_protocol_is_structural():
|
|
assert issubclass(GiteaForgejoAdapter, ForgeClient)
|
|
assert issubclass(NullAdapter, ForgeClient)
|
|
|
|
|
|
def test_null_adapter_is_noop():
|
|
adapter = NullAdapter()
|
|
r = adapter.create_issue("title", "body")
|
|
assert r["created"] is False
|
|
r = adapter.add_comment(1, "comment")
|
|
assert r["created"] is False
|
|
assert adapter.get_issue_state(1) == {"state": "unknown"}
|
|
assert adapter.list_open_issues() == []
|
|
|
|
|
|
def test_create_forge_client_unconfigured_returns_null():
|
|
client = create_forge_client(None)
|
|
assert isinstance(client, NullAdapter)
|
|
|
|
|
|
def test_create_forge_client_configured_returns_adapter(monkeypatch):
|
|
monkeypatch.setenv("FORGE_URL", "https://forge.example.com")
|
|
monkeypatch.setenv("FORGE_TOKEN", "test_token")
|
|
client = create_forge_client({})
|
|
assert isinstance(client, GiteaForgejoAdapter)
|
|
assert client._base_url == "https://forge.example.com/api/v1/repos/owner/repo"
|
|
|
|
|
|
def test_gitea_adapter_create_issue_sends_post(monkeypatch):
|
|
adapter = GiteaForgejoAdapter("https://forge.example.com", "tok", owner="o", repo="r")
|
|
captured = {}
|
|
|
|
class FakeResponse:
|
|
def json(self):
|
|
return {"number": 42, "state": "open"}
|
|
|
|
def raise_for_status(self):
|
|
pass
|
|
|
|
def fake_post(url, **kwargs):
|
|
captured["url"] = url
|
|
captured["headers"] = kwargs.get("headers", {})
|
|
captured["json_data"] = kwargs.get("json", {})
|
|
return FakeResponse()
|
|
|
|
with patch("rune.forge_client.httpx.post", side_effect=fake_post):
|
|
result = adapter.create_issue("Test", "Body")
|
|
|
|
assert result["number"] == 42
|
|
assert "issues" in captured["url"]
|
|
assert captured["json_data"]["title"] == "Test"
|
|
assert captured["headers"]["Authorization"] == "token tok"
|