2026-10-20 21:17:58 +08:00

225 lines
10 KiB
Go

package workforcestats
import (
"droboticflow/internal/domain"
"reflect"
"testing"
"time"
)
func outputFixture() OutputSnapshot {
return OutputSnapshot{
Projects: []domain.ProjectRecord{{ID: "p", Name: "项目"}}, Users: []domain.UserRecord{{ID: "a", Name: "作者", Role: domain.UserRoleQualityReviewer}, {ID: "b", Name: "另一作者"}}, Suppliers: []domain.SupplierRecord{{ID: "v", Name: "供应商"}, {ID: "other", Name: "另一供应商"}},
Captures: []domain.CaptureRecord{{ID: "c", ProjectID: "p", ConnectionID: "bos", Bucket: "b", Prefix: "source/run/part0001", CaptureID: "run/part0001", RunID: "r"}},
Runs: []domain.RunRecord{{ID: "r", ProjectID: "p", TaskID: "c", CreatedAt: "2026-09-25T00:00:00Z", Mode: domain.RunModeProduction}},
Tasks: []domain.AnnotationTaskRecord{{ID: "t", ProjectID: "p", RunID: "r", SourceIsFullVideo: true, SourceDurationSeconds: 900, CurrentRevision: 4, AssignedUserID: "changed", SupplierID: "other", CreatedAt: "2026-09-26T00:00:00Z"}},
Items: []domain.HumanItemRecord{{ID: "i", LegacyTaskID: "t", ProjectID: "p"}},
Revisions: []domain.AnnotationRevisionRecord{{TaskID: "t", Revision: 4, AuthorUserID: "a", CountsAsOutput: true}},
Events: []domain.AnnotationWorklogEventRecord{{ID: "s", TaskID: "t", UserID: "a", SupplierID: "v", EventType: "combined_submitted", Revision: 4, CreatedAt: "2026-09-28T00:00:00Z"}},
Reviews: []domain.AnnotationReviewRecord{{ID: "q", TaskID: "t", Revision: 4, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-10-01T00:00:00Z"}},
Operations: []domain.ReviewOperation{{ID: "ac", StageID: "prepackage_acceptance", Decision: "approved", Status: "completed", SourceItemIDs: []string{"i"}, CompletedAt: "2026-10-03T00:00:00Z"}},
}
}
func outputQuery() OutputQuery {
from, _ := time.Parse(time.RFC3339, "2026-10-01T00:00:00Z")
to := from.AddDate(0, 0, 20)
return OutputQuery{From: from, To: to, Now: to, Location: time.FixedZone("Asia/Shanghai", 8*3600)}
}
func assertOutputSeconds(t *testing.T, m OutputMetric, count int, seconds float64) {
t.Helper()
if m.Count != count || m.Seconds == nil || *m.Seconds != seconds {
t.Fatalf("metric %+v, want %d / %v", m, count, seconds)
}
}
func TestAnnotationOutputDatesAttributionAndCumulative(t *testing.T) {
d := outputFixture()
before := outputFixture()
r := BuildOutput(d, outputQuery())
assertOutputSeconds(t, r.Totals.Submitted, 0, 0)
assertOutputSeconds(t, r.Totals.Quality, 1, 900)
assertOutputSeconds(t, r.Totals.Acceptance, 1, 900)
if len(r.Tasks) != 1 || r.Tasks[0].UserID != "a" || r.Tasks[0].SupplierID != "v" || r.Tasks[0].Status != "accepted" {
t.Fatalf("historical owner replaced: %+v", r.Tasks)
}
if len(r.Days) != 1 || r.Days[0].Date != "2026-10-03" {
t.Fatalf("day %+v", r.Days)
}
assertOutputSeconds(t, r.Days[0].CumulativeSubmitted, 1, 900)
if !reflect.DeepEqual(d, before) {
t.Fatal("report mutated its source")
}
q := outputQuery()
q.MatchUser = true
q.ScopeUserID = "b"
if len(BuildOutput(d, q).Tasks) != 0 {
t.Fatal("cross-author report")
}
q.ScopeUserID = "a"
if BuildOutput(d, q).Totals.Acceptance.Count != 1 {
t.Fatal("account role change erased production")
}
q.MatchSupplier = true
q.ScopeSupplierID = "other"
if len(BuildOutput(d, q).Tasks) != 0 {
t.Fatal("current supplier exposed historical output")
}
}
func TestAnnotationOutputRetriesReworkAndInvalidation(t *testing.T) {
d := outputFixture()
q := outputQuery()
q.From = q.From.AddDate(0, 0, -10)
retry := d.Events[0]
retry.ID = "retry"
retry.SubmissionID = "different"
retry.CreatedAt = "2026-10-02T00:00:00Z"
retry.SupplierID = "other"
d.Events = append(d.Events, retry)
r := BuildOutput(d, q)
assertOutputSeconds(t, r.Totals.Submitted, 1, 900)
if r.Tasks[0].SupplierID != "v" {
t.Fatal("retry changed original supplier")
}
d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "reject", TaskID: "t", Revision: 4, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewRejected, CreatedAt: "2026-10-05T00:00:00Z"})
r = BuildOutput(d, q)
assertOutputSeconds(t, r.Totals.Rework, 1, 900)
assertOutputSeconds(t, r.Totals.Acceptance, 0, 0)
d.Revisions = append(d.Revisions, domain.AnnotationRevisionRecord{TaskID: "t", Revision: 5, AuthorUserID: "a", IsRework: true})
d.Events = append(d.Events, domain.AnnotationWorklogEventRecord{ID: "resubmit", TaskID: "t", UserID: "a", SupplierID: "v", EventType: "rework_submitted", Revision: 5, CreatedAt: "2026-10-06T00:00:00Z"})
r = BuildOutput(d, q)
assertOutputSeconds(t, r.Totals.Submitted, 1, 900)
assertOutputSeconds(t, r.Totals.Rework, 0, 0)
assertOutputSeconds(t, r.Totals.Quality, 0, 0)
d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "new-quality", TaskID: "t", Revision: 5, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-10-07T00:00:00Z"})
r = BuildOutput(d, q)
assertOutputSeconds(t, r.Totals.Quality, 1, 900)
if r.Tasks[0].QualityAt != "2026-10-07T00:00:00Z" {
t.Fatal("invalidated quality evidence reused")
}
}
func TestAnnotationOutputIncompletePlansMissingDurationAndDebug(t *testing.T) {
d := outputFixture()
d.Operations[0].Status = "applying"
d.Tasks[0].SourceDurationSeconds = 0
d.Operations = append(d.Operations, domain.ReviewOperation{ID: "pending-q", Status: "applying", Effects: []domain.ReviewDecisionEffect{{Review: &domain.AnnotationReviewRecord{ID: "q"}}}})
r := BuildOutput(d, outputQuery())
if r.Totals.Quality.Count != 0 || r.Totals.Acceptance.Count != 0 {
t.Fatal("unfinished decision counted")
}
q := outputQuery()
q.From = q.From.AddDate(0, 0, -10)
r = BuildOutput(d, q)
if r.Totals.Submitted.Count != 1 || r.Totals.Submitted.Seconds != nil {
t.Fatal("unknown video seconds invented")
}
d.Runs[0].Mode = domain.RunModeDebug
if BuildOutput(d, q).Totals.Submitted.Count != 0 {
t.Fatal("debug output counted")
}
}
func TestAnnotationOutputPartIdentityAndMultipleTasks(t *testing.T) {
d := outputFixture()
second := d.Tasks[0]
second.ID = "t2"
d.Tasks = append(d.Tasks, second)
d.Revisions = append(d.Revisions, domain.AnnotationRevisionRecord{TaskID: "t2", Revision: 4, AuthorUserID: "a", CountsAsOutput: true})
r := BuildOutput(d, outputQuery())
if r.Totals.Quality.Count != 0 || r.Totals.Acceptance.Count != 0 {
t.Fatal("partial task set counted as a part")
}
e := d.Events[0]
e.ID = "s2"
e.TaskID = "t2"
d.Events = append(d.Events, e)
review := d.Reviews[0]
review.ID = "q2"
review.TaskID = "t2"
d.Reviews = append(d.Reviews, review)
d.Items = append(d.Items, domain.HumanItemRecord{ID: "i2", LegacyTaskID: "t2"})
d.Operations[0].SourceItemIDs = append(d.Operations[0].SourceItemIDs, "i2")
r = BuildOutput(d, outputQuery())
assertOutputSeconds(t, r.Totals.Acceptance, 1, 900)
c := d.Captures[0]
c.ID = "c2"
c.Prefix = "different/run/part0001"
c.RunID = "r2"
d.Captures = append(d.Captures, c)
run := d.Runs[0]
run.ID = "r2"
run.TaskID = "c2"
d.Runs = append(d.Runs, run)
third := second
third.ID = "t3"
third.RunID = "r2"
d.Tasks = append(d.Tasks, third)
e.ID = "s3"
e.TaskID = "t3"
e.SubmissionKind = "first"
e.CreatedAt = "2026-10-02T00:00:00Z"
d.Events = append(d.Events, e)
r = BuildOutput(d, outputQuery())
assertOutputSeconds(t, r.Totals.Submitted, 1, 900)
if len(r.Tasks) != 2 {
t.Fatal("same display name from different directories collapsed")
}
}
func TestAnnotationOutputLatestAttemptDoesNotReuseApproval(t *testing.T) {
d := outputFixture()
run := d.Runs[0]
run.ID = "new"
run.CreatedAt = "2026-10-10T00:00:00Z"
d.Runs = append(d.Runs, run)
task := d.Tasks[0]
task.ID = "new-task"
task.RunID = "new"
d.Tasks = append(d.Tasks, task)
q := outputQuery()
q.From = q.From.AddDate(0, 0, -10)
r := BuildOutput(d, q)
assertOutputSeconds(t, r.Totals.Submitted, 1, 900)
assertOutputSeconds(t, r.Totals.Acceptance, 0, 0)
}
func TestAnnotationOutputChangedAuthorKeepsStageAttribution(t *testing.T) {
d := outputFixture()
d.Operations = nil
d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "reject", TaskID: "t", Revision: 4, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewRejected, CreatedAt: "2026-10-02T00:00:00Z"})
d.Revisions = append(d.Revisions, domain.AnnotationRevisionRecord{TaskID: "t", Revision: 5, AuthorUserID: "b", IsRework: true})
d.Events = append(d.Events, domain.AnnotationWorklogEventRecord{ID: "s-new", TaskID: "t", Revision: 5, UserID: "b", SupplierID: "other", EventType: "rework_submitted", CreatedAt: "2026-10-03T00:00:00Z"})
d.Reviews = append(d.Reviews, domain.AnnotationReviewRecord{ID: "q-new", TaskID: "t", Revision: 5, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, CreatedAt: "2026-10-04T00:00:00Z"})
d.Operations = []domain.ReviewOperation{{ID: "ac-new", StageID: "prepackage_acceptance", Decision: "approved", Status: "completed", SourceItemIDs: []string{"i"}, CompletedAt: "2026-10-05T00:00:00Z"}}
q := outputQuery()
q.From = q.From.AddDate(0, 0, -10)
r := BuildOutput(d, q)
if r.Tasks[0].Attribution["submitted"].UserID != "a" || r.Tasks[0].Attribution["acceptance"].UserID != "b" {
t.Fatal("stage attribution conflated")
}
q.MatchUser = true
q.ScopeUserID = "a"
r = BuildOutput(d, q)
assertOutputSeconds(t, r.Totals.Acceptance, 0, 0)
if _, leaked := r.Tasks[0].Attribution["acceptance"]; leaked {
t.Fatal("other author's attribution exposed")
}
q.ScopeUserID = "b"
r = BuildOutput(d, q)
assertOutputSeconds(t, r.Totals.Submitted, 0, 0)
assertOutputSeconds(t, r.Totals.Acceptance, 1, 900)
}
func TestAnnotationOutputNeverUsesSliceWorklogAsWholeVideoDuration(t *testing.T) {
for _, full := range []bool{true, false} {
d := outputFixture()
d.Tasks[0].SourceDurationSeconds = 0
d.Tasks[0].SourceIsFullVideo = full
d.Tasks[0].ClipEnd = 120
keptSeconds := 90.0
d.Events[0].VideoSeconds = &keptSeconds
q := outputQuery()
q.From = q.From.AddDate(0, 0, -10)
r := BuildOutput(d, q)
if r.Totals.Submitted.Count != 1 || r.Totals.Submitted.Seconds != nil || r.Tasks[0].Seconds != nil {
t.Fatalf("slice/worklog duration used as whole video: %+v", r)
}
}
}