(executable
 (name test_runner)
 (modules test_runner)
 (libraries unix))

; Test executables

(executable
 (name test_resample)
 (modules test_resample)
 (libraries ffmpeg-av ffmpeg-swresample))

(executable
 (name test_info)
 (modules test_info)
 (libraries ffmpeg-av ffmpeg-swresample))

(executable
 (name test_subtitle_read)
 (modules test_subtitle_read)
 (libraries ffmpeg-av))

(executable
 (name test_unhandled_packet)
 (modules test_unhandled_packet)
 (libraries ffmpeg-av))

(executable
 (name normalize_line_endings)
 (modules normalize_line_endings))

; ============================================
; Standalone tests (no file dependencies)
; ============================================

(rule
 (alias test_list_filters)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/list_filters.exe))
 (action
  (run %{runner} "list_filters" %{exe})))

(rule
 (alias test_all_codecs)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/all_codecs.exe))
 (action
  (run %{runner} "all_codecs" %{exe})))

(rule
 (alias test_all_channel_layouts)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/all_channel_layouts.exe))
 (action
  (run %{runner} "all_channel_layouts" %{exe})))

(rule
 (alias test_all_bitstream_filters)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/all_bitstream_filters.exe))
 (action
  (run %{runner} "all_bitstream_filters" %{exe})))

(rule
 (alias test_hw_encode_device)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/hw_encode.exe))
 (action
  (run %{runner} "hw_encode_device" %{exe} nvenc.mp4 h264_nvenc device)))

(rule
 (alias test_hw_encode_frame)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/hw_encode.exe))
 (action
  (run %{runner} "hw_encode_frame" %{exe} nvenc.mp4 h264_nvenc frame)))

(rule
 (alias test_interrupt)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/interrupt.exe))
 (action
  (run %{runner} "interrupt" %{exe})))

(rule
 (alias test_resample)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe test_resample.exe))
 (action
  (run %{runner} "resample" %{exe})))

(rule
 (alias test_info)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe test_info.exe))
 (action
  (run %{runner} "info" %{exe})))

; ============================================
; Encode tests
; ============================================

(rule
 (alias test_encode_audio_flac)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/encode_audio.exe))
 (action
  (run %{runner} "encode_audio_flac" %{exe} A4.flac flac)))

(rule
 (alias test_encode_audio_mp2)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/encode_audio.exe))
 (action
  (run %{runner} "encode_audio_mp2" %{exe} A4.mp2 mp2)))

(rule
 (alias test_encode_video_ogg)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/encode_video.exe))
 (action
  (run %{runner} "encode_video_ogg" %{exe} video.ogg yuv420p libtheora)))

(rule
 (alias test_encode_video_webm)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/encode_video.exe))
 (action
  (run %{runner} "encode_video_webm" %{exe} video.webm yuva420p libvpx-vp9)))

(rule
 (alias test_encode_stream_flac)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/encode_stream.exe))
 (action
  (run %{runner} "encode_stream_flac" %{exe} S4.flac flac)))

(rule
 (alias test_encode_stream_mp2)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/encode_stream.exe))
 (action
  (run %{runner} "encode_stream_mp2" %{exe} S4.mp2 mp2)))

(rule
 (alias test_encoding)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/encoding.exe))
 (action
  (run %{runner} "encoding" %{exe} out.mkv aac mpeg4 ass)))

(rule
 (alias test_decode_stream_mp3)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/decode_stream.exe)
  (alias test_encode_audio_flac))
 (action
  (run %{runner} "decode_stream_mp3" %{exe} A4.flac A4.mp3 libmp3lame)))

(rule
 (alias test_decode_stream_ogg)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/decode_stream.exe)
  (alias test_encode_audio_mp2))
 (action
  (run %{runner} "decode_stream_ogg" %{exe} A4.mp2 A4.ogg libvorbis)))

(rule
 (alias test_audio_decoding_ogg)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/audio_decoding.exe)
  (alias test_decode_stream_ogg))
 (action
  (run %{runner} "audio_decoding_ogg" %{exe} A4.ogg ogg A4)))

(rule
 (alias test_audio_decoding_mkv)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/audio_decoding.exe)
  (alias test_encoding))
 (action
  (run %{runner} "audio_decoding_mkv" %{exe} out.mkv matroska out)))

; ============================================
; Remuxing test
; ============================================

(rule
 (alias test_remuxing)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/remuxing.exe)
  (alias test_encoding))
 (action
  (run %{runner} "remuxing" %{exe} out.mkv out_remuxed.mp4)))

; ============================================
; Transcode tests
; ============================================

(rule
 (alias test_transcode_aac)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/transcode_aac.exe)
  (alias test_decode_stream_ogg))
 (action
  (run %{runner} "transcode_aac" %{exe} A4.ogg A4_transcoded.mp4)))

