droboticflow/internal/store/admin_source_exceptions_test.go

178 lines
7.3 KiB
Go

package store
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func testAdminSourceExceptions(t *testing.T, s Store) {
t.Helper()
ctx := context.Background()
const org, project, run = "admin-source-org", "admin-source-project", "admin-source-run"
batch := domain.HumanBatchRecord{ID: "admin-source-batch", OrganizationID: org, ProjectID: project, RunID: run, PluginID: "annotation-qa", Stages: domain.DefaultFanyuV58HumanStages()}
if err := s.UpsertHumanBatch(ctx, batch); err != nil {
t.Fatal(err)
}
scenarios := []struct {
name, timeline, fingerprint, source, superseded, want string
modern, revalidate bool
}{
{name: "legacy"},
{name: "valid", modern: true, timeline: "completed"},
{name: "failed", modern: true, timeline: "failed", want: "failed"},
{name: "pending", modern: true, timeline: "queued", want: "pending"},
{name: "missing", modern: true, want: "unverified"},
{name: "payload-certificate", modern: true, timeline: "completed", want: "unverified"},
{name: "payload-proof", modern: true, timeline: "completed", revalidate: true},
{name: "payload-new-failure", modern: true, timeline: "completed", revalidate: true, fingerprint: "C", source: "failed", want: "failed"},
{name: "payload-new-missing", modern: true, timeline: "completed", revalidate: true, fingerprint: "C", want: "unverified"},
{name: "pending-current-proof", modern: true, timeline: "running", fingerprint: "B", superseded: "A", revalidate: true, want: "pending"},
{name: "source-failed", modern: true, timeline: "completed", fingerprint: "A", source: "failed", want: "failed"},
{name: "old-source-superseded", modern: true, timeline: "completed", fingerprint: "A", source: "failed", superseded: "A", revalidate: true},
{name: "new-source-failed", modern: true, timeline: "completed", fingerprint: "C", source: "failed", superseded: "A", revalidate: true, want: "failed"},
{name: "new-source-missing", modern: true, timeline: "completed", fingerprint: "C", superseded: "A", revalidate: true, want: "unverified"},
{name: "current-proof", modern: true, timeline: "completed", fingerprint: "B", superseded: "A", revalidate: true},
{name: "current-proof-failed", modern: true, timeline: "completed", fingerprint: "B", source: "failed", superseded: "A", revalidate: true, want: "failed"},
}
writeJob := func(job domain.AnnotationIndexJob) {
switch st := s.(type) {
case *MemoryStore:
st.annotationIndexJobs[job.ID] = job
case *MongoStore:
if err := mongoUpsert(ctx, st.db.Collection("annotationIndexJobs"), job.ID, job); err != nil {
t.Fatal(err)
}
}
}
wantStatuses := map[string]string{}
wantReady, wantFailed := 0, 0
for n, sc := range scenarios {
id := "admin-source-" + sc.name
task := domain.AnnotationTaskRecord{ID: id, OrganizationID: org, ProjectID: project, RunID: run, SourceIsFullVideo: sc.modern, TemplateVersion: 6, ArtifactID: "left", Status: domain.AnnotationTaskStatusPendingAnnotation}
if err := s.UpsertAnnotationTask(ctx, task); err != nil {
t.Fatal(err)
}
item := domain.HumanItemRecord{ID: id, OrganizationID: org, ProjectID: project, RunID: run, BatchID: batch.ID, PluginID: "annotation-qa", LegacyTaskID: id, CurrentStageID: domain.FanyuV58StageAnnotationCombined, Status: domain.HumanItemReady, Version: 7, CreatedAt: fmt.Sprintf("2026-11-02T00:00:%02dZ", n), Subject: domain.HumanSubjectRef{ItemKey: id, Payload: map[string]any{"draft": "keep"}}}
// Distinct fingerprints prevent one scenario's certificate from affecting another.
fp := func(v string) string {
if v == "" {
return ""
}
return sc.name + "-" + v
}
if strings.HasPrefix(sc.name, "payload-") {
item.Subject.Payload["materialization"] = map[string]any{"sourceIndex": true}
}
if sc.fingerprint != "" {
item.ProcessingJob = &domain.AnnotationProcessingJob{SourceIndex: true, SourceFingerprint: fp(sc.fingerprint)}
}
if err := s.UpsertHumanItem(ctx, item); err != nil {
t.Fatal(err)
}
if sc.timeline != "" {
j := annotationIndexJob(task)
j.Status = sc.timeline
j.RevalidateSource = sc.revalidate
j.SupersededSourceFingerprint = fp(sc.superseded)
if sc.revalidate {
j.SourceFingerprint = fp("B")
}
writeJob(j)
} else {
switch st := s.(type) {
case *MemoryStore:
delete(st.annotationIndexJobs, id)
case *MongoStore:
if _, err := st.db.Collection("annotationIndexJobs").DeleteOne(ctx, bson.M{"id": id}); err != nil {
t.Fatal(err)
}
}
}
if sc.source != "" {
j := annotationIndexJob(task)
j.ID = AnnotationSourceJobID(org, fp(sc.fingerprint))
j.Status = sc.source
writeJob(j)
}
wantStatuses[id] = sc.want
if sc.want == "" {
wantReady++
}
if sc.want == "failed" {
wantFailed++
}
}
q := AdminAnnotationPoolQuery{ProjectID: project, UserID: "admin", Pool: "all", Limit: 100}
rows, err := s.ListAdminAnnotationPool(ctx, org, q)
if err != nil {
t.Fatal(err)
}
if len(rows) != len(scenarios) {
t.Fatalf("all rows=%d", len(rows))
}
for _, row := range rows {
if row.SourceValidationStatus != wantStatuses[row.ID] {
t.Errorf("%s status=%q want=%q", row.ID, row.SourceValidationStatus, wantStatuses[row.ID])
}
}
counts, err := s.CountAdminAnnotationPools(ctx, org, q)
if err != nil || counts.All != len(scenarios) || counts.Available != wantReady {
t.Fatalf("counts=%+v ready=%d err=%v", counts, wantReady, err)
}
q.Pool = "available"
rows, err = s.ListAdminAnnotationPool(ctx, org, q)
if err != nil || len(rows) != wantReady {
t.Fatalf("available=%d want=%d err=%v", len(rows), wantReady, err)
}
for _, row := range rows {
if wantStatuses[row.ID] != "" {
t.Errorf("blocked task available: %s", row.ID)
}
}
q.Pool = "all"
q.Category = "source_failed"
rows, err = s.ListAdminAnnotationPool(ctx, org, q)
if err != nil || len(rows) != wantFailed {
t.Fatalf("failures=%d want=%d err=%v", len(rows), wantFailed, err)
}
for id := range wantStatuses {
item, err := s.GetHumanItem(ctx, id)
if id == "admin-source-payload-certificate" && err == nil && !AnnotationSourceCertificateRequired(*item) {
t.Fatalf("payload certificate marker lost after read: %T", item.Subject.Payload["materialization"])
}
if err != nil || item.Version != 7 || item.AssignedUserID != "" || item.Subject.Payload["draft"] != "keep" || item.SourceValidationStatus != "" {
t.Fatalf("read changed item %s: %+v %v", id, item, err)
}
}
}
func TestAdminSourceExceptionsMemory(t *testing.T) { testAdminSourceExceptions(t, NewMemoryStore()) }
func TestAdminSourceExceptionsMongo(t *testing.T) {
if os.Getenv("SOURCE_ADMISSION_LOCAL_MONGO") != "1" {
t.Skip("isolated local Mongo opt-in")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true").SetServerSelectionTimeout(5*time.Second))
if err != nil {
t.Fatal(err)
}
defer client.Disconnect(context.Background())
db := client.Database(fmt.Sprintf("admin_source_test_%d", time.Now().UnixNano()))
defer db.Drop(context.Background())
s := NewMongoStore(db)
if err := s.EnsureIndexes(ctx); err != nil {
t.Fatal(err)
}
testAdminSourceExceptions(t, s)
}