package qaopsmirror import ( "context" "encoding/json" "errors" "github.com/jackc/pgx/v5/pgconn" "knowledge-governance/internal/dobotaiops" pg "knowledge-governance/internal/platform/postgres" "strings" "testing" "time" ) func TestReportedHistoryIsIndependentExactScopedAndImmutable(t *testing.T) { p := mirrorFixture(t) ctx := context.Background() s := NewStore(p, "fixture-reported-history") insert := `INSERT INTO qa_ops_reported_weekly_rates(tenant_key,week_key,week_start,week_end,coverage_bp,correctness_bp,source_sha256,imported_by) VALUES($1,'2026WK40','2026-09-28T00:00:00+08:00','2026-10-05T00:00:00+08:00',5723,9234,$2,'fixture-admin')` if _, err := p.Exec(ctx, insert, s.tenant, strings.Repeat("a", 64)); err != nil { t.Fatal(err) } start, _ := time.Parse(time.RFC3339, "2026-10-05T00:00:00+08:00") end := start.AddDate(0, 0, 7) read := func(tenant, keyword string, until time.Time) map[string]any { t.Helper() data, err := NewStore(p, tenant).ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: tenant, Start: until.AddDate(0, 0, -7), End: until}, Keyword: keyword}) if err != nil { t.Fatal(err) } raw, _ := json.Marshal(data) var wire map[string]any if json.Unmarshal(raw, &wire) != nil { t.Fatal("bad dashboard JSON") } return wire } data := read(s.tenant, "", end) history, ok := data["reported_weeks"].([]any) if !ok || len(history) != 1 { t.Fatalf("reported percent-only history was not exposed independently: present=%v", ok) } row := history[0].(map[string]any) if row["coverage_percent"] != 57.23 || row["correctness_percent"] != 92.34 || row["source"] != "user_provided_history" || row["week"] != "2026WK40" { t.Fatal("reported precision or provenance lost") } for _, forbidden := range []string{"numerator", "denominator", "records", "frozen_at"} { if _, exists := row[forbidden]; exists { t.Fatal("invented historical evidence", forbidden) } } if len(data["frozen_weeks"].([]any)) != 0 { t.Fatal("historical input manufactured frozen evidence") } for _, wire := range []map[string]any{read(s.tenant, "needle", end), read("fixture-other-tenant", "", end), read(s.tenant, "", start.Add(-time.Hour))} { if value, ok := wire["reported_weeks"].([]any); !ok || len(value) != 0 { t.Fatal("history leaked across keyword, tenant or closing cutoff") } } for _, query := range []string{`UPDATE qa_ops_reported_weekly_rates SET coverage_bp=123 WHERE tenant_key=$1`, `DELETE FROM qa_ops_reported_weekly_rates WHERE tenant_key=$1`} { tx, e := p.Begin(ctx) if e != nil { t.Fatal(e) } _, e = tx.Exec(ctx, query, s.tenant) _ = tx.Rollback(ctx) var db *pgconn.PgError if !errors.As(e, &db) || db.Code != "55000" { t.Fatal("reported history could be overwritten or deleted") } } // The assertion belongs to migration 57, not whichever newer empty schema is last. for { var version int64 if e := p.QueryRow(ctx, `SELECT max(version)FROM schema_migrations`).Scan(&version); e != nil { t.Fatal(e) } if version == 57 { break } if version < 57 { t.Fatal("reported-history migration missing") } if e := pg.RollbackLatestMigration(ctx, p); e != nil { t.Fatalf("could not reach the historical-data rollback guard: %v", e) } } e := pg.RollbackLatestMigration(ctx, p) var failure *pgconn.PgError if !errors.As(e, &failure) || failure.Code != "55000" { t.Fatal("down did not explicitly protect historical data", e) } }