Autonomous AI sales agent powered by Claude Code or Codex. Finds prospects, writes cold emails, sends campaigns, handles replies, books meetings. No API keys needed — runs on your Claude or Codex subscriptions.
Add tests for the optional Precleared check
Gabe AO8031c79c9cd2
diff --git a/tests/test_precleared.py b/tests/test_precleared.pynew file mode 100644index 0000000..1080977--- /dev/null+++ b/tests/test_precleared.py@@ -0,0 +1,284 @@+"""Tests for the optional Precleared check in the native send path: off by+default, one check per address, blocked verdicts fail the outbox item, an+unavailable check holds cold email without failing it, replies are never+checked. No network: every Precleared call goes to an httpx.MockTransport."""++import json+import os+import tempfile+from types import SimpleNamespace++import aiosqlite+import httpx+import pytest+import pytest_asyncio+from pydantic import ValidationError++from harvey.config import PreclearedConfig+from harvey.integrations.precleared import SETTINGS_PREFIX, PreclearedClient+from harvey.state import StateManager+from tests.test_outbox_native import (+ Cfg,+ Env,+ FakeProvider,+ _now_iso,+ make_sender,+ seed_prospect,+)+++@pytest_asyncio.fixture+async def state():+ with tempfile.TemporaryDirectory() as tmpdir:+ sm = StateManager(os.path.join(tmpdir, "test.db"))+ await sm.init_db()+ yield sm+++OK_BODY = {+ "verdict": "ok_to_email",+ "confidence": "high",+ "reasons": [{+ "rule_key": "gb_corporate_ok",+ "text": "Corporate subscribers need no prior consent.",+ "source_title": "PECR reg. 22",+ }],+ "next_step": "Send, with a footer that says who you are and how to opt out.",+ "receipt": {"id": "cc_rcpt_OK0000000001"},+}++CONSENT_BODY = {+ "verdict": "consent_needed",+ "confidence": "high",+ "reasons": [{+ "rule_key": "gb_individual_consent",+ "text": "Sole traders count as individuals, so consent is needed.",+ "source_title": "PECR reg. 22(2)",+ }],+ "next_step": "Do not send cold email. Get consent first.",+ "receipt": {"id": "cc_rcpt_NO0000000001"},+}+++def mock_precleared(calls, body=None, status=200):+ """A Precleared stand-in that records each request."""+ def handler(request: httpx.Request) -> httpx.Response:+ calls.append({"json": json.loads(request.content), "headers": dict(request.headers)})+ if status != 200:+ return httpx.Response(status, json={"error": "daily_limit", "message": "Limit reached."})+ return httpx.Response(200, json=body)+ return httpx.MockTransport(handler)+++def with_check(sender, transport, api_key=""):+ sender.precleared = PreclearedClient(sender_country="GB", api_key=api_key, transport=transport)+ return sender+++async def queue(state, pid, step=1, kind="sequence", to="[email protected]"):+ return await state.add_outbox_item(+ prospect_id=pid, to_email=to, subject="hello",+ body="Fine body. Question?", send_at=_now_iso(), status="approved",+ campaign_id="c1" if kind == "sequence" else "", step=step, kind=kind,+ )+++async def logged_receipts(state):+ async with aiosqlite.connect(state.db_path) as db:+ async with db.execute(+ "SELECT details_json FROM actions WHERE action_type = 'email_sent'"+ ) as cursor:+ rows = await cursor.fetchall()+ return [json.loads(r[0]).get("precleared_receipt", "") for r in rows]+++# ── off by default ──+++def test_check_is_off_unless_enabled():+ assert PreclearedClient.from_config(Cfg(), Env()) is None+ off = SimpleNamespace(compliance=SimpleNamespace(precleared=PreclearedConfig()))+ assert PreclearedClient.from_config(off, Env()) is None+++async def test_disabled_sender_sends_without_a_check(state):+ pid = await seed_prospect(state)+ await queue(state, pid)+ provider = FakeProvider()+ sender = make_sender(state, provider)+ assert sender.precleared is None+ await sender._drain_due()+ assert len(provider.sent) == 1+++# ── config ──+++def test_config_needs_a_sender_country_when_enabled():+ with pytest.raises(ValidationError):+ PreclearedConfig(enabled=True)+ cfg = PreclearedConfig(enabled=True, sender_country="gb")+ assert cfg.sender_country == "GB"+ assert cfg.address_source == "unknown"+ with pytest.raises(ValidationError):+ PreclearedConfig(enabled=True, sender_country="US", address_source="guessed")+++def test_from_config_reads_settings_key_and_pitch():+ config = SimpleNamespace(+ compliance=SimpleNamespace(precleared=PreclearedConfig(+ enabled=True, sender_country="US", address_source="published",+ )),+ product=SimpleNamespace(description="Route planning for delivery firms"),+ )+ client = PreclearedClient.from_config(config, SimpleNamespace(precleared_api_key="pc_live_test"))+ assert client.sender_country == "US"+ assert client.address_source == "published"+ assert client.api_key == "pc_live_test"+ assert client.pitch == "Route planning for delivery firms"+++# ── verdicts ──+++async def test_ok_verdict_sends_and_logs_the_receipt(state):+ pid = await seed_prospect(state)+ await queue(state, pid)+ calls = []+ provider = FakeProvider()+ sender = with_check(make_sender(state, provider), mock_precleared(calls, OK_BODY))++ await sender._drain_due()++ assert len(provider.sent) == 1+ assert calls[0]["json"] == {+ "recipient_email": "[email protected]",+ "sender_country": "GB",+ "address_source": "unknown",+ "existing_customer": False,+ }+ assert "authorization" not in calls[0]["headers"]+ assert await logged_receipts(state) == ["cc_rcpt_OK0000000001"]+++async def test_api_key_is_sent_as_bearer(state):+ pid = await seed_prospect(state)+ await queue(state, pid)+ calls = []+ sender = with_check(make_sender(state, FakeProvider()),+ mock_precleared(calls, OK_BODY), api_key="pc_live_test")+ await sender._drain_due()+ assert calls[0]["headers"]["authorization"] == "Bearer pc_live_test"+++async def test_consent_needed_fails_the_item_with_the_reason(state):+ pid = await seed_prospect(state)+ await queue(state, pid)+ calls = []+ provider = FakeProvider()+ sender = with_check(make_sender(state, provider), mock_precleared(calls, CONSENT_BODY))++ await sender._drain_due()++ assert provider.sent == []+ failed = await state.get_outbox(status="failed")+ assert len(failed) == 1+ assert failed[0]["error"].startswith("precleared: consent_needed")+ assert "PECR reg. 22(2)" in failed[0]["error"]+ assert "Get consent first" in failed[0]["error"]+++async def test_one_check_per_address(state):+ pid = await seed_prospect(state)+ calls = []+ provider = FakeProvider()+ sender = with_check(make_sender(state, provider), mock_precleared(calls, OK_BODY))++ await queue(state, pid, step=1)+ await sender._drain_due()+ await queue(state, pid, step=2)+ await sender._drain_due()++ assert len(provider.sent) == 2+ assert len(calls) == 1+ assert await state.get_setting(SETTINGS_PREFIX + "[email protected]")+++async def test_blocked_verdict_is_remembered_for_later_steps(state):+ pid = await seed_prospect(state)+ calls = []+ provider = FakeProvider()+ sender = with_check(make_sender(state, provider), mock_precleared(calls, CONSENT_BODY))++ await queue(state, pid, step=1)+ await sender._drain_due()+ await queue(state, pid, step=2)+ await sender._drain_due()++ assert provider.sent == []+ assert len(await state.get_outbox(status="failed")) == 2+ assert len(calls) == 1+++# ── check unavailable ──+++async def test_daily_limit_holds_cold_email_but_replies_still_go(state):+ pid = await seed_prospect(state)+ other = await seed_prospect(state, email="[email protected]")+ await queue(state, pid)+ await queue(state, other, to="[email protected]")+ reply_pid = await seed_prospect(state, email="[email protected]", status="replied")+ await queue(state, reply_pid, kind="reply", to="[email protected]")+ calls = []+ provider = FakeProvider()+ sender = with_check(make_sender(state, provider), mock_precleared(calls, status=429))++ await sender._drain_due()++ # One failed call pauses the check for the rest of the cycle.+ assert len(calls) == 1+ assert [m["to"] for m in provider.sent] == ["[email protected]"]+ assert await state.get_outbox(status="failed") == []+ waiting = await state.get_outbox(status="approved")+ assert sorted(i["to_email"] for i in waiting) == ["[email protected]", "[email protected]"]+ # Nothing cached, so the next cycle asks again.+ assert await state.get_setting(SETTINGS_PREFIX + "[email protected]") == ""+++async def test_network_error_holds_the_email(state):+ pid = await seed_prospect(state)+ await queue(state, pid)++ def boom(request):+ raise httpx.ConnectError("no route to host")++ provider = FakeProvider()+ sender = with_check(make_sender(state, provider), httpx.MockTransport(boom))+ await sender._drain_due()++ assert provider.sent == []+ assert await state.get_outbox(status="failed") == []+ assert len(await state.get_outbox(status="approved")) == 1+++async def test_replies_are_never_checked(state):+ pid = await seed_prospect(state, status="replied")+ await queue(state, pid, kind="reply")+ calls = []+ provider = FakeProvider()+ sender = with_check(make_sender(state, provider), mock_precleared(calls, CONSENT_BODY))++ await sender._drain_due()++ assert calls == []+ assert len(provider.sent) == 1