droboticflow/internal/api/human_pool_count_scan_test.go

167 lines
5.4 KiB
Go

package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"sync"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
type countClaimReadProbe struct {
store.Store
taskReads int
}
func (p *countClaimReadProbe) GetAnnotationTask(ctx context.Context, id string) (*domain.AnnotationTaskRecord, error) {
p.taskReads++
return p.Store.GetAnnotationTask(ctx, id)
}
// Counting visible author history does not need to determine whether that
// author could claim the row. Claim-only source checks are not count filters.
func TestHumanPoolCountSkipsClaimOnlyReads(t *testing.T) {
f := newCombinedDraftFixture(t)
item := f.item
item.AssignedUserID = ""
item.Status = domain.HumanItemReady
item.LatestAuthorUserID = f.reviewer.ID
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
p := &countClaimReadProbe{Store: f.store}
f.handler = NewHandler(p, nil)
r := f.request("GET", "/api/human-item-counts?pluginId=annotation-qa", f.reviewer, nil)
var body struct{ Counts map[string]int }
if r.Code != 200 || json.Unmarshal(r.Body.Bytes(), &body) != nil || body.Counts["available"] != 1 {
t.Fatalf("count changed: %d %s", r.Code, r.Body.String())
}
if p.taskReads != 0 {
t.Fatalf("count made %d unused claim-only task reads", p.taskReads)
}
}
func TestHumanPoolCountScanMatchesAuthorizedPages(t *testing.T) {
f := sharedAcceptanceFixture(t)
for n := 0; n < 1005; n++ {
item := f.item
item.ID = fmt.Sprintf("scan-count-%04d", n)
item.CreatedAt = fmt.Sprintf("2026-11-01T%06d", n)
if n%4 == 0 {
item.AssignedUserID = f.reviewer.ID
item.Status = domain.HumanItemClaimed
}
if n%7 == 0 {
item.SupplierID = "foreign-supplier"
}
if n%17 == 0 {
item.BatchID = "missing-batch"
}
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
}
for _, revoked := range []bool{false, true} {
if revoked {
if _, err := f.store.SetSupplierAcceptanceAuthorization(f.ctx, f.item.OrganizationID, f.reviewer.SupplierID, false, 1, f.admin.ID); err != nil {
t.Fatal(err)
}
}
for _, actor := range []domain.UserRecord{f.reviewer, f.admin} {
for _, scope := range []string{"", "&stageIds=acceptance,prepackage_acceptance&reviewedStageIds=acceptance,prepackage_acceptance", "&projectId=" + f.item.ProjectID, "&search=scan-count-", "&search=no-such-item", "&category=available", "&category=pending_acceptance&reviewedStageIds=acceptance,prepackage_acceptance"} {
counts := f.request("GET", "/api/human-item-counts?pluginId=annotation-qa"+scope, actor, nil)
var body struct{ Counts map[string]int }
if counts.Code != 200 || json.Unmarshal(counts.Body.Bytes(), &body) != nil {
t.Fatal(counts.Code, counts.Body.String())
}
for pool, want := range body.Counts {
total, cursor := 0, ""
for page := 0; ; page++ {
if page > 100 {
t.Fatal("non-advancing list")
}
r := f.request("GET", "/api/human-items?pluginId=annotation-qa&pool="+pool+"&limit=80&cursor="+url.QueryEscape(cursor)+scope, actor, nil)
var got struct {
Items []domain.HumanItemRecord
HasMore bool
NextCursor string
}
if r.Code != 200 || json.Unmarshal(r.Body.Bytes(), &got) != nil {
t.Fatal(r.Code, r.Body.String())
}
total += len(got.Items)
if !got.HasMore {
break
}
cursor = got.NextCursor
}
if total != want {
t.Fatalf("role=%s revoked=%t pool=%s scope=%s count=%d authorized pages=%d", actor.Role, revoked, pool, scope, want, total)
}
}
}
}
}
}
type failedCountScan struct{ store.Store }
func (s failedCountScan) VisitHumanPoolCountCandidates(ctx context.Context, org string, q domain.HumanItemQuery, visit func([]domain.HumanItemRecord) error) error {
rows, err := s.Store.ListHumanItems(ctx, org, q)
if err != nil {
return err
}
if err = visit(rows); err != nil {
return err
}
return errors.New("synthetic interrupted count")
}
func TestHumanPoolCountScanFailureNeverReturnsPartialTotals(t *testing.T) {
f := sharedAcceptanceFixture(t)
f.handler = NewHandler(failedCountScan{f.store}, nil)
r := f.request("GET", "/api/human-item-counts?pluginId=annotation-qa", f.reviewer, nil)
var body map[string]any
json.Unmarshal(r.Body.Bytes(), &body)
if r.Code != 500 || body["counts"] != nil {
t.Fatalf("partial count leaked: %d %s", r.Code, r.Body.String())
}
}
func TestHumanPoolCountConcurrentActorsKeepRequestLocalPermissions(t *testing.T) {
f := sharedAcceptanceFixture(t)
// Revoke before the requests: an administrator still sees the item while
// the managed reviewer cannot inherit authority from another request.
if _, err := f.store.SetSupplierAcceptanceAuthorization(f.ctx, f.item.OrganizationID, f.reviewer.SupplierID, false, 1, f.admin.ID); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
problems := make(chan string, 16)
for n := 0; n < 16; n++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
actor := f.reviewer
want := 0
if n%2 == 0 {
actor = f.admin
want = 1
}
r := f.request("GET", "/api/human-item-counts?pluginId=annotation-qa", actor, nil)
var body struct{ Counts map[string]int }
if r.Code != 200 || json.Unmarshal(r.Body.Bytes(), &body) != nil || body.Counts["available"] != want {
problems <- fmt.Sprintf("role=%s status=%d available=%d want=%d", actor.Role, r.Code, body.Counts["available"], want)
}
}(n)
}
wg.Wait()
close(problems)
for p := range problems {
t.Error(p)
}
}