package ingest import ( "encoding/json" "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 TestNormalizeHAE(t *testing.T) { raw := `{"data":{ "workouts":[{"id":"W1","name":"Outdoor Walk","start":"2026-10-14 07:00:00 -0700","end":"2026-10-14 07:45:00 -0700","location":"Outdoor"}], "metrics":[ {"name":"step_count","units":"count","data":[{"date":"2026-10-14 07:00:00 -0700","qty":812,"source":"iPhone"},{"date":"bad"}]}, {"name":"sleep_analysis","units":"hr","data":[{"sleepStart":"2026-10-13 23:10:00 -0700","sleepEnd":"2026-10-14 06:40:00 -0700","asleep":7.1}]} ]}}` var p HAEPayload if err := json.Unmarshal([]byte(raw), &p); err != nil { t.Fatal(err) } acts, samples, err := NormalizeHAE(p) if err != nil { t.Fatal(err) } if len(acts) != 2 || len(samples) != 1 { t.Fatalf("got %d activities, %d samples", len(acts), len(samples)) } walk := acts[0] if walk.Kind != "walk" || walk.ExternalID != "W1" || walk.EndedAt.Sub(walk.StartedAt) != 45*time.Minute { t.Errorf("walk: %+v", walk) } if walk.StartedAt.Hour() != 14 { // 07:00 -0700 is 14:00 UTC t.Errorf("start not converted to UTC: %v", walk.StartedAt) } if acts[1].Kind != "sleep" || samples[0].Value != 812 { t.Errorf("sleep %+v, sample %+v", acts[1], samples[0]) } }