This repository has been archived on 2026-05-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
risv3-relay/tests/test_ntfy.py

40 lines
1.3 KiB
Python
Raw Permalink Normal View History

feat: relay daemon skeleton — queue, dispatch, conversation, ntfy (#1) First-PR scope from #1. Single-process Python daemon that relays between Claude Code instances and chat-Claude (Anthropic API). Components: * relay.config — .env + config.yaml loader. Auto-generates ntfy topic on first run and persists it back to .env. * relay.state — atomic file I/O via tempfile + rename, advisory flock at state/.lock to enforce single-instance. * relay.conversation — append-only history with summarization. Triggers a summarize call when total chars exceed HISTORY_CHAR_CAP (default 400k); replaces history with the summary plus the most recent 10 turns. * relay.anthropic_client — SDK wrapper. Marks the system prompt cacheable (5-min ephemeral cache); concatenates text blocks; estimates per-call cost from the Anthropic price table with cache-write/read accounted for. * relay.queue — JSON envelope intake; oldest-by-mtime; malformed envelopes moved to queue/.rejected/. * relay.dispatch — one-input-at-a-time per session (dispatch/<session_id>/input.txt). Won't overwrite a pending dispatch; queues internally and waits for CC to delete. * relay.ntfy — best-effort POST to https://ntfy.sh/<topic>; failures logged but never block the main loop. * relay.daemon — main loop. Polls jc_input.txt (priority) then queue/. Detects [NEEDS-JC] in the first 200 chars of any response and pauses dispatch until JC writes jc_input.txt. JC override supports @session-N: prefix for direct dispatch without an API call. * relay.__main__ — CLI: relay run / relay status / relay topic. Tests: 57 unit tests pass (config, state, conversation, queue, dispatch, anthropic_client, ntfy, full daemon loop with a fake client). One real-API smoke test marked real_api, opt-in via pytest -m real_api; skips cleanly on credit-balance errors. Out of scope for this PR (deferred to follow-ups): Flask status endpoint, multi-session config in production, exponential backoff, systemd unit, cost-tracking aggregation. Closes #1. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 15:24:47 +00:00
from __future__ import annotations
from unittest.mock import MagicMock, patch
from relay.ntfy import notify, topic_url
def test_topic_url_builds_correctly() -> None:
assert topic_url("abc123") == "https://ntfy.sh/abc123"
def test_notify_returns_false_on_empty_topic() -> None:
assert notify("", "title", "msg") is False
def test_notify_posts_with_headers_and_body() -> None:
with patch("relay.ntfy.requests.post") as post:
post.return_value = MagicMock(status_code=200)
ok = notify("topic", "the title", "body text", priority="high", tags=["warning"])
assert ok is True
args, kwargs = post.call_args
assert args[0] == "https://ntfy.sh/topic"
assert kwargs["data"] == b"body text"
assert kwargs["headers"]["Title"] == "the title"
assert kwargs["headers"]["Priority"] == "high"
assert kwargs["headers"]["Tags"] == "warning"
def test_notify_returns_false_on_non_200() -> None:
with patch("relay.ntfy.requests.post") as post:
post.return_value = MagicMock(status_code=500, text="oops")
assert notify("topic", "t", "m") is False
def test_notify_returns_false_on_network_error() -> None:
import requests
with patch("relay.ntfy.requests.post", side_effect=requests.ConnectionError("down")):
assert notify("topic", "t", "m") is False