import pytest

from unittest.mock import patch
from nail.core.file_editor import FileEditor, MissingFilePathError


def test_missing_file_path_error():
    with pytest.raises(MissingFilePathError):
        FileEditor()


def test_exists(tmp_path):
    file_path = tmp_path / "test.txt"
    file_path.write_text("Test content")

    file_editor = FileEditor(file_path)
    assert file_editor.exists() is True

    non_existent_file = tmp_path / "non_existent.txt"
    file_editor = FileEditor(non_existent_file)
    assert file_editor.exists() is False


def test_content(tmp_path):
    file_path = tmp_path / "test.txt"
    file_path.write_text("Test content")

    file_editor = FileEditor(file_path)
    assert file_editor.content() == "Test content"


@patch("subprocess.call")
def test_open_editor(mock_call, tmp_path):
    file_path = tmp_path / "test.txt"
    file_path.write_text("Test content")

    file_editor = FileEditor(file_path)
    file_editor.open_editor()

    # Check if subprocess.call was called with the correct arguments
    mock_call.assert_called_once_with(["vim", file_path])


def test_apply_changes(tmp_path, monkeypatch, capsys):
    file_path = tmp_path / "test.txt"
    file_path.write_text("Original content")

    file_editor = FileEditor(file_path)

    # Mock input to return 'y' for confirmation
    monkeypatch.setattr("builtins.input", lambda _: "y")

    assert file_editor.apply_changes("New content") is True
    assert file_editor.content() == "New content"

    # Mock input to return 'n' for discard changes
    monkeypatch.setattr("builtins.input", lambda _: "n")

    assert file_editor.apply_changes("Another content") is False
    assert file_editor.content() == "New content"

    # Check if the diff is printed correctly
    captured = capsys.readouterr()
    assert "+Another content" in captured.out
    assert "-New content" in captured.out
