214 lines
8.0 KiB
Go
214 lines
8.0 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"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"
|
|
}
|
|
task := f.task
|
|
task.ID = item.LegacyTaskID
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
completeFixtureSourceValidation(t, combinedDraftFixture{ctx: f.ctx, store: f.store}, task)
|
|
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")
|
|
}
|
|
}
|
|
|
|
func TestAdminAnnotationPoolBatchesPageDisplayReads(t *testing.T) {
|
|
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticAnnotation, domain.HumanItemReady, "", 0)
|
|
admin := domain.UserRecord{ID: "admin-batch-display", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
|
|
for n := 0; n < 20; n++ {
|
|
id := fmt.Sprint(n)
|
|
item := f.item
|
|
item.ID = "display-item-" + id
|
|
item.RunID = "display-run-" + id
|
|
item.LegacyTaskID = ""
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertRun(f.ctx, domain.RunRecord{ID: item.RunID, OrganizationID: item.OrganizationID, TaskID: "capture-" + id}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertCaptureRecord(f.ctx, domain.CaptureRecord{ID: "capture-" + id, OrganizationID: item.OrganizationID, CaptureID: "directory-" + id}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
probe := &displayCaptureReadStore{Store: f.store}
|
|
f.handler = NewHandler(probe, nil)
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/admin-pool?pool=all&limit=200", admin, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %s", r.Code, r.Body)
|
|
}
|
|
if probe.batches != 1 || probe.singles != 0 {
|
|
t.Fatalf("display fan-out: batch=%d single=%d", probe.batches, probe.singles)
|
|
}
|
|
var body struct {
|
|
Items []domain.HumanItemRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(body.Items) != 21 {
|
|
t.Fatalf("lost page entries: %d", len(body.Items))
|
|
}
|
|
for _, i := range body.Items {
|
|
if strings.HasPrefix(i.ID, "display-item-") && (i.Display == nil || len(i.Display.CaptureDirectories) != 1 || i.Display.CaptureDirectories[0] != "directory-"+strings.TrimPrefix(i.ID, "display-item-")) {
|
|
t.Fatalf("display changed: %+v", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAdminAnnotationPoolCountsSeparateNodeScopes(t *testing.T) {
|
|
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticAnnotation, domain.HumanItemReady, "", 0)
|
|
p := &adminPoolProbe{Store: f.store}
|
|
f.handler = NewHandler(p, nil)
|
|
admin := domain.UserRecord{ID: "admin-node", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
|
|
for _, node := range []string{"node-a", "node-b", "node-a"} {
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/admin-pool?pool=all&nodeId="+node, admin, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %s", r.Code, r.Body)
|
|
}
|
|
}
|
|
if p.counts != 2 {
|
|
t.Fatalf("different node scopes shared counts: %d", p.counts)
|
|
}
|
|
}
|