package pskr

import (
	"strings"
	"testing"
)

// The receiver square is a level the BROKER can filter on, which is the whole
// point: measured on the live feed, the four opening bands unfiltered are 83
// messages a second of which about one in a hundred survives the NearKm test.
// One ring of squares is under two a second, and the same for every operator —
// where filtering by DXCC ranged from 1.2 (OH) to 72.5 (K) on one band.
func TestTopicsFilterOnTheReceiverSquare(t *testing.T) {
	w := New(Config{Bands: []string{"6m"}, RxGrids: []string{"jn36", "JN37"}})
	got := w.topics()
	if len(got) != 2 {
		t.Fatalf("want one subscription per band × square, got %v", got)
	}
	// Level order: band/mode/txcall/rxcall/txgrid/RXGRID/txdxcc/rxdxcc
	want := "pskr/filter/v2/6m/+/+/+/+/JN36/+/+"
	if got[0] != want {
		t.Errorf("topic = %q, want %q", got[0], want)
	}
	if !strings.Contains(got[1], "/JN37/") {
		t.Errorf("square not upper-cased into the topic: %q", got[1])
	}
}

// With no squares the old behaviour stands: receive the band and decide here.
func TestTopicsWithoutSquares(t *testing.T) {
	w := New(Config{Bands: []string{"10m", "2m"}})
	got := w.topics()
	if len(got) != 2 || got[0] != "pskr/filter/v2/10m/#" {
		t.Errorf("unfiltered topics = %v", got)
	}
}

// Grid chasing wants every band. "+" is the MQTT single-level wildcard, so one
// subscription per square covers the lot instead of one per band per square.
func TestTopicsAllBands(t *testing.T) {
	w := New(Config{Bands: []string{"+"}, RxGrids: []string{"JN36"}})
	got := w.topics()
	if len(got) != 1 || got[0] != "pskr/filter/v2/+/+/+/+/+/JN36/+/+" {
		t.Errorf("all-band topic = %v", got)
	}
}
