package api import "testing" // What a car's page shows — which tabs, and which rows of the Information tab — // is stored on the car as the hidden sets, so the validation has to keep those // sets to keys the page actually renders. Information itself stays out of the // hideable tabs: a car with no tabs left would be a dead end. func TestNormalizeHiddenTabs(t *testing.T) { got, err := normalizeHidden([]string{" fuel ", "parts", "fuel", ""}, hideableCarTabs, "tab") if err != nil { t.Fatalf("normalizeHidden: %v", err) } want := []string{"fuel", "parts"} // trimmed, blanks dropped, deduped assertKeys(t, got, want) // Clearing the list is how a car goes back to showing everything. if empty, err := normalizeHidden(nil, hideableCarTabs, "tab"); err != nil || len(empty) != 0 { t.Errorf("normalizeHidden(nil) = %v, %v; want empty and no error", empty, err) } // Information is the car itself; hiding it would leave a page with no tabs. if _, err := normalizeHidden([]string{"info"}, hideableCarTabs, "tab"); err == nil { t.Error("normalizeHidden allowed hiding the info tab, want an error") } // A key from a stale or wrong client is an error, not something to drop // quietly while the tab stays visible. if _, err := normalizeHidden([]string{"fuel", "nonsense"}, hideableCarTabs, "tab"); err == nil { t.Error("normalizeHidden accepted an unknown tab, want an error") } // The hideable set is the contract the web app's HIDEABLE_TABS mirrors: // every tab the car page renders beside Information. for _, key := range []string{"provider", "services", "technical", "maintenance", "fuel", "documents", "parts", "reminders"} { if !hideableCarTabs[key] { t.Errorf("tab %q should be hideable", key) } } if len(hideableCarTabs) != 8 { t.Errorf("hideableCarTabs has %d entries, want the 8 tabs beside Information", len(hideableCarTabs)) } } func TestNormalizeHiddenFields(t *testing.T) { got, err := normalizeHidden([]string{"vin", " differentialOil ", "vin"}, hideableCarFields, "field") if err != nil { t.Fatalf("normalizeHidden: %v", err) } assertKeys(t, got, []string{"vin", "differentialOil"}) if _, err := normalizeHidden([]string{"oilSpec", "nonsense"}, hideableCarFields, "field"); err == nil { t.Error("normalizeHidden accepted an unknown field, want an error") } // A tab key is not a field key — the two sets are validated separately. if _, err := normalizeHidden([]string{"fuel"}, hideableCarFields, "field"); err == nil { t.Error("normalizeHidden accepted a tab key as a field, want an error") } // Every Information row the web app renders must be hideable; unlike the // tabs there is no row the page has to keep. for _, key := range []string{ "oilSpec", "transmissionOil", "differentialOil", "brakeFluid", "coolant", "odometer", "serviceInterval", "nextDue", "registrationPlate", "registrationCountry", "vin", "fuelType", "buildDate", "firstRegistration", } { if !hideableCarFields[key] { t.Errorf("field %q should be hideable", key) } } if len(hideableCarFields) != 14 { t.Errorf("hideableCarFields has %d entries, want the 14 Information rows", len(hideableCarFields)) } } func assertKeys(t *testing.T, got, want []string) { t.Helper() if len(got) != len(want) { t.Fatalf("keys = %v, want %v", got, want) } for i := range got { if got[i] != want[i] { t.Fatalf("keys = %v, want %v", got, want) } } }