droboticflow/internal/api/human_pool_count_mongo_test.go

196 lines
7.2 KiB
Go

package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"sort"
"strings"
"sync"
"testing"
"time"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Model the previous sorted-page transport while running the same current
// HTTP query preparation and authoritative permission filter on both sides.
type pagedCountBaseline struct{ store.Store }
func (s pagedCountBaseline) VisitHumanPoolCountCandidates(ctx context.Context, org string, q domain.HumanItemQuery, visit func([]domain.HumanItemRecord) error) error {
q.Limit, q.PoolCandidateProjection = 1000, true
for {
rows, err := s.ListHumanItems(ctx, org, q)
if err != nil {
return err
}
if err := visit(rows); err != nil {
return err
}
if len(rows) < q.Limit {
return nil
}
last := rows[len(rows)-1]
q.Cursor = &domain.HumanItemCursor{CreatedAt: last.CreatedAt, ID: last.ID}
}
}
func TestHumanPoolCountMongoTransport(t *testing.T) {
if os.Getenv("POOL_COUNT_LOCAL_MONGO") != "1" {
t.Skip("isolated local Mongo opt-in")
}
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
var mu sync.Mutex
finds, bytes := 0, 0
monitor := &event.CommandMonitor{
Started: func(_ context.Context, e *event.CommandStartedEvent) {
if e.CommandName == "find" {
if name, _ := e.Command.Lookup("find").StringValueOK(); name == "humanItems" {
mu.Lock()
finds++
mu.Unlock()
}
}
},
Succeeded: func(_ context.Context, e *event.CommandSucceededEvent) {
mu.Lock()
bytes += len(e.Reply)
mu.Unlock()
},
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true").SetMonitor(monitor).SetServerSelectionTimeout(5*time.Second))
if err != nil {
t.Fatal(err)
}
db := client.Database(fmt.Sprintf("pool_count_transport_%d", time.Now().UnixNano()))
t.Cleanup(func() {
cleanup, stop := context.WithTimeout(context.Background(), 10*time.Second)
defer stop()
if err := db.Drop(cleanup); err != nil {
t.Error(err)
}
client.Disconnect(cleanup)
})
ds := store.NewMongoStore(db)
must := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
must(ds.EnsureIndexes(ctx))
f := sharedAcceptanceFixture(t)
for n, user := range []domain.UserRecord{f.admin, f.reviewer} {
user.Phone = fmt.Sprintf("138000000%02d", n)
must(ds.UpsertUser(ctx, user))
}
sp, err := f.store.GetSupplier(ctx, f.reviewer.SupplierID)
must(err)
must(ds.UpsertSupplier(ctx, *sp))
_, err = ds.SetSupplierAcceptanceAuthorization(ctx, f.item.OrganizationID, f.reviewer.SupplierID, true, 0, f.admin.ID)
must(err)
batches, err := f.store.ListHumanBatches(ctx, f.item.OrganizationID, domain.HumanBatchQuery{})
must(err)
for _, b := range batches {
b.RunID = b.ID
must(ds.UpsertHumanBatch(ctx, b))
}
const count = 12000
for start := 0; start < count; start += 500 {
rows := make([]any, 0, 500)
for n := start; n < start+500; n++ {
item := f.item
item.ID = fmt.Sprintf("count-transport-%05d", n)
item.Subject.ItemKey = item.ID
item.CreatedAt = fmt.Sprintf("2026-11-01T%06d", n)
item.ProcessingJob = &domain.AnnotationProcessingJob{Kind: "submit", Status: "failed", Error: strings.Repeat("synthetic historical processing failure ", 100)}
rows = append(rows, item)
}
_, err := db.Collection("humanItems").InsertMany(ctx, rows)
must(err)
}
handlers := []http.Handler{NewHandler(pagedCountBaseline{ds}, nil), NewHandler(ds, nil)}
for _, user := range []domain.UserRecord{f.admin, f.reviewer} {
var baselineBody string
var baselineFinds, baselineBytes int
for index, handler := range handlers {
if index == 1 {
_, err := db.Collection("humanItems").Indexes().CreateMany(ctx, store.PlatformReadIndexes()["humanItems"])
must(err)
}
mu.Lock()
finds, bytes = 0, 0
mu.Unlock()
r := httptest.NewRequest("GET", "/api/human-item-counts?pluginId=annotation-qa", nil)
r = r.WithContext(context.WithValue(ctx, authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: user.OrganizationID, Status: domain.OrganizationStatusActive}}))
w := httptest.NewRecorder()
start := time.Now()
handler.ServeHTTP(w, r)
elapsed := time.Since(start)
var result struct{ Counts map[string]int }
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result.Counts["available"] != count {
t.Fatalf("count failed: role=%s status=%d body=%s", user.Role, w.Code, w.Body.String())
}
mu.Lock()
calls, replyBytes := finds, bytes
mu.Unlock()
t.Logf("role=%s baseline=%t count=%d finds=%d replyBytes=%d duration=%s", user.Role, index == 0, count, calls, replyBytes, elapsed)
if index == 0 {
baselineBody, baselineFinds, baselineBytes = w.Body.String(), calls, replyBytes
} else {
if w.Body.String() != baselineBody || calls*2 >= baselineFinds || replyBytes*2 >= baselineBytes {
t.Fatalf("count/transport regression: finds %d -> %d bytes %d -> %d", baselineFinds, calls, baselineBytes, replyBytes)
}
if elapsed > 2*time.Second {
t.Fatalf("candidate count exceeded 2s: %s", elapsed)
}
}
}
}
// Exercise the real HTTP filtered-count path at the same scale, including
// broad matches and exhaustive no-hit queries; do not substitute a page size
// for the authorized total. These are short local measurements, not a
// production concurrency/capacity acceptance.
for _, user := range []domain.UserRecord{f.admin, f.reviewer} {
for _, query := range []string{"", "&search=count-transport-", "&search=no-such-capture"} {
var samples []time.Duration
for n := 0; n < 3; n++ {
r := httptest.NewRequest("GET", "/api/human-item-counts?pluginId=annotation-qa"+query, nil)
r = r.WithContext(context.WithValue(ctx, authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: user.OrganizationID, Status: domain.OrganizationStatusActive}}))
w := httptest.NewRecorder()
start := time.Now()
handlers[1].ServeHTTP(w, r)
samples = append(samples, time.Since(start))
var body struct{ Counts map[string]int }
want := count
if strings.Contains(query, "no-such") {
want = 0
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &body) != nil || body.Counts["available"] != want {
t.Fatalf("filtered count role=%s query=%q code=%d body=%s", user.Role, query, w.Code, w.Body.String())
}
}
sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] })
t.Logf("role=%s query=%q local worst-of-3=%s", user.Role, query, samples[2])
if samples[2] > 2*time.Second {
t.Fatalf("count query exceeded local 2s target: %s", samples[2])
}
}
}
// A canceled scan may not publish a partial total or continue callbacks.
canceled, stop := context.WithCancel(ctx)
calls := 0
err = ds.VisitHumanPoolCountCandidates(canceled, f.item.OrganizationID, domain.HumanItemQuery{PluginID: "annotation-qa"}, func(_ []domain.HumanItemRecord) error { calls++; stop(); return nil })
if err != context.Canceled || calls != 1 {
t.Fatalf("canceled scan: callbacks=%d err=%v", calls, err)
}
}