package ingest import ( "context" "testing" "time" "github.com/jan/fm-rds-tx/internal/audio" ) type fakeSource struct { desc SourceDescriptor chunks chan PCMChunk errs chan error stats SourceStats } func newFakeSource() *fakeSource { return &fakeSource{ desc: SourceDescriptor{ID: "fake", Kind: "stdin-pcm"}, chunks: make(chan PCMChunk, 4), errs: make(chan error, 1), stats: SourceStats{State: "running", Connected: true}, } } func (s *fakeSource) Descriptor() SourceDescriptor { return s.desc } func (s *fakeSource) Start(context.Context) error { return nil } func (s *fakeSource) Stop() error { close(s.chunks); return nil } func (s *fakeSource) Chunks() <-chan PCMChunk { return s.chunks } func (s *fakeSource) Errors() <-chan error { return s.errs } func (s *fakeSource) Stats() SourceStats { return s.stats } func TestRuntimeWritesFramesToStreamSink(t *testing.T) { sink := audio.NewStreamSource(128, 44100) src := newFakeSource() rt := NewRuntime(sink, src) if err := rt.Start(context.Background()); err != nil { t.Fatalf("start: %v", err) } defer rt.Stop() src.chunks <- PCMChunk{ Channels: 2, SampleRateHz: 44100, Samples: []int32{1000 << 16, -1000 << 16}, } deadline := time.Now().Add(1 * time.Second) for sink.Available() < 1 && time.Now().Before(deadline) { time.Sleep(10 * time.Millisecond) } if sink.Available() < 1 { t.Fatal("expected at least one frame in sink") } }