package api import ( "encoding/json" "net/http" "net/url" "testing" "time" "droboticflow/internal/domain" ) // TestAnnotatorReturnedTaskSurvivesWindowedTaskList pins the paging contract // behind the 生产事故 "审核打回后标注员看不见回退任务" (2026-10-14): a quality // rejection reassigns the ORIGINAL annotation task, whose createdAt keeps the // original import date. When newer import waves fill the newest-first window // with tasks the annotator cannot read, the authorization filter empties the // page — and the response used to drop the continuation cursor, so the client // could never page through to its own returned task. func TestAnnotatorReturnedTaskSurvivesWindowedTaskList(t *testing.T) { fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticReview, domain.HumanItemClaimed, "annotator-v5", 1) // The item was authored by the annotator and rejected back to them // (rework round 1): the rejection keeps the original task age. item := fixture.item item.LatestAuthorUserID = fixture.annotator.ID item.ReviewRound = 1 if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil { t.Fatal(err) } oldTask := fixture.task oldTask.Status = domain.AnnotationTaskStatusRework oldTask.AssignedUserID = fixture.annotator.ID oldTask.CreatedAt = time.Now().UTC().Add(-72 * time.Hour).Format(time.RFC3339) if err := fixture.store.UpsertAnnotationTask(fixture.ctx, oldTask); err != nil { t.Fatal(err) } // A newer import wave from another supplier's project: the annotator is // not a member, so authorization filtering removes it from their list. foreign := oldTask foreign.ID = "task-foreign-newer" foreign.ProjectID = "project-foreign" foreign.WorkgroupID = "workgroup-foreign" foreign.VendorID = "vendor-foreign" foreign.Status = domain.AnnotationTaskStatusPendingAnnotation foreign.AssignedUserID = "" foreign.CreatedAt = time.Now().UTC().Format(time.RFC3339) if err := fixture.store.UpsertAnnotationTask(fixture.ctx, foreign); err != nil { t.Fatal(err) } // Page 1 (limit=1) holds only the foreign task: filtered to empty, but // the continuation cursor must survive so paging can reach page 2. first := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks?limit=1", fixture.annotator, nil) if first.Code != http.StatusOK { t.Fatalf("first page status=%d body=%s", first.Code, first.Body.String()) } var firstPage struct { Items []map[string]any `json:"items"` NextCursor string `json:"nextCursor"` HasMore bool `json:"hasMore"` } if err := json.Unmarshal(first.Body.Bytes(), &firstPage); err != nil { t.Fatal(err) } if len(firstPage.Items) != 0 { t.Fatalf("first page should be authorization-empty, got %d items", len(firstPage.Items)) } if firstPage.NextCursor == "" || !firstPage.HasMore { t.Fatalf("authorization-empty page must keep the continuation cursor, got nextCursor=%q hasMore=%v", firstPage.NextCursor, firstPage.HasMore) } // Page 2 reaches the annotator's own returned task. second := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks?limit=1&cursor="+url.QueryEscape(firstPage.NextCursor), fixture.annotator, nil) if second.Code != http.StatusOK { t.Fatalf("second page status=%d body=%s", second.Code, second.Body.String()) } var secondPage struct { Items []struct { ID string `json:"id"` Status string `json:"status"` } `json:"items"` } if err := json.Unmarshal(second.Body.Bytes(), &secondPage); err != nil { t.Fatal(err) } if len(secondPage.Items) != 1 || secondPage.Items[0].ID != oldTask.ID { t.Fatalf("second page should return the annotator's returned task %s, got %+v", oldTask.ID, secondPage.Items) } if secondPage.Items[0].Status != string(domain.AnnotationTaskStatusRework) { t.Fatalf("returned task status=%s, want rework", secondPage.Items[0].Status) } // The pool-item ids join (used by non-manager workspaces) reaches the // same task directly, regardless of window position. joined := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks?ids="+url.QueryEscape(oldTask.ID), fixture.annotator, nil) if joined.Code != http.StatusOK { t.Fatalf("ids join status=%d body=%s", joined.Code, joined.Body.String()) } var joinedPage struct { Items []struct { ID string `json:"id"` } `json:"items"` NextCursor string `json:"nextCursor"` } if err := json.Unmarshal(joined.Body.Bytes(), &joinedPage); err != nil { t.Fatal(err) } if len(joinedPage.Items) != 1 || joinedPage.Items[0].ID != oldTask.ID { t.Fatalf("ids join should return the returned task, got %+v", joinedPage.Items) } if joinedPage.NextCursor != "" { t.Fatalf("ids joins are unwindowed and must not advertise a cursor, got %q", joinedPage.NextCursor) } }