package hypr import ( "path/filepath" "testing" "time" "ccp/internal/ingest" ) func TestParseActiveWindow(t *testing.T) { event, data, ok := ParseLine("activewindow>>firefox,Inbox, 3 unread - Mozilla Firefox") if !ok || event != "activewindow" { t.Fatalf("ParseLine: got %q %v", event, ok) } got := ParseActiveWindow(data) want := Focus{Class: "firefox", Title: "Inbox, 3 unread - Mozilla Firefox"} if got != want { t.Fatalf("got %+v, want %+v", got, want) } if got := ParseActiveWindow(","); got != (Focus{}) { t.Fatalf("empty focus: got %+v", got) } } func TestSessionizer(t *testing.T) { t0 := time.Date(2026, 10, 14, 9, 0, 0, 0, time.UTC) at := func(sec int) time.Time { return t0.Add(time.Duration(sec) * time.Second) } var got []ingest.Event s := &Sessionizer{MinDuration: 2 * time.Second, Emit: func(e ingest.Event) { got = append(got, e) }} kitty := Focus{Class: "kitty", Title: "nvim"} ff := Focus{Class: "firefox", Title: "docs"} s.Focus(at(0), kitty) s.Focus(at(0), kitty) // duplicate event: no new interval s.Focus(at(60), ff) s.Focus(at(61), kitty) // firefox for 1s: dropped as flicker s.Pause(at(120), "idle") s.Focus(at(130), ff) // focus change while idle is remembered, not recorded s.Pause(at(300), "locked") s.Resume(at(600)) s.Close(at(660)) want := []struct { kind, label string start, end int }{ {"app_focus", "kitty", 0, 60}, {"app_focus", "kitty", 61, 120}, {"idle", "idle", 120, 300}, {"locked", "locked", 300, 600}, {"app_focus", "firefox", 600, 660}, } if len(got) != len(want) { t.Fatalf("got %d events, want %d: %+v", len(got), len(want), got) } for i, w := range want { g := got[i] if g.Kind != w.kind || g.Label != w.label || !g.StartedAt.Equal(at(w.start)) || !g.EndedAt.Equal(at(w.end)) { t.Errorf("event %d: got %s/%s %v-%v, want %+v", i, g.Kind, g.Label, g.StartedAt, g.EndedAt, w) } if g.ID == "" { t.Errorf("event %d has no ID", i) } } if got[0].Attrs["title"] != "nvim" { t.Errorf("title not recorded: %+v", got[0].Attrs) } } func TestBufferPersists(t *testing.T) { path := filepath.Join(t.TempDir(), "buf.jsonl") b, err := OpenBuffer(path) if err != nil { t.Fatal(err) } for _, id := range []string{"a", "b", "c"} { if err := b.Add(ingest.Event{ID: id, Kind: "app_focus"}); err != nil { t.Fatal(err) } } if err := b.Ack(2); err != nil { t.Fatal(err) } reopened, err := OpenBuffer(path) if err != nil { t.Fatal(err) } if ev := reopened.Peek(10); len(ev) != 1 || ev[0].ID != "c" { t.Fatalf("after reopen got %+v, want only c", ev) } }