Building AI agents, atomically
test: simplify failure-atomic chat history load regressions
Afloat1693b29ef7b851
diff --git a/atomic-agents/tests/context/test_chat_history.py b/atomic-agents/tests/context/test_chat_history.pyindex 4aa0085..5470bb9 100644--- a/atomic-agents/tests/context/test_chat_history.py+++ b/atomic-agents/tests/context/test_chat_history.py@@ -1,10 +1,11 @@from enum import Enum+from unittest.mock import Mock, callimport pytestimport jsonfrom typing import List, Dict, Unionfrom pathlib import Path-from pydantic import Field+from pydantic import Field, ValidationErrorfrom atomic_agents.context import ChatHistory, Messagefrom atomic_agents import BaseIOSchema, VideoURLimport instructor@@ -55,6 +56,12 @@ class MockMultimodalSchema(BaseIOSchema):audio: instructor.processing.multimodal.Audio = Field(..., description="The audio to analyze")+class MockImageSchema(BaseIOSchema):+ """Test schema for serialized image paths."""++ image: instructor.Image = Field(..., description="An image with a local path")++class ColorEnum(str, Enum):BLUE = "blue"RED = "red"@@ -290,78 +297,130 @@ def test_load_invalid_data(history):history.load("invalid json")- "failure",- [- "invalid_json",- "missing_max_messages",- "missing_current_turn_id",- "missing_history",- "invalid_history",- "missing_content",- "unknown_class",- "invalid_content_json",- "invalid_content",- "later_invalid_content",- "invalid_role",- "invalid_turn_id",- ],-)-def test_load_failure_preserves_existing_history(history, failure):+MISSING = object()+++def saved_history(history: ChatHistory) -> tuple[ChatHistory, List[Message], Message, str]:history.add_message("user", InputSchema(test_field="Keep this message"))- original_history = history.history- original_message = history.history[0]- original_dump = history.dump()- original_turn_id = history.current_turn_id+ return history, history.history, history.history[0], history.dump()++def replacement_history() -> ChatHistory:replacement = ChatHistory(max_messages=2)replacement.current_turn_id = "replacement-turn"replacement.add_message("user", InputSchema(test_field="Replacement user message"))replacement.add_message("assistant", MockOutputSchema(test_field="Replacement assistant message"))- replacement_data = json.loads(replacement.dump())- first_message = replacement_data["history"][0]-- if failure.startswith("missing_"):- field = failure.removeprefix("missing_")- del (first_message if field == "content" else replacement_data)[field]- elif failure == "invalid_history":- replacement_data["history"] = None- elif failure == "unknown_class":- first_message["content"]["class_name"] = f"{InputSchema.__module__}.NonexistentSchema"- elif failure == "invalid_content_json":- first_message["content"]["data"] = "invalid json"- elif failure == "invalid_content":- first_message["content"]["data"] = "{}"- elif failure == "later_invalid_content":- replacement_data["history"][1]["content"]["data"] = "{}"- elif failure == "invalid_role":- first_message["role"] = None- elif failure == "invalid_turn_id":- first_message["turn_id"] = []-- serialized_data = "invalid json" if failure == "invalid_json" else json.dumps(replacement_data)- with pytest.raises(ValueError):- history.load(serialized_data)+ return replacement++def _assert_history_unchanged(+ history: ChatHistory, original_history: List[Message], original_message: Message, original_dump: str+) -> None:+ original_data = json.loads(original_dump)assert history.history is original_historyassert history.history[0] is original_messageassert history.dump() == original_dump- assert history.max_messages == 5- assert history.current_turn_id == original_turn_id+ assert history.max_messages == original_data["max_messages"]+ assert history.current_turn_id == original_data["current_turn_id"]-def test_load_replaces_existing_history(history):- history.add_message("user", InputSchema(test_field="Old message"))- original_history = history.history+def test_load_invalid_json_preserves_existing_history(saved_history) -> None:+ history = saved_history[0]+ with pytest.raises(ValueError, match="^Invalid serialized data: ") as error:+ history.load("invalid json")+ assert type(error.value) is ValueError+ _assert_history_unchanged(*saved_history)+++ "path, value, error_type, error_match",+ [+ pytest.param(("max_messages",), MISSING, ValueError, "'max_messages'", id="missing_max_messages"),+ pytest.param(("current_turn_id",), MISSING, ValueError, "'current_turn_id'", id="missing_current_turn_id"),+ pytest.param(("history",), MISSING, ValueError, "'history'", id="missing_history"),+ pytest.param(("history",), None, ValueError, "^Invalid serialized data: ", id="invalid_history"),+ pytest.param(("history", 0, "content"), MISSING, ValueError, "'content'", id="missing_content"),+ pytest.param(+ ("history", 0, "content", "class_name"),+ f"{InputSchema.__module__}.NonexistentSchema",+ ValueError,+ "NonexistentSchema",+ id="unknown_class",+ ),+ pytest.param(+ ("history", 0, "content", "data"), "invalid json", ValidationError, "Invalid JSON", id="invalid_content_json"+ ),+ pytest.param(("history", 0, "content", "data"), "{}", ValidationError, "test_field", id="invalid_content"),+ pytest.param(("history", 1, "content", "data"), "{}", ValidationError, "test_field", id="later_invalid_content"),+ pytest.param(("history", 0, "role"), None, ValidationError, "role", id="invalid_role"),+ pytest.param(("history", 0, "turn_id"), [], ValidationError, "turn_id", id="invalid_turn_id"),+ pytest.param(("history", 1, "role"), None, ValidationError, "role", id="later_invalid_role"),+ pytest.param(("history", 1, "turn_id"), [], ValidationError, "turn_id", id="later_invalid_turn_id"),+ pytest.param(+ ("history", 1, "content", "class_name"),+ "nonexistent_chat_history_module.InputSchema",+ ModuleNotFoundError,+ "nonexistent_chat_history_module",+ id="later_missing_module",+ ),+ ],+)+def test_load_failure_preserves_existing_history(+ saved_history, replacement_history, path, value, error_type, error_match+) -> None:+ history = saved_history[0]+ replacement_data = json.loads(replacement_history.dump())+ target = replacement_data+ for field in path[:-1]:+ target = target[field]+ if value is MISSING:+ del target[path[-1]]+ else:+ target[path[-1]] = value++ with pytest.raises(error_type, match=error_match) as error:+ history.load(json.dumps(replacement_data))+ assert type(error.value) is error_type+ _assert_history_unchanged(*saved_history)+++ "normalization_error_type, error_type, error_match",+ [+ pytest.param(TypeError, ValueError, "^Invalid serialized data: late normalization failure$", id="wrapped_type_error"),+ pytest.param(RuntimeError, RuntimeError, "^late normalization failure$", id="unwrapped_runtime_error"),+ ],+)+def test_load_late_normalization_failure_preserves_existing_history(+ saved_history, monkeypatch, normalization_error_type, error_type, error_match+) -> None:+ history = saved_history[0]replacement = ChatHistory(max_messages=2)replacement.current_turn_id = "replacement-turn"- replacement.add_message("assistant", MockOutputSchema(test_field="Replacement message"))+ for source in ("test/first.jpg", "test/late.jpg"):+ replacement.add_message("user", MockImageSchema(image=instructor.Image(source=source, media_type="image/jpeg")))+ normalize_path = Mock(side_effect=[Path("test/first.jpg"), normalization_error_type("late normalization failure")])+ monkeypatch.setattr("atomic_agents.context.chat_history.Path", normalize_path)- history.load(replacement.dump())+ with pytest.raises(error_type, match=error_match) as error:+ history.load(replacement.dump())+ assert type(error.value) is error_type+ assert normalize_path.call_args_list == [call("test/first.jpg"), call("test/late.jpg")]+ _assert_history_unchanged(*saved_history)+++def test_load_replaces_existing_history(history: ChatHistory, replacement_history: ChatHistory) -> None:+ history.add_message("user", InputSchema(test_field="Old message"))+ original_history = history.history++ history.load(replacement_history.dump())assert history.history is not original_history- assert history.dump() == replacement.dump()- assert isinstance(history.history[0].content, MockOutputSchema)+ assert history.dump() == replacement_history.dump()+ assert isinstance(history.history[0].content, InputSchema)+ assert isinstance(history.history[1].content, MockOutputSchema)assert history.max_messages == 2assert history.current_turn_id == "replacement-turn"