package awardref

import (
	"regexp"
	"testing"
)

// The entities an operator meets: an island group that is its own DXCC, and a
// mainland country that is not.
func TestIOTAForDXCC(t *testing.T) {
	cases := []struct {
		dxcc int
		want string
		why  string
	}{
		{205, "AF-003", "Ascension Island is one island and one reference"},
		{5, "EU-002", "Aland Islands"},
		{12, "NA-022", "Anguilla"},
		{24, "AN-002", "Bouvet — the entity whose entry is checked most often and worked least"},
		// France holds hundreds of islands and, far more to the point, a
		// mainland. Filling a reference here would earn nothing and would put
		// one on nearly every European contact in the log.
		{227, "", "France is not one IOTA"},
		{291, "", "the United States is not one IOTA"},
		{0, "", "no entity resolved"},
		{99999, "", "not an entity at all"},
	}
	for _, c := range cases {
		if got := IOTAForDXCC(c.dxcc); got != c.want {
			t.Errorf("IOTAForDXCC(%d) = %q, want %q — %s", c.dxcc, got, c.want, c.why)
		}
	}
}

// Every reference in the table must be a well-formed IOTA reference, because
// one that is not would be written onto a contact's award references and count
// for nothing — and would then have to be found by hand, one QSO at a time.
func TestEveryOneIOTAEntryIsWellFormed(t *testing.T) {
	ref := regexp.MustCompile(`^(AF|AN|AS|EU|NA|OC|SA)-\d{3}$`)
	if len(iotaByDXCC) < 50 {
		t.Fatalf("the table holds %d entities — it has been truncated", len(iotaByDXCC))
	}
	for dxcc, r := range iotaByDXCC {
		if dxcc <= 0 {
			t.Errorf("entity number %d is not a DXCC entity", dxcc)
		}
		if !ref.MatchString(r) {
			t.Errorf("entity %d maps to %q, which is not an IOTA reference", dxcc, r)
		}
	}
}
