forked from longtao.wu/droboticflow
522 lines
26 KiB
Go
522 lines
26 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/workforcestats"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func seedWorkforceStats(t *testing.T, s Store) {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
org := "workforce-test"
|
|
at := "2026-09-28T12:00:00Z"
|
|
check := func(err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
check(s.UpsertSupplier(ctx, domain.SupplierRecord{ID: "v", OrganizationID: org, Name: "测试供应商", Status: "active"}))
|
|
check(s.UpsertUser(ctx, domain.UserRecord{ID: "u", OrganizationID: org, Name: "原标注员", SupplierID: "v", Role: domain.UserRoleAnnotator}))
|
|
check(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: "task", OrganizationID: org, ProjectID: "p", SupplierID: "changed-vendor", AssignedUserID: "changed-author"}))
|
|
check(s.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: "rev", OrganizationID: org, TaskID: "task", Revision: 4, AuthorUserID: "u", CountsAsOutput: true}))
|
|
check(s.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: "slices", OrganizationID: org, TaskID: "task", Revision: 4, Segments: []domain.SliceSegment{{ID: "slice", Start: 0, End: 79}}}))
|
|
check(s.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ID: "submitted", OrganizationID: org, ProjectID: "p", TaskID: "task", UserID: "u", SupplierID: "v", EventType: "combined_submitted", Revision: 4, OutputDelta: 1, CreatedAt: at}))
|
|
check(s.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ID: "foreign", OrganizationID: "foreign", ProjectID: "p", TaskID: "foreign-task", UserID: "u", SupplierID: "v", EventType: "combined_submitted", Revision: 4, OutputDelta: 1, CreatedAt: at}))
|
|
op := domain.ReviewOperation{ID: "review-operation-test", OrganizationID: org, ProjectID: "p", ItemID: "item", TaskID: "task", StageID: "quality_review", Status: "completed", Decision: "rejected", CreatedAt: at, CompletedAt: at, ReviewRound: 1,
|
|
Draft: domain.ReviewDraft{Problems: []domain.ReviewProblem{{ID: "p1", Scope: "slice", SliceID: "slice", Start: 0, End: 10, TimeBasis: "capture-seconds", Text: "保留意见一"}, {ID: "p2", Scope: "slice", SliceID: "slice", Start: 0, End: 10, TimeBasis: "capture-seconds", Text: "保留意见二"}}},
|
|
Effects: []domain.ReviewDecisionEffect{{Review: &domain.AnnotationReviewRecord{ID: "review-operation-test:review", TaskID: "task", Revision: 4}}, {BeforeTask: &domain.AnnotationTaskRecord{ID: "task", SupplierID: "v"}}, {BeforeItem: &domain.HumanItemRecord{ID: "item"}, Item: &domain.HumanItemRecord{ID: "item", LegacyTaskID: "task", SupplierID: "v", LatestAuthorUserID: "u"}}},
|
|
}
|
|
ok, err := s.CompareAndSwapReviewOperation(ctx, op, 0)
|
|
check(err)
|
|
if !ok {
|
|
t.Fatal("operation fixture not stored")
|
|
}
|
|
op.ID = "review-operation-failed"
|
|
op.Status = "failed_before_effects"
|
|
ok, err = s.CompareAndSwapReviewOperation(ctx, op, 0)
|
|
check(err)
|
|
if !ok {
|
|
t.Fatal("failed fixture not stored")
|
|
}
|
|
}
|
|
|
|
func checkWorkforceStats(t *testing.T, s Store) workforcestats.Report {
|
|
t.Helper()
|
|
q := workforcestats.Query{SupplierID: "v", UserID: "u", Details: true, ProjectID: "p"}
|
|
q.From, _ = time.Parse(time.RFC3339, "2026-09-28T00:00:00Z")
|
|
q.To = q.From.Add(24 * time.Hour)
|
|
before, err := s.GetReviewOperation(t.Context(), "review-operation-test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d, err := s.LoadWorkforceStats(t.Context(), "workforce-test", q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := workforcestats.Build(d, q)
|
|
// Role-filtered reads must preserve the same records and aggregate values.
|
|
if len(d.Users) != 1 || d.Users[0].Role != domain.UserRoleAnnotator {
|
|
t.Fatalf("statistics user projection lost role: %+v", d.Users)
|
|
}
|
|
for _, group := range []string{"member", "day"} {
|
|
for _, role := range []string{"", string(domain.UserRoleAnnotator), string(domain.UserRoleQualityReviewer)} {
|
|
filteredQuery := q
|
|
filteredQuery.GroupBy, filteredQuery.Role = group, role
|
|
filteredData, err := s.LoadWorkforceStats(t.Context(), "workforce-test", filteredQuery)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
filtered := workforcestats.Build(filteredData, filteredQuery)
|
|
if role == string(domain.UserRoleQualityReviewer) {
|
|
if len(filtered.Items) != 0 || filtered.Totals.FirstSubmittedTasks != 0 {
|
|
t.Fatalf("role filter leaked annotator output: %+v", filtered)
|
|
}
|
|
} else {
|
|
if len(filtered.Items) != 1 || filtered.Items[0].Role != string(domain.UserRoleAnnotator) || !reflect.DeepEqual(filtered.Totals, r.Totals) {
|
|
t.Fatalf("role filter dropped output (%s/%s): %+v", group, role, filtered)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if r.Totals.FirstSubmittedTasks != 1 || r.Totals.ReworkSubmissionCount != 0 || r.Totals.FirstVideoSeconds != 79 || r.Totals.QualityReturns.DecisionCount != 1 || r.Totals.QualityReturns.ProblemCount != 1 || r.Totals.QualityReturns.VideoSeconds != 10 {
|
|
t.Fatalf("incorrect report %+v", r.Totals)
|
|
}
|
|
if len(r.Details) != 1 || len(r.Details[0].Problems) != 2 || r.Details[0].Problems[1].Text != "保留意见二" {
|
|
t.Fatalf("historic detail lost %+v", r.Details)
|
|
}
|
|
if len(r.Items) != 1 || r.Items[0].SupplierName != "测试供应商" || r.Items[0].UserName != "原标注员" {
|
|
t.Fatalf("identity resolution %+v", r.Items)
|
|
}
|
|
after, err := s.GetReviewOperation(t.Context(), "review-operation-test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(before, after) {
|
|
t.Fatal("report changed formal history")
|
|
}
|
|
// A retry must preserve the date and owner of a durable successful submission.
|
|
event := domain.AnnotationWorklogEventRecord{ID: "durable-submit", SubmissionID: "submit-id", OrganizationID: "workforce-test", TaskID: "durable-task", EventType: "combined_submitted", SubmissionKind: "first", UserID: "original", CreatedAt: "2026-09-27T00:00:00Z"}
|
|
if err := s.UpsertAnnotationWorklogEvent(t.Context(), event); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
event.CreatedAt = "2026-09-28T00:00:00Z"
|
|
event.UserID = "retry-user"
|
|
if err := s.UpsertAnnotationWorklogEvent(t.Context(), event); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
events, err := s.ListAnnotationWorklogEvents(t.Context(), "workforce-test", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, e := range events {
|
|
if e.ID == event.ID && (e.CreatedAt != "2026-09-27T00:00:00Z" || e.UserID != "original") {
|
|
t.Fatalf("retry changed immutable submission: %+v", e)
|
|
}
|
|
}
|
|
return r
|
|
}
|
|
|
|
func TestWorkforceStatsMemory(t *testing.T) {
|
|
s := NewMemoryStore()
|
|
seedWorkforceStats(t, s)
|
|
checkWorkforceStats(t, s)
|
|
}
|
|
|
|
// Opt-in test on the verified local replica only. A fresh synthetic database is
|
|
// dropped after the test; snapshot/customer databases are never modified.
|
|
func TestWorkforceStatsMongoReplicaParity(t *testing.T) {
|
|
uri := os.Getenv("TEST_WORKFORCE_MONGO_URI")
|
|
if uri == "" {
|
|
t.Skip("set TEST_WORKFORCE_MONGO_URI for local replica verification")
|
|
}
|
|
if uri != "mongodb://127.0.0.1:27018" {
|
|
t.Fatal("only the verified local replica URI is allowed")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("workforce_stats_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
s := NewMongoStore(db)
|
|
seedWorkforceStats(t, s)
|
|
actual := checkWorkforceStats(t, s)
|
|
memory := NewMemoryStore()
|
|
seedWorkforceStats(t, memory)
|
|
expected := checkWorkforceStats(t, memory)
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Fatalf("Mongo/Memory mismatch\n%+v\n%+v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestDashboardHumanWorkStatsParityBetweenMemoryAndMongoAggregation(t *testing.T) {
|
|
uri := os.Getenv("DASHBOARD_STATS_MONGO_TEST_URI")
|
|
if uri == "" {
|
|
t.Skip("isolated Mongo required: DASHBOARD_STATS_MONGO_TEST_URI=mongodb://127.0.0.1:27028")
|
|
}
|
|
if uri != "mongodb://127.0.0.1:27028" {
|
|
t.Fatal("only isolated loopback test server allowed")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("dashboard_stats_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
|
|
memory := NewMemoryStore()
|
|
mongoStore := NewMongoStore(db)
|
|
|
|
seed := func(s Store) {
|
|
helper := &testing.T{}
|
|
_ = helper
|
|
items := []domain.HumanItemRecord{
|
|
{ID: "i1", OrganizationID: "o", CurrentStageID: "annotation", Status: domain.HumanItemReady,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "v-a", Payload: map[string]any{"durationSeconds": 30.5}}},
|
|
{ID: "i2", OrganizationID: "o", CurrentStageID: "quality_review", Status: domain.HumanItemRework,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "v-a", Payload: map[string]any{"durationSeconds": 12.5}}},
|
|
{ID: "i3", OrganizationID: "o", CurrentStageID: "annotation", Status: domain.HumanItemInProgress,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "v-b"}},
|
|
{ID: "i4", OrganizationID: "o", CurrentStageID: domain.FanyuV5StageCutReview, Status: domain.HumanItemSubmitted,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "v-b", Payload: map[string]any{"durationSeconds": 7.5}}},
|
|
{ID: "i5", OrganizationID: "o", CurrentStageID: "acceptance", Status: domain.HumanItemSubmitted,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "v-c", Payload: map[string]any{"durationSeconds": -3.0}}},
|
|
{ID: "i6", OrganizationID: "o", CurrentStageID: "done", Status: domain.HumanItemAccepted,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "v-c", Payload: map[string]any{"durationSeconds": 0}}},
|
|
{ID: "i7", OrganizationID: "o", CurrentStageID: "done", Status: domain.HumanItemRejected,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "", Payload: map[string]any{"durationSeconds": "broken"}}},
|
|
{ID: "i8", OrganizationID: "o", CurrentStageID: "annotation", Status: domain.HumanItemEscalated,
|
|
Subject: domain.HumanSubjectRef{ItemKey: ""}},
|
|
{ID: "i9", OrganizationID: "foreign", CurrentStageID: "annotation", Status: domain.HumanItemReady,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "v-x", Payload: map[string]any{"durationSeconds": 999.0}}},
|
|
}
|
|
for _, item := range items {
|
|
if err := s.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
packages := []domain.DeliveryPackageRecord{
|
|
{ID: "p1", OrganizationID: "o", ProjectID: "proj", Status: domain.DeliveryPackagePendingAcceptance},
|
|
{ID: "p2", OrganizationID: "o", ProjectID: "proj", Status: domain.DeliveryPackageAccepted, Mode: domain.RunModeProduction},
|
|
{ID: "p3", OrganizationID: "o", ProjectID: "proj", Status: domain.DeliveryPackagePendingAcceptance, Mode: domain.RunModeDebug},
|
|
{ID: "p4", OrganizationID: "foreign", ProjectID: "proj", Status: domain.DeliveryPackagePendingAcceptance},
|
|
}
|
|
for _, pkg := range packages {
|
|
if err := s.UpsertDeliveryPackage(ctx, pkg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
seed(memory)
|
|
seed(mongoStore)
|
|
|
|
fromMemory, err := memory.DashboardHumanWorkStats(ctx, "o")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fromMongo, err := mongoStore.DashboardHumanWorkStats(ctx, "o")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fromMemory != fromMongo {
|
|
t.Fatalf("memory and mongo counters diverge:\nmemory=%+v\nmongo =%+v", fromMemory, fromMongo)
|
|
}
|
|
if fromMemory.PendingAnnotation != 1 || fromMemory.PendingQuality != 2 || fromMemory.InAnnotation != 1 ||
|
|
fromMemory.PendingAcceptanceItems != 1 || fromMemory.Accepted != 1 || fromMemory.Rejected != 1 ||
|
|
fromMemory.Escalated != 1 || fromMemory.VideoCount != 3 || fromMemory.CaptureDurationSeconds != 49 ||
|
|
fromMemory.PackageCount != 2 || fromMemory.PackagesPendingAcceptance != 1 {
|
|
t.Fatalf("unexpected counters: %+v", fromMemory)
|
|
}
|
|
|
|
// Legacy full-materialization oracle: ListHumanItems + ListDeliveryPackages.
|
|
items, err := memory.ListHumanItems(ctx, "o", domain.HumanItemQuery{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
packages, err := memory.ListDeliveryPackages(ctx, "o", domain.DeliveryPackageQuery{Mode: domain.RunModeProduction})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(items) != 8 || len(packages) != 2 {
|
|
t.Fatalf("oracle fixture sizes changed: items=%d packages=%d", len(items), len(packages))
|
|
}
|
|
}
|
|
|
|
func TestLoadWorkforceStatsCachedServesSnapshotWithinTTL(t *testing.T) {
|
|
uri := os.Getenv("DASHBOARD_STATS_MONGO_TEST_URI")
|
|
if uri == "" {
|
|
t.Skip("isolated Mongo required: DASHBOARD_STATS_MONGO_TEST_URI=mongodb://127.0.0.1:27028")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("workforce_cache_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
s := NewMongoStore(db)
|
|
|
|
from := time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC)
|
|
to := from.Add(24 * time.Hour)
|
|
q := workforcestats.Query{From: from, To: to}
|
|
first, err := s.LoadWorkforceStatsCached(ctx, "cache-org", q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ID: "new-event", OrganizationID: "cache-org", ProjectID: "p", TaskID: "t", EventType: "annotation_submitted", CreatedAt: "2026-09-01T12:00:00Z"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cached, err := s.LoadWorkforceStatsCached(ctx, "cache-org", q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cached.Events) != len(first.Events) {
|
|
t.Fatalf("expected the cached snapshot within the TTL, got %d events vs %d", len(cached.Events), len(first.Events))
|
|
}
|
|
fresh, err := s.LoadWorkforceStats(ctx, "cache-org", q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(fresh.Events) != len(first.Events)+1 {
|
|
t.Fatalf("direct load must bypass the cache, got %d events", len(fresh.Events))
|
|
}
|
|
}
|
|
|
|
// A reviewer's own report must not join every task other reviewers approved in
|
|
// the window (nor price slice ranges it never uses), and trimming those reads
|
|
// must leave every scope's report identical to the unfiltered memory oracle.
|
|
func TestWorkforceStatsMongoReviewerScopeSkipsUnrelatedTasks(t *testing.T) {
|
|
uri := os.Getenv("TEST_WORKFORCE_MONGO_URI")
|
|
if uri == "" {
|
|
t.Skip("set TEST_WORKFORCE_MONGO_URI for local replica verification")
|
|
}
|
|
if uri != "mongodb://127.0.0.1:27018" {
|
|
t.Fatal("only the verified local replica URI is allowed")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("workforce_stats_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
|
|
org := "workforce-reviewer"
|
|
frozenSeconds := 50.0
|
|
at := "2026-09-28T12:00:00Z"
|
|
seed := func(s Store) {
|
|
check := func(err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
check(s.UpsertSupplier(ctx, domain.SupplierRecord{ID: "v", OrganizationID: org, Name: "测试供应商", Status: "active"}))
|
|
check(s.UpsertUser(ctx, domain.UserRecord{ID: "author", OrganizationID: org, Name: "标注员", SupplierID: "v", Role: domain.UserRoleAnnotator}))
|
|
check(s.UpsertUser(ctx, domain.UserRecord{ID: "qc", OrganizationID: org, Name: "质检员", SupplierID: "v", Role: domain.UserRoleQualityReviewer}))
|
|
for _, task := range []string{"approved-task", "legacy-task"} {
|
|
check(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: task, OrganizationID: org, ProjectID: "p", SupplierID: "v"}))
|
|
check(s.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: task + "-rev", OrganizationID: org, TaskID: task, Revision: 1, AuthorUserID: "author", CountsAsOutput: true}))
|
|
check(s.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: task + "-slices", OrganizationID: org, TaskID: task, Revision: 1, Segments: []domain.SliceSegment{{ID: "s", Start: 0, End: 42}}}))
|
|
check(s.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ID: task + "-submit", OrganizationID: org, ProjectID: "p", TaskID: task, UserID: "author", SupplierID: "v", EventType: "combined_submitted", Revision: 1, OutputDelta: 1, CreatedAt: at}))
|
|
}
|
|
// qc approves one task and leaves a legacy (operation-less) rejection on another.
|
|
check(s.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ID: "qc-approved", OrganizationID: org, ProjectID: "p", TaskID: "approved-task", UserID: "qc", SupplierID: "v", EventType: "quality_approved", Revision: 1, CreatedAt: at}))
|
|
check(s.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: "qc-approve", OrganizationID: org, TaskID: "approved-task", Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, ReviewerUserID: "qc", CreatedAt: at}))
|
|
check(s.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: "qc-legacy-reject", OrganizationID: org, TaskID: "legacy-task", Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewRejected, ReviewerUserID: "qc", ProblemSliceIDs: []string{"s"}, CreatedAt: at}))
|
|
// Final acceptance rejects the source qc approved: attributed to qc.
|
|
op := domain.ReviewOperation{ID: "acceptance-op", OrganizationID: org, ProjectID: "p", ItemID: "batch", TaskID: "batch-task", StageID: "acceptance", Status: "completed", Decision: "rejected", CreatedAt: at, CompletedAt: at, ReviewRound: 1,
|
|
Draft: domain.ReviewDraft{Problems: []domain.ReviewProblem{{ID: "a1", Scope: "slice", SourceItemID: "source", SliceID: "s", Start: 0, End: 5, TimeBasis: "capture-seconds"}}},
|
|
Effects: []domain.ReviewDecisionEffect{{BeforeItem: &domain.HumanItemRecord{ID: "source", LegacyTaskID: "approved-task", SupplierID: "v", CurrentRevision: 1}}},
|
|
}
|
|
ok, err := s.CompareAndSwapReviewOperation(ctx, op, 0)
|
|
check(err)
|
|
if !ok {
|
|
t.Fatal("operation fixture not stored")
|
|
}
|
|
// Final acceptance approves the same source: priced at its QC-approved version.
|
|
check(s.UpsertUser(ctx, domain.UserRecord{ID: "acc", OrganizationID: org, Name: "终审员", SupplierID: "v", Role: domain.UserRoleAcceptanceReviewer}))
|
|
check(s.UpsertHumanItem(ctx, domain.HumanItemRecord{ID: "source", OrganizationID: org, ProjectID: "p", LegacyTaskID: "approved-task", CurrentRevision: 1}))
|
|
approval := domain.ReviewOperation{ID: "acceptance-approve", OrganizationID: org, ProjectID: "p", UserID: "acc", ItemID: "batch-2", StageID: "prepackage_acceptance", Status: "completed", Decision: "approved", CreatedAt: "2026-09-28T13:00:00Z", CompletedAt: "2026-09-28T13:00:00Z", SourceItemIDs: []string{"source"},
|
|
// Frozen at decision time (the source version may be pruned later).
|
|
ReviewVideoSources: []domain.ReviewVideoSource{{SourceItemID: "source", TaskID: "approved-task", Revision: 1, VideoSeconds: &frozenSeconds}}}
|
|
ok, err = s.CompareAndSwapReviewOperation(ctx, approval, 0)
|
|
check(err)
|
|
if !ok {
|
|
t.Fatal("approval fixture not stored")
|
|
}
|
|
// Unrelated approvals by other reviewers, each with slice history.
|
|
for i := 0; i < 50; i++ {
|
|
task := fmt.Sprintf("noise-%02d", i)
|
|
check(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: task, OrganizationID: org, ProjectID: "p", SupplierID: "v"}))
|
|
check(s.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: task + "-slices", OrganizationID: org, TaskID: task, Revision: 1, Segments: []domain.SliceSegment{{ID: "s", Start: 0, End: 9}}}))
|
|
check(s.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: task + "-approve", OrganizationID: org, TaskID: task, Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, ReviewerUserID: "other", CreatedAt: at}))
|
|
}
|
|
}
|
|
mongoStore := NewMongoStore(db)
|
|
memory := NewMemoryStore()
|
|
seed(mongoStore)
|
|
seed(memory)
|
|
|
|
from, _ := time.Parse(time.RFC3339, "2026-09-28T00:00:00Z")
|
|
scopes := map[string]workforcestats.Query{
|
|
"reviewer-self": {UserID: "qc", MatchUser: true},
|
|
"acceptance-self": {UserID: "acc", MatchUser: true},
|
|
"annotator-self": {UserID: "author", MatchUser: true},
|
|
"organization": {},
|
|
}
|
|
for name, q := range scopes {
|
|
q.From, q.To = from, from.Add(24*time.Hour)
|
|
for _, details := range []bool{false, true} {
|
|
q.Details = details
|
|
fromMongo, err := mongoStore.LoadWorkforceStats(ctx, org, q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fromMemory, err := memory.LoadWorkforceStats(ctx, org, q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, want := workforcestats.Build(fromMongo, q), workforcestats.Build(fromMemory, q)
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("%s details=%v: Mongo/Memory mismatch\nmongo =%+v\nmemory=%+v", name, details, got, want)
|
|
}
|
|
if name == "reviewer-self" {
|
|
for _, slice := range fromMongo.Slices {
|
|
if slice.TaskID != "approved-task" {
|
|
t.Fatalf("reviewer scope read slices of %s", slice.TaskID)
|
|
}
|
|
}
|
|
if got.Totals.ReviewVideoSeconds != 42 {
|
|
t.Fatalf("reviewer review seconds: %+v", got.Totals)
|
|
}
|
|
for _, task := range fromMongo.Tasks {
|
|
if len(task.ID) > 6 && task.ID[:6] == "noise-" {
|
|
t.Fatalf("reviewer scope joined unrelated task %s", task.ID)
|
|
}
|
|
}
|
|
if got.Totals.QualityApproved != 1 || got.Totals.AcceptanceReturns.DecisionCount != 1 {
|
|
t.Fatalf("reviewer report lost its own work: %+v", got.Totals)
|
|
}
|
|
}
|
|
if name == "organization" {
|
|
roles := map[string]string{}
|
|
for _, item := range got.Items {
|
|
roles[item.UserID] = item.Role
|
|
}
|
|
if roles["qc"] != string(domain.UserRoleQualityReviewer) || roles["acc"] != string(domain.UserRoleAcceptanceReviewer) || roles["author"] != string(domain.UserRoleAnnotator) {
|
|
t.Fatalf("member roles not resolved: %+v", roles)
|
|
}
|
|
// Role narrowing keeps only that role's members.
|
|
narrowed := q
|
|
narrowed.Role = string(domain.UserRoleQualityReviewer)
|
|
members := workforcestats.Build(fromMongo, narrowed).Items
|
|
if len(members) != 1 || members[0].UserID != "qc" {
|
|
t.Fatalf("role filter: %+v", members)
|
|
}
|
|
}
|
|
if name == "acceptance-self" && (got.Totals.ReviewVideoSeconds != 50 || got.Totals.ReviewDecisionCount != 1) {
|
|
t.Fatalf("acceptance review seconds: %+v", got.Totals)
|
|
}
|
|
if name == "annotator-self" && (got.Totals.FirstVideoSeconds != 84 || got.Totals.QualityReturns.DecisionCount != 1) {
|
|
t.Fatalf("annotator report changed: %+v", got.Totals)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWorkforceCompleteHistoryReuseScope(t *testing.T) {
|
|
ctx := context.WithValue(t.Context(), workforceBuildBudgetKey{}, &workforceBuildBudget{})
|
|
all := workforcestats.Query{Mode: domain.RunModeAll}
|
|
if !workforceCanReuseCompleteHistory(ctx, all) {
|
|
t.Fatal("private complete build must reuse history")
|
|
}
|
|
if workforceCanReuseCompleteHistory(t.Context(), all) {
|
|
t.Fatal("ordinary reads must retain their history join")
|
|
}
|
|
version := 1
|
|
cases := map[string]func(*workforcestats.Query){
|
|
"from": func(q *workforcestats.Query) { q.From = time.Now() },
|
|
"to": func(q *workforcestats.Query) { q.To = time.Now() },
|
|
"relative-from": func(q *workforcestats.Query) { q.RelativeFrom = true },
|
|
"relative-to": func(q *workforcestats.Query) { q.RelativeTo = true },
|
|
"project": func(q *workforcestats.Query) { q.ProjectID = "p" },
|
|
"supplier": func(q *workforcestats.Query) { q.SupplierID = "s" },
|
|
"user": func(q *workforcestats.Query) { q.UserID = "u" },
|
|
"match-user": func(q *workforcestats.Query) { q.MatchUser = true },
|
|
"project-permissions": func(q *workforcestats.Query) { q.AllowedProjects = map[string]bool{} },
|
|
"supplier-permissions": func(q *workforcestats.Query) { q.AllowedSuppliers = map[string]bool{} },
|
|
"template": func(q *workforcestats.Query) { q.TemplateVersion = &version },
|
|
"role": func(q *workforcestats.Query) { q.Role = "annotator" },
|
|
"group": func(q *workforcestats.Query) { q.GroupBy = "user" },
|
|
"details": func(q *workforcestats.Query) { q.Details = true },
|
|
"group-template": func(q *workforcestats.Query) { q.GroupTemplate = true },
|
|
"mode": func(q *workforcestats.Query) { q.Mode = domain.RunModeProduction },
|
|
}
|
|
for name, mutate := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
q := all
|
|
mutate(&q)
|
|
if workforceCanReuseCompleteHistory(ctx, q) {
|
|
t.Fatal("partial or permission-scoped reads cannot reuse complete history")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWorkforceCompleteHistoryMatchesLegacyProjection(t *testing.T) {
|
|
seconds := 7.5
|
|
base := domain.AnnotationWorklogEventRecord{ID: "event", TaskID: "task", OrganizationID: "org", ProjectID: "p", UserID: "deleted-user", SupplierID: "s", Revision: 2, TemplateVersion: 1, Mode: domain.RunModeProduction, OutputDelta: 1, CreatedAt: "2026-11-01T23:59:59Z", DurationSeconds: 7, SubmissionID: "submission", SubmissionKind: "rework", VideoSeconds: &seconds, Temporary: true, LegacyAssignment: map[string]string{"private": "value"}, WorkgroupID: "private", VendorID: "private"}
|
|
for _, kind := range []string{"combined_submitted", "annotation_submitted", "rework_submitted"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
e := base
|
|
e.EventType = kind
|
|
want := e
|
|
want.OrganizationID = ""
|
|
want.Temporary = false
|
|
want.LegacyAssignment = nil
|
|
want.WorkgroupID = ""
|
|
want.VendorID = ""
|
|
got := workforceCompleteHistory([]domain.AnnotationWorklogEventRecord{e})
|
|
if !reflect.DeepEqual(got, []domain.AnnotationWorklogEventRecord{want}) {
|
|
t.Fatalf("history projection mismatch: %#v", got)
|
|
}
|
|
if !e.Temporary || e.OrganizationID != "org" {
|
|
t.Fatal("input mutated")
|
|
}
|
|
})
|
|
}
|
|
emptyTask := base
|
|
emptyTask.TaskID = ""
|
|
emptyTask.EventType = "annotation_submitted"
|
|
wrongType := base
|
|
wrongType.EventType = "annotation_saved"
|
|
if len(workforceCompleteHistory([]domain.AnnotationWorklogEventRecord{emptyTask, wrongType})) != 0 {
|
|
t.Fatal("non-history events included")
|
|
}
|
|
legacy := domain.AnnotationWorklogEventRecord{ID: "legacy", TaskID: "task", EventType: "combined_submitted"}
|
|
if !reflect.DeepEqual(workforceCompleteHistory([]domain.AnnotationWorklogEventRecord{legacy, legacy}), []domain.AnnotationWorklogEventRecord{legacy, legacy}) {
|
|
t.Fatal("legacy records or duplicate semantics changed")
|
|
}
|
|
}
|