forked from longtao.wu/droboticflow
145 lines
4.3 KiB
Go
145 lines
4.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
type qcProjectScopeProbe struct {
|
|
store.Store
|
|
legacy bool
|
|
candidates int
|
|
}
|
|
|
|
func (p *qcProjectScopeProbe) ListHumanItems(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
|
if p.legacy {
|
|
q.AvailableProjectIDs = nil
|
|
q.AvailableSupplierID = nil
|
|
}
|
|
rows, err := p.Store.ListHumanItems(ctx, org, q)
|
|
if q.PoolCandidateProjection {
|
|
p.candidates += len(rows)
|
|
}
|
|
return rows, err
|
|
}
|
|
func TestHumanPoolQCProjectScopeSkipsInvisibleBacklog(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
item := f.item
|
|
item.CurrentStageID = "quality_review"
|
|
item.AssignedUserID = ""
|
|
item.Status = domain.HumanItemReady
|
|
item.SupplierID = f.reviewer.SupplierID
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 90; i++ {
|
|
noise := item
|
|
noise.ID = fmt.Sprintf("hidden-%03d", i)
|
|
if i%2 == 0 {
|
|
noise.ProjectID = "foreign-project"
|
|
} else {
|
|
noise.SupplierID = "foreign-supplier"
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, noise); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
path := "/api/human-items?pluginId=annotation-qa&pool=available&stageIds=quality_review,semantic_review&limit=50"
|
|
old := &qcProjectScopeProbe{Store: f.store, legacy: true}
|
|
f.handler = NewHandler(old, nil)
|
|
before := f.request(http.MethodGet, path, f.reviewer, nil)
|
|
probe := &qcProjectScopeProbe{Store: f.store}
|
|
f.handler = NewHandler(probe, nil)
|
|
after := f.request(http.MethodGet, path, f.reviewer, nil)
|
|
if before.Code != 200 || after.Code != 200 || before.Body.String() != after.Body.String() {
|
|
t.Fatalf("changed response %d/%d", before.Code, after.Code)
|
|
}
|
|
var page struct{ Items []domain.HumanItemRecord }
|
|
if err := json.Unmarshal(after.Body.Bytes(), &page); err != nil || len(page.Items) != 1 {
|
|
t.Fatalf("expected one visible task: %s", after.Body.String())
|
|
}
|
|
if old.candidates != 91 || probe.candidates != 1 {
|
|
t.Fatalf("candidate reads %d -> %d", old.candidates, probe.candidates)
|
|
}
|
|
// Permissions are rebuilt on each request. A disabled account cannot reuse scope.
|
|
user := f.reviewer
|
|
user.Status = domain.UserStatusDisabled
|
|
if err := f.store.UpsertUser(f.ctx, user); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after = f.request(http.MethodGet, path, user, nil)
|
|
if after.Code == 200 {
|
|
var p struct{ Items []domain.HumanItemRecord }
|
|
json.Unmarshal(after.Body.Bytes(), &p)
|
|
if len(p.Items) > 0 {
|
|
t.Fatal("disabled account retained visible tasks")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHumanPoolQCProjectScopePreservesMultiPageVisibleTasks(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
item := f.item
|
|
item.CurrentStageID = "quality_review"
|
|
item.AssignedUserID = ""
|
|
item.Status = domain.HumanItemReady
|
|
item.SupplierID = f.reviewer.SupplierID
|
|
item.CreatedAt = "2026-01-01T00:00:00Z"
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 700; i++ {
|
|
noise := item
|
|
noise.ID = fmt.Sprintf("noise-%04d", i)
|
|
noise.SupplierID = "foreign-supplier"
|
|
noise.CreatedAt = "2026-02-01T00:00:00Z"
|
|
if err := f.store.UpsertHumanItem(f.ctx, noise); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
collect := func(legacy bool) []string {
|
|
f.handler = NewHandler(&qcProjectScopeProbe{Store: f.store, legacy: legacy}, nil)
|
|
cursor := ""
|
|
ids := []string{}
|
|
for page := 0; page < 10; page++ {
|
|
path := "/api/human-items?pluginId=annotation-qa&pool=available&stageIds=quality_review,semantic_review&limit=5"
|
|
if cursor != "" {
|
|
path += "&cursor=" + cursor
|
|
}
|
|
r := f.request(http.MethodGet, path, f.reviewer, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("list failed %d", r.Code)
|
|
}
|
|
var out struct {
|
|
Items []domain.HumanItemRecord
|
|
HasMore bool
|
|
NextCursor string
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, it := range out.Items {
|
|
ids = append(ids, it.ID)
|
|
}
|
|
if !out.HasMore {
|
|
return ids
|
|
}
|
|
if out.NextCursor == "" || out.NextCursor == cursor {
|
|
t.Fatal("cursor failed to advance")
|
|
}
|
|
cursor = out.NextCursor
|
|
}
|
|
t.Fatal("pagination did not finish")
|
|
return nil
|
|
}
|
|
before, after := collect(true), collect(false)
|
|
if len(before) != 1 || len(after) != 1 || before[0] != after[0] {
|
|
t.Fatalf("visible tasks changed %v %v", before, after)
|
|
}
|
|
}
|