forked from longtao.wu/droboticflow
330 lines
14 KiB
Go
330 lines
14 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func priorityFixture(t *testing.T) combinedDraftFixture {
|
|
f := sharedAcceptanceFixture(t)
|
|
// Existing acceptance is ordinary; source identities are separate from reviewer identity.
|
|
addPrioritySource(t, f, f.item, "ordinary-production")
|
|
_ = f.store.UpsertSupplier(f.ctx, domain.SupplierRecord{ID: "priority-production", OrganizationID: f.item.OrganizationID, Name: "Priority producer", Status: "active"})
|
|
return f
|
|
}
|
|
func addPrioritySource(t *testing.T, f combinedDraftFixture, item domain.HumanItemRecord, supplier string) {
|
|
t.Helper()
|
|
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
b := *batch
|
|
b.RequirePrePackageAcceptance = false
|
|
b.ID = "acceptance-batch-" + item.ID
|
|
b.SourceBatchID = "production-batch-" + item.ID
|
|
source := b
|
|
source.ID = b.SourceBatchID
|
|
source.SourceBatchID = ""
|
|
source.Stages = domain.DefaultFanyuV58HumanStages()
|
|
for _, v := range []domain.HumanBatchRecord{source, b} {
|
|
if e := f.store.UpsertHumanBatch(f.ctx, v); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
row := domain.HumanItemRecord{ID: "production-" + item.ID, OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, BatchID: source.ID, PluginID: "annotation-qa", SupplierID: supplier, Status: domain.HumanItemAccepted, CurrentStageID: domain.FanyuV58StageQualityReview, Version: 1, CreatedAt: item.CreatedAt}
|
|
if e := f.store.UpsertHumanItem(f.ctx, row); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
item.BatchID = b.ID
|
|
item.SupplierID = ""
|
|
item.LegacyTaskID = ""
|
|
if e := f.store.UpsertHumanItem(f.ctx, item); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
func priorityPolicy(t *testing.T, f combinedDraftFixture, enabled bool, version int, expiry string, projects []string) {
|
|
t.Helper()
|
|
r := f.request("PUT", "/api/suppliers/priority-production/acceptance-priority", f.admin, map[string]any{"enabled": enabled, "projectIds": projects, "expiresAt": expiry, "version": version})
|
|
if r.Code != 200 {
|
|
t.Fatalf("policy %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
func priorityCandidates(t *testing.T, f combinedDraftFixture) []domain.HumanItemRecord {
|
|
t.Helper()
|
|
r := f.request("GET", "/api/acceptance-claim-candidates?projectId="+f.item.ProjectID, f.reviewer, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("candidates %d %s", r.Code, r.Body.String())
|
|
}
|
|
var page humanItemListPage
|
|
if e := json.Unmarshal(r.Body.Bytes(), &page); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, item := range page.Items {
|
|
if item.SupplierID != "" || item.CanClaim == nil || !*item.CanClaim {
|
|
t.Fatal("identity leaked or unauthorized candidate")
|
|
}
|
|
}
|
|
if strings.Contains(r.Body.String(), "priority-production") || strings.Contains(r.Body.String(), "ordinary-production") {
|
|
t.Fatal("supplier leaked")
|
|
}
|
|
return page.Items
|
|
}
|
|
func TestAcceptancePriorityRandomAcrossPagesCountsAndCancellation(t *testing.T) {
|
|
f := priorityFixture(t)
|
|
for i := 0; i < 205; i++ {
|
|
item := f.item
|
|
item.ID = fmt.Sprintf("ordinary-%03d", i)
|
|
item.CreatedAt = "2026-01-02T00:00:00Z"
|
|
addPrioritySource(t, f, item, "ordinary-production")
|
|
}
|
|
target := f.item
|
|
target.ID = "priority-oldest"
|
|
target.CreatedAt = "2001-01-01T00:00:00Z"
|
|
addPrioritySource(t, f, target, "priority-production")
|
|
before := f.request("GET", "/api/human-item-counts?pluginId=annotation-qa&stageIds=prepackage_acceptance", f.reviewer, nil)
|
|
expiry := time.Now().Add(time.Hour).UTC().Format(time.RFC3339)
|
|
priorityPolicy(t, f, true, 0, expiry, []string{f.item.ProjectID})
|
|
after := f.request("GET", "/api/human-item-counts?pluginId=annotation-qa&stageIds=prepackage_acceptance", f.reviewer, nil)
|
|
if before.Code != 200 || after.Code != 200 || before.Body.String() != after.Body.String() {
|
|
t.Fatalf("counts changed: %s / %s", before.Body, after.Body)
|
|
}
|
|
candidates := priorityCandidates(t, f)
|
|
if len(candidates) != 1 || candidates[0].ID != target.ID {
|
|
t.Fatalf("old priority lost beyond page: %d", len(candidates))
|
|
}
|
|
// Reopened acceptance rounds keep production ownership and policy matching.
|
|
fresh, _ := f.store.GetHumanItem(f.ctx, target.ID)
|
|
fresh.ReviewRound++
|
|
fresh.Version++
|
|
fresh.Status = domain.HumanItemReady
|
|
_ = f.store.UpsertHumanItem(f.ctx, *fresh)
|
|
if got := priorityCandidates(t, f); len(got) != 1 || got[0].ReviewRound != fresh.ReviewRound {
|
|
t.Fatal("rework lost priority")
|
|
}
|
|
// Conflicting admin save never overwrites a newer policy.
|
|
stale := f.request("PUT", "/api/suppliers/priority-production/acceptance-priority", f.admin, map[string]any{"enabled": false, "version": 0})
|
|
if stale.Code != 409 {
|
|
t.Fatalf("stale admin %d", stale.Code)
|
|
}
|
|
priorityPolicy(t, f, false, 1, expiry, nil)
|
|
if got := priorityCandidates(t, f); len(got) < 2 {
|
|
t.Fatal("cancellation did not restore ordinary group")
|
|
}
|
|
}
|
|
func TestAcceptancePriorityContinuationOwnedFirstAndPermissionRefresh(t *testing.T) {
|
|
for _, owned := range []bool{false, true} {
|
|
t.Run(fmt.Sprint(owned), func(t *testing.T) {
|
|
f := priorityFixture(t)
|
|
item := f.item
|
|
item.ID = "priority-newer"
|
|
item.CreatedAt = "2090-01-01T00:00:00Z"
|
|
addPrioritySource(t, f, item, "priority-production")
|
|
priorityPolicy(t, f, true, 0, time.Now().Add(time.Hour).UTC().Format(time.RFC3339), nil)
|
|
if owned {
|
|
old, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
old.AssignedUserID = f.reviewer.ID
|
|
old.Status = domain.HumanItemClaimed
|
|
_ = f.store.UpsertHumanItem(f.ctx, *old)
|
|
}
|
|
r := continuationCall(f, "start", "priority-page", 0, map[string]any{"stage": "acceptance"}, nil)
|
|
result := continuationResult(t, r)
|
|
want := item.ID
|
|
if owned {
|
|
want = f.item.ID
|
|
}
|
|
if result.Item == nil || result.Item.ID != want {
|
|
t.Fatalf("continuation selected %+v want %s", result.Item, want)
|
|
}
|
|
// Revocation stops fresh admission immediately; existing current-round access remains.
|
|
_, err := f.store.SetSupplierAcceptanceAuthorization(f.ctx, f.item.OrganizationID, f.reviewer.SupplierID, false, 1, f.admin.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := priorityCandidates(t, f); len(got) != 0 {
|
|
t.Fatal("revoked reviewer sees new admission")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestAcceptancePriorityUnknownScopeExpiryAndAdminOnly(t *testing.T) {
|
|
f := priorityFixture(t)
|
|
expiry := time.Now().Add(time.Hour).UTC().Format(time.RFC3339)
|
|
for _, u := range []domain.UserRecord{f.annotator, f.reviewer, {ID: "manager", Role: domain.UserRoleSupplierManager, OrganizationID: f.item.OrganizationID, SupplierID: "priority-production"}} {
|
|
for _, method := range []string{"GET", "PUT"} {
|
|
r := f.request(method, "/api/suppliers/priority-production/acceptance-priority", u, nil)
|
|
if r.Code != 403 {
|
|
t.Fatalf("admin-only %s %d", method, r.Code)
|
|
}
|
|
}
|
|
}
|
|
// Missing source supplier: leave ordinary, report unknown only to admin.
|
|
target := f.item
|
|
target.ID = "unknown"
|
|
addPrioritySource(t, f, target, "")
|
|
priorityPolicy(t, f, true, 0, expiry, nil)
|
|
got := priorityCandidates(t, f)
|
|
if len(got) != 2 {
|
|
t.Fatalf("unknown dropped from ordinary group: %d", len(got))
|
|
}
|
|
admin := f.request("GET", "/api/suppliers/priority-production/acceptance-priority/statistics?version=1", f.admin, nil)
|
|
if admin.Code != 200 || !strings.Contains(admin.Body.String(), `"unresolvedCount":1`) {
|
|
t.Fatalf("unknown not reported %s", admin.Body)
|
|
}
|
|
// Expired policies are ignored without a cron/job/database mutation.
|
|
_, e := f.store.SaveAcceptancePriority(f.ctx, domain.AcceptancePriority{OrganizationID: f.item.OrganizationID, SupplierID: "priority-production", Enabled: true, ExpiresAt: time.Now().Add(-time.Second).UTC().Format(time.RFC3339)}, 1)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if got := priorityCandidates(t, f); len(got) != 2 {
|
|
t.Fatal("expiry blocked fallback")
|
|
}
|
|
// Invalid expiry and cross-organization project never persist.
|
|
bad := f.request("PUT", "/api/suppliers/priority-production/acceptance-priority", f.admin, map[string]any{"enabled": true, "expiresAt": "bad", "version": 2})
|
|
if bad.Code != 422 && bad.Code != 400 {
|
|
t.Fatalf("expiry validation %d", bad.Code)
|
|
}
|
|
}
|
|
func TestAcceptancePriorityConcurrentClaimCAS(t *testing.T) {
|
|
f := priorityFixture(t)
|
|
target := f.item
|
|
target.ID = "priority-race"
|
|
addPrioritySource(t, f, target, "priority-production")
|
|
priorityPolicy(t, f, true, 0, time.Now().Add(time.Hour).UTC().Format(time.RFC3339), nil)
|
|
adminCount := f.request("GET", "/api/suppliers/priority-production/acceptance-priority/statistics?version=1", f.admin, nil)
|
|
if !strings.Contains(adminCount.Body.String(), `"remainingCount":1`) {
|
|
t.Fatalf("admin count %s", adminCount.Body)
|
|
}
|
|
candidate := priorityCandidates(t, f)[0]
|
|
competitor := f.reviewer
|
|
competitor.ID = "priority-competitor"
|
|
_ = f.store.UpsertUser(f.ctx, competitor)
|
|
codes := make(chan int, 8)
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
actor := f.reviewer
|
|
if i%2 == 1 {
|
|
actor = competitor
|
|
}
|
|
go func() {
|
|
defer wg.Done()
|
|
r := f.request(http.MethodPost, "/api/human-items/"+candidate.ID+"/claim", actor, map[string]any{"version": candidate.Version})
|
|
codes <- r.Code
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
close(codes)
|
|
success := 0
|
|
for code := range codes {
|
|
if code == 200 {
|
|
success++
|
|
} else if code != 409 {
|
|
t.Fatalf("unexpected claim %d", code)
|
|
}
|
|
}
|
|
if success != 1 {
|
|
t.Fatalf("successful claims %d", success)
|
|
}
|
|
// Once exhausted, ordinary candidates remain available.
|
|
if got := priorityCandidates(t, f); len(got) != 1 || got[0].ID != f.item.ID {
|
|
t.Fatal("exhausted group did not fall back")
|
|
}
|
|
}
|
|
|
|
func TestAcceptancePriorityProjectScopeExpiryAndSourceConflict(t *testing.T) {
|
|
f := priorityFixture(t)
|
|
item := f.item
|
|
item.ID = "target-scoped"
|
|
addPrioritySource(t, f, item, "priority-production")
|
|
_ = f.store.UpsertProject(f.ctx, domain.ProjectRecord{ID: "other-project", OrganizationID: f.item.OrganizationID, Status: domain.ProjectStatusActive})
|
|
expiry := time.Now().Add(time.Hour).UTC().Format(time.RFC3339)
|
|
priorityPolicy(t, f, true, 0, expiry, []string{"other-project"})
|
|
if got := priorityCandidates(t, f); len(got) != 2 {
|
|
t.Fatal("project scope leaked priority into another project")
|
|
}
|
|
priorityPolicy(t, f, true, 1, expiry, []string{f.item.ProjectID})
|
|
if got := priorityCandidates(t, f); len(got) != 1 || got[0].ID != item.ID {
|
|
t.Fatal("project priority not applied")
|
|
}
|
|
// Expiry really changes the chosen group, while preserving all ordinary candidates.
|
|
_, err := f.store.SaveAcceptancePriority(f.ctx, domain.AcceptancePriority{OrganizationID: f.item.OrganizationID, SupplierID: "priority-production", Enabled: true, ExpiresAt: time.Now().Add(-time.Second).UTC().Format(time.RFC3339)}, 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := priorityCandidates(t, f); len(got) != 2 {
|
|
t.Fatal("expired target remained preferred")
|
|
}
|
|
// Resolve legacy blank human ownership from the stable task, never current user membership.
|
|
source, _ := f.store.GetHumanItem(f.ctx, "production-"+item.ID)
|
|
source.SupplierID = ""
|
|
source.LegacyTaskID = "source-task-owned"
|
|
source.LatestAuthorUserID = "deleted-annotator"
|
|
_ = f.store.UpsertHumanItem(f.ctx, *source)
|
|
task := f.task
|
|
task.ID = source.LegacyTaskID
|
|
task.SupplierID = "priority-production"
|
|
_ = f.store.UpsertAnnotationTask(f.ctx, task)
|
|
target, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
resolver, _ := (&server{store: f.store}).newAcceptancePriorityResolver(f.ctx, f.item.OrganizationID)
|
|
sid, err := resolver.supplier(*target)
|
|
if err != nil || sid != "priority-production" {
|
|
t.Fatalf("task ownership fallback %s %v", sid, err)
|
|
}
|
|
// Disagreeing historical identities are unknown rather than guessed from either side.
|
|
source.SupplierID = "ordinary-production"
|
|
_ = f.store.UpsertHumanItem(f.ctx, *source)
|
|
resolver, _ = (&server{store: f.store}).newAcceptancePriorityResolver(f.ctx, f.item.OrganizationID)
|
|
sid, err = resolver.supplier(*target)
|
|
if err != nil || sid != "" {
|
|
t.Fatal("conflicting source ownership was guessed")
|
|
}
|
|
// Source ownership is never copied onto the anonymous acceptance record.
|
|
stored, _ := f.store.GetHumanItem(f.ctx, target.ID)
|
|
if stored.SupplierID != "" {
|
|
t.Fatal("acceptance ownership changed")
|
|
}
|
|
}
|
|
|
|
// Admission must not fetch annotation bodies, and source-query failures must
|
|
// never be converted into an empty priority pool and ordinary admission.
|
|
type priorityReadGuardStore struct {
|
|
store.Store
|
|
failSources bool
|
|
}
|
|
|
|
func (s priorityReadGuardStore) ListHumanItems(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
|
if q.ListProjection && !q.PoolCandidateProjection && len(q.IDs) > 0 {
|
|
return nil, errors.New("unexpected evidence hydration in candidate scan")
|
|
}
|
|
return s.Store.ListHumanItems(ctx, org, q)
|
|
}
|
|
func (s priorityReadGuardStore) ResolveAcceptanceSourceSuppliers(ctx context.Context, org string, ids []string) (map[string]string, error) {
|
|
if s.failSources {
|
|
return nil, errors.New("source identity query unavailable")
|
|
}
|
|
return s.Store.ResolveAcceptanceSourceSuppliers(ctx, org, ids)
|
|
}
|
|
func TestAcceptancePriorityCandidateReadsAreLightAndFailClosed(t *testing.T) {
|
|
f := priorityFixture(t)
|
|
item := f.item
|
|
item.ID = "guarded-priority"
|
|
addPrioritySource(t, f, item, "priority-production")
|
|
priorityPolicy(t, f, true, 0, time.Now().Add(time.Hour).UTC().Format(time.RFC3339), nil)
|
|
f.handler = NewHandler(priorityReadGuardStore{Store: f.store}, nil)
|
|
got := priorityCandidates(t, f)
|
|
if len(got) != 1 || got[0].ID != item.ID {
|
|
t.Fatal("metadata admission changed candidate")
|
|
}
|
|
f.handler = NewHandler(priorityReadGuardStore{Store: f.store, failSources: true}, nil)
|
|
r := f.request("GET", "/api/acceptance-claim-candidates", f.reviewer, nil)
|
|
if r.Code != 500 || strings.Contains(r.Body.String(), `"items"`) {
|
|
t.Fatalf("source failure became ordinary admission %d %s", r.Code, r.Body)
|
|
}
|
|
}
|