droboticflow/internal/api/human_pool_recovery_test.go

65 lines
2.5 KiB
Go

package api
import (
"context"
"net/http"
"strings"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
type poolRecoveryStore struct {
store.Store
fail string
}
func (s *poolRecoveryStore) ListHumanItems(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
if s.fail == "candidate" && q.PoolCandidateProjection || s.fail == "detail" && !q.PoolCandidateProjection && len(q.IDs) > 0 {
s.fail = ""
return nil, context.DeadlineExceeded
}
return s.Store.ListHumanItems(ctx, org, q)
}
func (s *poolRecoveryStore) ListHumanRunDisplays(ctx context.Context, ids []string) ([]domain.HumanRunDisplay, error) {
if s.fail == "display" {
s.fail = ""
return nil, context.DeadlineExceeded
}
return s.Store.ListHumanRunDisplays(ctx, ids)
}
func TestHumanPoolReadTimeoutRetryAndFreshHandler(t *testing.T) {
for _, step := range []string{"candidate", "detail", "display"} {
t.Run(step, func(t *testing.T) {
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageCutReview, domain.HumanItemReady, "", 0)
actor := domain.UserRecord{ID: "recovery-admin", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
path := "/api/human-items?pluginId=annotation-qa&pool=all"
baseline := f.request(http.MethodGet, path, actor, nil)
if baseline.Code != 200 || !strings.Contains(baseline.Body.String(), f.item.ID) {
t.Fatal("fixture did not return work")
}
wrapped := &poolRecoveryStore{Store: f.store, fail: step}
f.handler = NewHandler(wrapped, nil)
failed := f.request(http.MethodGet, path, actor, nil)
if failed.Code != 500 || strings.Contains(failed.Body.String(), f.item.ID) {
t.Fatalf("timeout leaked partial success: %d %s", failed.Code, failed.Body.String())
}
retried := f.request(http.MethodGet, path, actor, nil)
if retried.Code != 200 || retried.Body.String() != baseline.Body.String() {
t.Fatal("retry did not recover identical response")
}
// A new handler has no request-local display/permission caches from the failure.
f.handler = NewHandler(f.store, nil)
fresh := f.request(http.MethodGet, path, actor, nil)
if fresh.Code != 200 || fresh.Body.String() != baseline.Body.String() {
t.Fatal("fresh handler lost task state")
}
persisted, err := f.store.GetHumanItem(f.ctx, f.item.ID)
if err != nil || persisted.Version != f.item.Version || persisted.Status != f.item.Status || persisted.AssignedUserID != f.item.AssignedUserID {
t.Fatal("read recovery mutated task state")
}
})
}
}