Files
tajniak81andClaude Opus 5 049da69c83 Cars: arrange the garage, and choose what a car's page shows
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>
2026-08-17 20:29:23 +02:00

93 lines
2.5 KiB
Go

package api
import (
"testing"
"drivervault/apiserver/internal/models"
)
// The garage arrangement is a per-user list of car ids, so it has to cope with
// lists that no longer line up with the cars the user actually has: a car sold
// since the last drag leaves a stale id, a car added or shared since leaves an
// id missing. These cover both directions plus the input cleaning.
func ids(cars []models.Car) []string {
out := make([]string, len(cars))
for i, c := range cars {
out[i] = c.ID
}
return out
}
func carsWithIDs(list ...string) []models.Car {
out := make([]models.Car, len(list))
for i, id := range list {
out[i] = models.Car{ID: id}
}
return out
}
func TestApplyCarOrder(t *testing.T) {
for _, tc := range []struct {
name string
cars []string
order []string
want []string
}{
{"arranged", []string{"a", "b", "c"}, []string{"c", "a", "b"}, []string{"c", "a", "b"}},
{"no arrangement keeps default order", []string{"a", "b", "c"}, nil, []string{"a", "b", "c"}},
{
// A car added or shared since the last drag isn't in the list; it
// belongs at the end rather than jumping into the middle.
"unarranged cars go last in their existing order",
[]string{"a", "b", "new1", "new2"}, []string{"b", "a"},
[]string{"b", "a", "new1", "new2"},
},
{
// A car that was sold since the last drag just drops out.
"stale ids are ignored",
[]string{"a", "b"}, []string{"gone", "b", "a"},
[]string{"b", "a"},
},
{"single car is untouched", []string{"a"}, []string{"b", "a"}, []string{"a"}},
} {
t.Run(tc.name, func(t *testing.T) {
cars := carsWithIDs(tc.cars...)
applyCarOrder(cars, tc.order)
got := ids(cars)
if len(got) != len(tc.want) {
t.Fatalf("order = %v, want %v", got, tc.want)
}
for i := range got {
if got[i] != tc.want[i] {
t.Fatalf("order = %v, want %v", got, tc.want)
}
}
})
}
}
func TestNormalizeCarOrder(t *testing.T) {
got, err := normalizeCarOrder([]string{" a ", "", "b", "a", " ", "c"})
if err != nil {
t.Fatalf("normalizeCarOrder: %v", err)
}
want := []string{"a", "b", "c"} // trimmed, blanks dropped, first "a" wins
if len(got) != len(want) {
t.Fatalf("normalized = %v, want %v", got, want)
}
for i := range got {
if got[i] != want[i] {
t.Fatalf("normalized = %v, want %v", got, want)
}
}
tooLong := make([]string, maxCarOrder+1)
for i := range tooLong {
tooLong[i] = "c"
}
if _, err := normalizeCarOrder(tooLong); err == nil {
t.Error("normalizeCarOrder accepted a list over the cap, want an error")
}
}