package rda

import (
	"testing"
	"time"
)

func day(s string) time.Time {
	t, err := time.Parse("2006-01-02", s)
	if err != nil {
		panic(err)
	}
	return t
}

func TestLoad(t *testing.T) {
	if n := Count(); n < 50000 {
		t.Fatalf("loaded %d callsigns, want the whole database (~58k)", n)
	}
}

// A station with no history: the current district, reported as an assumption.
func TestCurrentDistrict(t *testing.T) {
	m, ok := Lookup("R0AA", day("2020-05-01"))
	if !ok || m.District != "KK-08" {
		t.Fatalf("R0AA = %+v ok=%v, want KK-08", m, ok)
	}
	if m.Dated {
		t.Errorf("a district with no date range must not be reported as dated")
	}
}

// An expedition: the day decides, and a day outside every period is NOT
// answered with a neighbouring one.
func TestDatedDistrict(t *testing.T) {
	m, ok := Lookup("4K4/EK250RA", day("1991-09-15"))
	if !ok || m.District != "CK-05" || !m.Dated {
		t.Fatalf("got %+v ok=%v, want CK-05 dated", m, ok)
	}
	m, ok = Lookup("4K4/EK250RA", day("1991-08-07"))
	if !ok || m.District != "CK-08" {
		t.Fatalf("got %+v ok=%v, want CK-08 on the one-day period", m, ok)
	}
	if _, ok := Lookup("4K4/EK250RA", day("2020-01-01")); ok {
		t.Errorf("a day outside every recorded period must find nothing")
	}
}

// The callsign is matched exactly: a suffix means somewhere else.
func TestExactCallsign(t *testing.T) {
	if _, ok := Lookup("R0AA/P", day("2020-05-01")); ok {
		t.Errorf("R0AA/P must not resolve to R0AA's district")
	}
	if _, ok := Lookup("", day("2020-05-01")); ok {
		t.Errorf("an empty callsign must find nothing")
	}
	if _, ok := Lookup("F4BPO", day("2020-05-01")); ok {
		t.Errorf("a non-Russian callsign must find nothing")
	}
	// Case and stray spaces are the operator's, not the data's.
	if m, ok := Lookup("  r0aa  ", day("2020-05-01")); !ok || m.District != "KK-08" {
		t.Errorf("lookup must be case- and space-insensitive, got %+v ok=%v", m, ok)
	}
}
