package broker_test

import (
	"context"
	"testing"
	"time"

	"github.com/uopi/uopi/internal/broker"
)

// TestDataSourcesAndSource covers the registry accessors.
func TestDataSourcesAndSource(t *testing.T) {
	b, cancel := newBroker(t)
	defer cancel()

	all := b.DataSources()
	if len(all) != 1 || all[0].Name() != "stub" {
		t.Fatalf("DataSources = %v, want one 'stub'", all)
	}
	if ds, ok := b.Source("stub"); !ok || ds.Name() != "stub" {
		t.Errorf("Source(stub) = %v,%v want stub,true", ds, ok)
	}
	if _, ok := b.Source("nope"); ok {
		t.Error("Source(nope): want ok=false")
	}
}

// TestReadNow covers the one-shot read happy path plus the unknown-DS and
// context-timeout error branches.
func TestReadNow(t *testing.T) {
	b, cancel := newBroker(t)
	defer cancel()

	ctx, c := context.WithTimeout(context.Background(), time.Second)
	defer c()
	v, err := b.ReadNow(ctx, broker.SignalRef{DS: "stub", Name: "sine_1hz"})
	if err != nil {
		t.Fatalf("ReadNow: %v", err)
	}
	if v.Timestamp.IsZero() {
		t.Error("ReadNow returned a zero-timestamp value")
	}

	// Unknown data source.
	if _, err := b.ReadNow(ctx, broker.SignalRef{DS: "ghost", Name: "x"}); err == nil {
		t.Error("ReadNow(unknown ds): want error")
	}
}
