From c67c44bac27e84b810152ec13216eae39b780d66 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Fri, 25 Sep 2026 19:21:48 +0200 Subject: [PATCH] add test for encode cover --- src/lib/vorbis.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++++ tests/vorbis.bats | 26 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/lib/vorbis.sh b/src/lib/vorbis.sh index f25fc56..4a99d5d 100644 --- a/src/lib/vorbis.sh +++ b/src/lib/vorbis.sh @@ -28,4 +28,53 @@ vorbis_check_programs() || return $? } +vorbis_write_number_to_byte() +{ + local number="$1" + local hex_number= + hex_number=$(printf "%08x" "$number") + + for i in {0..6..2}; do + printf "%b" "\x${hex_number:$i:2}" + done +} + +vorbis_cover_to_metadata() +{ + local cover="$1" + local covermetadata="${cover/.jpg/.txt}" + local filesize=$(wc --bytes "$cover") + filesize=${filesize/ */} + + { + # picture typ (jpg) + vorbis_write_number_to_byte "3" + # mime type (image/jpeg) + vorbis_write_number_to_byte "10" + printf "%s" "image/jpeg" + # description length (zero) + vorbis_write_number_to_byte "0" + # width (zero) + vorbis_write_number_to_byte "0" + # heigth (zero) + vorbis_write_number_to_byte "0" + # color depth (zero) + vorbis_write_number_to_byte "0" + # color count (zero) + vorbis_write_number_to_byte "0" + # file size + vorbis_write_number_to_byte "$filesize" + cat "$cover" + } > "$covermetadata" +} + +vorbis_encode() +{ + local work_path="$1" + find "$work_path" -name '*.jpg' \ + | sort \ + | parallel --will-cite --ungroup \ + vorbis_cover_to_metadata {} +} + vorbis_check_programs || return $? \ No newline at end of file diff --git a/tests/vorbis.bats b/tests/vorbis.bats index 4dc23d1..9073924 100755 --- a/tests/vorbis.bats +++ b/tests/vorbis.bats @@ -3,9 +3,14 @@ setup() { bats_load_library 'bats-support' bats_load_library 'bats-assert' + mkdir --parents "build/tests/output" source "src/lib/vorbis.sh" } +teardown() { + rm --recursive --force "build" +} + # bats test_tags=vorbis:check_programs @test "failed: check programs missing flac" { command() { @@ -64,4 +69,25 @@ setup() { assert_failure assert_line --index 0 "❌ ERROR: command parallel not found" +} + +# bats test_tags=vorbis:covermetadata +@test "success: cover to metdata file" { + echo "testfile" > "build/tests/output/test.jpg" + { + printf "\x00\x00\x00\x03" + printf "\x00\x00\x00\x0a" + printf "image/jpeg" + printf "\x00\x00\x00\x00" + printf "\x00\x00\x00\x00" + printf "\x00\x00\x00\x00" + printf "\x00\x00\x00\x00" + printf "\x00\x00\x00\x00" + printf "\x00\x00\x00\x09" + printf "testfile\n" + } > "build/tests/output/expectedcover.txt" + + run vorbis_cover_to_metadata "build/tests/output/test.jpg" + + diff "build/tests/output/test.txt" "build/tests/output/expectedcover.txt" } \ No newline at end of file -- 2.47.3