Files
DriverVault/API Server/internal/api/cartabs_test.go
T
tajniak81andClaude Opus 5 cc1dafa9f7 Cars: drag the tabs into order, and a lock for every arrangement
Two things, both about layouts you arrange by dragging.

A car's tab bar now takes a drag: the tabs reorder as the pointer crosses
them and the arrangement saves on drop — or on dragend, since a tab
released in the gap beside the bar never produces a drop and would
otherwise revert on the next load. Same native drag events as the garage
and the Information rows, so also pointer-only, and it needs write access.

The order belongs to the car, like the choice of which tabs show at all,
so everyone it is shared with sees the same bar. It is stored as the full
list of keys, hidden tabs included, so a tab switched off and back on
returns to where it was rather than to the end; a key the stored
arrangement doesn't mention — a tab added in a later release — follows
the arranged ones. Information is arrangeable although it cannot be
switched off, which is why the validation needs arrangeableCarTabs rather
than reusing hideableCarTabs; it is derived from that set so the two
cannot drift as tabs are added. tabOrder rides on the existing PUT
/api/cars/{id}/view, so a tab drag never has to resend what is hidden.
Where the page opens is unchanged: Information, wherever it now sits.

And a padlock in the sidebar, above the theme toggle, holds every
arrangement in the app still at once — the garage, a car's tabs, its
Information rows, the provider's readings. It is a guard against nudging
a layout while reading it, not a permission: it is the user's own setting
and says nothing about what anybody may edit, so locking hides your own
drag handles rather than stopping a co-owner rearranging a shared car.
Stored as dragLocked on the profile, like the theme it sits above, so a
locked account is still locked on the next device — where a folded
provider card stays one browser's reading habit. Unlocked by default, so
nothing changes until it is clicked, and while locked the grab cursor and
the drag hints go with the drag.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-17 22:43:54 +02:00

174 lines
6.8 KiB
Go

package api
import "testing"
// What a car's page shows — which tabs, which rows of the Information tab, and
// the order those rows are laid out in — is stored on the car as key lists, so
// the validation has to keep them 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 := normalizeKeys([]string{" fuel ", "parts", "fuel", ""}, hideableCarTabs, "tab")
if err != nil {
t.Fatalf("normalizeKeys: %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 := normalizeKeys(nil, hideableCarTabs, "tab"); err != nil || len(empty) != 0 {
t.Errorf("normalizeKeys(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 := normalizeKeys([]string{"info"}, hideableCarTabs, "tab"); err == nil {
t.Error("normalizeKeys 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 := normalizeKeys([]string{"fuel", "nonsense"}, hideableCarTabs, "tab"); err == nil {
t.Error("normalizeKeys 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", "charging",
"documents", "parts", "reminders",
} {
if !hideableCarTabs[key] {
t.Errorf("tab %q should be hideable", key)
}
}
if len(hideableCarTabs) != 9 {
t.Errorf("hideableCarTabs has %d entries, want the 9 tabs beside Information", len(hideableCarTabs))
}
}
func TestNormalizeHiddenFields(t *testing.T) {
got, err := normalizeKeys([]string{"vin", " differentialOil ", "vin"}, hideableCarFields, "field")
if err != nil {
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"vin", "differentialOil"})
if _, err := normalizeKeys([]string{"oilSpec", "nonsense"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeKeys accepted an unknown field, want an error")
}
// A tab key is not a field key — the two sets are validated separately.
if _, err := normalizeKeys([]string{"fuel"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeKeys 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))
}
}
// The tabs arrange against a wider set than they hide against: Information
// cannot be switched off, but it can be moved off the front of the bar.
func TestNormalizeTabOrder(t *testing.T) {
got, err := normalizeKeys([]string{"reminders", "info", "fuel"}, arrangeableCarTabs, "tab")
if err != nil {
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"reminders", "info", "fuel"})
// Everything the bar renders has to be arrangeable — the hideable tabs plus
// Information, and nothing else.
for key := range hideableCarTabs {
if !arrangeableCarTabs[key] {
t.Errorf("tab %q should be arrangeable", key)
}
}
if !arrangeableCarTabs["info"] {
t.Error("the info tab should be arrangeable even though it cannot be hidden")
}
if len(arrangeableCarTabs) != len(hideableCarTabs)+1 {
t.Errorf("arrangeableCarTabs has %d entries, want the hideable tabs plus Information", len(arrangeableCarTabs))
}
// A field key is not a tab key, and an invented tab is still an error.
if _, err := normalizeKeys([]string{"vin"}, arrangeableCarTabs, "tab"); err == nil {
t.Error("normalizeKeys accepted a field key as a tab, want an error")
}
if _, err := normalizeKeys([]string{"info", "nonsense"}, arrangeableCarTabs, "tab"); err == nil {
t.Error("normalizeKeys accepted an unknown tab in an arrangement, want an error")
}
}
// The arrangement of the Information rows shares the field key set — every row
// can be moved — but not the meaning: here the order of the list is the point,
// so it has to survive validation exactly as it was sent.
func TestNormalizeFieldOrder(t *testing.T) {
got, err := normalizeKeys([]string{"vin", "odometer", "oilSpec"}, hideableCarFields, "field")
if err != nil {
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"vin", "odometer", "oilSpec"})
// A key repeated by a client that lost track keeps its first position; a
// second entry for the same row would put it in two places at once.
got, err = normalizeKeys([]string{"vin", "odometer", "vin"}, hideableCarFields, "field")
if err != nil {
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"vin", "odometer"})
// A partial arrangement is fine — the rows it leaves out follow the arranged
// ones — but an invented row is still an error.
if _, err := normalizeKeys([]string{"vin", "nonsense"}, hideableCarFields, "field"); err == nil {
t.Error("normalizeKeys accepted an unknown field in an arrangement, want an error")
}
}
// The connected service's headline readings arrange the same way, against the
// key set derived from what that panel renders.
func TestNormalizeMetricOrder(t *testing.T) {
got, err := normalizeKeys([]string{"evRangeWithAc", "odometer"}, arrangeableCarMetrics, "reading")
if err != nil {
t.Fatalf("normalizeKeys: %v", err)
}
assertKeys(t, got, []string{"evRangeWithAc", "odometer"})
// Every reading the panel can show has to be arrangeable, the two that are
// not measures included.
for _, key := range []string{
"odometer", "fuelLevel", "fuelRange", "batteryLevel", "evRange",
"evRangeWithAc", "chargingStatus", "location",
} {
if !arrangeableCarMetrics[key] {
t.Errorf("reading %q should be arrangeable", key)
}
}
// A field key is not a reading key — a car's two arrangements are separate.
if _, err := normalizeKeys([]string{"vin"}, arrangeableCarMetrics, "reading"); err == nil {
t.Error("normalizeKeys accepted a field key as a reading, want an error")
}
}
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)
}
}
}