import sys
from configparser import ConfigParser
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

import install


def test_cinnamon_autostart_desktop_entry_is_templated_and_installed(tmp_path: Path) -> None:
    desktop_file = Path(__file__).resolve().parent.parent / "packaging" / "codex-account-switcher.desktop"

    parser = ConfigParser(interpolation=None)
    parser.read(desktop_file, encoding="utf-8")

    template_section = parser["Desktop Entry"]

    dest_bin = tmp_path / "bin"
    dest_apps = tmp_path / "applications"
    dest_icons = tmp_path / "icons"
    dest_apps.mkdir(parents=True, exist_ok=True)

    report = install.install(dest_bin=dest_bin, dest_apps=dest_apps, dest_icons=dest_icons)

    installed_desktop = dest_apps / "codex-account-switcher.desktop"
    installed_parser = ConfigParser(interpolation=None)
    installed_parser.read(installed_desktop, encoding="utf-8")
    installed_section = installed_parser["Desktop Entry"]
    expected_exec = f"python3 {dest_bin / 'systray_codex_switcher.py'}"

    assert template_section["Type"] == "Application"
    assert template_section["Name"] == "Systray AI"
    assert template_section["Exec"] == "python3 __SYSTRAY_CDX_SWITCHER__"
    assert template_section["Icon"] == "codex-account-switcher"
    assert template_section["X-GNOME-Autostart-enabled"] == "true"
    assert template_section["NoDisplay"] == "true"

    assert report.created == (
        dest_bin / "account_lock_cli.py",
        dest_bin / "cdx.py",
        dest_bin / "cld.py",
        dest_bin / "claudex.py",
        dest_bin / "gateway_account_cli.py",
        dest_bin / "systray_codex_switcher.py",
        dest_bin / "statusline.py",
        dest_bin / "account-lock",
        dest_bin / "cld",
        dest_bin / "claudex",
        dest_bin / "systray-gateway",
        dest_apps / "codex-account-switcher.desktop",
    )
    assert report.existing == ()
    assert installed_section["Type"] == "Application"
    assert installed_section["Name"] == "Systray AI"
    assert installed_section["Exec"] == expected_exec
    assert installed_section["Icon"] == "codex-account-switcher"
    assert installed_section["X-GNOME-Autostart-enabled"] == "true"
    assert installed_section["NoDisplay"] == "true"
