droboticflow/internal/store/human_loop_cursor_mongo_test.go
d-robotics 58e0e50570
Some checks failed
web-ci / checks (push) Has been cancelled
web-ci / checks (pull_request) Has been cancelled
🧪 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

131 lines
4.7 KiB
Go

package store
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// TestHumanItemCursorFilterEncodesWithoutSelfReference guards the t8 blocker:
// the cursor branch used to embed the filter map inside itself ($and), which
// made BSON encoding recurse until the process died with a stack overflow.
// Marshalling the produced filter is the exact client-side encoding path a
// live Mongo command takes, so this fails (fatally) on the old code without
// needing a running database.
func TestHumanItemCursorFilterEncodesWithoutSelfReference(t *testing.T) {
query := domain.HumanItemQuery{
PluginID: "annotation-qa",
ProjectID: "project-a",
UnassignedOnly: true,
Statuses: []domain.HumanItemStatus{domain.HumanItemReady, domain.HumanItemSubmitted},
Cursor: &domain.HumanItemCursor{CreatedAt: "2026-10-09T00:00:00Z", ID: "human-item-1"},
}
filter := humanItemMongoFilter(domain.DefaultOrganizationID, query)
encoded, err := bson.Marshal(filter)
if err != nil {
t.Fatalf("cursor filter must encode: %v", err)
}
var round struct {
And []map[string]any `bson:"$and"`
}
if err := bson.Unmarshal(encoded, &round); err != nil {
t.Fatalf("cursor filter must decode: %v", err)
}
if len(round.And) != 2 {
t.Fatalf("cursor filter must AND base conditions with the cursor clause: %v", round.And)
}
base := round.And[0]
if base["organizationId"] != domain.DefaultOrganizationID || base["pluginId"] != "annotation-qa" {
t.Fatalf("base conditions must survive inside $and: %v", base)
}
if _, exists := base["$and"]; exists {
t.Fatalf("base filter must not reference itself: %v", base)
}
// Sanity: the non-cursor filter and the base inside $and agree on the
// pushed-down pool conditions.
if _, exists := base["assignedUserId"]; !exists {
t.Fatalf("pool pushdown lost inside $and: %v", base)
}
}
// TestHumanItemsListMongoCursorPagination runs the full MongoStore path
// (filter construction, real BSON commands, cursor decode) against an
// isolated throwaway database. Opt-in like the other *_mongo_test.go suites
// so CI without Mongo stays green; the always-run encoding guard above covers
// the crash class on every machine.
func TestHumanItemsListMongoCursorPagination(t *testing.T) {
if os.Getenv("MONGO_URI") == "" || os.Getenv("HUMAN_ITEM_CURSOR_INTEGRATION") != "1" {
t.Skip("opt-in Mongo cursor integration (set MONGO_URI and HUMAN_ITEM_CURSOR_INTEGRATION=1)")
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
if err != nil {
t.Fatalf("Mongo connection error: %T", err)
}
defer client.Disconnect(ctx)
dbName := fmt.Sprintf("human_item_cursor_test_%d", time.Now().UnixNano())
if !strings.HasPrefix(dbName, "human_item_cursor_test_") || dbName == os.Getenv("MONGO_DB") {
t.Fatal("invalid isolated test database")
}
db := client.Database(dbName)
defer func() {
if err := db.Drop(context.Background()); err != nil {
t.Error("isolated database cleanup failed")
}
}()
dataStore := NewMongoStore(db)
for index := 0; index < 5; index++ {
item := domain.HumanItemRecord{
ID: fmt.Sprintf("human-item-cursor-%d", index),
OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa",
BatchID: "batch-cursor", ProjectID: "project-cursor", WorkflowID: "workflow-cursor",
CurrentStageID: "split_annotation", Status: domain.HumanItemReady,
Subject: domain.HumanSubjectRef{ItemKey: "item-key"},
Version: 1,
CreatedAt: time.Now().UTC().Add(time.Duration(index) * time.Minute).Format(time.RFC3339Nano),
}
item.UpdatedAt = item.CreatedAt
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
t.Fatal(err)
}
}
first, err := dataStore.ListHumanItems(ctx, domain.DefaultOrganizationID, domain.HumanItemQuery{
PluginID: "annotation-qa", Limit: 3, ListProjection: true,
})
if err != nil {
t.Fatal(err)
}
if len(first) != 3 {
t.Fatalf("first page size=%d", len(first))
}
second, err := dataStore.ListHumanItems(ctx, domain.DefaultOrganizationID, domain.HumanItemQuery{
PluginID: "annotation-qa", Limit: 3, ListProjection: true,
Cursor: &domain.HumanItemCursor{CreatedAt: first[len(first)-1].CreatedAt, ID: first[len(first)-1].ID},
})
if err != nil {
t.Fatal(err)
}
if len(second) != 2 {
t.Fatalf("second page size=%d", len(second))
}
seen := map[string]bool{}
for _, item := range append(append([]domain.HumanItemRecord{}, first...), second...) {
if seen[item.ID] {
t.Fatalf("cursor page repeated item %s", item.ID)
}
seen[item.ID] = true
}
}