#!/usr/bin/env python

import pytest

from pyseq import lss, scopy, sdiff, sfind, smove, sremove, sstat, stree


def _raise_keyboard_interrupt(*args, **kwargs):
    raise KeyboardInterrupt()


@pytest.mark.parametrize(
    ("module", "argv", "patch_target"),
    [
        (lss, ["lss", "."], "get_sequences"),
        (sfind, ["sfind", "."], "walk_and_collect_sequences"),
        (stree, ["stree"], "print_tree"),
        (sdiff, ["sdiff", "a.%04d.exr", "b.%04d.exr"], "resolve_sequence"),
        (sstat, ["sstat", "a.%04d.exr"], "resolve_sequence"),
    ],
)
def test_cli_main_handles_keyboard_interrupt(
    monkeypatch, capsys, tmp_path, module, argv, patch_target
):
    monkeypatch.chdir(tmp_path)
    monkeypatch.setattr(module, patch_target, _raise_keyboard_interrupt)
    monkeypatch.setattr("sys.argv", argv)

    assert module.main() == 1
    captured = capsys.readouterr()
    assert "stopping..." in captured.err


@pytest.mark.parametrize(("module", "command"), [(scopy, "scopy"), (smove, "smv")])
def test_copy_move_cli_main_handles_keyboard_interrupt(
    monkeypatch, capsys, tmp_path, module, command
):
    dest_dir = tmp_path / "dest"
    dest_dir.mkdir()

    monkeypatch.setattr(module, "resolve_sequence_reference", _raise_keyboard_interrupt)
    monkeypatch.setattr("sys.argv", [command, "a.%04d.exr", str(dest_dir)])

    assert module.main() == 1
    captured = capsys.readouterr()
    assert "stopping..." in captured.err


def test_srm_cli_main_handles_keyboard_interrupt(monkeypatch, capsys, tmp_path):
    monkeypatch.chdir(tmp_path)
    monkeypatch.setattr(
        sremove, "resolve_sequence_reference", _raise_keyboard_interrupt
    )
    monkeypatch.setattr("sys.argv", ["srm", "a.%04d.exr"])

    assert sremove.main() == 1
    captured = capsys.readouterr()
    assert "stopping..." in captured.err
