import os
from pathlib import Path

from mf.utils.file import FileResult
from mf.utils.search import get_result_by_index, save_search_results


def test_get_file_by_index_valid(tmp_path, monkeypatch):
    monkeypatch.setenv(
        "LOCALAPPDATA" if os.name == "nt" else "XDG_CACHE_HOME", str(tmp_path)
    )
    movie_path = tmp_path / "some_movie.mp4"
    movie_path.write_text("data")
    save_search_results("*", [FileResult(movie_path)])
    assert get_result_by_index(1).file == movie_path


def test_get_file_by_index_invalid(tmp_path, monkeypatch):
    monkeypatch.setenv(
        "LOCALAPPDATA" if os.name == "nt" else "XDG_CACHE_HOME", str(tmp_path)
    )
    movie_path = Path("/tmp/some_movie.mp4")
    save_search_results("*", [FileResult(movie_path)])
    import click
    import pytest

    with pytest.raises(click.exceptions.Exit) as exc:
        get_result_by_index(2)
    assert exc.value.exit_code == 1
