package qaopsmirror import ( "context" "knowledge-governance/internal/dobotaiops" pg "knowledge-governance/internal/platform/postgres" "knowledge-governance/internal/qaopsquality" "testing" "time" ) func TestMasterHumanReviewIsIndependentOfSourceInputUpdates(t *testing.T) { p := mirrorFixture(t) ctx := context.Background() s := NewStore(p, "fixture-master-quality") queue := pg.NewStore(p) source := &fixtureSource{rows: []dobotaiops.MirrorRecord{fixtureEntry("master-review", "first")}} job := claimMirrorJob(t, s, queue, "review-first") 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) } rec, e := s.GetMasterRecord(ctx, s.tenant, "master-review") if e != nil { t.Fatal(e) } quality := s.MasterQualityStore() key := []dobotaiops.QualityKey{{ID: rec.ID, InputHash: rec.QualityInputHash}} views, e := quality.ReadViews(ctx, key) if e != nil { t.Fatal(e) } v := views[rec.ID] request := qaopsquality.SaveRequest{InputHash: v.InputHash, InputEpoch: v.InputEpoch, BaseRevision: v.Revision, Nonce: "fixture_master_review_001", Reason: "fixture only", Fields: qaopsquality.Fields{Coverage: "covered", Correctness: "correct", ProductCategory: "fixture-local"}} if _, e = quality.Save(ctx, rec, request, "fixture-admin"); e != nil { t.Fatal(e) } source.rows = []dobotaiops.MirrorRecord{fixtureEntry("master-review", "changed at source")} job = claimMirrorJob(t, s, queue, "review-source-change") if e = s.HandleSync(ctx, *job, func(context.Context) (Source, error) { return source, nil }); e != nil { t.Fatal(e) } views, e = quality.ReadViews(ctx, key) if e != nil || views[rec.ID].State != "reviewed" || views[rec.ID].Effective.ProductCategory != "fixture-local" { t.Fatal("source sync invalidated a locally-owned master review", e) } current, e := qaopsquality.NewStore(p, s.tenant).ReadViews(ctx, []dobotaiops.QualityKey{{ID: rec.ID, InputHash: sourceRecordHash(t, source.rows[0])}}) if e != nil || current[rec.ID].Revision != 0 { t.Fatal("master review modified original source evaluation", e) } } func sourceRecordHash(t *testing.T, r dobotaiops.MirrorRecord) string { t.Helper() v, e := dobotaiops.DecodeMirrorPayload(string(r.Payload)) if e != nil { t.Fatal(e) } return v.QualityInputHash }