package media import ( "bytes" "encoding/binary" "testing" ) func TestEncodeMonoWAVProducesBoundedInMemoryPCM16(t *testing.T) { pcm := bytes.Repeat([]byte{0x12, 0x34}, 3200) // 200 ms at 16 kHz. wav, durationMS, err := EncodeMonoWAV(pcm, int64(len(pcm)+44)) if err != nil { t.Fatal(err) } if durationMS != 200 || len(wav) != 44+len(pcm) || string(wav[:4]) != "RIFF" || string(wav[8:12]) != "WAVE" || string(wav[12:16]) != "fmt " || string(wav[36:40]) != "data" { t.Fatalf("in-memory recording does not contain the original PCM16: duration=%d bytes=%d", durationMS, len(wav)) } if binary.LittleEndian.Uint32(wav[4:8]) != uint32(len(wav)-8) || binary.LittleEndian.Uint16(wav[20:22]) != 1 || binary.LittleEndian.Uint16(wav[22:24]) != 1 || binary.LittleEndian.Uint32(wav[24:28]) != 16000 || binary.LittleEndian.Uint32(wav[28:32]) != 32000 || binary.LittleEndian.Uint16(wav[34:36]) != 16 || binary.LittleEndian.Uint32(wav[40:44]) != uint32(len(pcm)) || !bytes.Equal(wav[44:], pcm) { t.Fatal("WAV header or copied PCM differs from the 16-kHz mono original") } } func TestEncodeMonoWAVRejectsEmptyOddAndOverLimitAudio(t *testing.T) { for _, tc := range []struct { name string pcm []byte limit int64 }{ {"no recording", nil, 1024}, {"odd PCM16", []byte{1, 2, 3}, 1024}, {"no storage allowance", []byte{1, 2}, 44}, {"audio over bound", []byte{1, 2, 3, 4}, 47}, } { if _, _, err := EncodeMonoWAV(tc.pcm, tc.limit); err == nil { t.Fatalf("%s was fabricated as an uploadable WAV", tc.name) } } }