package qaopsmirror import ( "knowledge-governance/internal/qaopsquality" "testing" "time" ) func monthlyTestTime(t *testing.T, value string) time.Time { t.Helper() at, err := time.Parse(time.RFC3339, value) if err != nil { t.Fatal(err) } return at } func TestQualityMonthCrossingWeekIsPreviousMonthReport(t *testing.T) { for _, c := range []struct{ name, start, end, now, wantStart, wantEnd string }{ {"cross-month", "2026-09-28T00:00:00+08:00", "2026-10-05T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2026-09-01T00:00:00+08:00", "2026-10-01T00:00:00+08:00"}, {"within-month", "2026-10-05T00:00:00+08:00", "2026-10-12T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2026-10-01T00:00:00+08:00", "2026-10-12T00:00:00+08:00"}, {"current-week-clamps-now", "2026-10-19T00:00:00+08:00", "2026-10-26T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2026-10-01T00:00:00+08:00", "2026-10-21T08:00:00+08:00"}, {"cross-year", "2025-12-29T00:00:00+08:00", "2026-01-05T00:00:00+08:00", "2026-10-21T08:00:00+08:00", "2025-12-01T00:00:00+08:00", "2026-01-01T00:00:00+08:00"}, {"utc-same-boundary", "2026-09-27T16:00:00Z", "2026-10-04T16:00:00Z", "2026-10-21T00:00:00Z", "2026-09-01T00:00:00+08:00", "2026-10-01T00:00:00+08:00"}, {"crossing-week-before-month-complete", "2026-09-28T00:00:00+08:00", "2026-09-29T08:00:00+08:00", "2026-09-29T08:00:00+08:00", "2026-09-01T00:00:00+08:00", "2026-09-29T08:00:00+08:00"}, } { t.Run(c.name, func(t *testing.T) { begin, end := dashboardQualityMonth(monthlyTestTime(t, c.start), monthlyTestTime(t, c.end), monthlyTestTime(t, c.now)) if !begin.Equal(monthlyTestTime(t, c.wantStart)) || !end.Equal(monthlyTestTime(t, c.wantEnd)) { t.Fatalf("quality month = [%s,%s), want [%s,%s)", begin, end, c.wantStart, c.wantEnd) } }) } } func TestStrictQualityLeavesUnjudgeableAnswersOutOfTheCorrectnessDenominator(t *testing.T) { start := monthlyTestTime(t, "2026-10-01T00:00:00+08:00") end := monthlyTestTime(t, "2026-11-01T00:00:00+08:00") at := start.Add(time.Hour) view := func(coverage, correctness, cause string) qaopsquality.View { return qaopsquality.View{State: "unreviewed", Effective: qaopsquality.Fields{Coverage: coverage, Correctness: correctness, RootCause: cause}} } records := []dashboardTimedQuality{ {id: "correct", at: at, view: view("covered", "correct", "")}, {id: "real-error", at: at, view: view("covered", "incorrect", "答案生成错误")}, {id: "unclear", at: at, view: view("covered", "incorrect", dashboardUnclearCause)}, {id: "unlabelled", at: at, view: view("covered", "incorrect", "")}, {id: "pending", at: at, view: view("uncovered", "pending", "")}, } got := dashboardStrictQuality(records, start, end) if got.Correctness.Numerator != 1 || got.Correctness.Denominator != 2 { t.Fatalf("correctness = %d/%d, want 1/2 (only genuinely judged answers)", got.Correctness.Numerator, got.Correctness.Denominator) } if got.Unjudged != 2 { t.Fatalf("unjudged = %d, want 2 (结果不清 + missing root cause)", got.Unjudged) } if got.Coverage.Numerator != 4 || got.Coverage.Denominator != 5 { t.Fatalf("coverage = %d/%d, want 4/5 unchanged by the correctness rule", got.Coverage.Numerator, got.Coverage.Denominator) } if dashboardMonthlyPolicy != "calendar-month-strict-yes-over-yes-no-v2" { t.Fatal("policy version must change when the denominator changes") } }