droboticflow/internal/api/admin_annotation_pool_test.go
d-robotics 2876a14d4a 🐛 Separate administrative pool totals from bounded cursor pages
Administrator counters were derived from the stage-filtered first human-item page, while annotators used an aggregate. Add authorized whole-pool counters with a five-second bounded cache, page human items before joining task summaries, and search older metadata on the server. Cancel stale reads and preserve existing non-admin scope behavior. Cover permissions, mixed stages, real Mongo parity, old-page search, categories, pagination, stale responses and claim invalidation.
2026-10-21 15:31:05 +08:00

147 lines
5.3 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"testing"
"time"
)
func TestAdminAnnotationPoolTotalsAndOldPageSearch(t *testing.T) {
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticAnnotation, domain.HumanItemReady, "", 0)
f.batch.Stages = append(f.batch.Stages, domain.HumanStageDefinition{ID: "quality_review", Kind: domain.HumanStageReview, RequiredRole: "quality_reviewer"})
if err := f.store.UpsertHumanBatch(f.ctx, f.batch); err != nil {
t.Fatal(err)
}
admin := domain.UserRecord{ID: "pool-admin", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
for i := 0; i < 610; i++ {
item := f.item
item.ID = fmt.Sprintf("pool-%04d", i)
item.LegacyTaskID = fmt.Sprintf("pool-task-%04d", i)
item.CreatedAt = fmt.Sprintf("2026-10-21T00:%02d:%02dZ", i/60, i%60)
item.Subject.ItemKey = fmt.Sprintf("CAPTURE_%04d_camera_left_part0001.mp4", i)
if i >= 606 {
item.CurrentStageID = "quality_review"
}
if e := f.store.UpsertHumanItem(f.ctx, item); e != nil {
t.Fatal(e)
}
}
base := "/api/plugins/annotation-qa/admin-pool?projectId=" + f.task.ProjectID
read := func(path string) map[string]json.RawMessage {
t.Helper()
r := f.request(http.MethodGet, path, admin, nil)
if r.Code != 200 {
t.Fatalf("%d %s", r.Code, r.Body)
}
var d map[string]json.RawMessage
if e := json.Unmarshal(r.Body.Bytes(), &d); e != nil {
t.Fatal(e)
}
return d
}
first := read(base + "&pool=available&limit=100")
var counts struct{ Available, All, Mine int }
_ = json.Unmarshal(first["counts"], &counts)
if counts.Available != 607 || counts.All != 611 || counts.Mine != 0 {
t.Fatalf("counts %+v", counts)
}
var items []domain.HumanItemRecord
_ = json.Unmarshal(first["items"], &items)
if len(items) != 100 {
t.Fatalf("page=%d", len(items))
}
var cursor string
_ = json.Unmarshal(first["nextCursor"], &cursor)
if cursor == "" {
t.Fatal("missing cursor")
}
second := read(base + "&pool=available&limit=100&cursor=" + url.QueryEscape(cursor))
var nextCounts struct{ Available, All, Mine int }
_ = json.Unmarshal(second["counts"], &nextCounts)
if counts != nextCounts {
t.Fatal("paging changed totals")
}
old := read(base + "&pool=available&search=CAPTURE_0001")
_ = json.Unmarshal(old["items"], &items)
if len(items) != 1 || items[0].ID != "pool-0001" {
t.Fatalf("old search %+v", items)
}
_ = json.Unmarshal(old["counts"], &nextCounts)
if counts != nextCounts {
t.Fatal("search changed badges")
}
denied := f.request(http.MethodGet, base+"&pool=all", f.annotator, nil)
if denied.Code != 403 {
t.Fatalf("annotator=%d", denied.Code)
}
}
type adminPoolProbe struct {
store.Store
counts, pages int
fail bool
}
func (s *adminPoolProbe) CountAdminAnnotationPools(ctx context.Context, org string, q store.AdminAnnotationPoolQuery) (store.AdminAnnotationPoolCounts, error) {
s.counts++
if s.fail {
return store.AdminAnnotationPoolCounts{}, errors.New("synthetic unavailable")
}
return s.Store.CountAdminAnnotationPools(ctx, org, q)
}
func (s *adminPoolProbe) ListAdminAnnotationPool(ctx context.Context, org string, q store.AdminAnnotationPoolQuery) ([]domain.HumanItemRecord, error) {
s.pages++
if q.Limit > 201 {
panic("unbounded administrator page")
}
return s.Store.ListAdminAnnotationPool(ctx, org, q)
}
func TestAdminAnnotationPoolFreshPermissionsCacheAndFailures(t *testing.T) {
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticAnnotation, domain.HumanItemReady, "", 0)
probe := &adminPoolProbe{Store: f.store}
now := time.Now()
srv := &server{store: probe, dashboardResponses: dashboardResponseCache{now: func() time.Time { return now }}}
f.handler = http.HandlerFunc(srv.handleAdminAnnotationPool)
admin := domain.UserRecord{ID: "admin-cache", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
base := "/api/plugins/annotation-qa/admin-pool?projectId=" + f.task.ProjectID
for _, suffix := range []string{"&pool=available", "&pool=all&search=missing&category=pending_slice", "&pool=mine"} {
r := f.request(http.MethodGet, base+suffix, admin, nil)
if r.Code != 200 {
t.Fatalf("%d %s", r.Code, r.Body)
}
}
if probe.counts != 1 || probe.pages != 3 {
t.Fatalf("counts=%d pages=%d", probe.counts, probe.pages)
}
for _, role := range []domain.UserRole{domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer, domain.UserRoleSupplierManager} {
p := admin
p.Role = role
if r := f.request(http.MethodGet, base, p, nil); r.Code != 403 {
t.Fatalf("cached role %s=%d", role, r.Code)
}
}
for _, suffix := range []string{"&cursor=broken", "&limit=bad", "&pool=reviewed", "&category=unknown"} {
if r := f.request(http.MethodGet, base+suffix, admin, nil); r.Code != 422 && r.Code != 400 {
t.Fatalf("invalid %s=%d", suffix, r.Code)
}
}
// A failed total read must never masquerade as zero, even when the old page
// would fit. Cross-process/background changes become visible after 5 seconds.
now = now.Add(6 * time.Second)
probe.fail = true
r := f.request(http.MethodGet, base, admin, nil)
if r.Code != 500 {
t.Fatalf("failed aggregate=%d %s", r.Code, r.Body)
}
if probe.pages != 3 {
t.Fatal("page fetched after count failure")
}
}