-- codecs/audio/ApeTag tests: hand-made APEv1 / APEv2 tags. The real files are in tests/audio/wavpack.luau and ape.luau. local T = require("@tests/T") local ApeTag = require("@rosetta/codecs/audio/ApeTag") local t = T.suite("apetag") local function u32(v: number): string return string.char(v % 256, (v // 256) % 256, (v // 65536) % 256, (v // 16777216) % 256) end local function tag(items: { any }, version: number, header: boolean): string local body = {} for _, it in items do table.insert(body, u32(#it[2]) .. u32(it[3] or 0) .. it[1] .. "\0" .. it[2]) end local b = table.concat(body) local flags = if header then 0x80000000 else 0 local function block(isHeader: boolean): string return "APETAGEX" .. u32(version) .. u32(#b + 32) .. u32(#items) .. u32(flags + (if isHeader then 0x20000000 else 0)) .. string.rep("\0", 8) end return (if header then block(true) else "") .. b .. block(false) end do local s = "audio data" .. tag({ { "Title", "Hello" }, { "Artist", "A\0B" }, { "Year", "2026" }, { "Track", "3/9" }, { "Cover Art (Front)", "f.png\0PNG", 2 }, { "Album Artist", "Band" } }, 2000, true) local b = buffer.fromstring(s) local tags, info = ApeTag.find(b, 0, #s) t:check(tags and info, "v2 found") t:eq(tags.title, "Hello", "title") t:eq(tags.artist, "A; B", "multi value") t:eq(tags.date, "2026", "year -> date") t:eq(tags.track, "3/9", "track") t:eq(tags.albumartist, "Band", "album artist") t:eq(tags["cover art (front)"], nil, "binary item not a tag") t:eq(info.start, 10, "start at the header") t:eq(info.stop, #s, "stop") t:eq(info.version, 2000, "version") t:eq(#info.items, 6, "items") t:eq(info.items[5].kind, "binary", "binary kind") t:eq(info.items[5].value, "f.png\0PNG", "binary value") local s2 = s .. "TAG" .. string.rep("x", 125) local tags2, info2 = ApeTag.find(buffer.fromstring(s2), 0, #s2) t:check(tags2 and tags2.title == "Hello" and info2.stop == #s, "before ID3v1") local s3 = "zz" .. s .. "yyy" local tags3, info3 = ApeTag.find(buffer.fromstring(s3), 2, 2 + #s) t:check(tags3 and tags3.title == "Hello" and info3.start == 12, "sub-range") -- the start bounds the header local tags4, info4 = ApeTag.find(b, 50, #s) t:check(tags4 == nil and info4 == nil, "items before start") end do local s = tag({ { "Title", "Old" } }, 1000, false) local tags, info = ApeTag.find(buffer.fromstring(s), 0, #s) t:check(tags and tags.title == "Old" and info.start == 0 and info.version == 1000, "v1") t:eq(ApeTag.find(buffer.fromstring("no tag here at all, not even close to it"), 0, 40), nil, "none") end -- malformed: a huge item length, a huge count, a size larger than the file do local s = tag({ { "Title", "Hello" } }, 2000, false) local bad = string.sub(s, 1, 0) .. u32(0x7FFFFFFF) .. string.sub(s, 5) local tags = ApeTag.find(buffer.fromstring(bad), 0, #bad) t:check(tags and tags.title == nil, "bad item length stops") local big = string.sub(s, 1, #s - 32 + 12) .. u32(1e9) .. string.sub(s, #s - 32 + 17) t:eq(ApeTag.find(buffer.fromstring(big), 0, #big), nil, "size past the start") end local samples = { buffer.fromstring("data" .. tag({ { "Title", "Hello" }, { "Artist", "X" } }, 2000, true)), buffer.fromstring(tag({ { "A", "b" } }, 1000, false) .. "TAG" .. string.rep("\0", 125)), } t:fuzz("find", function(b: buffer) return ApeTag.find(b, 0, buffer.len(b)) end, samples, { mutations = 400 }) t:done()