|
| 1 | +# Tencent is pleased to support the open source community by making |
| 2 | +# tRPC-Agent-Python available. |
| 3 | +# |
| 4 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 5 | +# |
| 6 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 7 | + |
| 8 | +"""Unit tests for the replay consistency comparator.""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import dataclasses |
| 13 | +from typing import Any |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | + |
| 18 | +@dataclasses.dataclass |
| 19 | +class DiffEntry: |
| 20 | + """A single normalized field mismatch. Mirrors Go DiffEntry.""" |
| 21 | + session_id: str | None = None |
| 22 | + event_index: int | None = None |
| 23 | + memory_id: str | None = None |
| 24 | + summary_id: str | None = None |
| 25 | + track_name: str | None = None |
| 26 | + section: str = "" |
| 27 | + path: str = "" |
| 28 | + left: Any = None |
| 29 | + right: Any = None |
| 30 | + allowed: bool = False |
| 31 | + reason: str = "" |
| 32 | + |
| 33 | + |
| 34 | +class TestRecursiveDiff: |
| 35 | + """Tests for recursive_diff().""" |
| 36 | + |
| 37 | + def test_identical_dicts_no_diff(self): |
| 38 | + """Identical dicts produce zero diffs.""" |
| 39 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 40 | + left = {"a": 1, "b": 2} |
| 41 | + right = {"a": 1, "b": 2} |
| 42 | + diffs = recursive_diff(left, right) |
| 43 | + assert len(diffs) == 0 |
| 44 | + |
| 45 | + def test_different_dict_values(self): |
| 46 | + """Different values produce diffs with correct paths.""" |
| 47 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 48 | + left = {"a": 1, "b": 2} |
| 49 | + right = {"a": 1, "b": 99} |
| 50 | + diffs = recursive_diff(left, right) |
| 51 | + assert len(diffs) == 1 |
| 52 | + assert diffs[0].path == "b" |
| 53 | + assert diffs[0].left == 2 |
| 54 | + assert diffs[0].right == 99 |
| 55 | + |
| 56 | + def test_missing_key_in_right(self): |
| 57 | + """Key present in left but missing in right.""" |
| 58 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 59 | + left = {"a": 1, "b": 2} |
| 60 | + right = {"a": 1} |
| 61 | + diffs = recursive_diff(left, right) |
| 62 | + assert len(diffs) == 1 |
| 63 | + assert diffs[0].path == "b" |
| 64 | + assert diffs[0].left == 2 |
| 65 | + assert diffs[0].right is None |
| 66 | + |
| 67 | + def test_extra_key_in_right(self): |
| 68 | + """Key present in right but missing in left.""" |
| 69 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 70 | + left = {"a": 1} |
| 71 | + right = {"a": 1, "b": 2} |
| 72 | + diffs = recursive_diff(left, right) |
| 73 | + assert len(diffs) == 1 |
| 74 | + assert diffs[0].path == "b" |
| 75 | + assert diffs[0].left is None |
| 76 | + assert diffs[0].right == 2 |
| 77 | + |
| 78 | + def test_identical_lists_no_diff(self): |
| 79 | + """Identical lists produce zero diffs.""" |
| 80 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 81 | + left = [1, 2, 3] |
| 82 | + right = [1, 2, 3] |
| 83 | + diffs = recursive_diff(left, right) |
| 84 | + assert len(diffs) == 0 |
| 85 | + |
| 86 | + def test_list_length_mismatch(self): |
| 87 | + """Left longer than right — extra items diff as None.""" |
| 88 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 89 | + left = [1, 2, 3] |
| 90 | + right = [1, 2] |
| 91 | + diffs = recursive_diff(left, right) |
| 92 | + assert len(diffs) == 1 |
| 93 | + assert diffs[0].path == "[2]" |
| 94 | + assert diffs[0].left == 3 |
| 95 | + assert diffs[0].right is None |
| 96 | + |
| 97 | + def test_list_value_mismatch(self): |
| 98 | + """Same length, different value.""" |
| 99 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 100 | + left = [1, 2, 3] |
| 101 | + right = [1, 99, 3] |
| 102 | + diffs = recursive_diff(left, right) |
| 103 | + assert len(diffs) == 1 |
| 104 | + assert diffs[0].path == "[1]" |
| 105 | + |
| 106 | + def test_nested_dict_in_list(self): |
| 107 | + """Nested dict inside list diff.""" |
| 108 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 109 | + left = [{"name": "Alice", "score": 10}] |
| 110 | + right = [{"name": "Alice", "score": 20}] |
| 111 | + diffs = recursive_diff(left, right) |
| 112 | + assert len(diffs) == 1 |
| 113 | + assert "score" in diffs[0].path |
| 114 | + |
| 115 | + def test_deeply_nested_structure(self): |
| 116 | + """Three-level nested diff.""" |
| 117 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 118 | + left = {"events": [{"author": "user", "content": {"text": "Hi"}}]} |
| 119 | + right = {"events": [{"author": "user", "content": {"text": "Bye"}}]} |
| 120 | + diffs = recursive_diff(left, right) |
| 121 | + assert len(diffs) == 1 |
| 122 | + assert "text" in diffs[0].path |
| 123 | + assert diffs[0].left == "Hi" |
| 124 | + assert diffs[0].right == "Bye" |
| 125 | + |
| 126 | + def test_primitive_string_diff(self): |
| 127 | + """Direct string comparison.""" |
| 128 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 129 | + diffs = recursive_diff("hello", "world") |
| 130 | + assert len(diffs) == 1 |
| 131 | + |
| 132 | + def test_primitive_int_diff(self): |
| 133 | + """Direct int comparison.""" |
| 134 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 135 | + diffs = recursive_diff(42, 0) |
| 136 | + assert len(diffs) == 1 |
| 137 | + |
| 138 | + def test_none_vs_value(self): |
| 139 | + """None vs actual value.""" |
| 140 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 141 | + diffs = recursive_diff(None, "data") |
| 142 | + assert len(diffs) == 1 |
| 143 | + |
| 144 | + def test_detects_summary_injection(self): |
| 145 | + """Verify summary-specific diff detection.""" |
| 146 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 147 | + left = {"summaries": {"": "Correct summary"}} |
| 148 | + right: dict[str, Any] = {"summaries": {}} |
| 149 | + diffs = recursive_diff(left, right) |
| 150 | + assert len(diffs) > 0, "Missing summary should be detected" |
| 151 | + |
| 152 | + def test_unicode_diff(self): |
| 153 | + """Unicode text diff should be detected correctly.""" |
| 154 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 155 | + left = {"text": "你好世界"} |
| 156 | + right = {"text": "こんにちは"} |
| 157 | + diffs = recursive_diff(left, right) |
| 158 | + assert len(diffs) == 1 |
| 159 | + assert diffs[0].left == "你好世界" |
| 160 | + |
| 161 | + def test_compound_session_snapshot(self): |
| 162 | + """Full session snapshot comparison with multiple diff types.""" |
| 163 | + from tests.sessions.replay_consistency.comparator import recursive_diff |
| 164 | + left = { |
| 165 | + "events": [{"author": "user", "text": "Hello"}], |
| 166 | + "state": {"key": "value1"}, |
| 167 | + "memories": [{"content": "test memory"}], |
| 168 | + "tracks": [{"track": "exec", "payload": '{"ok":true}'}], |
| 169 | + } |
| 170 | + right = { |
| 171 | + "events": [{"author": "user", "text": "Different"}], |
| 172 | + "state": {"key": "value2"}, |
| 173 | + "memories": [{"content": "other memory"}], |
| 174 | + "tracks": [{"track": "exec", "payload": '{"ok":false}'}], |
| 175 | + } |
| 176 | + diffs = recursive_diff(left, right) |
| 177 | + # Should find diffs in all 4 sections. |
| 178 | + sections: set[str] = set() |
| 179 | + for d in diffs: |
| 180 | + path = d.path or "" |
| 181 | + top = path.split("[")[0].split(".")[0] |
| 182 | + sections.add(top) |
| 183 | + assert "events" in sections, f"Event diffs not detected in {sections}" |
| 184 | + assert "state" in sections, f"State diffs not detected in {sections}" |
| 185 | + assert "memories" in sections, f"Memory diffs not detected in {sections}" |
| 186 | + assert "tracks" in sections, f"Track diffs not detected in {sections}" |
0 commit comments