package qaopsmirror import ( "context" "encoding/json" "fmt" "knowledge-governance/internal/dobotaiops" pg "knowledge-governance/internal/platform/postgres" "strings" "testing" "time" ) func TestWeeklyPrefillUsesRealMasterDashboardAndDoesNotOverwriteExistingDraft(t *testing.T) { p := mirrorFixture(t) ctx := context.Background() s := NewStore(p, "fixture-weekly-prefill") queue := pg.NewStore(p) source := &fixtureSource{rows: []dobotaiops.MirrorRecord{fixtureEntry("prefill-event", "existing answer")}} job := claimMirrorJob(t, s, queue, "prefill-source") 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) } record, e := s.GetMasterRecord(ctx, s.tenant, "prefill-event") if e != nil { t.Fatal(e) } year, weekNumber := record.AskedAt.In(dashboardLocation).ISOWeek() week := fmt.Sprintf("%04d-W%02d", year, weekNumber) now := time.Now() start, end, e := weeklyDraftBounds(week, now) if e != nil { t.Fatal(e) } data, e := s.ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: s.tenant, Start: start, End: end}}) if e != nil { t.Fatal(e) } q := WeeklyDraftPrefillRequest{Nonce: "fixture_prefill_week_0001"} receipt, e := s.PrefillWeeklyDraft(ctx, week, q, "fixture-admin", now) if e != nil { t.Fatalf("real-source prefill unavailable: %v", e) } if receipt.Draft.Revision != 1 || !strings.Contains(receipt.Draft.Markdown, fmt.Sprintf("问题总数:%d", data.TotalIssues)) || !strings.Contains(receipt.Draft.Markdown, fmt.Sprintf("New This Week: %d", data.NewThisWeek)) { t.Fatal("prefill was not generated from matching master cutoff") } var seed map[string]any if json.Unmarshal(receipt.Draft.Seed, &seed) != nil || seed["source"] != "governance-master" || seed["week"] != week { t.Fatal("prefill source provenance missing") } edit := WeeklyDraftRequest{BaseRevision: 1, Nonce: "fixture_prefill_user_0002", Markdown: receipt.Draft.Markdown + "\n用户亲写内容,不许替换\n"} if _, e = s.SaveWeeklyDraft(ctx, week, edit, "fixture-admin", now); e != nil { t.Fatal(e) } if _, e = s.PrefillWeeklyDraft(ctx, week, WeeklyDraftPrefillRequest{BaseRevision: 1, Nonce: "fixture_prefill_stale_0003"}, "fixture-admin", now); e == nil { t.Fatal("stale regeneration silently replaced edited content") } fresh, e := s.ReadWeeklyDraft(ctx, week, now) if e != nil || fresh.Revision != 2 || fresh.Markdown != edit.Markdown { t.Fatal("prefill or stale request overwrote user input", e) } replay, e := s.PrefillWeeklyDraft(ctx, week, q, "fixture-admin", now) if e != nil || !replay.Replayed || replay.Draft.Revision != 1 { t.Fatal("prefill retry must return its exact historical receipt", e) } }