o2o-negosium-original/agent/tests/test_agent_integration.py
2026-06-18 17:02:12 +09:00

72 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""AGENT_INTEGRATION.md 규약 검증 (backend ↔ agent).
4-1 session_id honoring: backend 가 보낸 session_id(=negotiation.sessions.session_id)를
새 uuid 발급 없이 그대로 세션 키로 사용.
4-4 tenant 키 = company_id(uuid): 미등록 company_id 는 _base 자동 온보딩(cold-start) 으로 수용.
"""
import os
import pytest
from router.v1.chat.protocol import Req_Chat
from services.chat_service import ChatService, reset_sessions
from negotiation.chat.service.chat_session_repository import ChatSessionRepository
from tenancy.config_loader import TenantConfigLoader
from tenancy.registry import TenantEngineRegistry
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
# backend 가 보내는 값 형식: session_id = negotiation.sessions.session_id(uuid), X-Tenant-ID = company_id(uuid)
BACKEND_SESSION_ID = "11111111-1111-1111-1111-111111111111"
COMPANY_ID = "00000000-0000-0000-0000-000000000099"
def _reg():
return TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
@pytest.mark.asyncio
async def test_4_1_honors_backend_session_id(db_engine):
reset_sessions()
eng = await _reg().get_engine("ktcommerce")
svc = ChatService()
# 첫 턴: backend 의 session_id 를 그대로 키로 써야 함 (새 uuid 발급 X)
r = await svc.chat(eng, Req_Chat(session_id=BACKEND_SESSION_ID, rq_type="재협상",
target_price=10000, anchor_price=9900))
assert r.session_id == BACKEND_SESSION_ID
assert r.step == "서비스안내"
# 같은 session_id 로 이어가면 그 세션이 DB 에서 복원됨
r2 = await svc.chat(eng, Req_Chat(session_id=BACKEND_SESSION_ID, user_input="확인"))
assert r2.session_id == BACKEND_SESSION_ID and r2.step == "담당자확인"
# learning.chat_sessions 에 backend 의 session_id 그대로 저장됨
saved = await ChatSessionRepository(eng.company_id).get(BACKEND_SESSION_ID)
assert saved is not None and saved.session_id == BACKEND_SESSION_ID
@pytest.mark.asyncio
async def test_4_4_company_id_auto_onboard():
# 미등록 company_id → _base 자동 온보딩 (전용 tenant.yaml 없이도 엔진 생성)
eng = await _reg().get_engine(COMPANY_ID)
assert eng.tenant_id == COMPANY_ID
assert eng.company_id == COMPANY_ID # 학습/세션이 이 company_id 로 격리
assert eng.action_space_size == 9 # base 기본 카드(162×9 정합)
assert eng.state_space_size == 162
@pytest.mark.asyncio
async def test_4_4_company_id_chat_end_to_end(db_engine):
reset_sessions()
eng = await _reg().get_engine(COMPANY_ID) # 자동 온보딩 테넌트
svc = ChatService()
r = await svc.chat(eng, Req_Chat(session_id=BACKEND_SESSION_ID, rq_type="재협상",
target_price=10000, anchor_price=9900))
assert r.session_id == BACKEND_SESSION_ID and r.step == "서비스안내"
# 카드선택 턴까지 진행 → company_id 스코프로 학습 기록
for ui in ["확인", "예", "확인", "11000", "예"]:
r = await svc.chat(eng, Req_Chat(session_id=BACKEND_SESSION_ID, user_input=ui))
assert r.step == "가격협상" and r.card_id is not None