droboticflow/internal/api/human_items_pagination_test.go
d-robotics 58e0e50570 🧪 Retro 2026-10-12: CI gate, known-failure baselines, devboot regression test, workbench smoke, review standards
Six follow-ups from the round8-12 hotfix retrospective:

- web-ci.yml: lint + Go/frontend tests on every push and PR (nothing ran
  app checks before; the round10 dev-mode regression shipped green)
- tests/known-failures.json + check-test-baseline.mjs: gate by exact
  failing-test id instead of hand-counted failure totals (14 frontend,
  70 Go upstream debts recorded from the dev tip)
- devboot.test.mjs: boots the real Vite dev server and pins the movi
  loader contract that broke round10 (query-less DEV url, /vendor stays
  off the module pipeline)
- scripts/local/smoke-workbench.mjs: pre-release smoke covering the
  seven recurring acceptance points (login/pool/claim/play-while-loading/
  captions/4x/playhead), 3x green against the local replica
- docs/CODING_STANDARDS.md + AGENTS pointer: eligibility flags are
  server-shipped, the UI renders them (rounds 9+12 lesson)
- vendor README host-element quirks section, HANDOFF gate pointers and
  movi-path verification rule, perf-audit report pinned with the
  post-round12 production 500 observation
- husky pre-commit: lint-staged (repo prettier scope) + tsc + gofmt
- gofmt: restore lint:go green on the dev tip (drift shipped silently
  in the perf merge and round12 because nothing gated it)
2026-10-12 18:07:02 +05:30

259 lines
10 KiB
Go

