Fix location races, run database tests in CI, add make check
Location: a leave decided its destination before taking the device lock, so a "leave home" racing an "arrive gym" could undo the arrival. The decision (now ingest.Destination) runs inside MoveTo under the lock. Transition external IDs were device|label|time, which collided (500) when out-of-order events were clamped to the same start; they now name the interval being closed. hypr-agent: a control command arriving during shutdown no longer blocks its goroutine forever. Tests: internal/testdb gives each database test its own throwaway database, so they run in parallel and no longer truncate the dev data. New store tests cover both location bugs. CI starts a Postgres container so these tests actually run, and adds gofmt, staticcheck (pinned as a go tool) and -race. `make check` runs the same locally. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -61,3 +61,19 @@ func TestNormalizeLocation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDestination(t *testing.T) {
|
||||
tests := []struct{ event, place, cur, want string }{
|
||||
{"arrive", "gym", "", "gym"},
|
||||
{"arrive", "gym", "home", "gym"},
|
||||
{"leave", "home", "home", Away},
|
||||
{"leave", "home", "", Away},
|
||||
{"leave", "home", "gym", "gym"}, // stale leave: stay at the gym
|
||||
{"leave", "home", Away, Away},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := Destination(tt.event, tt.place, tt.cur); got != tt.want {
|
||||
t.Errorf("Destination(%q, %q, %q) = %q, want %q", tt.event, tt.place, tt.cur, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,3 +46,17 @@ func NormalizeLocation(e LocationEvent, now time.Time) (LocationEvent, error) {
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// Destination is where a device moves on an arrive or leave event for place,
|
||||
// given the place it is in now ("" if none yet). Leaving a place only counts
|
||||
// if the device is in it (or nowhere yet), so a late "leave home" after
|
||||
// "arrive gym" doesn't undo the arrival.
|
||||
func Destination(event, place, cur string) string {
|
||||
if event == "arrive" {
|
||||
return place
|
||||
}
|
||||
if cur != "" && cur != place {
|
||||
return cur
|
||||
}
|
||||
return Away
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user