110 lines
4.7 KiB
Python
110 lines
4.7 KiB
Python
"""Cron transport success must mean the message survives a chat reopen."""
|
|
import pytest
|
|
|
|
from gateway.config import PlatformConfig
|
|
from hermes_state import SessionDB
|
|
from pheby import hermes_bridge as hb, protocol as proto
|
|
from pheby.adapter import PhebyAdapter
|
|
from pheby.server import PhebyServer
|
|
|
|
|
|
@pytest.fixture
|
|
def delivery_stack(tmp_path, monkeypatch):
|
|
db = SessionDB(tmp_path / "state.db")
|
|
monkeypatch.setattr(hb, "_session_db", lambda: db)
|
|
monkeypatch.setattr(hb, "_session_store", lambda: None)
|
|
adapter = PhebyAdapter(PlatformConfig(enabled=True, extra={"secret": "test-secret"}))
|
|
server = PhebyServer(adapter._pcfg, adapter=adapter)
|
|
adapter._server = server
|
|
hb.set_adapter(adapter)
|
|
hb.set_server(server)
|
|
yield adapter, server, db
|
|
hb.clear_services(adapter)
|
|
hb.set_server(None)
|
|
hb._ACTIVE_RUNS.clear()
|
|
db.close()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_delivery_survives_reopen_without_touching_active_turn(delivery_stack):
|
|
adapter, server, db = delivery_stack
|
|
cid = "home"
|
|
key = hb._session_key_for(cid)
|
|
db.create_session("active-session", "pheby", session_key=key)
|
|
db.append_message("active-session", "user", "My ongoing question")
|
|
adapter._drafts[cid] = {"message_id": "draft-active", "text": "Still thinking"}
|
|
hb._ACTIVE_RUNS[cid] = {"run_id": "active-run", "started": 0}
|
|
|
|
# No WebSocket clients connected: receiving a push is not durable delivery.
|
|
result = await adapter.send(cid, "Inbox filed successfully", metadata={"job_id": "daily-inbox", "notify": True})
|
|
assert result.success
|
|
history, found, has_more = await hb.conversation_history(cid, 20)
|
|
assert [m["text"] for m in history] == ["My ongoing question", "Inbox filed successfully"]
|
|
assert history[-1]["message_id"] == result.message_id
|
|
assert found and not has_more
|
|
assert hb.active_run_id(cid) == "active-run"
|
|
assert adapter._drafts[cid]["message_id"] == "draft-active"
|
|
assert [m["content"] for m in db.get_messages_as_conversation("active-session")] == ["My ongoing question"]
|
|
assert await server.router.get_name(cid) is not None
|
|
conversations = await hb.list_conversations(server)
|
|
assert conversations[0]["conversation_id"] == cid
|
|
assert conversations[0]["last_active"] == history[-1]["ts"]
|
|
|
|
# Paging uses real global message row IDs across chat and delivery history.
|
|
previous, _, _ = await hb.conversation_history(cid, 20, before_id=int(result.message_id[1:]))
|
|
assert [m["text"] for m in previous] == ["My ongoing question"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_delivery_does_not_claim_success_when_save_fails(delivery_stack, monkeypatch):
|
|
adapter, server, db = delivery_stack
|
|
broadcasts = []
|
|
|
|
async def broadcast(event):
|
|
broadcasts.append(event)
|
|
|
|
def fail(*args, **kwargs):
|
|
raise OSError("disk unavailable")
|
|
|
|
monkeypatch.setattr(server, "broadcast", broadcast)
|
|
monkeypatch.setattr(db, "append_message", fail)
|
|
result = await adapter.send("home", "A report", metadata={"job_id": "daily"})
|
|
assert not result.success
|
|
assert broadcasts == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cron_delivery_is_readable_over_ws_after_reconnect(delivery_stack):
|
|
import aiohttp
|
|
|
|
adapter, server, db = delivery_stack
|
|
server.config.port = 0
|
|
assert await server.start()
|
|
try:
|
|
port = server._site._server.sockets[0].getsockname()[1]
|
|
async with aiohttp.ClientSession() as http:
|
|
async def connect():
|
|
ws = await http.ws_connect(f"http://127.0.0.1:{port}/ws")
|
|
await ws.send_json({"type": "hello", "secret": "test-secret", "protocol_version": proto.PROTOCOL_VERSION})
|
|
assert (await ws.receive_json(timeout=5))["type"] == proto.S_READY
|
|
return ws
|
|
|
|
ws = await connect()
|
|
result = await adapter.send("home", "Persistent scheduled report", metadata={"job_id": "daily"})
|
|
live = await ws.receive_json(timeout=5)
|
|
assert live["message_id"] == result.message_id
|
|
await ws.close()
|
|
ws = await connect()
|
|
await ws.send_json({"type": "conversation.open", "conversation_id": "home", "request_id": "reopen"})
|
|
history = await ws.receive_json(timeout=5)
|
|
assert history["type"] == proto.S_CONVERSATION_HISTORY
|
|
assert history["messages"] == [{"message_id": result.message_id, "role": "assistant", "text": live["text"], "ts": live["ts"]}]
|
|
await ws.close()
|
|
|
|
# Another chat cannot see this delivery, and deletion clears its archive.
|
|
assert (await hb.conversation_history("other-chat", 20))[0] == []
|
|
assert await hb.delete_conversation("home")
|
|
assert (await hb.conversation_history("home", 20))[0] == []
|
|
finally:
|
|
await server.stop()
|