180 lines
6.7 KiB
Python
180 lines
6.7 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def test_default_data_root_uses_windows_known_folder_without_environment(tmp_path: Path, monkeypatch) -> None:
|
|
from dx_productivity.trial_task_center import application
|
|
|
|
known_folder = tmp_path / "Local App Data"
|
|
monkeypatch.delenv("LOCALAPPDATA", raising=False)
|
|
monkeypatch.setattr(application, "_windows_local_app_data", lambda: known_folder)
|
|
|
|
assert application.resolve_data_root(None) == (
|
|
known_folder / "DxProductivity" / "trial-task-center"
|
|
).resolve()
|
|
|
|
|
|
def test_frozen_application_keeps_data_beside_the_executable(tmp_path: Path, monkeypatch) -> None:
|
|
from dx_productivity.trial_task_center import application
|
|
|
|
executable = tmp_path / "DxPartReplace" / "DxPartReplace.exe"
|
|
monkeypatch.setattr(application.sys, "frozen", True, raising=False)
|
|
monkeypatch.setattr(application.sys, "executable", str(executable))
|
|
|
|
assert application.resolve_data_root(None) == (executable.parent / "data").resolve()
|
|
|
|
|
|
def test_explicit_data_root_still_overrides_frozen_portable_default(tmp_path: Path, monkeypatch) -> None:
|
|
from dx_productivity.trial_task_center import application
|
|
|
|
monkeypatch.setattr(application.sys, "frozen", True, raising=False)
|
|
monkeypatch.setattr(application.sys, "executable", str(tmp_path / "app" / "DxPartReplace.exe"))
|
|
override = tmp_path / "override"
|
|
|
|
assert application.resolve_data_root(override) == override.resolve()
|
|
|
|
|
|
def test_main_configures_logging_before_qapplication(tmp_path: Path, monkeypatch) -> None:
|
|
"""Moving logging after Qt creation would lose startup and recovery diagnostics."""
|
|
from dx_productivity.trial_task_center import application
|
|
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
|
|
|
events: list[str] = []
|
|
context = configure_logging(tmp_path)
|
|
|
|
class Store:
|
|
path = tmp_path / "task-center.db"
|
|
|
|
def __init__(self, path: Path) -> None:
|
|
assert events == ["logging"]
|
|
self.path = path
|
|
|
|
def close(self) -> None:
|
|
events.append("closed")
|
|
|
|
class Application:
|
|
def __init__(self, _argv: list[str]) -> None:
|
|
assert events == ["logging"]
|
|
|
|
def exec(self) -> int:
|
|
return 0
|
|
|
|
class Service:
|
|
def __init__(self, _store, _context) -> None:
|
|
assert events == ["logging"]
|
|
|
|
class Window:
|
|
def __init__(self, _service) -> None:
|
|
events.append("window")
|
|
|
|
def show(self) -> None:
|
|
events.append("shown")
|
|
|
|
monkeypatch.setattr(application, "configure_logging", lambda _root: events.append("logging") or context)
|
|
monkeypatch.setattr(application, "TaskStore", Store)
|
|
monkeypatch.setattr(application, "TaskCenterService", Service)
|
|
monkeypatch.setattr(application, "QApplication", Application)
|
|
monkeypatch.setattr(application, "_load_window", lambda: Window)
|
|
|
|
assert application.main(["--data-root", str(tmp_path)]) == 0
|
|
assert events == ["logging", "window", "shown", "closed"]
|
|
assert f"version={application.VERSION}" in context.application_log_path.read_text(encoding="utf-8")
|
|
|
|
|
|
def test_smoke_test_closes_window_with_zero_delay_timer_and_reopens_database(tmp_path: Path, monkeypatch) -> None:
|
|
"""A packaged smoke run must exit cleanly and leave its SQLite database reusable."""
|
|
from dx_productivity.trial_task_center import application
|
|
|
|
calls: list[object] = []
|
|
|
|
class Application:
|
|
def __init__(self, _argv: list[str]) -> None:
|
|
calls.append("application")
|
|
|
|
def quit(self) -> None:
|
|
calls.append("quit")
|
|
|
|
def exec(self) -> int:
|
|
return 0
|
|
|
|
class Timer:
|
|
@staticmethod
|
|
def singleShot(delay: int, callback) -> None:
|
|
calls.append(delay)
|
|
callback()
|
|
|
|
class Window:
|
|
def __init__(self, _service) -> None:
|
|
calls.append("window")
|
|
|
|
def show(self) -> None:
|
|
calls.append("shown")
|
|
|
|
def close(self) -> None:
|
|
calls.append("closed")
|
|
|
|
monkeypatch.setattr(application, "QApplication", Application)
|
|
monkeypatch.setattr(application, "QTimer", Timer)
|
|
monkeypatch.setattr(application, "_load_window", lambda: Window)
|
|
|
|
arguments = ["--smoke-test", "--data-root", str(tmp_path)]
|
|
assert application.main(arguments) == 0
|
|
assert application.main(arguments) == 0
|
|
assert calls == [
|
|
"application", "window", "shown", 0, "closed", "quit",
|
|
"application", "window", "shown", 0, "closed", "quit",
|
|
]
|
|
assert (tmp_path / "task-center.db").exists()
|
|
|
|
|
|
def test_fatal_startup_exception_is_logged_with_traceback_and_nonzero_exit(tmp_path: Path, monkeypatch) -> None:
|
|
"""A database startup crash must be diagnosable without presenting a half-built window."""
|
|
from dx_productivity.trial_task_center import application
|
|
|
|
def fail_store(_path: Path):
|
|
raise RuntimeError("database unavailable")
|
|
|
|
monkeypatch.setattr(application, "TaskStore", fail_store)
|
|
|
|
assert application.main(["--data-root", str(tmp_path)]) == 1
|
|
application_log = (tmp_path / "logs" / "application.log").read_text(encoding="utf-8")
|
|
assert "RuntimeError: database unavailable" in application_log
|
|
assert "operation=startup" in application_log
|
|
|
|
|
|
def test_native_helper_mode_skips_database_logging_and_qt(tmp_path: Path, monkeypatch) -> None:
|
|
"""The helper subprocess must attach to DxDesigner without starting another GUI shell."""
|
|
from dx_productivity.trial_task_center import application, native_helper
|
|
|
|
snapshot = tmp_path / "snapshot.tsv"
|
|
snapshot.touch()
|
|
calls: list[Path] = []
|
|
monkeypatch.setattr(native_helper, "run", lambda path: calls.append(path) or 7)
|
|
monkeypatch.setattr(application, "configure_logging", lambda _root: (_ for _ in ()).throw(AssertionError("logging started")))
|
|
|
|
assert application.main(["--native-helper", "--snapshot", str(snapshot)]) == 7
|
|
assert calls == [snapshot]
|
|
|
|
|
|
def test_application_module_runs_as_a_script_for_the_frozen_entrypoint(tmp_path: Path) -> None:
|
|
"""Relative imports would make a PyInstaller script entrypoint fail before logging starts."""
|
|
source_root = Path(__file__).resolve().parents[2] / "src"
|
|
script = source_root / "dx_productivity" / "trial_task_center" / "application.py"
|
|
environment = os.environ | {"PYTHONPATH": str(source_root), "QT_QPA_PLATFORM": "offscreen"}
|
|
|
|
result = subprocess.run(
|
|
[sys.executable, str(script), "--smoke-test", "--data-root", str(tmp_path)],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=15,
|
|
env=environment,
|
|
check=False,
|
|
)
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
assert "application_clean_shutdown" in (tmp_path / "logs" / "application.log").read_text(encoding="utf-8")
|