Add read-only killmail tools: recent character and corp killmails (refs enriched with system/victim/attacker detail) plus zKillboard PvP stats via a tiny injectable zkill client. New charutil.character_targets helper for token-less per-character iteration. 85 tests, coverage 93%. Closes #30. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
97 lines
2.9 KiB
Python
97 lines
2.9 KiB
Python
"""Killmail tools (ESI char + corp) and zKillboard stats."""
|
|
|
|
import asyncio
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from conftest import route
|
|
from familiar import auth, killmails, zkill
|
|
from familiar.server import mcp
|
|
|
|
CHARS = [
|
|
{"character_id": 99, "character_name": "Alpha", "scopes": []},
|
|
{"character_id": 100, "character_name": "Beta", "scopes": []},
|
|
]
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_auth(monkeypatch):
|
|
monkeypatch.setattr(auth, "list_characters", lambda: list(CHARS))
|
|
|
|
async def get_auth(cid, **kw):
|
|
return f"token-{cid}", cid
|
|
|
|
monkeypatch.setattr(auth, "get_auth", get_auth)
|
|
|
|
def resolve(identifier):
|
|
for c in CHARS:
|
|
if (
|
|
str(c["character_id"]) == str(identifier)
|
|
or (c["character_name"] or "").lower() == str(identifier).lower()
|
|
):
|
|
return c["character_id"]
|
|
raise auth.NotAuthenticated("no such character")
|
|
|
|
monkeypatch.setattr(auth, "resolve_character", resolve)
|
|
return CHARS
|
|
|
|
|
|
def test_get_killmails_enriches(mock_esi, fake_auth):
|
|
detail = {
|
|
"killmail_id": 1,
|
|
"killmail_time": "2026-06-01T00:00:00Z",
|
|
"solar_system_id": 30000142,
|
|
"victim": {"ship_type_id": 670, "character_id": 99},
|
|
"attackers": [{}, {}, {}],
|
|
}
|
|
fixtures = {
|
|
"/killmails/recent/": [{"killmail_id": 1, "killmail_hash": "abc"}],
|
|
"/killmails/": detail,
|
|
}
|
|
mock_esi(lambda r: route(r, fixtures))
|
|
res = asyncio.run(killmails.get_killmails(character="Alpha", limit=5))
|
|
entry = res["characters"][0]
|
|
assert entry["count"] == 1
|
|
assert entry["recent"][0]["attacker_count"] == 3
|
|
assert entry["recent"][0]["solar_system_id"] == 30000142
|
|
|
|
|
|
def test_get_corp_killmails(mock_esi, fake_auth):
|
|
detail = {"killmail_id": 9, "victim": {"ship_type_id": 1}, "attackers": [{}]}
|
|
fixtures = {
|
|
"/killmails/recent/": [{"killmail_id": 9, "killmail_hash": "h"}],
|
|
"/killmails/": detail,
|
|
"/characters/": {"corporation_id": 2000},
|
|
}
|
|
mock_esi(lambda r: route(r, fixtures))
|
|
res = asyncio.run(killmails.get_corp_killmails(character=99))
|
|
assert res["characters"][0]["corporation_id"] == 2000
|
|
assert res["characters"][0]["count"] == 1
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_zkill():
|
|
def _make(payload):
|
|
zkill.set_client(
|
|
zkill.ZkillClient(
|
|
transport=httpx.MockTransport(lambda r: httpx.Response(200, json=payload))
|
|
)
|
|
)
|
|
|
|
yield _make
|
|
zkill.set_client(None)
|
|
|
|
|
|
def test_zkill_stats(fake_auth, mock_zkill):
|
|
mock_zkill({"shipsDestroyed": 100, "shipsLost": 5, "dangerRatio": 95})
|
|
res = asyncio.run(killmails.get_zkill_stats())
|
|
assert len(res["characters"]) == 2
|
|
assert res["characters"][0]["ships_destroyed"] == 100
|
|
assert res["characters"][0]["danger_ratio"] == 95
|
|
|
|
|
|
def test_killmail_tools_registered():
|
|
tools = {t.name for t in asyncio.run(mcp.list_tools())}
|
|
assert {"get_killmails", "get_corp_killmails", "get_zkill_stats"} <= tools
|