package httpapi import ( "context" "encoding/json" "knowledge-governance/internal/dobotaiops" "knowledge-governance/internal/qaopsquality" "testing" ) type downvoteSnapshotReader struct { QAOpsReader view qaopsquality.View } func (s downvoteSnapshotReader) ReadStoredQuality(context.Context, []dobotaiops.QualityKey) (map[string]qaopsquality.View, error) { return map[string]qaopsquality.View{"fixture-feedback-race": s.view}, nil } func TestMasterQualityEnrichmentUsesOneFeedbackDecisionEvenWhenVoteChangesBetweenReads(t *testing.T) { for _, test := range []struct { name string old, current dobotaiops.RecordFeedback label, correctness, rule string }{ {"new-downvote", dobotaiops.RecordFeedback{}, dobotaiops.RecordFeedback{Down: 1}, "否", "incorrect", "downvote-is-incorrect-v1"}, {"withdrawn-current-vote", dobotaiops.RecordFeedback{Down: 1}, dobotaiops.RecordFeedback{}, "是", "correct", ""}, } { t.Run(test.name, func(t *testing.T) { view := qaopsquality.View{EffectiveSource: "master-effective-fields-v1", FeedbackRule: test.rule, Effective: qaopsquality.Fields{Correctness: test.correctness}, MasterFeedback: &test.current} c := Config{QAOps: downvoteSnapshotReader{view: view}, QAQuality: &qualityFake{}} item := dobotaiops.Record{ID: "fixture-feedback-race", RecordOrigin: "dobotai_2_0", Feedback: test.old, MasterFields: map[string]string{"is_correct": "待确认"}} if err := enrichQAQualityRecord(context.Background(), c, &item); err != nil { t.Fatal(err) } if item.Feedback != test.current || item.MasterFields["is_correct"] != test.label { t.Fatal("detail combines an old feedback snapshot with a newer quality decision") } page := dobotaiops.RecordPage{Items: []dobotaiops.Record{{ID: item.ID, RecordOrigin: item.RecordOrigin, Feedback: test.old, MasterFields: map[string]string{"is_correct": "待确认"}}}} // The common projection must be shared by the list and detail enrichment paths. if err := enrichQAQualityPage(context.Background(), c, &page); err != nil { t.Fatal(err) } if page.Items[0].Feedback != test.current || page.Items[0].MasterFields["is_correct"] != test.label { t.Fatal("list combines an old feedback snapshot with a newer quality decision") } var quality map[string]any if json.Unmarshal(item.Quality, &quality) != nil { t.Fatal("quality response missing") } }) } }