136 lines
4.9 KiB
Python
136 lines
4.9 KiB
Python
"""모듈별 더미 입력으로 실제 LLM을 호출해 보는 테스트. 루트에서 `python -m tests.test_modules`로 실행한다."""
|
|
|
|
import asyncio
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from models.context import (
|
|
AnalysisInput,
|
|
BookMetadata,
|
|
ContextExtractionInput,
|
|
DiscussionContext,
|
|
DiscussionTurn,
|
|
NextSpeaker,
|
|
)
|
|
from models.persona import PersonaInfo, PersonaInput
|
|
from services.context_extraction import extract_context
|
|
from services.persona_analysis import prepare_persona, select_strategy
|
|
from services.utterance_guide import generate_style_guide
|
|
from utils.session_store import save_context
|
|
|
|
BOOK = BookMetadata(title="데미안", author="헤르만 헤세")
|
|
TOPIC = "싱클레어는 데미안을 만나지 않았어도 자기 자신에게 이르렀을까?"
|
|
|
|
TURN_1 = DiscussionTurn(
|
|
speaker="민준",
|
|
text="데미안이 없었다면 싱클레어는 끝까지 두 세계 사이에서 헤맸을 거예요. 크로머에게서 구해준 것도 데미안이잖아요.",
|
|
)
|
|
TURN_2 = DiscussionTurn(
|
|
speaker="서연",
|
|
text="그건 계기일 뿐이에요. '새는 알을 깨고 나온다'는 말처럼, 결국 알을 깬 건 싱클레어 자신이에요.",
|
|
)
|
|
TURN_3 = DiscussionTurn(
|
|
speaker="민준",
|
|
text="그래도 그 쪽지를 건넨 것도 데미안이에요. 혼자였다면 알이 있다는 것조차 몰랐을 거예요.",
|
|
)
|
|
|
|
SPEAKERS = [
|
|
NextSpeaker(
|
|
name="민준",
|
|
persona=PersonaInput(keywords=["열정적", "직관적", "감성적"], expertise=["철학과 대학생"], gender="Male", age=24),
|
|
),
|
|
NextSpeaker(
|
|
name="서연",
|
|
persona=PersonaInput(keywords=["침착함", "이성적", "논리적"], expertise=["심리상담사"], gender="Female"),
|
|
),
|
|
NextSpeaker(
|
|
name="도윤",
|
|
persona=PersonaInput(keywords=["호기심 많음", "질문형", "개방적"], expertise=["소프트웨어 개발자"], gender="Male", age=31),
|
|
),
|
|
]
|
|
NEXT_SPEAKER = SPEAKERS[1]
|
|
|
|
|
|
def _print(title: str, model: BaseModel) -> None:
|
|
print(f"\n===== {title} =====")
|
|
print(model.model_dump_json(indent=2))
|
|
|
|
|
|
def _claim_ids(context: DiscussionContext) -> list[str]:
|
|
return [claim.claim_id for claim in context.current_claims + context.past_claims]
|
|
|
|
|
|
def _assert_context_carried_over(previous: DiscussionContext, updated: DiscussionContext) -> None:
|
|
updated_ids = _claim_ids(updated)
|
|
assert len(updated_ids) == len(set(updated_ids)), f"주장 ID 중복: {updated_ids}"
|
|
missing_ids = set(_claim_ids(previous)) - set(updated_ids)
|
|
assert not missing_ids, f"이전 주장이 사라짐: {missing_ids}"
|
|
|
|
|
|
async def test_context_extraction() -> DiscussionContext:
|
|
first = await extract_context(
|
|
ContextExtractionInput(topic=TOPIC, book=BOOK, new_turns=[TURN_1])
|
|
)
|
|
_print("맥락 추출 1턴 (구 맥락 없음)", first)
|
|
assert not first.past_claims, "첫 턴인데 과거 주장이 있음"
|
|
|
|
second = await extract_context(
|
|
ContextExtractionInput(
|
|
topic=TOPIC,
|
|
book=BOOK,
|
|
new_turns=[TURN_2],
|
|
previous_turns=[TURN_1],
|
|
previous_context=first,
|
|
)
|
|
)
|
|
_print("맥락 추출 2턴 (1턴 결과를 구 맥락으로)", second)
|
|
_assert_context_carried_over(first, second)
|
|
return second
|
|
|
|
|
|
async def test_persona_analysis(context: DiscussionContext) -> None:
|
|
summary, view_point = await prepare_persona(NEXT_SPEAKER.persona, TOPIC, None)
|
|
strategy = await select_strategy(
|
|
NEXT_SPEAKER.name, summary, view_point, NEXT_SPEAKER.persona.expertise, context
|
|
)
|
|
persona_info = PersonaInfo(
|
|
keywords=NEXT_SPEAKER.persona.keywords,
|
|
expertise=NEXT_SPEAKER.persona.expertise,
|
|
summary=summary,
|
|
current_strategy=strategy,
|
|
topic_view_point=view_point,
|
|
)
|
|
_print("페르소나 분석 (이전 페르소나 정보 없음)", persona_info)
|
|
|
|
reused = await prepare_persona(NEXT_SPEAKER.persona, TOPIC, persona_info)
|
|
assert reused == (summary, view_point), "이전 페르소나 정보의 요약·관점이 재사용되지 않음"
|
|
|
|
|
|
async def test_style_guide(context: DiscussionContext) -> None:
|
|
discussion_id = "test-modules"
|
|
await save_context(discussion_id, context)
|
|
result = await generate_style_guide(
|
|
AnalysisInput(
|
|
topic=TOPIC,
|
|
book=BOOK,
|
|
new_turns=[TURN_3],
|
|
previous_turns=[TURN_1, TURN_2],
|
|
discussion_id=discussion_id,
|
|
next_speaker=NEXT_SPEAKER,
|
|
)
|
|
)
|
|
_print("스타일 가이드 전체", result)
|
|
_assert_context_carried_over(context, result.context)
|
|
assert result.persona_info.keywords == NEXT_SPEAKER.persona.keywords, "페르소나 키워드가 입력과 다름"
|
|
|
|
|
|
async def main() -> None:
|
|
context = await test_context_extraction()
|
|
await test_persona_analysis(context)
|
|
await test_style_guide(context)
|
|
print("\n모든 테스트 통과")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|