70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
"""P8-A 검증 — /chat 세션 상태 DB 영속화 (재시작/멀티워커 안전).
|
|
|
|
1. 세션이 DB(learning.chat_sessions)에 저장된다.
|
|
2. 새 ChatService 인스턴스(=다른 워커/재시작 모사)로 같은 session_id 를 이어가도 진행 상태 복원.
|
|
3. company_id 격리(타테넌트 session_id 로는 조회 안 됨).
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from negotiation.chat.service.chat_session_repository import ChatSessionRepository
|
|
from router.v1.chat.protocol import Req_Chat
|
|
from services.chat_service import ChatService
|
|
from tenancy.config_loader import TenantConfigLoader
|
|
from tenancy.registry import TenantEngineRegistry
|
|
import os
|
|
|
|
_TENANTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tenants")
|
|
|
|
|
|
def _eng():
|
|
reg = TenantEngineRegistry(loader=TenantConfigLoader(tenants_dir=_TENANTS_DIR, cache_ttl_seconds=0))
|
|
return reg
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_session_persists_and_resumes_across_instances(db_engine):
|
|
reg = _eng()
|
|
eng = await reg.get_engine("ktcommerce")
|
|
|
|
# 인스턴스 1: 협상 시작 + 몇 턴 진행
|
|
svc1 = ChatService()
|
|
r = await svc1.chat(eng, Req_Chat(rq_type="재협상", target_price=10000, anchor_price=8000))
|
|
sid = r.session_id
|
|
for ui in ["확인", "예", "확인", "11000"]:
|
|
r = await svc1.chat(eng, Req_Chat(session_id=sid, user_input=ui))
|
|
step_before = r.step
|
|
assert step_before == "가격협상_확인" # 11000 제시 후 확인 단계
|
|
|
|
# DB 에 저장됐는지 확인
|
|
saved = await ChatSessionRepository(eng.company_id).get(sid)
|
|
assert saved is not None and saved.step == step_before
|
|
assert saved.context["anchor_price"] == 8000
|
|
|
|
# 인스턴스 2 (재시작/다른 워커 모사): 같은 session_id 로 이어가기
|
|
svc2 = ChatService()
|
|
r2 = await svc2.chat(eng, Req_Chat(session_id=sid, user_input="예"))
|
|
assert r2.step == "가격협상" # 상태가 복원되어 다음 step 으로 진행
|
|
assert r2.card_id is not None # 가격협상 턴 → 카드 선택 이어짐
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_session_company_scoped(db_engine):
|
|
reg = _eng()
|
|
eng = await reg.get_engine("ktcommerce")
|
|
svc = ChatService()
|
|
r = await svc.chat(eng, Req_Chat(rq_type="재협상"))
|
|
sid = r.session_id
|
|
|
|
# 자사(ktcommerce)로는 조회됨
|
|
assert await ChatSessionRepository(eng.company_id).get(sid) is not None
|
|
# 타테넌트(imarketkorea) company_id 로는 조회 안 됨 (격리)
|
|
assert await ChatSessionRepository("imarketkorea").get(sid) is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_none_for_missing(db_engine):
|
|
repo = ChatSessionRepository("ktcommerce")
|
|
assert await repo.get(None) is None
|
|
assert await repo.get("00000000-0000-0000-0000-000000000000") is None
|