|
| 1 | +package cr_api_websocket_proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "azul3d.org/engine/audio" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAudioBufferPools(t *testing.T) { |
| 14 | + t.Run("samples buffer pool", func(t *testing.T) { |
| 15 | + samples := audioSamplesBufferPool.Get() |
| 16 | + if samples == nil { |
| 17 | + t.Error("expected non-nil samples buffer") |
| 18 | + } |
| 19 | + if got := len(samples); got != samplesPerChunk { |
| 20 | + t.Errorf("samples buffer length = %v, want %v", got, samplesPerChunk) |
| 21 | + } |
| 22 | + |
| 23 | + audioSamplesBufferPool.Put(samples) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("bytes buffer pool", func(t *testing.T) { |
| 27 | + buffer := audioBytesBufferPool.Get() |
| 28 | + if buffer == nil { |
| 29 | + t.Error("expected non-nil bytes buffer") |
| 30 | + } |
| 31 | + if got := len(buffer); got != bytesPerChunk { |
| 32 | + t.Errorf("bytes buffer length = %v, want %v", got, bytesPerChunk) |
| 33 | + } |
| 34 | + |
| 35 | + audioBytesBufferPool.Put(buffer) |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +type mockBody struct { |
| 40 | + *bytes.Buffer |
| 41 | +} |
| 42 | + |
| 43 | +func (m mockBody) Close() error { |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func TestFlacDecoder_InvalidData(t *testing.T) { |
| 48 | + t.Run("invalid audio data", func(t *testing.T) { |
| 49 | + req := &http.Request{ |
| 50 | + Body: mockBody{bytes.NewBuffer([]byte("invalid audio data"))}, |
| 51 | + } |
| 52 | + |
| 53 | + decoder, err := NewAudioDecoder(req) |
| 54 | + if err == nil { |
| 55 | + t.Error("expected error for invalid audio data") |
| 56 | + } |
| 57 | + if decoder != nil { |
| 58 | + t.Error("expected nil decoder for invalid audio data") |
| 59 | + } |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func TestFlacDecoder_ValidData(t *testing.T) { |
| 64 | + req := &http.Request{ |
| 65 | + Body: mockBody{bytes.NewBuffer(readTestFile(t, "testdata/16khz.flac"))}, |
| 66 | + } |
| 67 | + |
| 68 | + decoder, err := NewAudioDecoder(req) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("failed to create decoder: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + // Verify decoder config |
| 74 | + config := decoder.Config() |
| 75 | + if config.SampleRate != expectedSampleRate { |
| 76 | + t.Errorf("sample rate = %v, want %v", config.SampleRate, expectedSampleRate) |
| 77 | + } |
| 78 | + |
| 79 | + // Try reading some samples |
| 80 | + samples := make(audio.Int16, 1024) |
| 81 | + n, err := decoder.Read(samples) |
| 82 | + if err != nil && err != io.EOF { |
| 83 | + t.Errorf("failed to read samples: %v", err) |
| 84 | + } |
| 85 | + if n == 0 { |
| 86 | + t.Error("expected to read some samples") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestFlacDecoder_InvalidSampleRate(t *testing.T) { |
| 91 | + req := &http.Request{ |
| 92 | + Body: mockBody{bytes.NewBuffer(readTestFile(t, "testdata/8khz.flac"))}, |
| 93 | + } |
| 94 | + |
| 95 | + decoder, err := NewAudioDecoder(req) |
| 96 | + if decoder != nil { |
| 97 | + t.Error("expected nil decoder for invalid sample rate") |
| 98 | + } |
| 99 | + if err == nil { |
| 100 | + t.Error("expected error for invalid sample rate") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Helper to read test file contents |
| 105 | +func readTestFile(t *testing.T, path string) []byte { |
| 106 | + t.Helper() |
| 107 | + data, err := os.ReadFile(path) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("failed to read test file %s: %v", path, err) |
| 110 | + } |
| 111 | + return data |
| 112 | +} |
0 commit comments