"""Tests for the executable application entry point."""

from types import SimpleNamespace

import pytest

from transcription import __main__ as entrypoint


@pytest.mark.unit
def test_main_passes_constructed_app_to_uvicorn(monkeypatch):
    """Non-reload execution keeps the parsed settings instance in the app."""
    settings = SimpleNamespace(host="127.0.0.1", port=8123, log_level="debug", reload=False)
    application = object()
    captured = {}

    def create_app(*, settings: object) -> object:
        assert settings is expected_settings
        return application

    expected_settings = settings
    monkeypatch.setattr(entrypoint, "parse_cli_settings", lambda: settings)
    monkeypatch.setattr(entrypoint, "create_app", create_app)
    monkeypatch.setattr(
        entrypoint.uvicorn,
        "run",
        lambda app, **kwargs: captured.update(application=app, **kwargs),
    )

    entrypoint.main()

    assert captured == {
        "application": application,
        "factory": False,
        "host": "127.0.0.1",
        "port": 8123,
        "log_level": "debug",
        "reload": False,
    }


@pytest.mark.unit
def test_main_uses_cli_factory_for_reload(monkeypatch):
    """Reload execution gives Uvicorn an importable CLI-aware factory."""
    settings = SimpleNamespace(host="127.0.0.1", port=8123, log_level="info", reload=True)
    captured = {}
    monkeypatch.setattr(entrypoint, "parse_cli_settings", lambda: settings)
    monkeypatch.setattr(
        entrypoint.uvicorn,
        "run",
        lambda app, **kwargs: captured.update(application=app, **kwargs),
    )

    entrypoint.main()

    assert captured["application"] == "transcription.__main__:create_cli_app"
    assert captured["factory"] is True
    assert captured["reload"] is True
