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:
+4
-18
@@ -195,9 +195,7 @@ func (s *Server) ingestEvents(spec ingest.Spec) http.HandlerFunc {
|
||||
|
||||
// ingestLocation takes an arrive or leave from a phone geofence automation.
|
||||
// The place comes from the path (/arrive/gym), ?place=, the body's "place",
|
||||
// or defaults to home.
|
||||
// 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.
|
||||
// or defaults to home. ingest.Destination decides where the device ends up.
|
||||
func (s *Server) ingestLocation(w http.ResponseWriter, r *http.Request) {
|
||||
event := r.PathValue("event")
|
||||
if event != "arrive" && event != "leave" {
|
||||
@@ -223,25 +221,13 @@ func (s *Server) ingestLocation(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
dev := deviceFrom(r.Context()).ID
|
||||
cur, err := s.store.CurrentPlace(r.Context(), dev)
|
||||
place, changed, err := s.store.MoveTo(r.Context(), deviceFrom(r.Context()).ID, e.At,
|
||||
func(cur string) string { return ingest.Destination(event, e.Place, cur) })
|
||||
if err != nil {
|
||||
s.internalError(w, r, err)
|
||||
return
|
||||
}
|
||||
target := e.Place
|
||||
if event == "leave" {
|
||||
target = ingest.Away
|
||||
if cur != "" && cur != e.Place {
|
||||
target = cur
|
||||
}
|
||||
}
|
||||
changed, err := s.store.MoveTo(r.Context(), dev, target, e.At)
|
||||
if err != nil {
|
||||
s.internalError(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"place": target, "changed": changed})
|
||||
writeJSON(w, http.StatusOK, map[string]any{"place": place, "changed": changed})
|
||||
}
|
||||
|
||||
func (s *Server) activities(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -7,40 +7,20 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"ccp/internal/auth"
|
||||
"ccp/internal/migrations"
|
||||
"ccp/internal/store"
|
||||
"ccp/internal/testdb"
|
||||
)
|
||||
|
||||
// Requires a disposable database, e.g. the one from deploy/docker-compose.yml:
|
||||
//
|
||||
// TEST_DATABASE_URL=postgres://tracker:tracker@localhost:5432/tracker go test ./internal/api
|
||||
// Needs TEST_DATABASE_URL; see internal/testdb.
|
||||
func setup(t *testing.T) (*httptest.Server, string) {
|
||||
url := os.Getenv("TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
t.Skip("TEST_DATABASE_URL not set")
|
||||
}
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if err := migrations.Up(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `TRUNCATE activities, samples, goals, devices RESTART IDENTITY`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
st := store.New(pool)
|
||||
st := store.New(testdb.New(t))
|
||||
_, key, err := st.CreateDevice(ctx, "test", "hyprland")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user