Three things you can now set up rather than live with.
The garage takes a drag: cards reorder as you drag across them and the
arrangement saves on drop — or on dragend, since a card released in the
gap between cards never produces a drop and would otherwise revert on
the next load. It is a per-user list of car ids on the profile, so it
covers cars shared with you and never reorders anybody else's garage;
the API returns /api/cars in that order, so a client only sends the new
one back. Pointer-only: touch browsers don't fire the native drag
events, and this is not worth a dependency.
A car's page is now configurable from the gear in its header: which tabs
it shows, and which of the 14 Information rows. Both belong to the car,
so everyone it is shared with sees the same page — Fuel off on an EV
stays off for all of them — and setting them needs write access. Stored
as the hidden sets, so anything added in a later release is on by
default. PUT /api/cars/{id}/view is its own endpoint precisely so an
ordinary save of the car form, which sends every other field, can never
reveal something that was deliberately switched off. Information itself
can't be hidden: a page with no tabs left would be a dead end.
The connected-service cards fold away, remembered per device, so a
provider that reports eight sections can be trimmed to the two worth
watching. A failed section keeps a short badge in its collapsed header
and puts the provider's own message — a few hundred characters of JSON,
which used to stretch the page sideways — inside the body with
everything else.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
87 lines
3.3 KiB
Go
87 lines
3.3 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|