droboticflow/internal/api/human_list_reads_test.go

160 lines
5.5 KiB
Go

package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
type acceptanceSourceReadProbe struct {
store.Store
singles, batches int
failSecondBatch bool
}
func TestHumanCountReadCacheRetainsOnlyActivePage(t *testing.T) {
for _, count := range []bool{true, false} {
t.Run(fmt.Sprintf("count=%v", count), func(t *testing.T) {
s := &server{store: store.NewMemoryStore()}
ctx := context.Background()
if count {
ctx = context.WithValue(ctx, humanPoolReadModeKey{}, humanPoolReadCount)
}
for page := 0; page < 3; page++ {
items := make([]domain.HumanItemRecord, 16)
for i := range items {
id := fmt.Sprintf("page-%d-task-%d", page, i)
items[i] = domain.HumanItemRecord{ID: id, LegacyTaskID: id, OrganizationID: "org", ProjectID: "project", PluginID: "annotation-qa"}
}
var err error
ctx, err = s.withHumanListReads(ctx, "org", "project", items)
if err != nil {
t.Fatal(err)
}
cache := ctx.Value(humanListReadsKey{}).(*humanListReads)
want := len(items) * (page + 1)
if count {
want = len(items)
}
if len(cache.itemsByTask) != want {
t.Fatalf("retained %d task groups, want %d", len(cache.itemsByTask), want)
}
got, err := s.humanItemForLegacyTask(ctx, domain.AnnotationTaskRecord{ID: items[0].LegacyTaskID, OrganizationID: "org", ProjectID: "project"})
if err != nil || got == nil || got.ID != items[0].ID {
t.Fatalf("active-page legacy lookup changed: %v %v", got, err)
}
}
})
}
}
func (p *acceptanceSourceReadProbe) GetHumanBatch(ctx context.Context, id string) (*domain.HumanBatchRecord, error) {
p.singles++
return p.Store.GetHumanBatch(ctx, id)
}
func (p *acceptanceSourceReadProbe) ListHumanBatchAccessRecords(ctx context.Context, org string, ids []string) ([]domain.HumanBatchRecord, error) {
p.batches++
if p.failSecondBatch && p.batches == 2 {
return nil, errors.New("synthetic bulk source read failure")
}
return p.Store.ListHumanBatchAccessRecords(ctx, org, ids)
}
func TestHumanPoolBatchesAcceptanceSourcesWithoutChangingAnonymization(t *testing.T) {
f := sharedAcceptanceFixture(t)
original, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
for n := 0; n < 20; n++ {
source := *original
source.ID = fmt.Sprint("source-display-", n)
source.SourceBatchID = ""
source.ProjectID = "historical-project"
source.Stages = domain.DefaultFanyuV58HumanStages()
if err := f.store.UpsertHumanBatch(f.ctx, source); err != nil {
t.Fatal(err)
}
batch := *original
batch.ID = fmt.Sprint("acceptance-display-", n)
batch.SourceBatchID = source.ID
if err := f.store.UpsertHumanBatch(f.ctx, batch); err != nil {
t.Fatal(err)
}
item := f.item
item.ID = fmt.Sprint("source-display-item-", n)
item.BatchID = batch.ID
item.SupplierID = "historical-supplier"
item.WorkgroupID = "private-workgroup"
item.VendorID = "private-vendor"
item.LegacyAssignment = map[string]string{"private": "assignment"}
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
}
probe := &acceptanceSourceReadProbe{Store: f.store}
f.handler = NewHandler(probe, nil)
result := f.request("GET", "/api/human-item-summaries?pluginId=annotation-qa&pool=available&limit=200", f.reviewer, nil)
var body struct {
Summaries []humanPoolSummary `json:"summaries"`
}
if result.Code != 200 || json.Unmarshal(result.Body.Bytes(), &body) != nil || len(body.Summaries) != 21 {
t.Fatalf("pool %d %s", result.Code, result.Body.String())
}
for _, item := range body.Summaries {
if item.SupplierID != "" || item.LegacyAssignment != nil {
t.Fatalf("source privacy changed for %s", item.ID)
}
}
if probe.singles != 0 || probe.batches != 2 {
t.Fatalf("source batch reads: singles=%d batches=%d; want two bulk reads", probe.singles, probe.batches)
}
}
func TestHumanPoolSourceReadFailureRetainsAnonymizationFallback(t *testing.T) {
f := sharedAcceptanceFixture(t)
probe := &acceptanceSourceReadProbe{Store: f.store, failSecondBatch: true}
f.handler = NewHandler(probe, nil)
result := f.request("GET", "/api/human-item-summaries?pluginId=annotation-qa&pool=available&limit=200", f.reviewer, nil)
var body struct {
Summaries []humanPoolSummary `json:"summaries"`
}
if result.Code != 200 || json.Unmarshal(result.Body.Bytes(), &body) != nil || len(body.Summaries) != 1 || body.Summaries[0].SupplierID != "" {
t.Fatalf("fallback changed %d %s", result.Code, result.Body.String())
}
if probe.singles != 1 || probe.batches != 2 {
t.Fatalf("fallback reads singles=%d batches=%d", probe.singles, probe.batches)
}
}
func TestHumanPoolAnonymousAndCountsAvoidSourceReads(t *testing.T) {
for _, counts := range []bool{false, true} {
t.Run(fmt.Sprint("counts=", counts), func(t *testing.T) {
f := sharedAcceptanceFixture(t)
if !counts {
f.item.SupplierID = ""
f.item.LegacyAssignment = nil
f.item.WorkgroupID = ""
f.item.VendorID = ""
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
t.Fatal(err)
}
}
probe := &acceptanceSourceReadProbe{Store: f.store}
f.handler = NewHandler(probe, nil)
endpoint := "/api/human-item-summaries"
if counts {
endpoint = "/api/human-item-counts"
}
result := f.request("GET", endpoint+"?pluginId=annotation-qa&pool=available&limit=200", f.reviewer, nil)
if result.Code != 200 {
t.Fatalf("%d %s", result.Code, result.Body.String())
}
if probe.singles != 0 || probe.batches != 1 {
t.Fatalf("unnecessary source read singles=%d batches=%d", probe.singles, probe.batches)
}
})
}
}