diff --git a/drb-c2-core/tests/test_mqtt_handler.py b/drb-c2-core/tests/test_mqtt_handler.py index e2fc6f9..cdbd063 100644 --- a/drb-c2-core/tests/test_mqtt_handler.py +++ b/drb-c2-core/tests/test_mqtt_handler.py @@ -67,7 +67,9 @@ async def test_checkin_creates_new_node(handler): ) mock_fstore.doc_set.assert_called_once() - _, _, doc, _ = mock_fstore.doc_set.call_args[0] + # doc_set(collection, doc_id, data, merge=False) — merge is passed as a + # kwarg in mqtt_handler.py, so only 3 positional args land in call_args[0]. + _, _, doc = mock_fstore.doc_set.call_args[0] assert doc["node_id"] == "new-node" assert doc["name"] == "Pi Zero W" assert doc["status"] == "unconfigured" @@ -84,7 +86,7 @@ async def test_checkin_new_node_defaults_lat_lon(handler): await handler._handle_checkin("new-node", {}) - _, _, doc, _ = mock_fstore.doc_set.call_args[0] + _, _, doc = mock_fstore.doc_set.call_args[0] assert doc["lat"] == 0.0 assert doc["lon"] == 0.0 @@ -200,13 +202,17 @@ async def test_call_start_creates_call_doc(handler): } with patch("app.internal.mqtt_handler.fstore") as mock_fstore: - mock_fstore.doc_get = AsyncMock(return_value=node) + # _on_call_start looks the node up via doc_get_cached (cached read, + # added to cut Firestore read volume — see doc_get_cached in + # app/internal/firestore.py), not the uncached doc_get. + mock_fstore.doc_get_cached = AsyncMock(return_value=node) mock_fstore.doc_set = AsyncMock() await handler._on_call_start("node-01", payload) mock_fstore.doc_set.assert_called_once() - _, _, doc, _ = mock_fstore.doc_set.call_args[0] + # doc_set(collection, doc_id, data, merge=False) — merge is a kwarg here too. + _, _, doc = mock_fstore.doc_set.call_args[0] assert doc["call_id"] == "call-abc123" assert doc["node_id"] == "node-01" assert doc["system_id"] == "sys-001" @@ -233,12 +239,12 @@ async def test_call_start_uses_now_when_started_at_missing(handler): payload = {"call_id": "call-xyz", "tgid": 99} with patch("app.internal.mqtt_handler.fstore") as mock_fstore: - mock_fstore.doc_get = AsyncMock(return_value=node) + mock_fstore.doc_get_cached = AsyncMock(return_value=node) mock_fstore.doc_set = AsyncMock() await handler._on_call_start("node-01", payload) - _, _, doc, _ = mock_fstore.doc_set.call_args[0] + _, _, doc = mock_fstore.doc_set.call_args[0] assert doc["started_at"] is not None @@ -250,11 +256,17 @@ async def test_call_end_updates_status_and_times(handler): } with patch("app.internal.mqtt_handler.fstore") as mock_fstore: - mock_fstore.doc_update = AsyncMock() + # _on_call_end writes via doc_set(merge=True) now, not doc_update — see + # the "Fix Upload 404 warning" commit: doc_update raised "No document + # to update" when call_end raced ahead of call_start, so it was + # switched to a merging doc_set. It also reads the node via the + # cached doc_get_cached to stamp org_id. + mock_fstore.doc_get_cached = AsyncMock(return_value=None) + mock_fstore.doc_set = AsyncMock() await handler._on_call_end("node-01", payload) - updates = mock_fstore.doc_update.call_args[0][2] + updates = mock_fstore.doc_set.call_args[0][2] assert updates["status"] == "ended" assert updates["ended_at"] is not None @@ -268,11 +280,12 @@ async def test_call_end_sets_audio_url_when_present(handler): } with patch("app.internal.mqtt_handler.fstore") as mock_fstore: - mock_fstore.doc_update = AsyncMock() + mock_fstore.doc_get_cached = AsyncMock(return_value=None) + mock_fstore.doc_set = AsyncMock() await handler._on_call_end("node-01", payload) - updates = mock_fstore.doc_update.call_args[0][2] + updates = mock_fstore.doc_set.call_args[0][2] assert updates["audio_url"] == "https://storage.example.com/call.mp3" diff --git a/drb-c2-core/tests/test_node_sweeper.py b/drb-c2-core/tests/test_node_sweeper.py index caeab98..4f06cfb 100644 --- a/drb-c2-core/tests/test_node_sweeper.py +++ b/drb-c2-core/tests/test_node_sweeper.py @@ -35,8 +35,16 @@ def _node_naive(node_id, status, age_seconds): async def test_stale_online_node_marked_offline(): nodes = [_node("node-01", "online", age_seconds=120)] + # A stale node also triggers app.routers.tokens.release_token(node_id) — + # added by the PulseAudio/Discord-token work (commit 2a690ec). It's + # imported inline inside _sweep, so it must be patched at its source + # module rather than relying on the global asyncio.to_thread patch above, + # which is scoped to the node-query call and would otherwise feed + # release_token's own internal to_thread call the wrong shape of data + # (raw node dicts instead of Firestore doc snapshots with .id). with patch("asyncio.to_thread", new=AsyncMock(return_value=nodes)), \ - patch("app.internal.node_sweeper.fstore") as mock_fstore: + patch("app.internal.node_sweeper.fstore") as mock_fstore, \ + patch("app.routers.tokens.release_token", new=AsyncMock()): mock_fstore.doc_update = AsyncMock() await _sweep() @@ -50,7 +58,8 @@ async def test_stale_recording_node_marked_offline(): nodes = [_node("node-02", "recording", age_seconds=200)] with patch("asyncio.to_thread", new=AsyncMock(return_value=nodes)), \ - patch("app.internal.node_sweeper.fstore") as mock_fstore: + patch("app.internal.node_sweeper.fstore") as mock_fstore, \ + patch("app.routers.tokens.release_token", new=AsyncMock()): mock_fstore.doc_update = AsyncMock() await _sweep() @@ -106,7 +115,8 @@ async def test_tz_naive_last_seen_is_handled(): nodes = [_node_naive("node-06", "online", age_seconds=120)] with patch("asyncio.to_thread", new=AsyncMock(return_value=nodes)), \ - patch("app.internal.node_sweeper.fstore") as mock_fstore: + patch("app.internal.node_sweeper.fstore") as mock_fstore, \ + patch("app.routers.tokens.release_token", new=AsyncMock()): mock_fstore.doc_update = AsyncMock() await _sweep() @@ -141,10 +151,16 @@ async def test_only_stale_nodes_updated_in_batch(): ] with patch("asyncio.to_thread", new=AsyncMock(return_value=nodes)), \ - patch("app.internal.node_sweeper.fstore") as mock_fstore: + patch("app.internal.node_sweeper.fstore") as mock_fstore, \ + patch("app.routers.tokens.release_token", new=AsyncMock()) as mock_release: mock_fstore.doc_update = AsyncMock() await _sweep() assert mock_fstore.doc_update.call_count == 2 updated_ids = {call.args[1] for call in mock_fstore.doc_update.call_args_list} assert updated_ids == {"node-08", "node-11"} + + # Both newly-offline nodes should have their Discord token freed. + assert mock_release.call_count == 2 + released_ids = {call.args[0] for call in mock_release.call_args_list} + assert released_ids == {"node-08", "node-11"}