"""Tests for runtime environment setup helpers."""

import os

from r36s_dlna_browser.platform import runtime


def test_wayland_does_not_override_video_driver(monkeypatch):
    monkeypatch.setenv("WAYLAND_DISPLAY", "wayland-0")
    monkeypatch.setenv("DISPLAY", ":0")
    monkeypatch.delenv("SDL_VIDEODRIVER", raising=False)
    monkeypatch.setattr(runtime, "is_r36s", lambda: False)

    runtime.sdl_env_setup()

    assert "SDL_VIDEODRIVER" not in os.environ


def test_respects_existing_sdl_videodriver(monkeypatch):
    monkeypatch.setenv("WAYLAND_DISPLAY", "wayland-0")
    monkeypatch.setenv("DISPLAY", ":0")
    monkeypatch.setenv("SDL_VIDEODRIVER", "wayland")
    monkeypatch.setattr(runtime, "is_r36s", lambda: False)

    runtime.sdl_env_setup()

    assert os.environ["SDL_VIDEODRIVER"] == "wayland"


def test_r36s_sets_alsa_audio_driver(monkeypatch):
    monkeypatch.delenv("SDL_AUDIODRIVER", raising=False)
    monkeypatch.delenv("SDL_VIDEODRIVER", raising=False)
    monkeypatch.delenv("DISPLAY", raising=False)
    monkeypatch.delenv("WAYLAND_DISPLAY", raising=False)
    monkeypatch.setattr(runtime, "is_r36s", lambda: True)

    runtime.sdl_env_setup()

    assert os.environ["SDL_AUDIODRIVER"] == "alsa"
    assert os.environ["SDL_VIDEODRIVER"] == "kmsdrm"
