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) } } }