Files
familiar/tests/test_comms.py
T
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

78 lines
2.4 KiB
Python

"""Mail/calendar read tools and guarded write actions."""
import asyncio
import httpx
import pytest
from conftest import route
from familiar import auth, comms, 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_mail_read(mock_esi, fake_auth):
mock_esi(lambda r: route(r, {"/mail/": [{"mail_id": 1, "subject": "hi"}]}))
res = asyncio.run(comms.get_mail())
assert res["characters"][0]["count"] == 1
def test_send_mail_dry_run_does_not_post(mock_esi, fake_auth):
posted = []
mock_esi(lambda r: (posted.append(r), httpx.Response(201, json=1))[1])
res = asyncio.run(comms.send_mail(99, [{"recipient_id": 1}], "s", "b"))
assert res["dry_run"] is True
assert not posted
def test_send_mail_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=1))
with pytest.raises(writes.WriteDisabled):
asyncio.run(comms.send_mail(99, [{"recipient_id": 1}], "s", "b", confirm=True))
def test_send_mail_executes_when_enabled(mock_esi, fake_auth, writes_on):
mock_esi(lambda r: httpx.Response(201, json=987654))
res = asyncio.run(comms.send_mail(99, [{"recipient_id": 1}], "s", "b", confirm=True))
assert res["executed"] is True
assert res["result"] == 987654
def test_respond_calendar_validates_response(mock_esi, fake_auth, writes_on):
mock_esi(lambda r: httpx.Response(204, content=b""))
res = asyncio.run(comms.respond_calendar_event(99, 5, "banana", confirm=True))
assert res["action"] == "respond_calendar_event"
assert res["executed"] is True
def test_comms_tools_registered():
tools = {t.name for t in asyncio.run(mcp.list_tools())}
assert {
"get_mail",
"send_mail",
"set_mail_read",
"get_calendar",
"respond_calendar_event",
} <= tools