(rule
 (alias test_transcoding)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/transcoding.exe)
  (alias test_encoding))
 (action
  (run %{runner} "transcoding" %{exe} out.mkv out_transcoded.mp4)))

; ============================================
; Decoding test
; ============================================

(rule
 (alias test_decoding)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/decoding.exe)
  (alias test_encoding))
 (action
  (run %{runner} "decoding" %{exe} out.mkv)))

; ============================================
; FPS test
; ============================================

(rule
 (alias test_fps)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/fps.exe)
  (alias test_encoding))
 (action
  (run %{runner} "fps" %{exe} out.mkv out_fps.mp4)))

; ============================================
; Aresample test
; ============================================

(rule
 (alias test_aresample)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/aresample.exe)
  (alias test_encoding))
 (action
  (run %{runner} "aresample" %{exe} out.mkv out_aresample.mp4)))

; ============================================
; Demuxing/decoding test
; ============================================

(rule
 (alias test_demuxing_decoding)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/demuxing_decoding.exe)
  (alias test_encoding))
 (action
  (run %{runner} "demuxing_decoding" %{exe} out.mkv vo.raw ao.raw)))

; ============================================
; Read metadata tests
; ============================================

(rule
 (alias test_read_metadata_flac)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/read_metadata.exe)
  (alias test_encode_audio_flac)
  (alias test_decode_stream_mp3))
 (action
  (run %{runner} "read_metadata_flac" %{exe} A4.flac flac A4.mp3 libmp3lame)))

(rule
 (alias test_read_metadata_mp2)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe ../examples/read_metadata.exe)
  (alias test_encode_audio_mp2)
  (alias test_decode_stream_ogg))
 (action
  (run %{runner} "read_metadata_mp2" %{exe} A4.mp2 mp2 A4.ogg libvorbis)))

; ============================================
; Combined CI test alias (runs all tests)
; ============================================

(alias
 (name citest)
 (package ffmpeg)
 (deps
  (alias test_list_filters)
  (alias test_all_codecs)
  (alias test_all_channel_layouts)
  (alias test_all_bitstream_filters)
  (alias test_hw_encode_device)
  (alias test_hw_encode_frame)
  (alias test_encode_audio_flac)
  (alias test_encode_audio_mp2)
  (alias test_encode_video_ogg)
  (alias test_encode_video_webm)
  (alias test_encode_stream_flac)
  (alias test_encode_stream_mp2)
  (alias test_encoding)
  (alias test_decode_stream_mp3)
  (alias test_decode_stream_ogg)
  (alias test_audio_decoding_ogg)
  (alias test_audio_decoding_mkv)
  (alias test_remuxing)
  (alias test_transcode_aac)
  (alias test_transcoding)
  (alias test_decoding)
  (alias test_fps)
  (alias test_aresample)
  (alias test_demuxing_decoding)
  (alias test_read_metadata_flac)
  (alias test_read_metadata_mp2)
  (alias test_interrupt)
  (alias test_resample)
  (alias test_info)
  (alias test_subtitle_read)
  (alias test_subtitle_remux)
  (alias test_unhandled_packet)))

; ============================================
; Subtitle tests
; ============================================

(rule
 (targets test_with_subs.mkv)
 (deps fixtures/sample.srt)
 (action
  (run
   ffmpeg
   -y
   -f
   lavfi
   -i
   "color=c=blue:s=320x240:d=25"
   -f
   lavfi
   -i
   "anullsrc=r=44100:cl=stereo:d=25"
   -i
   %{deps}
   -c:v
   mpeg4
   -c:a
   aac
   -c:s
   srt
   -shortest
   %{targets})))

(rule
 (alias test_subtitle_read)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe test_subtitle_read.exe)
  test_with_subs.mkv)
 (action
  (run %{runner} "subtitle_read" %{exe} test_with_subs.mkv)))

(rule
 (alias test_unhandled_packet)
 (package ffmpeg)
 (deps
  (:runner test_runner.exe)
  (:exe test_unhandled_packet.exe)
  test_with_subs.mkv)
 (action
  (run %{runner} "unhandled_packet" %{exe} test_with_subs.mkv)))

(rule
 (targets raw_remuxed_subs.srt)
 (deps
  (:exe ../examples/subtitle_remux.exe)
  test_with_subs.mkv)
 (action
  (run %{exe} test_with_subs.mkv %{targets} subrip)))

(rule
 (targets remuxed_subs.srt)
 (deps
  (:exe normalize_line_endings.exe)
  (:input raw_remuxed_subs.srt))
 (action
  (with-stdout-to
   %{targets}
   (run %{exe} %{input}))))

(rule
 (alias test_subtitle_remux)
 (package ffmpeg)
 (deps remuxed_subs.srt fixtures/sample.srt)
 (action
  (diff fixtures/sample.srt remuxed_subs.srt)))
