POST /api/v1/location/{arrive,leave} records place intervals (home or a
named place, and away in between) from Shortcuts automations. A stale
"leave" for a place the device has already left is ignored. The Health
Auto Export endpoint and its normalizer are removed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package ingest
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNormalizeValidation(t *testing.T) {
|
|
now := time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)
|
|
ok := Event{ID: "1", Kind: "app_focus", Label: "kitty", StartedAt: now.Add(-time.Hour), EndedAt: now}
|
|
|
|
tests := []struct {
|
|
name string
|
|
mutate func(*Event)
|
|
wantErr string
|
|
}{
|
|
{"valid", func(e *Event) {}, ""},
|
|
{"missing id", func(e *Event) { e.ID = "" }, "id"},
|
|
{"wrong kind", func(e *Event) { e.Kind = "tab_focus" }, "not accepted"},
|
|
{"reversed", func(e *Event) { e.EndedAt = e.StartedAt.Add(-time.Second) }, "before"},
|
|
{"future", func(e *Event) { e.EndedAt = now.Add(time.Hour) }, "future"},
|
|
{"too long", func(e *Event) { e.StartedAt = now.Add(-8 * 24 * time.Hour) }, "longer"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
e := ok
|
|
tt.mutate(&e)
|
|
acts, err := Desktop.Normalize(Batch{Events: []Event{e}}, now)
|
|
if tt.wantErr == "" {
|
|
if err != nil || len(acts) != 1 || acts[0].Source != "hyprland" {
|
|
t.Fatalf("got %+v, %v", acts, err)
|
|
}
|
|
return
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("got err %v, want containing %q", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNormalizeLocation(t *testing.T) {
|
|
now := time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)
|
|
|
|
e, err := NormalizeLocation(LocationEvent{}, now)
|
|
if err != nil || e.Place != "home" || !e.At.Equal(now) {
|
|
t.Fatalf("defaults: got %+v, %v", e, err)
|
|
}
|
|
e, err = NormalizeLocation(LocationEvent{Place: " Gym "}, now)
|
|
if err != nil || e.Place != "gym" {
|
|
t.Fatalf("got %+v, %v", e, err)
|
|
}
|
|
for _, bad := range []LocationEvent{
|
|
{Place: "Away"},
|
|
{At: now.Add(time.Hour)},
|
|
{At: now.Add(-8 * 24 * time.Hour)},
|
|
} {
|
|
if _, err := NormalizeLocation(bad, now); err == nil {
|
|
t.Errorf("%+v: want error", bad)
|
|
}
|
|
}
|
|
}
|