package httpapi import ( "context" "knowledge-governance/internal/dobotaiops" "knowledge-governance/internal/qaopsquality" "net/http" "testing" "time" ) type mirroredQualitySource struct { qualitySource reads int } func (s *mirroredQualitySource) ReadStoredQuality(_ context.Context, keys []dobotaiops.QualityKey) (map[string]qaopsquality.View, error) { s.reads++ out := map[string]qaopsquality.View{} for _, key := range keys { out[key.ID] = qaopsquality.Project(key.InputHash, 1, 0, qaopsquality.Check{Result: dobotaiops.CheckMarkerCoverage(true)}, nil) } return out, nil } func TestMirroredQualityQueriesDoNotRepeatObservationWrites(t *testing.T) { source := &mirroredQualitySource{qualitySource: qualitySource{record: dobotaiops.Record{ID: "fixture-turn", Question: "fixture", Answer: "暂无已发布"}}} e, _ := qaopsquality.EvidenceFor(source.record) source.record.QualityInputHash = e.Hash legacy := &qualityFake{fail: true} auth := newFakeAuthStore() auth.sessions["quality-admin"] = "usr-boss" h, e2 := NewHandler(Config{Store: &fakeJobStore{}, Auth: &AuthConfig{Store: auth}, QAOps: source, QAOpsTenantKey: "fixed", QAQuality: legacy, Now: func() time.Time { return time.Date(2026, 10, 17, 0, 0, 0, 0, time.UTC) }}) if e2 != nil { t.Fatal(e2) } for _, q := range []struct{ method, path, body string }{{"GET", "/api/v1/qa-ops/records/fixture-turn", ""}, {"GET", "/api/v1/qa-ops/records?start=2026-10-14T00:00:00Z&end=2026-10-15T00:00:00Z", ""}, {"POST", "/api/v1/qa-ops/quality/query", `{"start":"2026-10-14T00:00:00Z","end":"2026-10-15T00:00:00Z","keyword":""}`}} { w := qualityRequest(h, q.method, q.path, "quality-admin", q.body) if w.Code != http.StatusOK { t.Fatal("local mirror read went back through observation writes", q.path, w.Code) } } if source.reads != 3 || legacy.calls != 0 { t.Fatal("batched stored projection not used", source.reads, legacy.calls) } }