package main

import (
	"context"
	"testing"
)

func TestQuerySourceSinkCollectsTheClaimingName(t *testing.T) {
	ctx, sink := withQuerySourceSink(context.Background())
	if sink.Name() != "" {
		t.Fatalf("a fresh sink names a source: %q", sink.Name())
	}
	noteQuerySource(ctx, "kiwix")
	if got := sink.Name(); got != "kiwix" {
		t.Errorf("sink.Name() = %q, want kiwix", got)
	}
}

// A turn with no sink must not panic. The mic path asks for no source, and a
// query source calls noteQuerySource unconditionally.
func TestNoteQuerySourceWithoutASinkIsSilent(t *testing.T) {
	noteQuerySource(context.Background(), "search")
}

// The last source to claim wins, because only one does: actionQuery returns on
// the first claim. This pins that the sink overwrites rather than appends, so a
// second turn on the same context could not read a stale name.
func TestQuerySourceSinkKeepsTheLastNote(t *testing.T) {
	ctx, sink := withQuerySourceSink(context.Background())
	noteQuerySource(ctx, "search")
	noteQuerySource(ctx, "kiwix")
	if got := sink.Name(); got != "kiwix" {
		t.Errorf("sink.Name() = %q, want kiwix", got)
	}
}
