"""계측 기능 테스트 — ⑩ 긴장도·페르소나, ⑦ 계약 데이터화.""" import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from config.config_models import AppConfig, InstrumentConfig, ProfilingConfig from router.v1.nego.protocol import NegotiationRequest from services.negotiation.engine import ChatEngine from services.negotiation.tension import estimate_tension, select_persona SNAP = {"rq_type": "재협상", "session_id": "T-1", "index": 5, "user_input": "예", "item_price": 163600.0, "revenue": 163600.0, "dist": "R", "partners_yn": "없음", "partners": 0, "anchor": 8099.0, "target": 8180.0, "initial": 9000.0, "input": 9000.0, "iteration": 0, "is_new_quote": True, "profiling": False} def make_engine(tmp_path): config = AppConfig( profiling=ProfilingConfig(enabled=False), instrument=InstrumentConfig( tension_log=str(tmp_path / "tension.jsonl"), response_time_log="", contracts_log=str(tmp_path / "contracts.jsonl"), ), ) return ChatEngine(config), tmp_path def test_persona_bands(): assert select_persona(0.9)["name"] == "단호한 압박형" assert select_persona(0.5)["name"] == "균형 협상형" assert select_persona(0.1)["name"] == "우호적 마무리형" def test_estimate_tension_zones(): # PZ1/PZ2(지표 ≥ 80) → 0, 초기제안가 = 최대 긴장 근사 assert estimate_tension(99, 8180, 9000) == 0.0 assert estimate_tension(34, 8180, 9000) > 0.5 def test_tension_log_has_persona(tmp_path): engine, tmp = make_engine(tmp_path) req = NegotiationRequest(**dict(SNAP, step="가격협상_확인")) res = engine.process_request(req) assert res.step == "가격협상" rows = [json.loads(x) for x in (tmp / "tension.jsonl").read_text().splitlines()] assert rows, "긴장도 로그 미기록" row = rows[-1] assert 0.0 <= row["predicted"] <= 1.0 assert row["persona"] in ("단호한 압박형", "균형 협상형", "우호적 마무리형") assert isinstance(row["indicator"], int) def test_contract_logged_on_completion(tmp_path): engine, tmp = make_engine(tmp_path) # 1% 와일드카드 수락 → 협상완료 (입력가 8000 ≤ 앵커 8099로 진입) req = NegotiationRequest(**dict(SNAP, step="wild_card_1pct", user_input="예", input=8000.0)) res = engine.process_request(req) assert res.step == "협상완료" rows = [json.loads(x) for x in (tmp / "contracts.jsonl").read_text().splitlines()] assert len(rows) == 1 rec = rows[0] assert rec["final_price"] == 7920 # 8000 × 0.99 assert rec["reduction_pct"] is not None assert rec["rq_type"] == "재협상" def test_contract_not_logged_midway(tmp_path): engine, tmp = make_engine(tmp_path) engine.process_request(NegotiationRequest(**dict(SNAP, step="시작", index=0))) assert not (tmp / "contracts.jsonl").exists() or \ not (tmp / "contracts.jsonl").read_text().strip()