105 lines
3.6 KiB
Go
105 lines
3.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type poolCandidateProbe struct {
|
|
store.Store
|
|
payloadBytes int
|
|
candidates int
|
|
legacy bool
|
|
onHydrate func()
|
|
fallback int
|
|
}
|
|
|
|
func (s *poolCandidateProbe) ListHumanItems(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
|
if q.PoolCandidateProjection && s.legacy {
|
|
q.PoolCandidateProjection = false
|
|
}
|
|
if !q.PoolCandidateProjection && len(q.IDs) > 0 && s.onHydrate != nil {
|
|
f := s.onHydrate
|
|
s.onHydrate = nil
|
|
f()
|
|
}
|
|
if !q.PoolCandidateProjection && len(q.IDs) == 0 {
|
|
s.fallback++
|
|
}
|
|
rows, err := s.Store.ListHumanItems(ctx, org, q)
|
|
if q.PoolCandidateProjection {
|
|
s.candidates++
|
|
}
|
|
for _, row := range rows {
|
|
b, _ := json.Marshal(row.Subject.Payload)
|
|
s.payloadBytes += len(b)
|
|
}
|
|
return rows, err
|
|
}
|
|
func TestHumanPoolReadsPayloadOnlyForReturnedPage(t *testing.T) {
|
|
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageCutAdjustment, domain.HumanItemReady, "", 0)
|
|
for i := 0; i < 120; i++ {
|
|
item := f.item
|
|
item.ID = fmt.Sprintf("candidate-%03d", i)
|
|
item.Subject.Payload = map[string]any{"largeSourceMetadata": strings.Repeat("x", 32768)}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
admin := domain.UserRecord{ID: "pool-admin", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
|
|
path := "/api/human-items?pluginId=annotation-qa&pool=all&limit=3"
|
|
f.handler = NewHandler(&poolCandidateProbe{Store: f.store, legacy: true}, nil)
|
|
before := f.request(http.MethodGet, path, admin, nil)
|
|
probe := &poolCandidateProbe{Store: f.store}
|
|
f.handler = NewHandler(probe, nil)
|
|
after := f.request(http.MethodGet, path, admin, nil)
|
|
if before.Code != 200 || after.Code != 200 || before.Body.String() != after.Body.String() {
|
|
t.Fatalf("response changed: %d %d", before.Code, after.Code)
|
|
}
|
|
if probe.candidates == 0 || probe.payloadBytes > 4*32768 {
|
|
t.Fatalf("excess candidate payload reads: candidates=%d bytes=%d", probe.candidates, probe.payloadBytes)
|
|
}
|
|
}
|
|
|
|
func TestHumanPoolFallsBackWhenSelectedMetadataChanges(t *testing.T) {
|
|
for _, change := range []string{"assignee", "project", "version"} {
|
|
t.Run(change, func(t *testing.T) {
|
|
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageCutAdjustment, domain.HumanItemReady, "", 0)
|
|
admin := domain.UserRecord{ID: "pool-admin", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
|
|
path := "/api/human-items?pluginId=annotation-qa&pool=available&projectId=" + f.item.ProjectID + "&limit=3"
|
|
probe := &poolCandidateProbe{Store: f.store}
|
|
probe.onHydrate = func() {
|
|
item := f.item
|
|
switch change {
|
|
case "assignee":
|
|
item.AssignedUserID = "other"
|
|
case "project":
|
|
item.ProjectID = "foreign"
|
|
case "version":
|
|
item.Version++
|
|
}
|
|
// Scope and assignment changes intentionally do not bump version.
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
f.handler = NewHandler(probe, nil)
|
|
actual := f.request(http.MethodGet, path, admin, nil)
|
|
if probe.fallback != 1 {
|
|
t.Fatalf("missing full reread: %d", probe.fallback)
|
|
}
|
|
f.handler = NewHandler(&poolCandidateProbe{Store: f.store, legacy: true}, nil)
|
|
expected := f.request(http.MethodGet, path, admin, nil)
|
|
if actual.Code != 200 || actual.Body.String() != expected.Body.String() {
|
|
t.Fatalf("stale response after %s", change)
|
|
}
|
|
})
|
|
}
|
|
}
|