| 1 | import sys |
| 2 | import threading |
| 3 | from pathlib import Path |
| 4 | |
| 5 | import pytest |
| 6 | from flask import Flask |
| 7 | |
| 8 | PROJECT_ROOT = Path(__file__).resolve().parents[1] |
| 9 | if str(PROJECT_ROOT) not in sys.path: |
| 10 | sys.path.insert(0, str(PROJECT_ROOT)) |
| 11 | |
| 12 | from agent import AgentContext |
| 13 | from initialize import initialize_agent |
| 14 | from api.poll import Poll |
| 15 | |
| 16 | |
| 17 | @pytest.mark.asyncio |
| 18 | async def test_snapshot_builder_matches_poll_output_for_null_context(): |
| 19 | app = Flask("snapshot-parity-test") |
| 20 | app.secret_key = "test-secret" |
| 21 | lock = threading.RLock() |
| 22 | |
| 23 | poll = Poll(app, lock) |
| 24 | poll_payload = await poll.process( |
| 25 | { |
| 26 | "context": None, |
| 27 | "log_from": 0, |
| 28 | "notifications_from": 0, |
| 29 | "timezone": "UTC", |
| 30 | }, |
| 31 | None, # Poll.process does not access the flask Request object. |
| 32 | ) |
| 33 | |
| 34 | from helpers import state_snapshot as snapshot |
| 35 | |
| 36 | builder_payload = await snapshot.build_snapshot( |
| 37 | context=None, |
| 38 | log_from=0, |
| 39 | notifications_from=0, |
| 40 | timezone="UTC", |
| 41 | ) |
| 42 | |
| 43 | assert builder_payload == poll_payload |
| 44 | |
| 45 | |
| 46 | @pytest.mark.asyncio |
| 47 | async def test_snapshot_builder_active_context_includes_incremental_logs(): |
| 48 | ctxid = "ctx-snapshot-parity" |
| 49 | ctx = AgentContext(config=initialize_agent(), id=ctxid, set_current=False) |
| 50 | try: |
| 51 | ctx.log.log(type="user", heading="hi", content="hello") |
| 52 | first = await Poll(Flask("parity-active"), threading.RLock()).process( |
| 53 | { |
| 54 | "context": ctxid, |
| 55 | "log_from": 0, |
| 56 | "notifications_from": 0, |
| 57 | "timezone": "UTC", |
| 58 | }, |
| 59 | None, |
| 60 | ) |
| 61 | assert first["context"] == ctxid |
| 62 | assert first["logs"] |
| 63 | assert first["log_version"] == len(ctx.log.updates) |
| 64 | |
| 65 | from helpers import state_snapshot as snapshot |
| 66 | |
| 67 | second = await snapshot.build_snapshot( |
| 68 | context=ctxid, |
| 69 | log_from=first["log_version"], |
| 70 | notifications_from=0, |
| 71 | timezone="UTC", |
| 72 | ) |
| 73 | assert second["context"] == ctxid |
| 74 | assert second["logs"] == [] |
| 75 | assert second["log_version"] == first["log_version"] |
| 76 | finally: |
| 77 | AgentContext.remove(ctxid) |
| 78 | |
| 79 | |
| 80 | @pytest.mark.asyncio |
| 81 | async def test_snapshot_prunes_saved_context_missing_from_chat_files(monkeypatch): |
| 82 | from helpers import persist_chat |
| 83 | from helpers import state_snapshot as snapshot |
| 84 | |
| 85 | missing_id = "ctx-saved-missing-chat-file" |
| 86 | unsaved_id = "ctx-unsaved-chat-file" |
| 87 | missing = AgentContext(config=initialize_agent(), id=missing_id, set_current=False) |
| 88 | unsaved = AgentContext(config=initialize_agent(), id=unsaved_id, set_current=False) |
| 89 | persist_chat.mark_chat_saved(missing) |
| 90 | monkeypatch.setattr(persist_chat, "saved_chat_ids", lambda: set()) |
| 91 | |
| 92 | try: |
| 93 | payload = await snapshot.build_snapshot( |
| 94 | context=missing_id, |
| 95 | log_from=0, |
| 96 | notifications_from=0, |
| 97 | timezone="UTC", |
| 98 | ) |
| 99 | |
| 100 | context_ids = {ctx["id"] for ctx in payload["contexts"]} |
| 101 | assert payload["deselect_chat"] is True |
| 102 | assert payload["context"] == "" |
| 103 | assert missing_id not in context_ids |
| 104 | assert unsaved_id in context_ids |
| 105 | assert AgentContext.get(missing_id) is None |
| 106 | finally: |
| 107 | AgentContext.remove(missing_id) |
| 108 | AgentContext.remove(unsaved_id) |