Connected cars: update the odometer automatically

The provider tab gets an "Update odometer automatically" switch and an
interval (30 minutes to a week, daily by default). The API Server does
the updating on the scheduler's ticker, so it carries on with no page
open: every five minutes it takes the provider's odometer for each due
car and stores it when it is ahead, never backwards, the same as the
Update odometer button.

The setting belongs to the car. Readings are fetched under the account
of whoever switched it on, and stop if that user can no longer edit the
car. Each attempt and its error, if any, are stored on the car and shown
under the switch. The switch also shows when the provider can't be
reached, so updates that are failing can still be switched off.

New car fields odometer_auto_sync, odometer_sync_minutes,
odometer_sync_by, odometer_synced_at and odometer_sync_error; new route
PUT /api/cars/{id}/provider/auto-sync. Unlinking leaves the setting as
it is; the sweep only looks at linked cars.

launch.json gains api-server-8081 / web-app-8081 for when 8080 is taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 5.5 committed 2026-11-18 12:07:02 +01:00
1 parent 914aeeee23
commit 3db12dba63
14 files changed
+512 -11

No files matched your search

@@ -0,0 +1,65 @@
package api
import (
"net/http"
"strings"
"testing"
"time"
)
func TestOdometerSyncDue(t *testing.T) {
now := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)
at := func(d time.Duration) string { return now.Add(-d).Format(time.RFC3339) }
cases := []struct {
name string
rec carRecord
want bool
}{
{"never attempted", carRecord{OdometerSyncMinutes: 60}, true},
{"unreadable timestamp", carRecord{OdometerSyncMinutes: 60, OdometerSyncedAt: "yesterday"}, true},
{"inside the interval", carRecord{OdometerSyncMinutes: 60, OdometerSyncedAt: at(59 * time.Minute)}, false},
{"exactly at the interval", carRecord{OdometerSyncMinutes: 60, OdometerSyncedAt: at(time.Hour)}, true},
{"no interval falls back to daily", carRecord{OdometerSyncedAt: at(23 * time.Hour)}, false},
{"daily default runs out", carRecord{OdometerSyncedAt: at(24 * time.Hour)}, true},
}
for _, c := range cases {
if got := odometerSyncDue(c.rec, now); got != c.want {
t.Errorf("%s: due = %v, want %v", c.name, got, c.want)
}
}
}
func TestOdometerAutoSyncNeedsLinkedCar(t *testing.T) {
srv := newProviderTestServer(t, &fakeProviderPB{})
status, body := call(t, srv, http.MethodPut, "/api/cars/"+provCarID+"/provider/auto-sync", `{"enabled":true}`)
if status != http.StatusBadRequest || !strings.Contains(toStr(body["error"]), "not linked") {
t.Errorf("status = %d (%v), want 400 not linked", status, body["error"])
}
}
func TestOdometerAutoSyncRejectsOddInterval(t *testing.T) {
srv := newProviderTestServer(t, &fakeProviderPB{carProvider: "toyota", carVehicleID: "VIN1"})
status, body := call(t, srv, http.MethodPut, "/api/cars/"+provCarID+"/provider/auto-sync",
`{"enabled":true,"intervalMinutes":7}`)
if status != http.StatusBadRequest || !strings.Contains(toStr(body["error"]), "interval") {
t.Errorf("status = %d (%v), want 400 unsupported interval", status, body["error"])
}
}
func TestOdometerAutoSyncSwitchOff(t *testing.T) {
fake := &fakeProviderPB{carProvider: "toyota", carVehicleID: "VIN1"}
srv := newProviderTestServer(t, fake)
status, body := call(t, srv, http.MethodPut, "/api/cars/"+provCarID+"/provider/auto-sync",
`{"enabled":false,"intervalMinutes":360}`)
if status != http.StatusOK {
t.Fatalf("status = %d (%v), want 200", status, body["error"])
}
patch := fake.patch()
if patch["odometer_auto_sync"] != false || patch["odometer_sync_minutes"] != float64(360) {
t.Errorf("patch = %+v, want updates off at 360 minutes", patch)
}
if _, set := patch["odometer_sync_by"]; set {
t.Error("switching off must not record whose account to read under")
}
}