From 773c064c5c50210ba459ae8f9d78d0f30eb10ac5 Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 5 Aug 2026 00:11:33 +0800 Subject: [PATCH] test(emotion-e3): add deterministic replay matrix --- tests/emotion_eval_cases.py | 58 ++++++++++++++++++++++++++++++ tests/test_emotion_e3_replay.py | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 tests/emotion_eval_cases.py create mode 100644 tests/test_emotion_e3_replay.py diff --git a/tests/emotion_eval_cases.py b/tests/emotion_eval_cases.py new file mode 100644 index 0000000..5315136 --- /dev/null +++ b/tests/emotion_eval_cases.py @@ -0,0 +1,58 @@ +"""Deterministic synthetic emotion-evaluation matrix shared by both repositories.""" +from __future__ import annotations + +import hashlib +import json +from typing import Any + + +EMOTION_EVAL_SCHEMA_VERSION = "emotion_eval_case.v1" +EVENT_TEMPLATES = ( + {"key": "hurt", "event_type": "hurt", "intensity": 90, "confidence": 0.92, "target": "bot"}, + {"key": "apology", "event_type": "apology", "intensity": 68, "confidence": 0.86, "target": "bot"}, + {"key": "comfort", "event_type": "comfort", "intensity": 48, "confidence": 0.82, "target": "bot"}, + {"key": "praise", "event_type": "praise", "intensity": 38, "confidence": 0.78, "target": "bot"}, + {"key": "self_low", "event_type": "comfort_need", "intensity": 62, "confidence": 0.88, "target": "self"}, + {"key": "third_party", "event_type": "external_negative", "intensity": 54, "confidence": 0.80, "target": "other"}, + {"key": "play", "event_type": "play", "intensity": 35, "confidence": 0.75, "target": "bot"}, + {"key": "intimacy", "event_type": "intimacy", "intensity": 55, "confidence": 0.80, "target": "bot"}, + {"key": "boundary", "event_type": "boundary", "intensity": 80, "confidence": 0.90, "target": "bot"}, + {"key": "neutral", "event_type": "neutral", "intensity": 0, "confidence": 0.95, "target": "none"}, +) +STATE_VARIANTS = ( + {"key": "friend_normal", "role": "friend", "mode": "normal", "score": 100, "energy": 70, "mood": "平稳", "busy": False, "boundary": False}, + {"key": "friend_tired", "role": "friend", "mode": "normal", "score": 300, "energy": 20, "mood": "疲惫", "busy": False, "boundary": False}, + {"key": "friend_close", "role": "friend", "mode": "normal", "score": 800, "energy": 75, "mood": "轻快", "busy": False, "boundary": False}, + {"key": "owner", "role": "owner", "mode": "owner_exclusive", "score": 1200, "energy": 80, "mood": "温柔", "busy": False, "boundary": False}, + {"key": "contact_boundary", "role": "friend", "mode": "normal", "score": 600, "energy": 70, "mood": "平稳", "busy": False, "boundary": True}, + {"key": "busy", "role": "friend", "mode": "normal", "score": 600, "energy": 65, "mood": "平稳", "busy": True, "boundary": False}, +) + + +def build_emotion_eval_cases() -> list[dict[str, Any]]: + cases: list[dict[str, Any]] = [] + for event in EVENT_TEMPLATES: + for state in STATE_VARIANTS: + cases.append({ + "schema_version": EMOTION_EVAL_SCHEMA_VERSION, + "case_id": f"{event['key']}__{state['key']}", + "event": dict(event), + "state": dict(state), + "clock": "2026-08-04T12:00:00+08:00", + }) + return cases + + +def emotion_eval_fingerprint() -> str: + payload = json.dumps(build_emotion_eval_cases(), ensure_ascii=False, sort_keys=True, separators=(",", ":")) + return hashlib.sha256(payload.encode("utf-8")).hexdigest() + + +__all__ = [ + "EMOTION_EVAL_SCHEMA_VERSION", + "EVENT_TEMPLATES", + "STATE_VARIANTS", + "build_emotion_eval_cases", + "emotion_eval_fingerprint", +] + diff --git a/tests/test_emotion_e3_replay.py b/tests/test_emotion_e3_replay.py new file mode 100644 index 0000000..06f244b --- /dev/null +++ b/tests/test_emotion_e3_replay.py @@ -0,0 +1,63 @@ +from __future__ import annotations + +from pathlib import Path +import sys +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from companion_interaction_expression import build_expression_decision # noqa: E402 +from emotion_event_contract import normalize_emotion_event # noqa: E402 +from tests.emotion_eval_cases import build_emotion_eval_cases, emotion_eval_fingerprint # noqa: E402 + + +EVENT_BANDS = { + "hurt": "hurt", + "apology": "warm", + "comfort": "warm", + "praise": "lively", + "comfort_need": "warm", + "external_negative": "warm", + "play": "lively", + "intimacy": "warm", + "boundary": "avoidant", + "neutral": "relaxed", +} + + +class EmotionE3ReplayTests(unittest.TestCase): + def test_sixty_case_matrix_is_deterministic_and_safe(self) -> None: + cases = build_emotion_eval_cases() + self.assertEqual(60, len(cases)) + self.assertEqual(emotion_eval_fingerprint(), emotion_eval_fingerprint()) + for case in cases: + event_data = case["event"] + state = case["state"] + event = normalize_emotion_event({ + "event_type": event_data["event_type"], + "intensity": event_data["intensity"], + "confidence": event_data["confidence"], + "session_id": "qq:FriendMessage:u1", + "dedupe_key": case["case_id"], + "occurred_at": case["clock"], + }, producer_plugin="emotion_eval") + decision = build_expression_decision({ + "relationship_role": state["role"], + "relationship_mode": state["mode"], + "relationship_score": state["score"], + "current_interaction": EVENT_BANDS[event["event_type"]], + "bot_state": {"energy": state["energy"], "mood": state["mood"]}, + "schedule": {"mode": "busy" if state["busy"] else "ordinary"}, + "safety_constraints": {"contact_boundary": state["boundary"]}, + }) + if state["boundary"]: + self.assertEqual("contact_boundary", decision.blocker) + else: + self.assertIsNone(decision.blocker) + self.assertNotIn("raw_text", repr(event)) + + +if __name__ == "__main__": + unittest.main()