package api
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"droboticflow/internal/domain"
)
func TestHumanItemsListPaginatesAndProjectsPayload(t *testing.T) {
fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageCutAdjustment, domain.HumanItemReady, "", 0)
admin := domain.UserRecord{ID: "admin-pagination", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
// Five extra items with list-needed metadata in sliceWorkflow plus a heavy
// annotationWorkspace draft that must never cross the list wire.
for index := 0; index < 5; index++ {
item := fixture.item
item.ID = domain.NewID("human-item")
item.CreatedAt = time.Now().UTC().Add(time.Duration(index) * time.Minute).Format(time.RFC3339Nano)
item.UpdatedAt = item.CreatedAt
item.AssignedUserID = ""
item.Status = domain.HumanItemReady
item.Subject.Payload = map[string]any{
"sliceWorkflow": map[string]any{"status": "editing", "phase": "annotate"},
"annotationWorkspace": map[string]any{"sliceSegments": []any{map[string]any{"id": "seg", "start": 0, "end": 10}}},
}
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
t.Fatal(err)
}
}
first := fixture.request(http.MethodGet, "/api/human-items?pluginId=annotation-qa&pool=all&limit=3", admin, nil)
if first.Code != http.StatusOK {
t.Fatalf("page 1 status=%d body=%s", first.Code, first.Body.String())
}
var page1 struct {
Items []domain.HumanItemRecord `json:"items"`
NextCursor string `json:"nextCursor"`
HasMore bool `json:"hasMore"`
}
if err := json.Unmarshal(first.Body.Bytes(), &page1); err != nil {
t.Fatal(err)
}
if len(page1.Items) != 3 || !page1.HasMore || page1.NextCursor == "" {
t.Fatalf("page 1 items=%d hasMore=%v cursor=%q", len(page1.Items), page1.HasMore, page1.NextCursor)
}
seen := map[string]bool{}
for _, item := range page1.Items {
seen[item.ID] = true
if _, ok := item.Subject.Payload["sliceWorkflow"]; !ok {
t.Fatalf("list projection must keep sliceWorkflow for %s", item.ID)
}
if _, ok := item.Subject.Payload["annotationWorkspace"]; ok {
t.Fatalf("list projection must drop annotationWorkspace for %s", item.ID)
}
}
second := fixture.request(http.MethodGet, "/api/human-items?pluginId=annotation-qa&pool=all&limit=3&cursor="+page1.NextCursor, admin, nil)
if second.Code != http.StatusOK {
t.Fatalf("page 2 status=%d body=%s", second.Code, second.Body.String())
}
var page2 struct {
Items []domain.HumanItemRecord `json:"items"`
HasMore bool `json:"hasMore"`
}
if err := json.Unmarshal(second.Body.Bytes(), &page2); err != nil {
t.Fatal(err)
}
if len(page2.Items) != 3 || page2.HasMore {
t.Fatalf("page 2 items=%d hasMore=%v", len(page2.Items), page2.HasMore)
}
for _, item := range page2.Items {
if seen[item.ID] {
t.Fatalf("cursor page repeated item %s", item.ID)
}
}
}
// TestHumanItemsListBackfillsAcrossFilteredRawPages reproduces the t17 review
// finding: the handler-level access filters can swallow an entire raw page
// (here via a stage that is absent from the batch definition), and the fetch
// loop must advance the raw cursor between rounds. Without that, every round
// re-reads the same page and the response silently starves or repeats.
func TestHumanItemsListBackfillsAcrossFilteredRawPages(t *testing.T) {
fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageCutAdjustment, domain.HumanItemReady, "", 0)
admin := domain.UserRecord{ID: "admin-backfill", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
// A full raw page (fetch floor is 100) of items whose stage is missing
// from the batch definition: the store returns them, the handler drops
// every one via humanStage lookup failure.
for index := 0; index < 100; index++ {
ghost := fixture.item
ghost.ID = domain.NewID("human-item")
ghost.CurrentStageID = "stage-that-does-not-exist"
ghost.CreatedAt = time.Now().UTC().Add(time.Duration(200-index) * time.Second).Format(time.RFC3339Nano)
ghost.UpdatedAt = ghost.CreatedAt
if err := fixture.store.UpsertHumanItem(fixture.ctx, ghost); err != nil {
t.Fatal(err)
}
}
// Older visible items: only reachable once the loop skips past the ghost page.
visible := make([]domain.HumanItemRecord, 0, 4)
for index := 0; index < 4; index++ {
item := fixture.item
item.ID = fmt.Sprintf("human-item-visible-%d", index)
item.CreatedAt = time.Now().UTC().Add(time.Duration(50-index) * time.Second).Format(time.RFC3339Nano)
item.UpdatedAt = item.CreatedAt
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
t.Fatal(err)
}
visible = append(visible, item)
}
recorder := fixture.request(http.MethodGet, "/api/human-items?pluginId=annotation-qa&pool=all&limit=3", admin, nil)
if recorder.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
var page struct {
Items []domain.HumanItemRecord `json:"items"`
NextCursor string `json:"nextCursor"`
HasMore bool `json:"hasMore"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &page); err != nil {
t.Fatal(err)
}
if len(page.Items) != 3 {
t.Fatalf("expected the loop to backfill to the page limit, got %d items", len(page.Items))
}
seen := map[string]bool{}
for _, item := range page.Items {
if seen[item.ID] {
t.Fatalf("duplicate item %s in a single page", item.ID)
}
seen[item.ID] = true
if item.CurrentStageID == "stage-that-does-not-exist" {
t.Fatalf("handler-filtered item leaked into the page: %s", item.ID)
}
}
if !page.HasMore || page.NextCursor == "" {
t.Fatalf("continuation metadata missing after backfill: hasMore=%v cursor=%q", page.HasMore, page.NextCursor)
}
// The continuation cursor must point strictly after the last returned
// item: the next page contains the remaining visible item and no overlap.
next := fixture.request(http.MethodGet, "/api/human-items?pluginId=annotation-qa&pool=all&limit=3&cursor="+page.NextCursor, admin, nil)
if next.Code != http.StatusOK {
t.Fatalf("follow-up status=%d body=%s", next.Code, next.Body.String())
}
var followUp struct {
Items []domain.HumanItemRecord `json:"items"`
HasMore bool `json:"hasMore"`
}
if err := json.Unmarshal(next.Body.Bytes(), &followUp); err != nil {
t.Fatal(err)
}
for _, item := range followUp.Items {
if seen[item.ID] {
t.Fatalf("continuation page repeated item %s", item.ID)
}
seen[item.ID] = true
}
if len(seen) != len(page.Items)+len(followUp.Items) || seen[visible[len(visible)-1].ID] != true {
t.Fatalf("pagination lost items: unique=%d", len(seen))
}
// Malformed cursors are rejected like the runs endpoint.
bad := fixture.request(http.MethodGet, "/api/human-items?pool=all&cursor=not-a-cursor", admin, nil)
if bad.Code != http.StatusBadRequest {
t.Fatalf("malformed cursor status=%d body=%s", bad.Code, bad.Body.String())
}
}
func TestHumanItemsAvailablePoolPushesFiltersIntoStore(t *testing.T) {
fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageCutAdjustment, domain.HumanItemReady, "", 0)
// An assigned item and a cancelled item must both be excluded by the store
// filter itself, not by post-fetch dropping.
assigned := fixture.item
assigned.ID = domain.NewID("human-item")
assigned.AssignedUserID = fixture.annotator.ID
assigned.Status = domain.HumanItemClaimed
if err := fixture.store.UpsertHumanItem(fixture.ctx, assigned); err != nil {
t.Fatal(err)
}
cancelled := fixture.item
cancelled.ID = domain.NewID("human-item")
cancelled.AssignedUserID = ""
cancelled.Status = domain.HumanItemCancelled
if err := fixture.store.UpsertHumanItem(fixture.ctx, cancelled); err != nil {
t.Fatal(err)
}
pushed, err := fixture.store.ListHumanItems(fixture.ctx, domain.DefaultOrganizationID, domain.HumanItemQuery{
PluginID: "annotation-qa",
UnassignedOnly: true,
Statuses: []domain.HumanItemStatus{domain.HumanItemReady, domain.HumanItemRework, domain.HumanItemSubmitted},
})
if err != nil {
t.Fatal(err)
}
if len(pushed) != 1 || pushed[0].ID != fixture.item.ID {
t.Fatalf("pushdown query returned %#v", pushed)
}
}
func TestRunsListDefaultsToPaginatedContract(t *testing.T) {
fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageCutAdjustment, domain.HumanItemReady, "", 0)
admin := domain.UserRecord{ID: "admin-runs-default", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
for index := 0; index < 60; index++ {
run := domain.RunRecord{
ID: domain.NewID("run"), OrganizationID: domain.DefaultOrganizationID,
ProjectID: "project-v5", WorkflowID: fixture.batch.WorkflowID,
Status: domain.RunStatusCompleted,
CreatedAt: time.Now().UTC().Add(-time.Duration(index) * time.Minute).Format(time.RFC3339Nano),
}
if err := fixture.store.UpsertRun(fixture.ctx, run); err != nil {
t.Fatal(err)
}
}
response := fixture.request(http.MethodGet, "/api/runs", admin, nil)
if response.Code != http.StatusOK {
t.Fatalf("runs status=%d body=%s", response.Code, response.Body.String())
}
var payload struct {
Items []domain.RunSummary `json:"items"`
NextCursor string `json:"nextCursor"`
HasMore bool `json:"hasMore"`
Total int `json:"total"`
StatusCounts map[string]int `json:"statusCounts"`
}
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
// The no-parameter path keeps the legacy {"items"}-only shape but must
// never stream the whole collection again.
if len(payload.Items) != 50 {
t.Fatalf("default runs page items=%d", len(payload.Items))
}
paged := fixture.request(http.MethodGet, "/api/runs?limit=10", admin, nil)
if paged.Code != http.StatusOK {
t.Fatalf("paged runs status=%d body=%s", paged.Code, paged.Body.String())
}
if err := json.Unmarshal(paged.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
if len(payload.Items) != 10 || !payload.HasMore || payload.NextCursor == "" {
t.Fatalf("paged runs items=%d hasMore=%v", len(payload.Items), payload.HasMore)
}
if payload.Total != 60 || payload.StatusCounts[string(domain.RunStatusCompleted)] != 60 {
t.Fatalf("aggregates missing: total=%d counts=%v", payload.Total, payload.StatusCounts)
}
}