Files
web-page-backend/agent-office/tests/test_insta_autonomous.py
gahusb 7c5ca15b64 feat(agent-office): InstaAgent 자율 발급 경로 + 커버 프리뷰
- on_schedule에 autonomous_issue 분기 추가 (eligible 픽만 선별·max_per_day 제한)
- _generate_and_preview 메서드: 슬레이트 생성 → 커버 PNG → 인라인 승인 버튼
- messaging.send_photo 신규 추가 (multipart/form-data, reply_markup 지원)
- insta_get_preferences 실패를 warning으로 격리해 자율 경로 중단 방지

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-11 02:36:26 +09:00

50 lines
1.8 KiB
Python

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