droboticflow/internal/store/source_readiness_test.go

102 lines
3.3 KiB
Go

package store
import (
"context"
"fmt"
"os"
"reflect"
"testing"
"time"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func TestSourceReadinessMongoParityReadOnly(t *testing.T) {
if os.Getenv("SOURCE_ADMISSION_LOCAL_MONGO") != "1" {
t.Skip("isolated loopback Mongo opt-in")
}
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
t.Setenv("ANNOTATION_LEGACY_SOURCE_CUTOFF", "")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
c, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true").SetServerSelectionTimeout(3*time.Second))
if err != nil {
t.Fatal(err)
}
defer c.Disconnect(context.Background())
db := c.Database(fmt.Sprintf("source_readiness_%d", time.Now().UnixNano()))
defer db.Drop(context.Background())
ms := NewMongoStore(db)
mem := NewMemoryStore()
mem.annotationIndexJobs = map[string]domain.AnnotationIndexJob{}
must := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
before := map[string]*domain.HumanItemRecord{}
for n, state := range []string{"", "queued", "failed", "completed", "foreign"} {
id := fmt.Sprintf("i%d", n)
org := "o"
if state == "foreign" {
org = "foreign"
}
task := domain.AnnotationTaskRecord{ID: "t" + id, OrganizationID: org, ProjectID: "p", RunID: id}
item := domain.HumanItemRecord{ID: id, OrganizationID: org, ProjectID: "p", RunID: id, PluginID: "annotation-qa", LegacyTaskID: task.ID, CurrentStageID: "annotation_combined", Status: domain.HumanItemClaimed, AssignedUserID: "owner", Version: 1, Subject: domain.HumanSubjectRef{Payload: map[string]any{"annotationDraft": map[string]any{"description": "retained"}}}}
must(ms.UpsertAnnotationTask(ctx, task))
must(mem.UpsertAnnotationTask(ctx, task))
must(ms.UpsertHumanItem(ctx, item))
must(mem.UpsertHumanItem(ctx, item))
if state != "" && state != "foreign" {
job := domain.AnnotationIndexJob{ID: task.ID, OrganizationID: org, ProjectID: "p", RunID: id, Status: state}
if state == "completed" {
job.SourceFingerprint = "fp"
job.RevalidateSource = true
}
_, err = db.Collection("annotationIndexJobs").InsertOne(ctx, job)
must(err)
mem.annotationIndexJobs[task.ID] = job
}
before[id], err = ms.GetHumanItem(ctx, id)
must(err)
}
for _, org := range []string{"o", ""} {
a, err := ms.SourceAdmissionReadiness(ctx, org, "")
must(err)
b, err := mem.SourceAdmissionReadiness(ctx, org, "")
must(err)
a.GeneratedAt = ""
b.GeneratedAt = ""
a.Database = ""
b.Database = ""
if !reflect.DeepEqual(a, b) {
t.Fatalf("Mongo/Go strict readiness mismatch: %+v / %+v", a, b)
}
total := int64(0)
for _, r := range a.Rows {
total += r.Total
if r.Total != r.Allowed+r.Pending+r.Unverified+r.Failed {
t.Fatal("counts do not reconcile")
}
}
if org == "o" && total != 4 || org == "" && total != 5 {
t.Fatal("readiness scope mismatch")
}
}
for id, before := range before {
after, err := ms.GetHumanItem(ctx, id)
must(err)
if !reflect.DeepEqual(before, after) {
t.Fatal("inventory changed saved annotations or ownership")
}
}
canceled, stop := context.WithCancel(ctx)
stop()
if r, err := ms.SourceAdmissionReadiness(canceled, "", ""); err == nil || r != nil {
t.Fatal("canceled report claimed completeness")
}
}