// VideoProbe: video story and video message metadata (FR-30, FR-51). Real ffprobe/ffmpeg are used when installed.
#include <cstdlib>
#include <filesystem>
#include "tttest.hpp"
#include "mologram/ProcessRunner.hpp"
#include "mologram/tapi/VideoProbe.hpp"
#include "tapi/tl/types.hpp"
using namespace mologram;
TEST_CASE("video paths: the extensions Telegram treats as video", "[tapi][video][unit]") {
CHECK(VideoProbe::isVideoPath("/x/clip.MP4"));
CHECK(VideoProbe::isVideoPath("a.mov"));
CHECK(VideoProbe::isVideoPath("a.webm"));
CHECK(!VideoProbe::isVideoPath("a.jpg"));
CHECK(!VideoProbe::isVideoPath("noextension"));
}
TEST_CASE("an unreadable or missing file gives an unknown probe and no attribute, never a guess", "[tapi][video][unit]") {
VideoInfo v = VideoProbe::probe("/nonexistent/clip.mp4");
CHECK(!v.known);
CHECK(VideoProbe::attribute(v) == nullptr);
}
TEST_CASE("a real video is probed: size, length, streaming attribute", "[tapi][video][integration]") {
std::string out;
if (ProcessRunner::capture({"ffmpeg", "-version"}, out) != 0) SKIP_TEST("ffmpeg is not installed");
char t[] = "/tmp/mologram-video-XXXXXX";
std::string dir = mkdtemp(t);
std::string path = dir + "/t.mp4";
std::string log;
int rc = ProcessRunner::capture({"ffmpeg", "-v", "error", "-f", "lavfi", "-i", "color=c=blue:s=64x48:d=1:r=10", "-pix_fmt", "yuv420p", path}, log);
if (rc != 0) { std::filesystem::remove_all(dir); SKIP_TEST("ffmpeg could not make the test video"); }
VideoInfo v = VideoProbe::probe(path);
CHECK(v.known);
CHECK_EQ(v.width, 64);
CHECK_EQ(v.height, 48);
CHECK(v.duration > 0.5 && v.duration < 1.5);
auto attr = tapi::tl::as_tl<tapi::tl::types::DocumentAttributeVideo>(VideoProbe::attribute(v));
REQUIRE(attr != nullptr);
CHECK(attr->supports_streaming);
CHECK_EQ(attr->w, 64);
std::filesystem::remove_all(dir);
}