Files
ccp/internal/ingest/ingest_test.go
adamfandClaude Opus 5.5 cfe17ecf5d
Test and publish / test (push) Has been cancelled
Test and publish / publish (push) Has been cancelled
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]>
2026-10-14 22:48:41 +01:00

80 lines
2.3 KiB
Go

package ingest
import (
"strings"
"testing"
"time"
)
func TestNormalizeValidation(t *testing.T) {
now := time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)
ok := Event{ID: "1", Kind: "app_focus", Label: "kitty", StartedAt: now.Add(-time.Hour), EndedAt: now}
tests := []struct {
name string
mutate func(*Event)
wantErr string
}{
{"valid", func(e *Event) {}, ""},
{"missing id", func(e *Event) { e.ID = "" }, "id"},
{"wrong kind", func(e *Event) { e.Kind = "tab_focus" }, "not accepted"},
{"reversed", func(e *Event) { e.EndedAt = e.StartedAt.Add(-time.Second) }, "before"},
{"future", func(e *Event) { e.EndedAt = now.Add(time.Hour) }, "future"},
{"too long", func(e *Event) { e.StartedAt = now.Add(-8 * 24 * time.Hour) }, "longer"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := ok
tt.mutate(&e)
acts, err := Desktop.Normalize(Batch{Events: []Event{e}}, now)
if tt.wantErr == "" {
if err != nil || len(acts) != 1 || acts[0].Source != "hyprland" {
t.Fatalf("got %+v, %v", acts, err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("got err %v, want containing %q", err, tt.wantErr)
}
})
}
}
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)
}
e, err = NormalizeLocation(LocationEvent{Place: " Gym "}, now)
if err != nil || e.Place != "gym" {
t.Fatalf("got %+v, %v", e, err)
}
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)
}
}
}
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)
}
}
}