package qaopsmirror import ( "context" "encoding/json" "knowledge-governance/internal/dobotaiops" pg "knowledge-governance/internal/platform/postgres" "testing" "time" ) func TestMasterInlineFieldPatchPersistsOnlyChosenField(t *testing.T) { p := mirrorFixture(t) ctx := context.Background() s := NewStore(p, "fixture-inline-fields") queue := pg.NewStore(p) source := &fixtureSource{rows: []dobotaiops.MirrorRecord{fixtureEntry("inline-field", "first")}} job := claimMirrorJob(t, s, queue, "inline-initial") 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, "inline-field") if e != nil { t.Fatal(e) } keys := []dobotaiops.QualityKey{{ID: rec.ID, InputHash: rec.QualityInputHash}} before, e := s.ReadMasterQualityViews(ctx, keys) if e != nil { t.Fatal(e) } var original string if e = p.QueryRow(ctx, `SELECT md5(payload::text||local_fields::text) FROM qa_ops_master_records WHERE tenant_key=$1 AND id=$2`, s.tenant, rec.ID).Scan(&original); e != nil { t.Fatal(e) } q := MasterFieldPatch{InputHash: rec.QualityInputHash, InputEpoch: before[rec.ID].InputEpoch, BaseReviewRevision: before[rec.ID].Revision, Nonce: "fixture_inline_field_001", Fields: map[string]string{"product_category": "MG400"}} receipt, e := s.PatchMasterFields(ctx, rec.ID, q, "fixture-admin") if e != nil { t.Fatalf("single-field persistent edit rejected: %v", e) } if receipt.SavedRevision != 1 || receipt.Replayed { t.Fatalf("invalid receipt: %+v", receipt) } after, e := NewStore(p, s.tenant).ReadMasterQualityViews(ctx, keys) if e != nil { t.Fatal(e) } fresh, e := s.GetMasterRecord(ctx, s.tenant, rec.ID) if e != nil || fresh.FieldEditRevision != 1 || fresh.MasterFields["product_category"] != "MG400" { t.Fatalf("detail/list read model did not expose saved field version and value: %+v error=%v", fresh, e) } b, a := before[rec.ID], after[rec.ID] b.Effective.ProductCategory = "MG400" rawB, _ := json.Marshal(b) rawA, _ := json.Marshal(a) if string(rawB) != string(rawA) { t.Fatalf("unselected conclusions/review provenance changed: before=%s after=%s", rawB, rawA) } var current string var audits int if e = p.QueryRow(ctx, `SELECT md5(payload::text||local_fields::text) FROM qa_ops_master_records WHERE tenant_key=$1 AND id=$2`, s.tenant, rec.ID).Scan(¤t); e != nil || original != current { t.Fatal("source/master payload or stored labels were rewritten", e) } if e = p.QueryRow(ctx, `SELECT count(*) FROM qa_ops_master_field_edits WHERE tenant_key=$1 AND record_id=$2`, s.tenant, rec.ID).Scan(&audits); e != nil || audits != 1 { t.Fatal("field edit audit missing", e) } }