import shutil
from pathlib import Path

import pytest

from autoshorts.cli.demo_local_preview_video import main

pytestmark = pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="ffmpeg is required")


def test_demo_local_preview_video_cli_renders_mp4_and_prints_metadata(tmp_path, capsys):
    exit_code = main(["--out-dir", str(tmp_path)])

    captured = capsys.readouterr()
    output_path = tmp_path / "preview.mp4"
    concat_path = tmp_path / "preview.concat.txt"

    assert exit_code == 0
    assert output_path.exists()
    assert output_path.stat().st_size > 0
    assert concat_path.exists()
    assert "Preview video: " in captured.out
    assert str(output_path) in captured.out
    assert "Concat file: " in captured.out
    assert str(concat_path) in captured.out
    assert "Frame count: 5" in captured.out
    assert "Approval language: de" in captured.out
    assert "Side effects: write_concat, call_ffmpeg, write_mp4" in captured.out
    assert "External calls: ffmpeg" in captured.out
    assert "Script: ai_life_systems-ai-weekly-review-routine" in captured.out
    assert captured.err == ""
    assert "publish" not in captured.out.casefold()
    assert "post" not in captured.out.casefold()
