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]>
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"ccp/internal/ingest"
|
|
"ccp/internal/testdb"
|
|
)
|
|
|
|
func setup(t *testing.T) (*Store, int64) {
|
|
t.Parallel()
|
|
s := New(testdb.New(t))
|
|
d, _, err := s.CreateDevice(context.Background(), "phone", "location")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s, d.ID
|
|
}
|
|
|
|
func to(place string) func(string) string { return func(string) string { return place } }
|
|
|
|
func places(t *testing.T, s *Store) []string {
|
|
t.Helper()
|
|
acts, err := s.Activities(context.Background(), ActivityFilter{
|
|
From: time.Now().Add(-48 * time.Hour), To: time.Now().Add(time.Hour), Kind: ingest.PlaceKind, Limit: 100,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out []string
|
|
for _, a := range acts {
|
|
out = append(out, a.Label)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Events older than the open interval are clamped to its start, so several
|
|
// moves can share a timestamp. They must still get distinct external IDs.
|
|
func TestMoveToClampedTimestamps(t *testing.T) {
|
|
s, dev := setup(t)
|
|
ctx := context.Background()
|
|
t0 := time.Now().Add(-time.Hour).Truncate(time.Second)
|
|
|
|
if _, _, err := s.MoveTo(ctx, dev, t0, to("home")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range []string{ingest.Away, "home", ingest.Away, "home"} {
|
|
if _, changed, err := s.MoveTo(ctx, dev, t0.Add(-time.Minute), to(p)); err != nil || !changed {
|
|
t.Fatalf("move to %s: changed=%v err=%v", p, changed, err)
|
|
}
|
|
}
|
|
if got := places(t, s); len(got) != 5 || got[4] != "home" {
|
|
t.Errorf("places = %v", got)
|
|
}
|
|
}
|
|
|
|
// A leave and an arrive racing each other must agree on the order they ran
|
|
// in: whichever runs second sees the first's move.
|
|
func TestMoveToConcurrent(t *testing.T) {
|
|
s, dev := setup(t)
|
|
ctx := context.Background()
|
|
if _, _, err := s.MoveTo(ctx, dev, time.Now().Add(-time.Hour), to("home")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
for _, ev := range [][2]string{{"leave", "home"}, {"arrive", "gym"}} {
|
|
wg.Go(func() {
|
|
if _, _, err := s.MoveTo(ctx, dev, time.Now(), func(cur string) string {
|
|
return ingest.Destination(ev[0], ev[1], cur)
|
|
}); err != nil {
|
|
t.Error(err)
|
|
}
|
|
})
|
|
}
|
|
wg.Wait()
|
|
|
|
// home→away→gym or home→gym (the leave then being stale); never ending away.
|
|
got := places(t, s)
|
|
if got[len(got)-1] != "gym" {
|
|
t.Errorf("places = %v, want to end at gym", got)
|
|
}
|
|
}
|