import os import sys import tempfile _fd, _TMP = tempfile.mkstemp(suffix=".db") os.close(_fd) os.unlink(_TMP) os.environ["AGENT_OFFICE_DB_PATH"] = _TMP sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import pytest from unittest.mock import AsyncMock from app.agents.insta import InstaAgent @pytest.fixture(autouse=True) def _init_db(): import gc gc.collect() if os.path.exists(_TMP): os.remove(_TMP) from app.db import init_db init_db() yield gc.collect() @pytest.mark.asyncio async def test_autonomous_issue_previews_eligible(monkeypatch): agent = InstaAgent() agent.state = "idle" monkeypatch.setattr("app.agents.insta.get_agent_config", lambda aid: {"custom_config": {"autonomous_issue": True, "select_threshold": 0.5, "max_per_day": 2}}) monkeypatch.setattr(agent, "transition", AsyncMock()) monkeypatch.setattr(agent, "_run_collect_and_extract", AsyncMock()) monkeypatch.setattr("app.agents.insta.service_proxy.insta_ranked", AsyncMock(return_value=[ {"id": 1, "keyword": "금리", "category": "economy", "eligible": True, "final_score": 0.8, "breakdown": {}}, {"id": 2, "keyword": "x", "category": "economy", "eligible": False, "final_score": 0.1, "breakdown": {}}, ])) preview = AsyncMock() monkeypatch.setattr(agent, "_generate_and_preview", preview) monkeypatch.setattr("app.agents.insta.create_task", lambda *a, **k: "t1") monkeypatch.setattr("app.agents.insta.update_task_status", lambda *a, **k: None) monkeypatch.setattr("app.agents.insta.add_log", lambda *a, **k: None) await agent.on_schedule() assert preview.await_count == 1 assert preview.await_args.args[0]["id"] == 1