"""
Tests for the CLI scripts.
"""

from pathlib import Path
import subprocess
from subprocess import PIPE

from chives import cli  # noqa: F401


class TestConvertSrtToVtt:
    """
    Tests for the `srt-to-vtt` command.
    """

    def test_no_args(self) -> None:
        """
        If you don't supply any arguments, you get an error.
        """
        proc = subprocess.Popen(["srt-to-vtt"], stdout=PIPE, stderr=PIPE, text=True)
        stdout, stderr = proc.communicate(timeout=1)

        assert proc.returncode == 1
        assert stdout == ""
        assert stderr == "Usage: srt-to-vtt <SRT_PATH>...\n"

    def test_multiple_args(self, tmp_path: Path) -> None:
        """
        If you supply multiple SRT paths, they all get converted to VTT.
        """
        srt_path1 = tmp_path / "example1.en.srt"
        srt_path1.write_text(
            "1\n00:00:01,001 --> 00:00:10,010\nSomebody said the first thing\n"
        )

        srt_path2 = tmp_path / "example2.en.srt"
        srt_path2.write_text(
            "1\n00:02:00,002 --> 00:20:00,020\nSomebody else said the second thing\n"
        )

        proc = subprocess.Popen(
            ["srt-to-vtt", str(srt_path1), str(srt_path2)],
            stdout=PIPE,
            stderr=PIPE,
            text=True,
        )
        stdout, stderr = proc.communicate(timeout=1)

        assert proc.returncode == 0
        assert stdout == (f"{tmp_path}/example1.en.vtt\n{tmp_path}/example2.en.vtt\n")
        assert stderr == ""
