package qaopsmirror import ( "context" "encoding/json" "knowledge-governance/internal/dobotaiops" pg "knowledge-governance/internal/platform/postgres" "testing" "time" ) func TestMasterLiveCumulativeWeeksClampNowAndKeepZeroDenominatorsNull(t *testing.T) { p := mirrorFixture(t) ctx := context.Background() s := NewStore(p, "fixture-cumulative-zero") tx, err := p.Begin(ctx) if err != nil { t.Fatal(err) } defer tx.Rollback(ctx) future := time.Now().AddDate(0, 0, 30) points, err := s.readMasterCumulativeDashboard(ctx, tx, "", future) if err != nil { t.Fatal(err) } if len(points) != 4 { t.Fatal("expected the four target weeks on current acceptance date") } for _, point := range points { expected := point.WeekEnd if point.CalculatedAt.Before(expected) { expected = point.CalculatedAt } if future.Before(expected) { expected = future } if !point.CutoffAt.Equal(expected) || point.Records != 0 || point.Coverage.Denominator != 0 || point.Correctness.Denominator != 0 || point.Coverage.Percent != nil || point.Correctness.Percent != nil { t.Fatal("empty live cumulative point fabricated a rate or used a future cutoff") } } } func TestMasterLiveCumulativeWeeksUseWholeLedgerAndWeeklyStrictRates(t *testing.T) { p := mirrorFixture(t) ctx := context.Background() s := NewStore(p, "fixture-live-cumulative") queue := pg.NewStore(p) dates := []string{"2026-09-20T08:00:00+08:00", "2026-09-28T08:00:00+08:00", "2026-10-05T00:00:00+08:00", "2026-10-12T00:00:00+08:00", "2026-10-19T00:00:00+08:00"} ids := []string{"older", "wk40", "boundary41", "wk42partial", "wk43pending"} coverage := []string{"是", "是", "否", "部分覆盖", "是"} correct := []string{"是", "否", "是", "否", "待确认"} source := &fixtureSource{} for i, at := range dates { entry := fixtureEntry(ids[i], "已有正文") var wire map[string]any _ = json.Unmarshal(entry.Payload, &wire) wire["asked_at"] = at entry.Payload, _ = json.Marshal(wire) source.rows = append(source.rows, entry) } job := claimMirrorJob(t, s, queue, "seed-live-cumulative") if e := s.HandleSync(ctx, *job, func(context.Context) (Source, error) { return source, nil }); e != nil { t.Fatal(e) } if e := queue.Complete(ctx, job.ID, job.LeaseOwner, time.Now()); e != nil { t.Fatal(e) } for i, id := range ids { fields, _ := json.Marshal(map[string]string{"coverage": coverage[i], "is_correct": correct[i]}) if _, e := p.Exec(ctx, "UPDATE qa_ops_master_records SET local_fields=local_fields||$3::jsonb WHERE tenant_key=$1 AND id=$2", s.tenant, id, fields); e != nil { t.Fatal(e) } } start, _ := time.Parse(time.RFC3339, "2026-10-19T00:00:00+08:00") end := start.AddDate(0, 0, 1) data, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}}) if e != nil { t.Fatal(e) } raw, _ := json.Marshal(data) var wire map[string]any _ = json.Unmarshal(raw, &wire) points, ok := wire["cumulative_weeks"].([]any) if !ok || len(points) != 4 { t.Fatal("missing current master cumulative W40-W43; frozen snapshots are not a substitute") } expected := [][5]float64{{2, 2, 2, 1, 2}, {3, 2, 3, 1, 2}, {4, 2, 3, 1, 3}, {5, 3, 4, 1, 3}} for i, item := range points { point := item.(map[string]any) cov := point["coverage"].(map[string]any) acc := point["correctness"].(map[string]any) want := expected[i] if point["records"] != want[0] || cov["numerator"] != want[1] || cov["denominator"] != want[2] || acc["numerator"] != want[3] || acc["denominator"] != want[4] { t.Fatalf("week %d is not whole-ledger strict weekly numerator/denominator: %v", i, point) } if point["source"] != "master_live_cumulative_v1" || point["policy_version"] != "weekly-strict-yes-over-yes-no-v1" { t.Fatal("live cumulative provenance missing") } } var before string if e = p.QueryRow(ctx, `SELECT md5(string_agg(md5(payload::text||original_fields::text||local_fields::text),'' ORDER BY id)) FROM qa_ops_master_records WHERE tenant_key=$1`, s.tenant).Scan(&before); e != nil { t.Fatal(e) } if _, e = p.Exec(ctx, `UPDATE qa_ops_mirror_records SET payload=jsonb_set(payload,'{feedback}','{"asker_value":"down","down":1,"up":0}')WHERE tenant_key=$1 AND id='older'`, s.tenant); e != nil { t.Fatal(e) } fresh, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}}) if e != nil { t.Fatal(e) } if len(fresh.CumulativeWeeks) != 4 || fresh.CumulativeWeeks[0].Correctness.Numerator != 0 || fresh.CumulativeWeeks[1].Correctness.Numerator != 0 || fresh.CumulativeWeeks[3].Correctness.Numerator != 0 { t.Fatal("later source feedback did not update every affected cumulative week") } filtered, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}, Keyword: "\n"}) if e != nil { t.Fatal(e) } if len(filtered.CumulativeWeeks) != 4 || filtered.CumulativeWeeks[3].Records != filtered.TotalIssues { t.Fatal("cumulative keyword scope diverged from dashboard totals") } for _, point := range filtered.CumulativeWeeks { if point.Records > filtered.TotalIssues { t.Fatal("cumulative keyword scope exceeded dashboard totals") } } var after string if e = p.QueryRow(ctx, `SELECT md5(string_agg(md5(payload::text||original_fields::text||local_fields::text),'' ORDER BY id)) FROM qa_ops_master_records WHERE tenant_key=$1`, s.tenant).Scan(&after); e != nil || after != before { t.Fatal("cumulative reads or feedback rewrote master business fields", e) } arrival := fixtureEntry("later-arrival", "新记录的成功非空答案") var arrivalWire map[string]any _ = json.Unmarshal(arrival.Payload, &arrivalWire) arrivalWire["asked_at"] = start.Add(time.Hour).Format(time.RFC3339) arrival.Payload, _ = json.Marshal(arrivalWire) source.rows = append(source.rows, arrival) nextJob := claimMirrorJob(t, s, queue, "append-live-cumulative-arrival") if e = s.HandleSync(ctx, *nextJob, func(context.Context) (Source, error) { return source, nil }); e != nil { t.Fatal(e) } appended, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}}) if e != nil { t.Fatal(e) } for i, point := range appended.CumulativeWeeks { previous := fresh.CumulativeWeeks[i] if i < 3 { if point.Records != previous.Records || point.Coverage.Denominator != previous.Coverage.Denominator { t.Fatal("new current-week event changed an earlier cutoff membership") } continue } if point.Records != previous.Records+1 || point.Coverage.Numerator != previous.Coverage.Numerator+1 || point.Coverage.Denominator != previous.Coverage.Denominator+1 || point.Correctness.Numerator != previous.Correctness.Numerator || point.Correctness.Denominator != previous.Correctness.Denominator { t.Fatal("new event failed to update live cumulative counts or manufactured correctness") } } if e = p.QueryRow(ctx, `SELECT md5(string_agg(md5(payload::text||original_fields::text||local_fields::text),'' ORDER BY id))FROM qa_ops_master_records WHERE tenant_key=$1 AND id<>'later-arrival'`, s.tenant).Scan(&after); e != nil || after != before { t.Fatal("source append overwrote existing master rows", e) } }