Files
familiar/tests/test_contacts_fittings.py
LatteandClaude Opus 4.8 0dfd6d2350
CI / lint (pull_request) Successful in 51s
CI / test (pull_request) Successful in 58s
CI / publish (pull_request) Has been skipped
CI / lint (push) Successful in 50s
CI / test (push) Successful in 57s
CI / publish (push) Successful in 51s
feat(writes): enable write actions by default (opt-out)
Invert the global write toggle so writes are ENABLED by default and the
user opts OUT, while keeping all other safety intact (per-call confirm=true
dry-run preview and the on-disk audit log are unchanged).

- writes.py: use a `writes_disabled` flag file instead of `writes_enabled`;
  `writes_enabled()` returns True unless the flag exists (env var still
  honored either way); `set_writes(False)` creates the flag, `set_writes(True)`
  removes it. Update user-facing notes.
- tests: default is now ENABLED; adjust toggle/disabled tests (set disabled
  explicitly via set_writes(False) or FAMILIAR_ENABLE_WRITES=0).
- README.md / CLAUDE.md: document writes are on by default, opt out via
  `familiar writes disable`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-27 20:39:25 +02:00

98 lines
3.1 KiB
Python

"""Contacts and fittings: reads + guarded writes."""
import asyncio
import httpx
import pytest
from conftest import route
from familiar import auth, contacts, fittings, writes
from familiar.server import mcp
CHARS = [{"character_id": 99, "character_name": "Alpha", "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)
monkeypatch.setattr(auth, "resolve_character", lambda i: 99)
return CHARS
@pytest.fixture
def writes_on(tmp_path, monkeypatch):
monkeypatch.setenv("FAMILIAR_DATA_DIR", str(tmp_path))
monkeypatch.setenv("FAMILIAR_ENABLE_WRITES", "true")
def test_get_contacts(mock_esi, fake_auth):
mock_esi(lambda r: route(r, {"/contacts/": [{"contact_id": 1, "standing": 5.0}]}))
res = asyncio.run(contacts.get_contacts())
assert res["characters"][0]["count"] == 1
def test_alliance_contacts(mock_esi, fake_auth):
fixtures = {
"/characters/99/": {"corporation_id": 2000},
"/corporations/2000/": {"alliance_id": 3000},
"/alliances/": [{"contact_id": 7}],
}
mock_esi(lambda r: route(r, fixtures))
res = asyncio.run(contacts.get_alliance_contacts())
assert res["characters"][0]["alliance_id"] == 3000
assert res["characters"][0]["count"] == 1
def test_add_contacts_dry_run(mock_esi, fake_auth):
posted = []
mock_esi(lambda r: (posted.append(r), httpx.Response(200, json=[1]))[1])
res = asyncio.run(contacts.add_contacts(99, [1, 2], standing=5.0))
assert res["dry_run"] is True
assert not posted
def test_remove_contacts_executes(mock_esi, fake_auth, writes_on):
mock_esi(lambda r: httpx.Response(204, content=b""))
res = asyncio.run(contacts.remove_contacts(99, [1], confirm=True))
assert res["executed"] is True
def test_get_fittings(mock_esi, fake_auth):
mock_esi(lambda r: route(r, {"/fittings/": [{"fitting_id": 1, "name": "PvP Rifter"}]}))
res = asyncio.run(fittings.get_fittings())
assert res["characters"][0]["count"] == 1
def test_create_fitting_disabled_raises(mock_esi, fake_auth, tmp_path, monkeypatch):
monkeypatch.setenv("FAMILIAR_DATA_DIR", str(tmp_path))
monkeypatch.setenv("FAMILIAR_ENABLE_WRITES", "0")
mock_esi(lambda r: httpx.Response(201, json={"fitting_id": 5}))
with pytest.raises(writes.WriteDisabled):
asyncio.run(fittings.create_fitting(99, {"name": "x"}, confirm=True))
def test_create_fitting_executes(mock_esi, fake_auth, writes_on):
mock_esi(lambda r: httpx.Response(201, json={"fitting_id": 5}))
res = asyncio.run(fittings.create_fitting(99, {"name": "x", "ship_type_id": 587}, confirm=True))
assert res["executed"] is True
assert res["result"]["fitting_id"] == 5
def test_contacts_fittings_registered():
tools = {t.name for t in asyncio.run(mcp.list_tools())}
assert {
"get_contacts",
"get_corp_contacts",
"get_alliance_contacts",
"add_contacts",
"remove_contacts",
"get_fittings",
"create_fitting",
"delete_fitting",
} <= tools