98 lines
3.1 KiB
Python
98 lines
3.1 KiB
Python
import pytest
|
|
from unittest.mock import MagicMock, patch
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# We can safely import gui_2 if we don't instantiate App without mocking its threads
|
|
import gui_2
|
|
from gui_2 import App
|
|
|
|
@pytest.fixture
|
|
@pytest.fixture
|
|
def mock_config(tmp_path: Path) -> Path:
|
|
config_path = tmp_path / "config.toml"
|
|
config_path.write_text("""[projects]
|
|
paths = []
|
|
active = ""
|
|
[ai]
|
|
provider = "gemini"
|
|
model = "model"
|
|
""", encoding="utf-8")
|
|
return config_path
|
|
|
|
@pytest.fixture
|
|
@pytest.fixture
|
|
def mock_project(tmp_path: Path) -> Path:
|
|
project_path = tmp_path / "project.toml"
|
|
project_path.write_text("""[project]
|
|
name = "test"
|
|
[discussion]
|
|
roles = ["User", "AI"]
|
|
active = "main"
|
|
[discussion.discussions.main]
|
|
history = []
|
|
""", encoding="utf-8")
|
|
return project_path
|
|
|
|
@pytest.fixture
|
|
@pytest.fixture
|
|
def app_instance(mock_config: Path, mock_project: Path, monkeypatch: pytest.MonkeyPatch) -> App:
|
|
monkeypatch.setattr("gui_2.CONFIG_PATH", mock_config)
|
|
with patch("project_manager.load_project") as mock_load, \
|
|
patch("session_logger.open_session"):
|
|
mock_load.return_value = {
|
|
"project": {"name": "test"},
|
|
"discussion": {"roles": ["User", "AI"], "active": "main", "discussions": {"main": {"history": []}}},
|
|
"files": {"paths": []},
|
|
"screenshots": {"paths": []}
|
|
}
|
|
# Mock the __init__ to do nothing, then set the fields we need manually
|
|
with patch.object(App, '__init__', lambda self: None):
|
|
app = App()
|
|
app.show_windows = {"Log Management": False}
|
|
app.ui_state = MagicMock()
|
|
app.ui_files_base_dir = "."
|
|
app.files = []
|
|
# Since we bypassed __init__, we need to bind the method manually
|
|
# but python allows calling it directly.
|
|
return app
|
|
|
|
def test_log_management_init(app_instance: App) -> None:
|
|
app = app_instance
|
|
assert "Log Management" in app.show_windows
|
|
assert app.show_windows["Log Management"] is False
|
|
assert hasattr(app, "_render_log_management")
|
|
assert callable(app._render_log_management)
|
|
|
|
def test_render_log_management_logic(app_instance: App) -> None:
|
|
app = app_instance
|
|
app.show_windows["Log Management"] = True
|
|
# Mock LogRegistry
|
|
with patch("gui_2.LogRegistry") as MockRegistry, \
|
|
patch("gui_2.imgui.begin") as mock_begin, \
|
|
patch("gui_2.imgui.begin_table") as mock_begin_table, \
|
|
patch("gui_2.imgui.text") as mock_text, \
|
|
patch("gui_2.imgui.end_table") as mock_end_table, \
|
|
patch("gui_2.imgui.end") as mock_end, \
|
|
patch("gui_2.imgui.push_style_color"), \
|
|
patch("gui_2.imgui.pop_style_color"), \
|
|
patch("gui_2.imgui.table_setup_column"), \
|
|
patch("gui_2.imgui.table_headers_row"), \
|
|
patch("gui_2.imgui.table_next_row"), \
|
|
patch("gui_2.imgui.table_next_column"), \
|
|
patch("gui_2.imgui.button"):
|
|
mock_reg = MockRegistry.return_value
|
|
mock_reg.data = {
|
|
"session_1": {
|
|
"start_time": "2023-01-01",
|
|
"whitelisted": False,
|
|
"metadata": {"reason": "test", "size_kb": 10, "message_count": 5}
|
|
}
|
|
}
|
|
mock_begin.return_value = (True, True)
|
|
mock_begin_table.return_value = True
|
|
app._render_log_management()
|
|
mock_begin.assert_called_with("Log Management", app.show_windows["Log Management"])
|
|
mock_begin_table.assert_called()
|
|
mock_text.assert_any_call("session_1")
|