forked from longtao.wu/droboticflow
95 lines
2.9 KiB
Go
95 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestHumanRejectCursorPage(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
for i := 0; i < 5; i++ {
|
|
if err := f.store.UpsertHumanReject(f.ctx, domain.HumanRejectRecord{ID: fmt.Sprintf("reject-%d", i), OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID, ItemID: f.item.ID, CreatedAt: "2026-10-28T00:00:00Z"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cursor := ""
|
|
seen := map[string]bool{}
|
|
for page := 0; page < 4; page++ {
|
|
r := f.request(http.MethodGet, "/api/human-rejects?limit=2&cursor="+url.QueryEscape(cursor), f.reviewer, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
var out struct {
|
|
Items []domain.HumanRejectRecord `json:"items"`
|
|
Next string `json:"nextCursor"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(out.Items) > 2 {
|
|
t.Fatal("unbounded page")
|
|
}
|
|
for _, v := range out.Items {
|
|
if seen[v.ID] {
|
|
t.Fatal("duplicate", v.ID)
|
|
}
|
|
seen[v.ID] = true
|
|
}
|
|
cursor = out.Next
|
|
if cursor == "" {
|
|
break
|
|
}
|
|
}
|
|
if len(seen) != 5 {
|
|
t.Fatalf("lost records: %v", seen)
|
|
}
|
|
for _, q := range []string{"limit=0", "limit=101", "limit=2&cursor=invalid"} {
|
|
r := f.request("GET", "/api/human-rejects?"+q, f.reviewer, nil)
|
|
if r.Code != 400 {
|
|
t.Fatalf("accepted %s: %d", q, r.Code)
|
|
}
|
|
}
|
|
r := f.request("GET", "/api/human-rejects", f.reviewer, nil)
|
|
var legacy struct {
|
|
Items []domain.HumanRejectRecord `json:"items"`
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &legacy)
|
|
if r.Code != 200 || len(legacy.Items) != 5 {
|
|
t.Fatalf("legacy compatibility: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHumanRejectFilteredPageStillAdvances(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
for _, r := range []domain.HumanRejectRecord{
|
|
{ID: "z-hidden", ItemID: "missing-item", OrganizationID: f.item.OrganizationID, CreatedAt: "2026-10-28T00:00:00Z"},
|
|
{ID: "a-visible", ItemID: f.item.ID, OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID, CreatedAt: "2026-10-28T00:00:00Z"},
|
|
} {
|
|
if e := f.store.UpsertHumanReject(f.ctx, r); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
first := f.request("GET", "/api/human-rejects?limit=1", f.reviewer, nil)
|
|
var page struct {
|
|
Items []domain.HumanRejectRecord `json:"items"`
|
|
Next string `json:"nextCursor"`
|
|
}
|
|
if e := json.Unmarshal(first.Body.Bytes(), &page); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if first.Code != 200 || len(page.Items) != 0 || page.Next == "" {
|
|
t.Fatalf("filtered first page %d %s", first.Code, first.Body.String())
|
|
}
|
|
next := f.request("GET", "/api/human-rejects?limit=1&cursor="+url.QueryEscape(page.Next), f.reviewer, nil)
|
|
if e := json.Unmarshal(next.Body.Bytes(), &page); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if next.Code != 200 || len(page.Items) != 1 || page.Items[0].ID != "a-visible" || page.Next != "" {
|
|
t.Fatalf("next page %d %s", next.Code, next.Body.String())
|
|
}
|
|
}
|