From 976879dce0103eafd74f0f59933cef7d0335ed17 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 11 May 2026 17:15:04 -0400 Subject: [PATCH] feat(models): Extend FileItem.custom_slices with tag and comment fields --- src/models.py | 13 ++++++ tests/test_custom_slices_annotations.py | 56 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/test_custom_slices_annotations.py diff --git a/src/models.py b/src/models.py index 6343b1e..33cbb47 100644 --- a/src/models.py +++ b/src/models.py @@ -503,6 +503,19 @@ class FileItem: custom_slices: list[dict] = field(default_factory=list) injected_at: Optional[float] = None + def __post_init__(self): + if self.custom_slices: + normalized = [] + for slc in self.custom_slices: + if isinstance(slc, dict): + new_slc = slc.copy() + if "tag" not in new_slc: new_slc["tag"] = None + if "comment" not in new_slc: new_slc["comment"] = None + normalized.append(new_slc) + else: + normalized.append(slc) + self.custom_slices = normalized + def to_dict(self) -> Dict[str, Any]: """ [C: src/personas.py:PersonaManager.save_persona, src/presets.py:PresetManager.save_preset, src/project_manager.py:save_project, src/project_manager.py:save_track_state, src/tool_presets.py:ToolPresetManager.save_bias_profile, src/tool_presets.py:ToolPresetManager.save_preset, src/workspace_manager.py:WorkspaceManager.save_profile, tests/test_bias_models.py:test_bias_profile_model, tests/test_bias_models.py:test_tool_model, tests/test_bias_models.py:test_tool_preset_extension, tests/test_event_serialization.py:test_user_request_event_serialization, tests/test_external_editor.py:TestExternalEditorConfig.test_to_dict, tests/test_external_editor.py:TestTextEditorConfig.test_to_dict, tests/test_file_item_model.py:test_file_item_to_dict, tests/test_gui_events_v2.py:test_user_request_event_payload, tests/test_mcp_config.py:test_mcp_configuration_to_from_dict, tests/test_mcp_config.py:test_mcp_server_config_to_from_dict, tests/test_per_ticket_model.py:test_model_override_serialization, tests/test_persona_id.py:test_ticket_persona_id_serialization, tests/test_persona_models.py:test_persona_defaults, tests/test_persona_models.py:test_persona_serialization, tests/test_thinking_gui.py:test_thinking_segment_model_compatibility, tests/test_ticket_queue.py:test_ticket_to_dict_priority, tests/test_tiered_aggregation.py:test_persona_aggregation_strategy, tests/test_track_state_schema.py:test_track_state_to_dict, tests/test_track_state_schema.py:test_track_state_to_dict_with_none, tests/test_ui_summary_only_removal.py:test_file_item_serialization_with_flags] diff --git a/tests/test_custom_slices_annotations.py b/tests/test_custom_slices_annotations.py new file mode 100644 index 0000000..1c307b7 --- /dev/null +++ b/tests/test_custom_slices_annotations.py @@ -0,0 +1,56 @@ +import pytest +from src.models import FileItem + +def test_file_item_custom_slices_serialization_with_annotations(): + # Test that FileItem correctly serializes custom_slices with tag and comment. + slices = [ + {'start_line': 1, 'end_line': 10, 'tag': 'init', 'comment': 'Constructor logic'}, + {'start_line': 20, 'end_line': 30, 'tag': 'process', 'comment': 'Main processing loop'} + ] + item = FileItem(path='src/app.py', custom_slices=slices) + + serialized = item.to_dict() + assert 'custom_slices' in serialized + assert len(serialized['custom_slices']) == 2 + assert serialized['custom_slices'][0]['tag'] == 'init' + assert serialized['custom_slices'][0]['comment'] == 'Constructor logic' + assert serialized['custom_slices'][1]['tag'] == 'process' + assert serialized['custom_slices'][1]['comment'] == 'Main processing loop' + +def test_file_item_custom_slices_deserialization_with_annotations(): + # Test that FileItem correctly deserializes custom_slices with tag and comment. + data = { + 'path': 'src/app.py', + 'custom_slices': [ + {'start_line': 5, 'end_line': 15, 'tag': 'helper', 'comment': 'Utility function'}, + {'start_line': 40, 'end_line': 50} # Missing optional fields + ] + } + + item = FileItem.from_dict(data) + assert len(item.custom_slices) == 2 + + # First slice has annotations + assert item.custom_slices[0]['tag'] == 'helper' + assert item.custom_slices[0]['comment'] == 'Utility function' + + # Second slice should have default None or empty string for optional fields + # This is where it will likely FAIL if we haven't updated from_dict to inject defaults + assert 'tag' in item.custom_slices[1] + assert 'comment' in item.custom_slices[1] + assert item.custom_slices[1]['tag'] is None + assert item.custom_slices[1]['comment'] is None + +def test_file_item_custom_slices_round_trip_annotations(): + # Test a full round trip of FileItem with slice annotations. + item = FileItem(path='src/test.py', custom_slices=[ + {'start_line': 1, 'end_line': 5, 'tag': 'test', 'comment': 'Unit test case'} + ]) + + serialized = item.to_dict() + deserialized = FileItem.from_dict(serialized) + + assert deserialized.custom_slices == item.custom_slices + assert deserialized.custom_slices[0]['tag'] == 'test' + assert deserialized.custom_slices[0]['comment'] == 'Unit test case' +