droboticflow/internal/store/blind_annotation_scope_test.go

103 lines
3.5 KiB
Go

package store
import (
"fmt"
"testing"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
)
// Null/missing historic fields decode as empty domain strings. Positive ranges
// must preserve those cases, unknown future names and the exact role resolver.
func TestBlindAnnotationHistoricalCandidateScopeMongo(t *testing.T) {
ctx, s := poolLocalMongo(t)
type field struct {
value interface{}
present bool
}
values := func(names ...string) []field {
result := []field{{nil, false}, {nil, true}}
for _, n := range names {
result = append(result, field{n, true})
}
return result
}
kinds := values("", "annotation", "review", "acceptance", "approval", "legacy-unknown")
ids := values("", "split_annotation", "cut_adjustment", "split_quality_review", "cut_review", "semantic_annotation", "quality_review", "semantic_review", "acceptance", "prepackage_acceptance", "custom-stage")
roles := values("", "annotator", "cutter", "quality_reviewer", "acceptance_reviewer", "historical-unknown")
docs := []interface{}{}
expected := map[string]bool{}
org := "historic-interval-org"
for _, kind := range kinds {
for _, id := range ids {
for _, role := range roles {
raw := bson.M{}
for key, f := range map[string]field{"kind": kind, "id": id, "requiredRole": role} {
if f.present {
raw[key] = f.value
}
}
bytes, err := bson.Marshal(raw)
if err != nil {
t.Fatal(err)
}
var stage domain.HumanStageDefinition
if err := bson.Unmarshal(bytes, &stage); err != nil {
t.Fatal(err)
}
name := fmt.Sprintf("historic-%04d", len(docs))
expected[name] = stage.Kind != domain.HumanStageAnnotation && domain.AnnotationRoleForHumanStage(stage) == domain.AnnotationRoleAnnotator
docs = append(docs, bson.M{"id": name, "organizationId": org, "runId": name, "gateNodeId": "gate", "stages": bson.A{raw}})
}
}
}
if _, err := s.humanBatches.InsertMany(ctx, docs); err != nil {
t.Fatal(err)
}
for _, indexed := range []bool{false, true} {
t.Run(fmt.Sprintf("indexed=%t", indexed), func(t *testing.T) {
if indexed {
if _, err := s.humanBatches.Indexes().CreateOne(ctx, blindAnnotationStageExceptionIndex()); err != nil {
t.Fatal(err)
}
}
filter, err := s.blindAnnotationMongoVisibility(ctx, org, domain.HumanItemQuery{PluginID: "annotation-qa", BlindAnnotationActorID: "actor"})
if err != nil {
t.Fatal(err)
}
// Query the generated actual item filter against one item per domain case.
// It must agree exactly after the candidate superset is resolved in Go.
col := s.db.Collection(fmt.Sprintf("syntheticItems%t", indexed))
items := make([]interface{}, 0, len(docs))
for _, doc := range docs {
b := doc.(bson.M)
raw := b["stages"].(bson.A)[0].(bson.M)
id, _ := raw["id"].(string)
items = append(items, bson.M{"id": b["id"], "batchId": b["id"], "pluginId": "annotation-qa", "currentStageId": id})
}
if _, err := col.InsertMany(ctx, items); err != nil {
t.Fatal(err)
}
cur, err := col.Find(ctx, filter)
if err != nil {
t.Fatal(err)
}
var rows []struct{ ID string }
if err := cur.All(ctx, &rows); err != nil {
t.Fatal(err)
}
seen := map[string]bool{}
for _, row := range rows {
seen[row.ID] = true
}
for id, want := range expected {
if seen[id] != want {
t.Errorf("case %s visibility=%t want=%t", id, seen[id], want)
}
}
t.Logf("%d historical kind/ID/role combinations preserve exact visibility", len(expected))
})
}
}