# ABOUTME: Tests for session detection — parsing .desktop files from wayland/xsessions dirs.
# ABOUTME: Uses temporary directories to simulate session file locations.

from pathlib import Path

import pytest

from moongreet.sessions import Session, get_sessions


class TestGetSessions:
    """Tests for discovering available sessions from .desktop files."""

    def test_finds_wayland_session(self, tmp_path: Path) -> None:
        wayland_dir = tmp_path / "wayland-sessions"
        wayland_dir.mkdir()
        desktop = wayland_dir / "hyprland.desktop"
        desktop.write_text(
            "[Desktop Entry]\n"
            "Name=Hyprland\n"
            "Exec=Hyprland\n"
            "Type=Application\n"
        )

        sessions = get_sessions(wayland_dirs=[wayland_dir], xsession_dirs=[])

        assert len(sessions) == 1
        assert sessions[0].name == "Hyprland"
        assert sessions[0].exec_cmd == "Hyprland"
        assert sessions[0].session_type == "wayland"

    def test_finds_xsession(self, tmp_path: Path) -> None:
        x_dir = tmp_path / "xsessions"
        x_dir.mkdir()
        desktop = x_dir / "i3.desktop"
        desktop.write_text(
            "[Desktop Entry]\n"
            "Name=i3\n"
            "Exec=i3\n"
            "Type=Application\n"
        )

        sessions = get_sessions(wayland_dirs=[], xsession_dirs=[x_dir])

        assert len(sessions) == 1
        assert sessions[0].session_type == "x11"

    def test_finds_sessions_from_multiple_dirs(self, tmp_path: Path) -> None:
        wayland_dir = tmp_path / "wayland-sessions"
        wayland_dir.mkdir()
        (wayland_dir / "sway.desktop").write_text(
            "[Desktop Entry]\nName=Sway\nExec=sway\n"
        )

        x_dir = tmp_path / "xsessions"
        x_dir.mkdir()
        (x_dir / "openbox.desktop").write_text(
            "[Desktop Entry]\nName=Openbox\nExec=openbox-session\n"
        )

        sessions = get_sessions(wayland_dirs=[wayland_dir], xsession_dirs=[x_dir])

        names = {s.name for s in sessions}
        assert names == {"Sway", "Openbox"}

    def test_returns_empty_for_no_sessions(self, tmp_path: Path) -> None:
        empty = tmp_path / "empty"

        sessions = get_sessions(wayland_dirs=[empty], xsession_dirs=[empty])

        assert sessions == []

    def test_skips_files_without_name(self, tmp_path: Path) -> None:
        wayland_dir = tmp_path / "wayland-sessions"
        wayland_dir.mkdir()
        (wayland_dir / "broken.desktop").write_text(
            "[Desktop Entry]\nExec=something\n"
        )

        sessions = get_sessions(wayland_dirs=[wayland_dir], xsession_dirs=[])

        assert sessions == []

    def test_skips_files_without_exec(self, tmp_path: Path) -> None:
        wayland_dir = tmp_path / "wayland-sessions"
        wayland_dir.mkdir()
        (wayland_dir / "noexec.desktop").write_text(
            "[Desktop Entry]\nName=NoExec\n"
        )

        sessions = get_sessions(wayland_dirs=[wayland_dir], xsession_dirs=[])

        assert sessions == []

    def test_handles_exec_with_arguments(self, tmp_path: Path) -> None:
        wayland_dir = tmp_path / "wayland-sessions"
        wayland_dir.mkdir()
        (wayland_dir / "sway.desktop").write_text(
            "[Desktop Entry]\nName=Sway\nExec=sway --config /etc/sway/config\n"
        )

        sessions = get_sessions(wayland_dirs=[wayland_dir], xsession_dirs=[])

        assert sessions[0].exec_cmd == "sway --config /etc/sway/config"
