Replace Apple Health ingest with iPhone location geofences
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]>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package ingest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -41,32 +40,24 @@ func TestNormalizeValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeHAE(t *testing.T) {
|
||||
raw := `{"data":{
|
||||
"workouts":[{"id":"W1","name":"Outdoor Walk","start":"2026-10-14 07:00:00 -0700","end":"2026-10-14 07:45:00 -0700","location":"Outdoor"}],
|
||||
"metrics":[
|
||||
{"name":"step_count","units":"count","data":[{"date":"2026-10-14 07:00:00 -0700","qty":812,"source":"iPhone"},{"date":"bad"}]},
|
||||
{"name":"sleep_analysis","units":"hr","data":[{"sleepStart":"2026-10-13 23:10:00 -0700","sleepEnd":"2026-10-14 06:40:00 -0700","asleep":7.1}]}
|
||||
]}}`
|
||||
var p HAEPayload
|
||||
if err := json.Unmarshal([]byte(raw), &p); err != nil {
|
||||
t.Fatal(err)
|
||||
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)
|
||||
}
|
||||
acts, samples, err := NormalizeHAE(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
e, err = NormalizeLocation(LocationEvent{Place: " Gym "}, now)
|
||||
if err != nil || e.Place != "gym" {
|
||||
t.Fatalf("got %+v, %v", e, err)
|
||||
}
|
||||
if len(acts) != 2 || len(samples) != 1 {
|
||||
t.Fatalf("got %d activities, %d samples", len(acts), len(samples))
|
||||
}
|
||||
walk := acts[0]
|
||||
if walk.Kind != "walk" || walk.ExternalID != "W1" || walk.EndedAt.Sub(walk.StartedAt) != 45*time.Minute {
|
||||
t.Errorf("walk: %+v", walk)
|
||||
}
|
||||
if walk.StartedAt.Hour() != 14 { // 07:00 -0700 is 14:00 UTC
|
||||
t.Errorf("start not converted to UTC: %v", walk.StartedAt)
|
||||
}
|
||||
if acts[1].Kind != "sleep" || samples[0].Value != 812 {
|
||||
t.Errorf("sleep %+v, sample %+v", acts[1], samples[0])
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user