package adif

import "testing"

// ADIF defines CNTY as "STATE,COUNTY" — "GA,BARROW". OpsLog exported the bare
// county, which a receiving logger cannot resolve: county names repeat across
// states, and there is a Washington County in thirty of them.
func TestADIFCountyFormat(t *testing.T) {
	for _, tc := range []struct{ state, county, want string }{
		{"GA", "BARROW", "GA,BARROW"},
		{"ga", "Barrow", "GA,Barrow"},    // the state is a code, upper-cased; the name is not touched
		{"", "BARROW", "BARROW"},         // no state known: a bare name beats nothing
		{"GA", "", ""},                   // no county: nothing to write
		{"GA", "GA,BARROW", "GA,BARROW"}, // already formatted — never double the prefix
		{"  GA  ", " BARROW ", "GA,BARROW"},
	} {
		if got := adifCounty(tc.state, tc.county); got != tc.want {
			t.Errorf("adifCounty(%q, %q) = %q, want %q", tc.state, tc.county, got, tc.want)
		}
	}
}

// And the mirror: a file from a logger that follows the standard must fill both
// columns, while a file carrying the bare county still imports unchanged.
func TestSplitADIFCounty(t *testing.T) {
	for _, tc := range []struct{ in, county, state string }{
		{"GA,BARROW", "BARROW", "GA"},
		{"ga, Barrow", "Barrow", "GA"},
		{"BARROW", "BARROW", ""}, // the old OpsLog form, and many other loggers
		{"", "", ""},
		{"  ", "", ""},
	} {
		c, s := splitADIFCounty(tc.in)
		if c != tc.county || s != tc.state {
			t.Errorf("splitADIFCounty(%q) = (%q, %q), want (%q, %q)", tc.in, c, s, tc.county, tc.state)
		}
	}
}

// Round trip: what OpsLog writes, OpsLog reads back to the same two values.
func TestCountyRoundTrip(t *testing.T) {
	c, s := splitADIFCounty(adifCounty("GA", "BARROW"))
	if c != "BARROW" || s != "GA" {
		t.Errorf("round trip gave (%q, %q), want (BARROW, GA)", c, s)
	}
}
