droboticflow/internal/store/human_pool_candidate_test.go

75 lines
2.5 KiB
Go

package store
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func TestHumanPoolCandidateProjectionPreservesEligibilityAndPaging(t *testing.T) {
verify := func(t *testing.T, s Store) {
ctx := context.Background()
for i := 0; i < 7; i++ {
row := domain.HumanItemRecord{ID: fmt.Sprintf("item-%d", i), OrganizationID: domain.DefaultOrganizationID, ProjectID: "p", BatchID: "b", PluginID: "annotation-qa", CurrentStageID: "quality_review", Status: domain.HumanItemReady, LatestAuthorUserID: "author", SupplierID: "supplier", Version: i + 1, CreatedAt: fmt.Sprintf("2026-10-01T00:00:0%dZ", i), Subject: domain.HumanSubjectRef{Payload: map[string]any{"arbitraryLegacyName": strings.Repeat("x", 32768)}}}
if err := s.UpsertHumanItem(ctx, row); err != nil {
t.Fatal(err)
}
}
q := domain.HumanItemQuery{ProjectID: "p", Limit: 3, ListProjection: true}
seen := 0
for {
full, err := s.ListHumanItems(ctx, domain.DefaultOrganizationID, q)
if err != nil {
t.Fatal(err)
}
candidateQ := q
candidateQ.PoolCandidateProjection = true
candidates, err := s.ListHumanItems(ctx, domain.DefaultOrganizationID, candidateQ)
if err != nil {
t.Fatal(err)
}
if len(full) != len(candidates) {
t.Fatal("page length changed")
}
for i, row := range full {
if !reflect.DeepEqual(row.PoolCandidate(), candidates[i]) || candidates[i].Subject.Payload != nil {
t.Fatalf("candidate metadata mismatch: %s", row.ID)
}
}
seen += len(full)
if len(full) < q.Limit {
break
}
last := full[len(full)-1]
q.Cursor = &domain.HumanItemCursor{ID: last.ID, CreatedAt: last.CreatedAt}
}
if seen != 7 {
t.Fatalf("lost records: %d", seen)
}
}
t.Run("memory", func(t *testing.T) { verify(t, NewMemoryStore()) })
t.Run("mongo", func(t *testing.T) {
if os.Getenv("ANNOTATION_BUFFER_MONGO_STORE") != "1" {
t.Skip("local isolated 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"))
if err != nil {
t.Fatal(err)
}
defer client.Disconnect(context.Background())
db := client.Database(fmt.Sprintf("pool_candidate_test_%d", time.Now().UnixNano()))
defer db.Drop(context.Background())
verify(t, NewMongoStore(db))
})
}