refactor(mesh_graph_recorder): truth-test the graph once in _mesh_graph_capturing

Codex review of f5628acf: returning the `and` chain handed a falsy mesh_graph back to the caller's `if`, which truth-tested it a second time; the inline guards tested it once. The helper now returns a bool built from one test of the graph and one of capture_enabled. New tests count the truth tests and fail on the previous helper.
This commit is contained in:
agessaman committed 2026-10-03 11:06:48 -07:00
1 parent f5628acf80
commit c57fae64cf
2 files changed
+31 -4

No files matched your search

+8 -3
View File
@@ -16,9 +16,14 @@ class MeshGraphRecorderMixin:
bot: Any
logger: Any
def _mesh_graph_capturing(self) -> Any:
"""Truthy when the bot has a mesh graph and it is capturing edges."""
return hasattr(self.bot, "mesh_graph") and self.bot.mesh_graph and self.bot.mesh_graph.capture_enabled
def _mesh_graph_capturing(self) -> bool:
"""True when the bot has a mesh graph and it is capturing edges.
Truth-tests the graph and capture_enabled once each, as the inline guards did.
"""
if not hasattr(self.bot, "mesh_graph") or not self.bot.mesh_graph:
return False
return bool(self.bot.mesh_graph.capture_enabled)
def _update_mesh_graph(self, path_nodes: list[str], packet_info: dict[str, Any]) -> None:
"""Update mesh graph with edges from a message path.
+23 -1
View File
@@ -3,7 +3,7 @@
import asyncio
import configparser
import time
from unittest.mock import AsyncMock, Mock, patch
from unittest.mock import AsyncMock, MagicMock, Mock, patch
import pytest
@@ -2973,3 +2973,25 @@ class TestNewContactCapacityLogs:
rm.manage_contact_list.assert_awaited_once_with(auto_cleanup=True)
else:
rm.manage_contact_list.assert_not_called()
class TestMeshGraphCapturing:
"""_mesh_graph_capturing truth-tests the graph and capture_enabled once each."""
def test_falsy_graph_is_tested_once(self, handler, bot):
graph = MagicMock()
graph.__bool__ = Mock(side_effect=[False, RuntimeError("tested twice")])
bot.mesh_graph = graph
assert handler._mesh_graph_capturing() is False
assert graph.__bool__.call_count == 1
def test_capture_enabled_is_tested_once(self, handler, bot):
flag = MagicMock()
flag.__bool__ = Mock(side_effect=[True, RuntimeError("tested twice")])
bot.mesh_graph = Mock(capture_enabled=flag)
assert handler._mesh_graph_capturing() is True
assert flag.__bool__.call_count == 1
def test_no_graph_attribute(self, handler, bot):
del bot.mesh_graph
assert handler._mesh_graph_capturing() is False