114 lines
4.4 KiB
Go
114 lines
4.4 KiB
Go
package qaopsmirror
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/dobotaiops"
|
|
pg "knowledge-governance/internal/platform/postgres"
|
|
)
|
|
|
|
// A monthly board must cut every panel from the selected range: the in-range
|
|
// count, the daily trend, the strict quality sample and the previous range.
|
|
func TestMonthlyGranularityCutsEveryPanelFromTheSelectedRange(t *testing.T) {
|
|
pool := mirrorFixture(t)
|
|
ctx := context.Background()
|
|
s := NewStore(pool, "fixture-monthly-period")
|
|
queue := pg.NewStore(pool)
|
|
rows := make([]dobotaiops.MirrorRecord, 0, 3)
|
|
for _, spec := range []struct{ id, askedAt string }{
|
|
{"oct-record", "2026-10-15T08:00:00+08:00"},
|
|
{"nov-early", "2026-11-02T08:00:00+08:00"},
|
|
{"nov-late", "2026-11-30T08:00:00+08:00"},
|
|
} {
|
|
entry := fixtureEntry(spec.id, "已有资料")
|
|
var wire map[string]any
|
|
if err := json.Unmarshal(entry.Payload, &wire); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wire["asked_at"] = spec.askedAt
|
|
entry.Payload, _ = json.Marshal(wire)
|
|
rows = append(rows, entry)
|
|
}
|
|
job := claimMirrorJob(t, s, queue, "monthly-period-seed")
|
|
if err := s.HandleSync(ctx, *job, func(context.Context) (Source, error) {
|
|
return &fixtureSource{rows: rows}, nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start, _ := time.Parse(time.RFC3339, "2026-11-01T00:00:00+08:00")
|
|
end, _ := time.Parse(time.RFC3339, "2026-12-01T00:00:00+08:00")
|
|
// The handler builds the window through dobotaiops.NewWindow, which
|
|
// normalises both ends to UTC. Feed the store the same shape here, otherwise
|
|
// this test keeps a +08 location the production path never has.
|
|
start, end = start.UTC(), end.UTC()
|
|
got, err := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{
|
|
Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end},
|
|
Granularity: dashboardGranularityMonth,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Period == nil || got.Period.Kind != dashboardGranularityMonth || got.Period.Label != "2026-11" {
|
|
t.Fatalf("period = %+v", got.Period)
|
|
}
|
|
if !got.Period.PrevStart.Equal(start.AddDate(0, -1, 0)) || !got.Period.PrevEnd.Equal(start) {
|
|
t.Fatalf("previous range = %s..%s", got.Period.PrevStart, got.Period.PrevEnd)
|
|
}
|
|
if got.PeriodIssues == nil || *got.PeriodIssues != 2 {
|
|
t.Fatalf("period issues = %v, want 2", got.PeriodIssues)
|
|
}
|
|
if got.PrevPeriodIssues == nil || *got.PrevPeriodIssues != 1 {
|
|
t.Fatalf("previous period issues = %v, want 1", got.PrevPeriodIssues)
|
|
}
|
|
if got.TotalIssues != 3 {
|
|
t.Fatalf("cumulative total = %d, want 3 (everything before the range end)", got.TotalIssues)
|
|
}
|
|
if len(got.Daily) != 30 || got.Daily[0].Date != "2026-11-01" || got.Daily[29].Date != "2026-11-30" {
|
|
t.Fatalf("daily trend does not cover the whole month: %d points, first=%v", len(got.Daily), got.Daily[0])
|
|
}
|
|
sum := 0
|
|
for _, point := range got.Daily {
|
|
sum += point.Count
|
|
}
|
|
if sum != *got.PeriodIssues || got.NewThisWeek != *got.PeriodIssues {
|
|
t.Fatalf("daily sum=%d, new_this_week=%d, period=%d", sum, got.NewThisWeek, *got.PeriodIssues)
|
|
}
|
|
if got.PeriodQuality == nil || got.PeriodQuality.Records != got.Quality.Total {
|
|
t.Fatalf("period quality = %+v, quality total = %d", got.PeriodQuality, got.Quality.Total)
|
|
}
|
|
}
|
|
|
|
// Omitting granularity must keep the weekly board exactly as before.
|
|
func TestWeeklyBoardKeepsLegacyPanelsWithoutGranularity(t *testing.T) {
|
|
pool := mirrorFixture(t)
|
|
ctx := context.Background()
|
|
s := NewStore(pool, "fixture-weekly-legacy")
|
|
queue := pg.NewStore(pool)
|
|
entry := fixtureEntry("legacy-week", "已有资料")
|
|
var wire map[string]any
|
|
_ = json.Unmarshal(entry.Payload, &wire)
|
|
wire["asked_at"] = "2026-11-30T08:00:00+08:00"
|
|
entry.Payload, _ = json.Marshal(wire)
|
|
job := claimMirrorJob(t, s, queue, "legacy-week-seed")
|
|
if err := s.HandleSync(ctx, *job, func(context.Context) (Source, error) {
|
|
return &fixtureSource{rows: []dobotaiops.MirrorRecord{entry}}, nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start, _ := time.Parse(time.RFC3339, "2026-11-30T00:00:00+08:00")
|
|
end, _ := time.Parse(time.RFC3339, "2026-12-03T00:00:00+08:00")
|
|
got, err := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Period != nil || got.PeriodIssues != nil || got.PeriodQuality != nil || got.Granularity != "" {
|
|
t.Fatalf("granularity leaked into the legacy board: %+v", got.Period)
|
|
}
|
|
if got.NewThisWeek != 1 || len(got.Daily) != 3 {
|
|
t.Fatalf("legacy weekly panels changed: new=%d daily=%d", got.NewThisWeek, len(got.Daily))
|
|
}
|
|
}
|