import os
import pytest
from src.app_controller import AppController
from pathlib import Path

def test_skeleton_injection_state_variables():
 ctrl = AppController()
 assert hasattr(ctrl, "_inject_file_path")
 assert hasattr(ctrl, "_inject_mode")
 assert hasattr(ctrl, "_inject_preview")
 assert hasattr(ctrl, "_show_inject_modal")
 assert ctrl._inject_mode == "skeleton"
 assert ctrl._inject_preview == ""
 assert ctrl._show_inject_modal is False

def test_update_inject_preview_skeleton(tmp_path):
 ctrl = AppController()
 mock_file = tmp_path / "mock.py"
 # Use 1-space indent in the mock content too for consistency
 content = '"""Module docstring"""\ndef foo():\n """Foo docstring"""\n print("hello")\n\nclass Bar:\n """Bar docstring"""\n def baz(self):\n  pass\n'
 mock_file.write_text(content)
 
 ctrl._inject_file_path = str(mock_file)
 ctrl._inject_mode = "skeleton"
 ctrl._update_inject_preview()
 
 # Skeleton should contain signatures and docstrings but not bodies
 assert "def foo():" in ctrl._inject_preview
 assert "class Bar:" in ctrl._inject_preview
 assert 'print("hello")' not in ctrl._inject_preview
 assert "pass" not in ctrl._inject_preview
 assert '"""Foo docstring"""' in ctrl._inject_preview

def test_update_inject_preview_full(tmp_path):
 ctrl = AppController()
 mock_file = tmp_path / "mock.py"
 content = "line 1\n" * 10
 mock_file.write_text(content)
 
 ctrl._inject_file_path = str(mock_file)
 ctrl._inject_mode = "full"
 ctrl._update_inject_preview()
 
 assert ctrl._inject_preview.strip() == content.strip()

def test_update_inject_preview_truncation(tmp_path):
 ctrl = AppController()
 mock_file = tmp_path / "large.py"
 content = "\n".join([f"line {i}" for i in range(1000)])
 mock_file.write_text(content)
 
 ctrl._inject_file_path = str(mock_file)
 ctrl._inject_mode = "full"
 ctrl._update_inject_preview()
 
 lines = ctrl._inject_preview.splitlines()
 # It should be 500 lines + 1 for truncated message
 assert len(lines) == 501
 assert "... (truncated)" in lines[-1]
 # The 500th line is "line 499" (starting from 0)
 assert "line 499" in lines[499]
 # The 501st line should be "line 500", but it should be replaced by truncation msg
 assert "line 500" not in ctrl._inject_preview
