diet-guard/diet_guard/tests/test_claude_seams.py
Krzysztof kuhy Rudnicki 3d8d358abd Add Claude relay seams and entry points
Dependencies (exact pins): claude-agent-sdk 0.2.183, rtdb-relay 0.1.1,
httpx 0.28.1, hypothesis 6.170.15 (dev). The pylint hook env gets the
same pins.

Entry points, each reachable only through one module attribute:
_claude_connect.connect_sealed (RTDB + e2e unlock), _claude_sdk.run_model
(the model call), _claude_sync.pull_peers / publish (narrow pull, full
tick). SDK and rtdb_relay are imported only by these modules, never by the
gate or CLI at load time.

Test isolation, all fail closed: connect_sealed and run_model are replaced
by a function that raises; pull/publish return None; the sandbox dir and
kuhy's real Claude login path point into tmp_path; _test_guard also
protects ~/.config/diet_guard_claude and ~/.cache/diet_guard. Each of the
six redirects was removed once and its canary went red (fail closed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
2026-11-17 21:02:35 +01:00

50 lines
1.8 KiB
Python

"""Canaries: the relay/estimator seams must fail closed, not open.
A redirect that names a moved or renamed target redirects nothing and the
suite stays green while it reaches the live database or kuhy's Claude quota
(``fail-open-test-seams``). Each test here is red the moment its patch is
missing from ``_claude_redirects.claude_redirects``: the entry point must
*raise* the blocker, the sync touch points must be mocks, and the on-disk
paths must live under ``tmp_path``.
"""
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
from unittest.mock import Mock
import pytest
from diet_guard import _claude_connect, _claude_sandbox, _claude_sdk, _claude_sync
from diet_guard.tests._claude_redirects import RealIoBlockedError
if TYPE_CHECKING:
from pathlib import Path
def test_rtdb_connect_is_blocked() -> None:
"""The worker's RTDB connect raises instead of dialling Firebase."""
with pytest.raises(RealIoBlockedError):
asyncio.run(_claude_connect.connect_sealed())
def test_model_call_is_blocked() -> None:
"""The model call raises instead of spending kuhy's subscription."""
with pytest.raises(RealIoBlockedError):
asyncio.run(_claude_sdk.run_model("s", [], {}, budget_s=1))
@pytest.mark.parametrize("name", ["pull_peers", "publish"])
def test_sync_touch_points_are_patched(name: str) -> None:
"""Neither the narrow pull nor the full tick can reach the remote."""
touch = getattr(_claude_sync, name)
assert isinstance(touch, Mock)
assert touch() is None
def test_sandbox_and_host_login_are_under_tmp(tmp_path: Path) -> None:
"""The sandbox dir and kuhy's real login file never name the real home."""
assert _claude_sandbox.SANDBOX_DIR.is_relative_to(tmp_path)
assert _claude_sandbox.HOST_LOGIN_FILE.is_relative_to(tmp_path)