import asyncio class _Result: def __init__(self, row=None): self._row = row def fetchone(self): return self._row class _Session: def __init__(self, exists=False): self.exists = exists self.calls = [] self.commits = 0 def execute(self, statement, params=None): self.calls.append((str(statement), params or {})) return _Result((1,) if self.exists else None) def commit(self): self.commits += 1 def rollback(self): pass def close(self): pass def test_nemotron_direct_response_is_not_persisted(monkeypatch): import services.ai_orchestrator as module session = _Session() monkeypatch.setattr(module, "get_session", lambda: session) plan = { "session_id": "tg-1", "dispatch_to": "direct_response", "action_plan": [{"action": "reply_simple", "params": {"message": "hello"}}], } asyncio.run(module.AIOrchestrator()._save_action_plan(plan)) assert session.calls == [] assert session.commits == 0 def test_nemotron_duplicate_executable_plan_is_skipped(monkeypatch): import services.ai_orchestrator as module session = _Session(exists=True) monkeypatch.setattr(module, "get_session", lambda: session) plan = { "session_id": "tg-1", "plan_type": "price_adjust", "sku": "SKU-1", "action_plan": [{"action": "flag_for_human_review"}], } asyncio.run(module.AIOrchestrator()._save_action_plan(plan)) assert len(session.calls) == 1 assert "SELECT id" in session.calls[0][0] assert session.commits == 0 def test_code_review_active_file_dedupe_uses_file_path_marker(): from services.action_plan_dedupe import active_code_review_action_exists session = _Session(exists=True) assert active_code_review_action_exists(session, "services/config.py") is True params = session.calls[0][1] assert params["desc_prefix"] == "Code Review 修復:services/config.py|%" assert '"file": "services/config.py"' in params["metadata_marker"] def test_openclaw_metadata_contains_stable_fingerprint(): import json from services.action_plan_dedupe import openclaw_action_metadata first = json.loads(openclaw_action_metadata(10, "**立即調查**:昨日業績為零")) second = json.loads(openclaw_action_metadata(10, "立即調查 : 昨日業績為零")) assert first["created_by"] == "openclaw" assert first["dedupe_fingerprint"] == second["dedupe_fingerprint"]