forked from longtao.wu/droboticflow
373 lines
16 KiB
Go
373 lines
16 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func continuationCall(f combinedDraftFixture, action, page string, generation int, extra map[string]any, headers map[string]string) *httptest.ResponseRecorder {
|
|
body := map[string]any{"action": action, "pageId": page, "generation": generation, "stage": "quality"}
|
|
for k, v := range extra {
|
|
body[k] = v
|
|
}
|
|
return reviewSessionRequest(f, http.MethodPost, "/api/projects/"+f.item.ProjectID+"/review-continuation", f.reviewer, body, headers)
|
|
}
|
|
|
|
func TestReviewContinuationOrdersInstantsAndInvalidClaimFallback(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
items []domain.HumanItemRecord
|
|
want string
|
|
}{
|
|
{"fractional seconds", []domain.HumanItemRecord{{ID: "z-whole", CreatedAt: "2026-01-01T00:00:00Z"}, {ID: "a-fraction", CreatedAt: "2026-01-01T00:00:00.1Z"}}, "z-whole,a-fraction"},
|
|
{"offsets and equal instants", []domain.HumanItemRecord{{ID: "b-offset", CreatedAt: "2026-01-01T08:00:00+08:00"}, {ID: "a-utc", CreatedAt: "2026-01-01T00:00:00.000Z"}, {ID: "c-earlier", CreatedAt: "2026-01-01T01:00:00+02:00"}}, "c-earlier,a-utc,b-offset"},
|
|
{"invalid claims use creation", []domain.HumanItemRecord{{ID: "z-created", AssignedUserID: "reviewer", ClaimedAt: "bad", CreatedAt: "2020-01-01T00:00:00Z"}, {ID: "a-claimed", AssignedUserID: "reviewer", ClaimedAt: "2021-01-01T00:00:00Z", CreatedAt: "2019-01-01T00:00:00Z"}, {ID: "b-unknown", AssignedUserID: "reviewer", ClaimedAt: "bad", CreatedAt: "bad"}, {ID: "a-unknown", AssignedUserID: "reviewer"}, {ID: "public", CreatedAt: "2010-01-01T00:00:00Z"}}, "z-created,a-claimed,a-unknown,b-unknown,public"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
sort.Slice(tc.items, func(i, j int) bool { return continuationCandidateLess(tc.items[i], tc.items[j], "reviewer") })
|
|
ids := make([]string, len(tc.items))
|
|
for i, item := range tc.items {
|
|
ids[i] = item.ID
|
|
}
|
|
if got := strings.Join(ids, ","); got != tc.want {
|
|
t.Fatalf("order %s, want %s", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReviewContinuationSkipsLegacyTasksAndDraftSaveReceipts(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
legacyBatch := *batch
|
|
legacyBatch.ID = "legacy-manual-review"
|
|
legacyBatch.RequirePrePackageAcceptance = false
|
|
_ = f.store.UpsertHumanBatch(f.ctx, legacyBatch)
|
|
legacy := f.item
|
|
legacy.ID = "legacy-oldest"
|
|
legacy.BatchID = legacyBatch.ID
|
|
legacy.LegacyTaskID = ""
|
|
legacy.CreatedAt = "2000-01-01T00:00:00Z"
|
|
legacy.ClaimedAt = legacy.CreatedAt
|
|
_ = f.store.UpsertHumanItem(f.ctx, legacy)
|
|
_, headers := openReviewDraftSession(t, f)
|
|
started := continuationResult(t, continuationCall(f, "start", "queue", 0, nil, nil))
|
|
if started.Item == nil || started.Item.ID != f.item.ID {
|
|
t.Fatalf("selected unsupported legacy: %+v", started.Item)
|
|
}
|
|
bound := continuationResult(t, continuationCall(f, "bind", "queue", started.Generation, map[string]any{"itemId": f.item.ID}, headers))
|
|
session, _ := f.store.GetReviewSession(f.ctx, reviewSessionKey(f.item))
|
|
op := domain.ReviewOperation{ID: "draft-operation", OperationID: "draft-save", OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID, UserID: f.reviewer.ID, ItemID: f.item.ID, Kind: "quality_draft", Status: "completed", SessionKey: session.ID, PageID: session.PageID, Generation: session.Generation}
|
|
if ok, err := f.store.CompareAndSwapReviewOperation(f.ctx, op, 0); !ok || err != nil {
|
|
t.Fatalf("save receipt: %v %v", ok, err)
|
|
}
|
|
denied := continuationCall(f, "next", "queue", bound.Generation, map[string]any{"operationId": op.OperationID}, nil)
|
|
if denied.Code != 409 || !strings.Contains(denied.Body.String(), "review_decision_not_completed") {
|
|
t.Fatalf("draft receipt advanced task: %d %s", denied.Code, denied.Body.String())
|
|
}
|
|
}
|
|
func continuationResult(t *testing.T, w *httptest.ResponseRecorder) struct {
|
|
Generation int `json:"generation"`
|
|
Status string `json:"status"`
|
|
Item *domain.HumanItemRecord `json:"item"`
|
|
} {
|
|
t.Helper()
|
|
if w.Code != 200 {
|
|
t.Fatalf("continuation %d: %s", w.Code, w.Body.String())
|
|
}
|
|
var out struct {
|
|
Generation int `json:"generation"`
|
|
Status string `json:"status"`
|
|
Item *domain.HumanItemRecord `json:"item"`
|
|
}
|
|
if e := json.Unmarshal(w.Body.Bytes(), &out); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return out
|
|
}
|
|
func TestReviewContinuationOwnPriorityAllCandidatesAndStableOrdering(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
// The earliest owned item lies beyond a typical first page of newest records.
|
|
f.item.ClaimedAt = "2026-01-01T00:00:00Z"
|
|
f.item.CreatedAt = "2025-01-01T00:00:00Z"
|
|
_ = f.store.UpsertHumanItem(f.ctx, f.item)
|
|
for i := 0; i < 121; i++ {
|
|
v := f.item
|
|
v.ID = fmt.Sprintf("candidate-%03d", i)
|
|
v.LegacyTaskID = ""
|
|
v.AssignedUserID = ""
|
|
v.Status = domain.HumanItemSubmitted
|
|
v.CreatedAt = fmt.Sprintf("2026-02-%02dT00:%02d:00Z", 1+i/60, i%60)
|
|
_ = f.store.UpsertHumanItem(f.ctx, v)
|
|
}
|
|
out := continuationResult(t, continuationCall(f, "start", "queue-a", 0, nil, nil))
|
|
if out.Item == nil || out.Item.ID != f.item.ID {
|
|
t.Fatalf("owned first: %+v", out.Item)
|
|
}
|
|
repeated := continuationResult(t, continuationCall(f, "next", "queue-a", out.Generation, nil, nil))
|
|
if repeated.Item == nil || repeated.Item.ID != f.item.ID {
|
|
t.Fatal("unbound task must be returned, not claim more")
|
|
}
|
|
items, _ := f.store.ListHumanItems(f.ctx, f.item.OrganizationID, domain.HumanItemQuery{ProjectID: f.item.ProjectID, ActorUserID: f.reviewer.ID})
|
|
if len(items) != 1 {
|
|
t.Fatalf("claimed extra tasks: %d", len(items))
|
|
}
|
|
}
|
|
func TestReviewContinuationBoundTaskRequiresCompletedReceiptAndStopsOldPage(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
session := f.request(http.MethodPost, "/api/human-items/"+f.item.ID+"/review-session", f.reviewer, map[string]any{"action": "acquire", "pageId": "review-a"})
|
|
var s struct {
|
|
Generation int `json:"generation"`
|
|
}
|
|
_ = json.Unmarshal(session.Body.Bytes(), &s)
|
|
headers := map[string]string{"X-Review-Page": "review-a", "X-Review-Generation": fmt.Sprint(s.Generation)}
|
|
started := continuationResult(t, continuationCall(f, "start", "queue-a", 0, map[string]any{"itemId": f.item.ID}, headers))
|
|
denied := continuationCall(f, "next", "queue-a", started.Generation, nil, nil)
|
|
if denied.Code != 409 {
|
|
t.Fatalf("no receipt advanced: %d %s", denied.Code, denied.Body.String())
|
|
}
|
|
_ = f.request(http.MethodPost, "/api/human-items/"+f.item.ID+"/review-session", f.reviewer, map[string]any{"action": "takeover", "pageId": "review-b"})
|
|
stale := continuationCall(f, "renew", "queue-a", started.Generation, nil, nil)
|
|
if stale.Code != 409 {
|
|
t.Fatalf("old review generation accepted: %d %s", stale.Code, stale.Body.String())
|
|
}
|
|
continuationResult(t, continuationCall(f, "pause", "queue-a", started.Generation, nil, nil))
|
|
if got := continuationCall(f, "next", "queue-a", started.Generation, nil, nil); got.Code != 409 {
|
|
t.Fatalf("paused advanced: %d", got.Code)
|
|
}
|
|
}
|
|
|
|
func TestReviewContinuationReceiptSuccessThenEmptyPollAndNewTask(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
version, headers := openReviewDraftSession(t, f)
|
|
started := continuationResult(t, continuationCall(f, "start", "queue-a", 0, map[string]any{"itemId": f.item.ID}, headers))
|
|
denied := continuationCall(f, "next", "queue-a", started.Generation, map[string]any{"operationId": "unknown"}, nil)
|
|
if denied.Code != 409 {
|
|
t.Fatal("unknown outcome cannot claim")
|
|
}
|
|
payload := map[string]any{"stage": "quality", "decision": "rejected", "reasonCodes": []string{"manual_review_reject"}, "problemSliceIds": []string{"left"}, "comment": "需要修正", "operationId": "continue-receipt", "draftVersion": 0, "resultVersion": version}
|
|
formal := reviewSessionRequest(f, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/reviews", f.reviewer, payload, headers)
|
|
if formal.Code != 201 {
|
|
t.Fatalf("decision %d %s", formal.Code, formal.Body.String())
|
|
}
|
|
waiting := continuationResult(t, continuationCall(f, "next", "queue-a", started.Generation, map[string]any{"operationId": "continue-receipt"}, nil))
|
|
if waiting.Status != "waiting" || waiting.Item != nil {
|
|
t.Fatalf("expected empty wait %+v", waiting)
|
|
}
|
|
// The completed source may leave this reviewer/stage. Waiting only depends on its own lease now.
|
|
continuationResult(t, continuationCall(f, "next", "queue-a", started.Generation, nil, nil))
|
|
next := f.item
|
|
next.ID = "new-review-item"
|
|
next.LegacyTaskID = ""
|
|
next.AssignedUserID = ""
|
|
next.Status = domain.HumanItemSubmitted
|
|
next.CreatedAt = "2020-01-01T00:00:00Z"
|
|
_ = f.store.UpsertHumanItem(f.ctx, next)
|
|
resumed := continuationResult(t, continuationCall(f, "next", "queue-a", started.Generation, nil, nil))
|
|
if resumed.Item == nil || resumed.Item.ID != next.ID {
|
|
t.Fatalf("new task %+v", resumed.Item)
|
|
}
|
|
}
|
|
|
|
type continuationRaceStore struct {
|
|
store.Store
|
|
beforeClaim func()
|
|
fired bool
|
|
}
|
|
|
|
func (s *continuationRaceStore) ClaimHumanItemVersioned(ctx context.Context, item domain.HumanItemRecord, version int, role domain.UserRole) (bool, error) {
|
|
if !s.fired {
|
|
s.fired = true
|
|
s.beforeClaim()
|
|
}
|
|
return s.Store.ClaimHumanItemVersioned(ctx, item, version, role)
|
|
}
|
|
func TestReviewContinuationPauseDuringAdmittedClaimAndCompetingCandidate(t *testing.T) {
|
|
for _, kind := range []string{"pause", "conflict"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
f.item.AssignedUserID = ""
|
|
f.item.Status = domain.HumanItemSubmitted
|
|
f.item.CreatedAt = "2020-01-01T00:00:00Z"
|
|
_ = f.store.UpsertHumanItem(f.ctx, f.item)
|
|
second := f.item
|
|
second.ID = "second-review"
|
|
second.LegacyTaskID = ""
|
|
second.CreatedAt = "2021-01-01T00:00:00Z"
|
|
_ = f.store.UpsertHumanItem(f.ctx, second)
|
|
wrapped := &continuationRaceStore{Store: f.store}
|
|
f.handler = NewHandler(wrapped, nil)
|
|
wrapped.beforeClaim = func() {
|
|
if kind == "pause" {
|
|
continuationResult(t, continuationCall(f, "pause", "queue-a", 1, nil, nil))
|
|
} else {
|
|
first, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
first.AssignedUserID = f.annotator.ID
|
|
first.Status = domain.HumanItemClaimed
|
|
first.Version++
|
|
_ = f.store.UpsertHumanItem(f.ctx, *first)
|
|
}
|
|
}
|
|
out := continuationResult(t, continuationCall(f, "start", "queue-a", 0, nil, nil))
|
|
if kind == "pause" {
|
|
if out.Status != "paused" || out.Item == nil || out.Item.ID != f.item.ID {
|
|
t.Fatalf("late claim should remain mine only %+v", out)
|
|
}
|
|
denied := continuationCall(f, "next", "queue-a", out.Generation, nil, nil)
|
|
if denied.Code != 409 {
|
|
t.Fatal("pause allowed another claim")
|
|
}
|
|
mine, _ := f.store.ListHumanItems(f.ctx, f.item.OrganizationID, domain.HumanItemQuery{ActorUserID: f.reviewer.ID})
|
|
if len(mine) != 1 {
|
|
t.Fatalf("claimed %d", len(mine))
|
|
}
|
|
} else if out.Item == nil || out.Item.ID != second.ID {
|
|
t.Fatalf("did not skip taken candidate %+v", out.Item)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestReviewContinuationPublicStableOrderAndHistoricalOwnedFallback(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
f.item.Status = domain.HumanItemAccepted
|
|
_ = f.store.UpsertHumanItem(f.ctx, f.item)
|
|
for _, id := range []string{"z-earliest", "a-earliest"} {
|
|
v := f.item
|
|
v.ID = id
|
|
v.LegacyTaskID = ""
|
|
v.AssignedUserID = ""
|
|
v.Status = domain.HumanItemSubmitted
|
|
v.CreatedAt = "2020-01-01T00:00:00Z"
|
|
_ = f.store.UpsertHumanItem(f.ctx, v)
|
|
}
|
|
out := continuationResult(t, continuationCall(f, "start", "queue-a", 0, nil, nil))
|
|
if out.Item == nil || out.Item.ID != "a-earliest" {
|
|
t.Fatalf("unstable sort %+v", out.Item)
|
|
}
|
|
continuationResult(t, continuationCall(f, "pause", "queue-a", out.Generation, nil, nil))
|
|
old := f.item
|
|
old.ID = "legacy-owned"
|
|
old.LegacyTaskID = ""
|
|
old.Status = domain.HumanItemClaimed
|
|
old.ClaimedAt = ""
|
|
old.CreatedAt = "2010-01-01T00:00:00Z"
|
|
_ = f.store.UpsertHumanItem(f.ctx, old)
|
|
again := continuationResult(t, continuationCall(f, "start", "queue-a", 0, nil, nil))
|
|
if again.Item == nil || again.Item.ID != old.ID {
|
|
t.Fatalf("legacy fallback %+v", again.Item)
|
|
}
|
|
}
|
|
|
|
type continuationLostReplyStore struct {
|
|
store.Store
|
|
fired bool
|
|
}
|
|
|
|
func (s *continuationLostReplyStore) ClaimHumanItemVersioned(ctx context.Context, item domain.HumanItemRecord, version int, role domain.UserRole) (bool, error) {
|
|
ok, e := s.Store.ClaimHumanItemVersioned(ctx, item, version, role)
|
|
if ok && e == nil && !s.fired {
|
|
s.fired = true
|
|
return false, fmt.Errorf("lost claim response")
|
|
}
|
|
return ok, e
|
|
}
|
|
func TestReviewContinuationUnknownClaimReconcilesSameCandidateOnExplicitResume(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
f.item.AssignedUserID = ""
|
|
f.item.Status = domain.HumanItemSubmitted
|
|
_ = f.store.UpsertHumanItem(f.ctx, f.item)
|
|
wrapped := &continuationLostReplyStore{Store: f.store}
|
|
f.handler = NewHandler(wrapped, nil)
|
|
first := continuationCall(f, "start", "queue-a", 0, nil, nil)
|
|
if first.Code != 500 {
|
|
t.Fatalf("expected lost reply %d %s", first.Code, first.Body.String())
|
|
}
|
|
continuationResult(t, continuationCall(f, "pause", "queue-a", 1, nil, nil))
|
|
resumed := continuationResult(t, continuationCall(f, "start", "queue-a", 0, nil, nil))
|
|
if resumed.Item == nil || resumed.Item.ID != f.item.ID {
|
|
t.Fatalf("pending claim lost %+v", resumed)
|
|
}
|
|
mine, _ := f.store.ListHumanItems(f.ctx, f.item.OrganizationID, domain.HumanItemQuery{ActorUserID: f.reviewer.ID})
|
|
if len(mine) != 1 {
|
|
t.Fatalf("extra claim %d", len(mine))
|
|
}
|
|
}
|
|
func TestReviewContinuationAcceptanceKeepsItsOwnTaskUnitAndScope(t *testing.T) {
|
|
f := acceptanceReviewDraftFixture(t)
|
|
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
source := *batch
|
|
source.ID = "continuation-acceptance-source"
|
|
source.Stages = domain.DefaultFanyuV58HumanStages()
|
|
source.RequirePrePackageAcceptance = true
|
|
source.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
_ = f.store.UpsertHumanBatch(f.ctx, source)
|
|
batch.SourceBatchID = source.ID
|
|
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
_ = f.store.UpsertHumanBatch(f.ctx, *batch)
|
|
f.item.AssignedUserID = ""
|
|
f.item.Status = domain.HumanItemReady
|
|
_ = f.store.UpsertHumanItem(f.ctx, f.item)
|
|
out := continuationResult(t, continuationCall(f, "start", "acceptance-page", 0, map[string]any{"stage": "acceptance"}, nil))
|
|
if out.Item == nil || out.Item.ID != f.item.ID {
|
|
t.Fatalf("acceptance task unit %+v", out.Item)
|
|
}
|
|
wrong := continuationCall(f, "start", "wrong-stage", 0, nil, nil)
|
|
if wrong.Code != 403 {
|
|
t.Fatalf("role mismatch %d %s", wrong.Code, wrong.Body.String())
|
|
}
|
|
f.reviewer.Status = domain.UserStatusDisabled
|
|
_ = f.store.UpsertUser(f.ctx, f.reviewer)
|
|
disabled := continuationCall(f, "renew", "acceptance-page", out.Generation, map[string]any{"stage": "acceptance"}, nil)
|
|
if disabled.Code != 403 {
|
|
t.Fatalf("disabled account %d", disabled.Code)
|
|
}
|
|
}
|
|
|
|
func TestReviewContinuationCompactRenewKeepsLegacyContract(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
if f.item.Subject.Payload == nil {
|
|
f.item.Subject.Payload = map[string]any{}
|
|
}
|
|
f.item.Subject.Payload["largeFixture"] = strings.Repeat("representative-data-", 500)
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
started := continuationResult(t, continuationCall(f, "start", "compact-page", 0, nil, nil))
|
|
if started.Item == nil {
|
|
t.Fatal("missing initial item")
|
|
}
|
|
compact := continuationCall(f, "renew", "compact-page", started.Generation, map[string]any{"compact": true}, nil)
|
|
if compact.Code != 200 {
|
|
t.Fatalf("compact renew: %d %s", compact.Code, compact.Body.String())
|
|
}
|
|
var body map[string]any
|
|
if err := json.Unmarshal(compact.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body["itemId"] != started.Item.ID || body["status"] != "active" {
|
|
t.Fatalf("incorrect identity: %v", body)
|
|
}
|
|
if _, exists := body["item"]; exists {
|
|
t.Fatal("compact response contains full item")
|
|
}
|
|
if compact.Body.Len() > 1024 {
|
|
t.Fatalf("compact response too large: %d", compact.Body.Len())
|
|
}
|
|
legacyResponse := continuationCall(f, "renew", "compact-page", started.Generation, nil, nil)
|
|
if compact.Body.Len()*10 >= legacyResponse.Body.Len() {
|
|
t.Fatalf("compact %d not 90%% smaller than legacy %d", compact.Body.Len(), legacyResponse.Body.Len())
|
|
}
|
|
legacy := continuationResult(t, legacyResponse)
|
|
if legacy.Item == nil || legacy.Item.ID != started.Item.ID {
|
|
t.Fatal("legacy response lost item")
|
|
}
|
|
}
|