import pytest
from unittest.mock import patch
from typing import Any
from src.gui_2 import App


@pytest.fixture
def app_instance() -> Any:
 with (
  patch("src.app_controller.AppController.load_config", return_value={"ai": {}, "projects": {}}),
  patch("src.app_controller.AppController.save_config"),
  patch("src.gui_2.project_manager"),
  patch("src.app_controller.project_manager") as mock_pm,
  patch("src.gui_2.session_logger"),
  patch("src.gui_2.immapp.run"),
  patch("src.app_controller.AppController._load_active_project"),
  patch("src.app_controller.AppController._fetch_models"),
  patch.object(App, "_load_fonts"),
  patch.object(App, "_post_init"),
  patch("src.app_controller.AppController._prune_old_logs"),
  patch("src.app_controller.AppController.start_services"),
  patch("src.app_controller.AppController._init_ai_and_hooks"),
 ):
  app = App()
  app.project = {}
  app.ui_files_base_dir = "."
  yield app, mock_pm


def test_mma_dashboard_refresh(app_instance: Any) -> None:
 app, mock_pm = app_instance
 mock_tracks = [
  {
   "id": "track_1",
   "title": "Track 1",
   "status": "new",
   "complete": 0,
   "total": 0,
   "progress": 0.0,
  },
  {
   "id": "track_2",
   "title": "Track 2",
   "status": "new",
   "complete": 0,
   "total": 0,
   "progress": 0.0,
  },
 ]
 mock_pm.get_all_tracks.return_value = mock_tracks
 app._refresh_from_project()
 assert hasattr(app, "tracks"), "App instance should have a 'tracks' attribute"
 assert app.tracks == mock_tracks
 assert len(app.tracks) == 2
 assert app.tracks[0]["id"] == "track_1"
 assert app.tracks[1]["id"] == "track_2"
 mock_pm.get_all_tracks.assert_called_with(app.active_project_root)


def test_mma_dashboard_initialization_refresh(app_instance: Any) -> None:
 app, mock_pm = app_instance
 mock_tracks = [
  {
   "id": "init_track",
   "title": "Initial Track",
   "status": "new",
   "complete": 0,
   "total": 0,
   "progress": 0.0,
  }
 ]
 mock_pm.get_all_tracks.return_value = mock_tracks
 app._refresh_from_project()
 assert app.tracks == mock_tracks
 assert app.tracks[0]["id"] == "init_track"
