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:
@@ -118,3 +118,59 @@ func TestIngestAndSummary(t *testing.T) {
|
||||
t.Fatalf("activities: %d %v", code, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocation(t *testing.T) {
|
||||
srv, key := setup(t)
|
||||
at := func(d time.Duration) string {
|
||||
return `"at":"` + time.Now().UTC().Add(d).Format(time.RFC3339) + `"`
|
||||
}
|
||||
post := func(event, body string) map[string]any {
|
||||
t.Helper()
|
||||
code, out := do(t, "POST", srv.URL+"/api/v1/location/"+event, key, body)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("%s %s: %d %v", event, body, code, out)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// A bare POST means home, now.
|
||||
if out := post("leave", `{`+at(-3*time.Hour)+`}`); out["place"] != "away" || out["changed"] != true {
|
||||
t.Fatalf("first leave: %v", out)
|
||||
}
|
||||
if out := post("arrive?place=Gym", `{`+at(-2*time.Hour)+`}`); out["place"] != "gym" {
|
||||
t.Fatalf("arrive gym: %v", out)
|
||||
}
|
||||
// Leaving home while at the gym is stale and changes nothing.
|
||||
if out := post("leave", `{`+at(-90*time.Minute)+`}`); out["place"] != "gym" || out["changed"] != false {
|
||||
t.Fatalf("stale leave: %v", out)
|
||||
}
|
||||
post("leave/gym", `{`+at(-time.Hour)+`}`)
|
||||
post("arrive", "")
|
||||
if out := post("arrive", ""); out["changed"] != false {
|
||||
t.Fatalf("repeat arrive should be a no-op: %v", out)
|
||||
}
|
||||
|
||||
code, body := do(t, "GET", srv.URL+"/api/v1/activities?kind=place", key, "")
|
||||
acts := body["activities"].([]any)
|
||||
if code != http.StatusOK || len(acts) != 4 {
|
||||
t.Fatalf("activities: %d %v", code, body)
|
||||
}
|
||||
var labels []string
|
||||
for _, a := range acts {
|
||||
labels = append(labels, a.(map[string]any)["label"].(string))
|
||||
}
|
||||
if strings.Join(labels, ",") != "away,gym,away,home" {
|
||||
t.Errorf("labels = %v", labels)
|
||||
}
|
||||
if d := acts[0].(map[string]any)["duration_s"]; d != 3600.0 {
|
||||
t.Errorf("first away = %v s", d)
|
||||
}
|
||||
// The open home interval reads as running until now.
|
||||
if end, _ := time.Parse(time.RFC3339Nano, acts[3].(map[string]any)["ended_at"].(string)); time.Since(end) > time.Minute {
|
||||
t.Errorf("open interval ended at %v", end)
|
||||
}
|
||||
|
||||
if code, _ := do(t, "POST", srv.URL+"/api/v1/location/teleport", key, ""); code != http.StatusNotFound {
|
||||
t.Errorf("unknown event: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user