import pytest
from src.models import ContextPreset, ContextFileEntry

def test_context_file_entry_serialization():
 p = ContextFileEntry(path="test.py", view_mode="skeleton")
 d = p.to_dict()
 # Check for default custom_slices
 assert d == {"path": "test.py", "view_mode": "skeleton", "custom_slices": [], "ast_definitions": False, "ast_mask": {}, "ast_signatures": False}
 
 p2 = ContextFileEntry.from_dict(d)
 assert p2.path == "test.py"
 assert p2.view_mode == "skeleton"
 assert p2.custom_slices == []

 # Test with custom_slices
 p3 = ContextFileEntry(path="test.py", view_mode="summary", custom_slices=["1-10"])
 d3 = p3.to_dict()
 assert d3["custom_slices"] == ["1-10"]
 p4 = ContextFileEntry.from_dict(d3)
 assert p4.custom_slices == ["1-10"]

def test_context_preset_serialization():
 f1 = ContextFileEntry(path="a.py", view_mode="full")
 f2 = ContextFileEntry(path="b.py", view_mode="summary", custom_slices=["5-15"])
 
 preset = ContextPreset(
  name="test_preset",
  files=[f1, f2],
  screenshots=["shot1.png"],
  description="Test description"
 )
 
 d = preset.to_dict()
 assert len(d["files"]) == 2
 assert d["files"][0]["path"] == "a.py"
 assert d["files"][1]["custom_slices"] == ["5-15"]
 assert d["screenshots"] == ["shot1.png"]
 assert d["description"] == "Test description"
 
 p2 = ContextPreset.from_dict("test_preset", d)
 assert p2.name == "test_preset"
 assert p2.description == "Test description"
 assert len(p2.files) == 2
 assert p2.files[0].view_mode == "full"
 assert p2.files[1].custom_slices == ["5-15"]
 assert p2.screenshots == ["shot1.png"]

def test_context_preset_from_dict_legacy():
 # Test handling of legacy string paths in preset files and missing description/custom_slices
 d = {
  "files": ["legacy.py", {"path": "new.py", "view_mode": "skeleton"}],
  "screenshots": []
 }
 preset = ContextPreset.from_dict("legacy_test", d)
 assert len(preset.files) == 2
 assert preset.files[0].path == "legacy.py"
 assert preset.files[0].view_mode == "summary" # Default
 assert preset.files[0].custom_slices == []
 assert preset.files[1].path == "new.py"
 assert preset.files[1].view_mode == "skeleton"
 assert preset.files[1].custom_slices == []
 assert preset.description == ""
