package service import ( "testing" "time" "github.com/freefire/jiang13-bbs/model" ) func TestSummarizePageviews(t *testing.T) { day := time.Date(2026, 9, 24, 0, 0, 0, 0, time.Local) at := func(hour int) time.Time { return day.Add(time.Duration(hour) * time.Hour) } events := []model.VisitEvent{ {Kind: model.VisitKindPageview, CreatedAt: at(9), UserID: 1, VidHash: "aaa"}, {Kind: model.VisitKindPageview, CreatedAt: at(9), VidHash: "aaa"}, // 同 vid 同日:去重 {Kind: model.VisitKindPageview, CreatedAt: at(10), VidHash: "bbb"}, {Kind: model.VisitKindBot, CreatedAt: at(9), VidHash: "aaa"}, // bot 不计 {Kind: model.VisitKindProbe, CreatedAt: at(9), VidHash: "bbb"}, // probe 不计 } out := summarizePageviews(events) d, ok := out[day] if !ok { t.Fatal("缺少当日聚合") } if d.PV != 3 { t.Errorf("PV = %d, want 3", d.PV) } if d.LoggedInPV != 1 { t.Errorf("LoggedInPV = %d, want 1", d.LoggedInPV) } if len(d.VidHashes) != 2 { t.Errorf("UV 候选 = %d, want 2", len(d.VidHashes)) } } func TestHashVisitorID(t *testing.T) { a := HashVisitorID(" ABcd1234-0000-0000-0000-000000000000 ") b := HashVisitorID("abcd1234-0000-0000-0000-000000000000") if a != b { t.Error("大小写/空白应归一后哈希一致") } if len(a) != 64 { t.Errorf("哈希长度 = %d, want 64", len(a)) } }