package apperr import ( "testing" "time" ) func TestGroupRecords(t *testing.T) { now := time.Date(2026, 9, 7, 10, 0, 0, 0, time.UTC) recs := []Record{ {FP: "a", Source: SourceEdge, Kind: KindHTTP, Name: "500 GET /x", Host: "h1", Release: "v1", Count: 3, First: now.Add(-10 * time.Minute), Last: now.Add(-5 * time.Minute), Stack: "s1"}, {FP: "a", Source: SourceEdge, Kind: KindHTTP, Name: "500 GET /x", Host: "h2", Release: "v2", Count: 2, First: now.Add(-20 * time.Minute), Last: now.Add(-1 * time.Minute), Stack: "s2"}, {FP: "b", Source: SourceLog, Kind: KindLog, Name: "boom", Level: "error", Host: "h1", Count: 1, First: now.Add(-30 * time.Minute), Last: now.Add(-30 * time.Minute)}, {FP: OverflowFP, Source: SourceEdge, Name: "500 GET /z", Host: "h1", Count: 40, First: now, Last: now}, } gs := GroupRecords(recs) if len(gs) != 3 { t.Fatalf("groups = %d", len(gs)) } if gs[0].FP != OverflowFP || gs[1].FP != "a" || gs[2].FP != "b" { t.Fatalf("order by Last desc: %s %s %s", gs[0].FP, gs[1].FP, gs[2].FP) } a := gs[1] if a.Count != 5 || !a.First.Equal(now.Add(-20*time.Minute)) || !a.Last.Equal(now.Add(-1*time.Minute)) { t.Fatalf("a = %+v", a) } if len(a.Hosts) != 2 || a.Hosts[0] != "h1" || a.Hosts[1] != "h2" || len(a.Releases) != 2 { t.Fatalf("a hosts/releases = %v %v", a.Hosts, a.Releases) } if a.Sample.Stack != "s2" { t.Fatal("sample must be the earliest record") } }