|
| 1 | +// audio_sources_test.go - Tests for audio source listing endpoints. |
| 2 | +package api |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/labstack/echo/v4" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/tphakala/birdnet-go/internal/audiocore" |
| 14 | + "github.com/tphakala/birdnet-go/internal/audiocore/engine" |
| 15 | + "github.com/tphakala/birdnet-go/internal/conf" |
| 16 | +) |
| 17 | + |
| 18 | +// setupAudioSourcesTestEnv creates a Controller with an AudioEngine whose |
| 19 | +// registry contains the given sources. Sources are registered directly in the |
| 20 | +// registry to avoid requiring real audio hardware or network streams. |
| 21 | +// The engine is stopped automatically when the test completes. |
| 22 | +func setupAudioSourcesTestEnv(t *testing.T, sources []*audiocore.SourceConfig) (*echo.Echo, *Controller) { |
| 23 | + t.Helper() |
| 24 | + |
| 25 | + ctx := t.Context() |
| 26 | + log := audiocore.GetLogger() |
| 27 | + eng := engine.New(ctx, &engine.Config{Logger: log}, nil) |
| 28 | + t.Cleanup(eng.Stop) |
| 29 | + |
| 30 | + // Register sources directly in the registry instead of using |
| 31 | + // engine.AddSource, which tries to start real hardware capture |
| 32 | + // for audio cards and FFmpeg for streams. |
| 33 | + registry := eng.Registry() |
| 34 | + for _, cfg := range sources { |
| 35 | + _, err := registry.Register(cfg) |
| 36 | + require.NoError(t, err) |
| 37 | + } |
| 38 | + |
| 39 | + e := echo.New() |
| 40 | + controller := &Controller{ |
| 41 | + Echo: e, |
| 42 | + Group: e.Group("/api/v2"), |
| 43 | + Settings: &conf.Settings{}, |
| 44 | + engine: eng, |
| 45 | + } |
| 46 | + return e, controller |
| 47 | +} |
| 48 | + |
| 49 | +func TestListAudioSources(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + t.Attr("component", "system") |
| 52 | + t.Attr("type", "integration") |
| 53 | + t.Attr("feature", "audio-sources") |
| 54 | + |
| 55 | + t.Run("No engine returns empty list", func(t *testing.T) { |
| 56 | + e := echo.New() |
| 57 | + controller := &Controller{ |
| 58 | + Echo: e, |
| 59 | + Group: e.Group("/api/v2"), |
| 60 | + Settings: &conf.Settings{}, |
| 61 | + } |
| 62 | + |
| 63 | + req := httptest.NewRequest(http.MethodGet, "/api/v2/system/audio/sources", http.NoBody) |
| 64 | + rec := httptest.NewRecorder() |
| 65 | + ctx := e.NewContext(req, rec) |
| 66 | + ctx.SetPath("/api/v2/system/audio/sources") |
| 67 | + |
| 68 | + err := controller.ListAudioSources(ctx) |
| 69 | + require.NoError(t, err) |
| 70 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 71 | + |
| 72 | + var resp AudioSourceListResponse |
| 73 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp)) |
| 74 | + assert.Empty(t, resp.Sources) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("Returns RTSP and audio card sources", func(t *testing.T) { |
| 78 | + sources := []*audiocore.SourceConfig{ |
| 79 | + { |
| 80 | + DisplayName: "Backyard Mic", |
| 81 | + Type: audiocore.SourceTypeAudioCard, |
| 82 | + ConnectionString: "hw:0,0", |
| 83 | + SampleRate: 48000, |
| 84 | + BitDepth: 16, |
| 85 | + Channels: 1, |
| 86 | + }, |
| 87 | + { |
| 88 | + DisplayName: "Front Camera", |
| 89 | + Type: audiocore.SourceTypeRTSP, |
| 90 | + ConnectionString: "rtsp://192.168.1.10:554/audio", |
| 91 | + SampleRate: 48000, |
| 92 | + BitDepth: 16, |
| 93 | + Channels: 1, |
| 94 | + }, |
| 95 | + } |
| 96 | + e, controller := setupAudioSourcesTestEnv(t, sources) |
| 97 | + |
| 98 | + req := httptest.NewRequest(http.MethodGet, "/api/v2/system/audio/sources", http.NoBody) |
| 99 | + rec := httptest.NewRecorder() |
| 100 | + ctx := e.NewContext(req, rec) |
| 101 | + ctx.SetPath("/api/v2/system/audio/sources") |
| 102 | + |
| 103 | + err := controller.ListAudioSources(ctx) |
| 104 | + require.NoError(t, err) |
| 105 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 106 | + |
| 107 | + var resp AudioSourceListResponse |
| 108 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp)) |
| 109 | + assert.Len(t, resp.Sources, 2) |
| 110 | + |
| 111 | + // Sorted by DisplayName |
| 112 | + assert.Equal(t, "Backyard Mic", resp.Sources[0].Name) |
| 113 | + assert.Equal(t, "audio_card", resp.Sources[0].Type) |
| 114 | + assert.Equal(t, "Front Camera", resp.Sources[1].Name) |
| 115 | + assert.Equal(t, "rtsp", resp.Sources[1].Type) |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +func TestListStreamSources(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + t.Attr("component", "streams") |
| 122 | + t.Attr("type", "integration") |
| 123 | + t.Attr("feature", "stream-sources") |
| 124 | + |
| 125 | + t.Run("No engine returns empty list", func(t *testing.T) { |
| 126 | + e := echo.New() |
| 127 | + controller := &Controller{ |
| 128 | + Echo: e, |
| 129 | + Group: e.Group("/api/v2"), |
| 130 | + Settings: &conf.Settings{}, |
| 131 | + } |
| 132 | + |
| 133 | + req := httptest.NewRequest(http.MethodGet, "/api/v2/streams/sources", http.NoBody) |
| 134 | + rec := httptest.NewRecorder() |
| 135 | + ctx := e.NewContext(req, rec) |
| 136 | + ctx.SetPath("/api/v2/streams/sources") |
| 137 | + |
| 138 | + err := controller.ListStreamSources(ctx) |
| 139 | + require.NoError(t, err) |
| 140 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 141 | + |
| 142 | + var resp AudioSourceListResponse |
| 143 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp)) |
| 144 | + assert.Empty(t, resp.Sources) |
| 145 | + }) |
| 146 | + |
| 147 | + t.Run("Filters to stream types only", func(t *testing.T) { |
| 148 | + sources := []*audiocore.SourceConfig{ |
| 149 | + { |
| 150 | + DisplayName: "Backyard Mic", |
| 151 | + Type: audiocore.SourceTypeAudioCard, |
| 152 | + ConnectionString: "hw:0,0", |
| 153 | + SampleRate: 48000, |
| 154 | + BitDepth: 16, |
| 155 | + Channels: 1, |
| 156 | + }, |
| 157 | + { |
| 158 | + DisplayName: "Front Camera", |
| 159 | + Type: audiocore.SourceTypeRTSP, |
| 160 | + ConnectionString: "rtsp://192.168.1.10:554/audio", |
| 161 | + SampleRate: 48000, |
| 162 | + BitDepth: 16, |
| 163 | + Channels: 1, |
| 164 | + }, |
| 165 | + { |
| 166 | + DisplayName: "HTTP Stream", |
| 167 | + Type: audiocore.SourceTypeHTTP, |
| 168 | + ConnectionString: "http://192.168.1.20:8000/stream", |
| 169 | + SampleRate: 48000, |
| 170 | + BitDepth: 16, |
| 171 | + Channels: 1, |
| 172 | + }, |
| 173 | + } |
| 174 | + e, controller := setupAudioSourcesTestEnv(t, sources) |
| 175 | + |
| 176 | + req := httptest.NewRequest(http.MethodGet, "/api/v2/streams/sources", http.NoBody) |
| 177 | + rec := httptest.NewRecorder() |
| 178 | + ctx := e.NewContext(req, rec) |
| 179 | + ctx.SetPath("/api/v2/streams/sources") |
| 180 | + |
| 181 | + err := controller.ListStreamSources(ctx) |
| 182 | + require.NoError(t, err) |
| 183 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 184 | + |
| 185 | + var resp AudioSourceListResponse |
| 186 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp)) |
| 187 | + assert.Len(t, resp.Sources, 2, "Should only include stream sources, not audio cards") |
| 188 | + |
| 189 | + types := make([]string, len(resp.Sources)) |
| 190 | + for i, s := range resp.Sources { |
| 191 | + types[i] = s.Type |
| 192 | + } |
| 193 | + assert.NotContains(t, types, "audio_card") |
| 194 | + }) |
| 195 | +} |
0 commit comments