package api import ( "droboticflow/internal/domain" "encoding/json" "net/http" "testing" ) func TestQualityRejectionReturnsToEligibleAuthor(t *testing.T) { for _, durable := range []bool{false, true} { for _, scenario := range []string{"eligible", "disabled", "wrong-supplier", "no-membership", "supplier-inactive", "no-author", "escalated"} { t.Run(scenario+map[bool]string{true: "-durable", false: "-legacy"}[durable], func(t *testing.T) { f := decisionFixture(t) f.item.LatestAuthorUserID = f.annotator.ID f.item.SupplierID = f.annotator.SupplierID f.task.SupplierID = f.annotator.SupplierID f.item.MaxReworkRounds = 5 switch scenario { case "disabled": f.annotator.Status = domain.UserStatusDisabled f.store.UpsertUser(f.ctx, f.annotator) case "wrong-supplier": f.store.UpsertSupplier(f.ctx, domain.SupplierRecord{ID: "other", OrganizationID: f.item.OrganizationID, Name: "Other", Status: "active"}) f.annotator.SupplierID = "other" f.annotator.ID = "different-supplier-author" f.item.LatestAuthorUserID = f.annotator.ID f.store.UpsertUser(f.ctx, f.annotator) case "no-membership": f.store.DeleteProjectMember(f.ctx, "member-"+f.annotator.ID) case "supplier-inactive": f.store.UpsertSupplier(f.ctx, domain.SupplierRecord{ID: f.annotator.SupplierID, OrganizationID: f.item.OrganizationID, Name: "Inactive", Status: "inactive"}) case "no-author": f.item.LatestAuthorUserID = "" case "escalated": f.item.MaxReworkRounds = 1 } f.store.UpsertHumanItem(f.ctx, f.item) f.store.UpsertAnnotationTask(f.ctx, f.task) input := map[string]any{"stage": "quality", "decision": "rejected", "reasonCodes": []string{"manual_review_reject"}, "comment": "请修正", "problemSliceIds": []string{"left"}} endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/reviews" var code int var body string if durable { version, headers := openReviewDraftSession(t, f) input["operationId"] = "return-author" input["draftVersion"] = 0 input["resultVersion"] = version r := reviewSessionRequest(f, http.MethodPost, endpoint, f.reviewer, input, headers) code = r.Code body = r.Body.String() } else { r := f.request(http.MethodPost, endpoint, f.reviewer, input) code = r.Code body = r.Body.String() } if code != 201 { t.Fatalf("review %d %s", code, body) } item, _ := f.store.GetHumanItem(f.ctx, f.item.ID) task, _ := f.store.GetAnnotationTask(f.ctx, f.task.ID) expected := "" if scenario == "eligible" { expected = f.annotator.ID } if item.AssignedUserID != expected || task.AssignedUserID != expected { t.Fatalf("return owner item=%q task=%q want=%q", item.AssignedUserID, task.AssignedUserID, expected) } if item.CurrentRevision != f.item.CurrentRevision { t.Fatal("annotation revision changed") } if expected != "" { if item.Status != domain.HumanItemClaimed || task.Status != domain.AnnotationTaskStatusRework { t.Fatalf("returned author must be editable while task remains rework: item=%s task=%s", item.Status, task.Status) } r := f.request(http.MethodGet, "/api/human-items?pool=mine&projectId="+item.ProjectID, f.annotator, nil) var p struct { Items []domain.HumanItemRecord `json:"items"` } json.Unmarshal(r.Body.Bytes(), &p) if len(p.Items) != 1 { t.Fatalf("returned task missing in author's pool: %s", r.Body.String()) } seedSliceDictionary(t, f, "同名切片") draft := map[string]any{"version": item.Version, "sliceSegments": []map[string]any{{"id": "left", "name": "同名切片", "start": 0.0, "end": 5.0}, {"id": "right", "name": "同名切片", "start": 5.0, "end": 10.0}}, "segments": []map[string]any{{"id": "left-action", "start": 0.0, "end": 5.0, "description": "修正后的动作"}, {"id": "right-action", "start": 5.0, "end": 10.0, "description": "右段动作"}}, "statuses": map[string]string{"left": "labeled", "right": "labeled"}} saved := f.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/drafts", f.annotator, draft) if saved.Code != http.StatusOK { t.Fatalf("returned author cannot save: %d %s", saved.Code, saved.Body.String()) } stale := f.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/drafts", f.annotator, draft) if stale.Code != http.StatusConflict { t.Fatalf("stale draft must still conflict: %d", stale.Code) } } }) } } }