droboticflow/internal/api/pool_count_performance_test.go

182 lines
7.2 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"encoding/json"
"errors"
"net/http"
"testing"
)
type poolCountProbeStore struct {
store.Store
fetched, pages int
rejectCandidateLoad bool
}
func (s *poolCountProbeStore) ListHumanItems(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
items, err := s.Store.ListHumanItems(ctx, org, q)
s.pages++
s.fetched += len(items)
return items, err
}
func (s *poolCountProbeStore) ListAnnotationClaimCandidates(ctx context.Context, org, project string) ([]domain.HumanItemRecord, error) {
if s.rejectCandidateLoad {
return nil, errors.New("claim count must aggregate without loading candidate records")
}
return s.Store.ListAnnotationClaimCandidates(ctx, org, project)
}
func TestPoolPerformanceBlindAnnotatorSkipsUnrelatedBacklog(t *testing.T) {
f := randomClaimFixture(t, 601)
probe := &poolCountProbeStore{Store: f.store}
f.handler = NewHandler(probe, nil)
r := f.request(http.MethodGet, "/api/human-items?pluginId=annotation-qa&pool=available&limit=1", f.annotator, nil)
if r.Code != 200 {
t.Fatal(r.Code, r.Body.String())
}
if probe.fetched != 0 || probe.pages != 1 {
t.Fatalf("blind pool fetched %d irrelevant candidates across %d pages", probe.fetched, probe.pages)
}
}
func TestPoolPerformanceClaimCountDoesNotMaterializeCandidates(t *testing.T) {
f := randomClaimFixture(t, 3)
f.handler = NewHandler(&poolCountProbeStore{Store: f.store, rejectCandidateLoad: true}, nil)
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/claim-count?projectId="+f.task.ProjectID, f.annotator, nil)
if r.Code != 200 {
t.Fatalf("aggregate count: %d %s", r.Code, r.Body.String())
}
var result struct {
Count int `json:"count"`
}
if err := json.Unmarshal(r.Body.Bytes(), &result); err != nil || result.Count != 3 {
t.Fatalf("count=%d err=%v", result.Count, err)
}
}
func TestDirectedReworkDoesNotWaitForPublicInventory(t *testing.T) {
f := randomClaimFixture(t, 3)
item := f.item
item.ReviewRound = 1
item.Status = domain.HumanItemRework
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
f.handler = NewHandler(&poolCountProbeStore{Store: f.store, rejectCandidateLoad: true}, nil)
got := claimRandom(t, f, "rework-fast-path-0001")
if got.ID != item.ID || got.Version != item.Version || got.AssignedUserID != f.annotator.ID {
t.Fatal("directed rework changed identity, version or ownership")
}
}
func TestPoolPerformanceKeepsLegacyStageExceptionsAndAuthorHistory(t *testing.T) {
f := randomClaimFixture(t, 2)
batch, err := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
if err != nil {
t.Fatal(err)
}
exceptional := *batch
exceptional.ID = "legacy-nonannotation-batch"
exceptional.Stages = []domain.HumanStageDefinition{{ID: "old-cutter-review", Kind: domain.HumanStageReview, RequiredRole: "cutter"}}
if err := f.store.UpsertHumanBatch(f.ctx, exceptional); err != nil {
t.Fatal(err)
}
add := func(id string, edit func(*domain.HumanItemRecord)) {
v := f.item
v.ID = id
v.Subject.ItemKey = id
v.AssignedUserID = ""
v.LatestAuthorUserID = ""
v.Status = domain.HumanItemReady
edit(&v)
if err := f.store.UpsertHumanItem(f.ctx, v); err != nil {
t.Fatal(err)
}
}
add("legacy-exception", func(v *domain.HumanItemRecord) { v.BatchID = exceptional.ID; v.CurrentStageID = "old-cutter-review" })
add("own-author-history", func(v *domain.HumanItemRecord) {
v.CurrentStageID = domain.FanyuV58StageQualityReview
v.LatestAuthorUserID = f.annotator.ID
})
add("foreign-supplier-author", func(v *domain.HumanItemRecord) {
v.SupplierID = "foreign-vendor"
v.LatestAuthorUserID = f.annotator.ID
})
add("another-plugin-exception", func(v *domain.HumanItemRecord) {
v.PluginID = "another-plugin"
v.BatchID = exceptional.ID
v.CurrentStageID = "old-cutter-review"
})
r := f.request(http.MethodGet, "/api/human-items?pool=available&limit=100", f.annotator, nil)
if r.Code != 200 {
t.Fatal(r.Code, r.Body.String())
}
var result struct {
Items []domain.HumanItemRecord `json:"items"`
}
if err := json.Unmarshal(r.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
got := map[string]bool{}
for _, item := range result.Items {
got[item.ID] = true
}
if len(got) != 3 || !got["legacy-exception"] || !got["own-author-history"] || !got["another-plugin-exception"] {
t.Fatalf("incorrect visible scope %v", got)
}
}
func TestPoolPerformanceCountMatchesExistingMembershipPredicate(t *testing.T) {
f := randomClaimFixture(t, 0)
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
suppliers := map[string]bool{"random-supplier": true}
variants := []annotationProjectRole{
{ProjectID: f.task.ProjectID, SupplierID: "random-supplier", Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "legacy"},
{ProjectID: f.task.ProjectID, SupplierID: "random-supplier", Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "supplier_grant", GrantStatus: "active"},
{ProjectID: f.task.ProjectID, SupplierID: "random-supplier", Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "both", GrantStatus: "revoked", DrainTaskIDs: map[string]bool{"drain-task": true}},
{ProjectID: f.task.ProjectID, SupplierID: "random-supplier", Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "supplier_grant", GrantStatus: "revoked", DrainTaskIDs: map[string]bool{"drain-task": true}},
{ProjectID: f.task.ProjectID, SupplierID: "random-supplier", Role: domain.AnnotationRoleQualityReviewer, AuthorizationMode: "both", GrantStatus: "active"},
{ProjectID: "other-project", SupplierID: "random-supplier", Role: domain.AnnotationRoleAnnotator, AuthorizationMode: "both", GrantStatus: "active"},
}
for _, pool := range []bool{false, true} {
batch.ProcessingProfile = ""
if pool {
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
}
if err := f.store.UpsertHumanBatch(f.ctx, *batch); err != nil {
t.Fatal(err)
}
for _, supplier := range []string{"", "random-supplier", "other-vendor"} {
for _, internal := range []bool{false, true} {
candidate := f.item
candidate.ID = "count-equivalence"
candidate.LegacyTaskID = "drain-task"
candidate.AssignedUserID = ""
candidate.LatestAuthorUserID = ""
candidate.Status = domain.HumanItemRework
candidate.SupplierID = supplier
candidate.ProcessingScope = ""
if internal {
candidate.ProcessingScope = "internal"
}
if err := f.store.UpsertHumanItem(f.ctx, candidate); err != nil {
t.Fatal(err)
}
for _, member := range variants {
members := []annotationProjectRole{member}
legacy := domain.AnnotationTaskRecord{ID: candidate.LegacyTaskID, ProjectID: candidate.ProjectID, SupplierID: supplier, ProcessingScope: candidate.ProcessingScope, SupplierPool: pool}
want := 0
if membershipHasRole(members, legacy, domain.AnnotationRoleAnnotator) && (supplier == "" || suppliers[supplier]) {
want = 1
}
got, err := f.store.CountAnnotationClaimCandidates(f.ctx, candidate.OrganizationID, annotationClaimCountScope(f.task.ProjectID, suppliers, members))
if err != nil || got != want {
t.Fatalf("pool=%t supplier=%q internal=%t member=%+v count=%d want=%d err=%v", pool, supplier, internal, member, got, want, err)
}
}
}
}
}
}