package api

import "testing"

// A build date is stored as an ISO 8601 reduced-precision date, because it is
// often only half known: the VIN plate carries a month, the papers a day, a grey
// import neither. The column is free text, so the shapes have to be checked here
// or a value no reader can print gets stored.
func TestNormalizeBuildDate(t *testing.T) {
	for _, tc := range []struct {
		in   string
		want string
	}{
		{"", ""},                     // not set at all
		{"2015", "2015"},             // the year off the plate
		{"2015-03", "2015-03"},       // year and month
		{"2015-03-10", "2015-03-10"}, // the papers' own date
		{"  2015-03  ", "2015-03"},   // trimmed, like every other text field
		{"2024-02-29", "2024-02-29"}, // a leap day is a day
	} {
		got, err := normalizeBuildDate(tc.in)
		if err != nil {
			t.Errorf("normalizeBuildDate(%q): %v", tc.in, err)
			continue
		}
		if got != tc.want {
			t.Errorf("normalizeBuildDate(%q) = %q, want %q", tc.in, got, tc.want)
		}
	}

	// Rejected rather than stored and puzzled over later. The last two are the
	// ones a plain length check would have let through.
	for _, bad := range []string{
		"15",         // two-digit year
		"2015-3",     // unpadded month
		"2015/03/10", // not ISO
		"March 2015", // words
		"2015-13",    // there is no thirteenth month
		"2015-02-31", // there is no such day
	} {
		if got, err := normalizeBuildDate(bad); err == nil {
			t.Errorf("normalizeBuildDate(%q) = %q, want an error", bad, got)
		}
	}
}
