Private
Public Access
0
0
Files
manual_slop/tests/test_project_serialization.py
T
ed 9e07fac1db refactor(consumers): replace 'models.<moved_class>' with direct imports
Per post_module_taxonomy_de_cruft_20260627 Phase 2 (FR7 continued).
The previous migration commit (8f11340b) handled the
'from src.models import X' pattern (85 sites). This commit handles
the 'models.<moved_class>' attribute access pattern (44 sites in 20
files), which the __getattr__ shim previously supported.

The migration was performed by the one-time script
scripts/tier2/artifacts/post_module_taxonomy_de_cruft_20260627/migrate_models_attr.py
which:
 1. For each 'models.<moved_class>' reference, replaces it with the
    bare class name (e.g., 'models.MCPConfiguration' -> 'MCPConfiguration')
 2. Adds the import 'from src.<destination> import <moved_class>' at
    the top of the file (deduplicated if the import already exists)
 3. Skips moved classes that the file already imports directly

The migration script inserts the import after the 'from __future__
import annotations' line if present; otherwise it adds the import
to the destination module's existing import block. Two files
required manual fixes because the script's regex didn't handle them:
 - src/rag_engine.py: uses 'from src import models' (not 'from
                            src.models import X'); the class is accessed
                            via 'models.RAGConfig'. Replaced with a
                            direct 'from src.mcp_client import RAGConfig'
                            import and removed the 'from src import models'.
 - tests/test_project_context_20260627.py: uses the parens-style
                            multi-line 'from src.models import (X, Y, Z)'.
                            Replaced with the parens-style direct import.

After this commit:
 - 'models.MCPConfiguration', 'models.FileItem', 'models.Ticket', etc.
   no longer work in src/ and tests/ (the AttributeError raises
   because models.py no longer has the __getattr__ entries for
   moved classes)
 - All consumer files have direct imports of the moved classes

Total: 44 'models.<moved_class>' references rewritten across 20 files.
2026-06-26 14:06:03 -04:00

90 lines
2.9 KiB
Python

import os
import unittest
import tempfile
from pathlib import Path
from src import project_manager
from src import models
from src.app_controller import AppController
class TestProjectSerialization(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.TemporaryDirectory()
self.project_path = Path(self.test_dir.name) / "test_project.toml"
def tearDown(self):
self.test_dir.cleanup()
def test_fileitem_roundtrip(self):
"""Verify that FileItem objects survive a save/load cycle."""
proj = project_manager.default_project("test")
file1 = FileItem(path="src/main.py", auto_aggregate=True, force_full=False)
file2 = FileItem(path="docs/readme.md", auto_aggregate=False, force_full=True)
proj["files"]["paths"] = [file1, file2]
# Save
project_manager.save_project(proj, self.project_path)
# Load
loaded_proj = project_manager.load_project(self.project_path)
paths = loaded_proj["files"]["paths"]
self.assertEqual(len(paths), 2)
self.assertIsInstance(paths[0], FileItem)
self.assertEqual(paths[0].path, "src/main.py")
self.assertTrue(paths[0].auto_aggregate)
self.assertFalse(paths[0].force_full)
self.assertIsInstance(paths[1], FileItem)
self.assertEqual(paths[1].path, "docs/readme.md")
self.assertFalse(paths[1].auto_aggregate)
self.assertTrue(paths[1].force_full)
def test_backward_compatibility_strings(self):
"""Verify that old-style string paths are converted to FileItem objects by AppController."""
# Create a project file manually with string paths
content = """
[project]
name = "legacy"
[files]
base_dir = "."
paths = ["file1.py", "file2.md"]
[discussion]
roles = ["User", "AI"]
"""
with open(self.project_path, "w") as f:
f.write(content)
# Load via project_manager (should load as strings)
proj = project_manager.load_project(self.project_path)
self.assertEqual(proj["files"]["paths"], ["file1.py", "file2.md"])
# Initialize AppController state logic
controller = AppController()
controller.project = proj
# Trigger deserialization (copied from init_state)
raw_paths = controller.project.get("files", {}).get("paths", [])
controller.files = []
for p in raw_paths:
if isinstance(p, FileItem):
controller.files.append(p)
elif isinstance(p, dict):
controller.files.append(FileItem.from_dict(p))
else:
controller.files.append(FileItem(path=str(p)))
self.assertEqual(len(controller.files), 2)
self.assertIsInstance(controller.files[0], FileItem)
self.assertEqual(controller.files[0].path, "file1.py")
self.assertIsInstance(controller.files[1], FileItem)
self.assertEqual(controller.files[1].path, "file2.md")
def test_default_roles_include_context(self):
"""Verify that 'Context' is in default project roles."""
proj = project_manager.default_project("test")
self.assertIn("Context", proj["discussion"]["roles"])
if __name__ == "__main__":
unittest.main()