droboticflow/internal/api/production_quality_test.go

102 lines
4.4 KiB
Go

package api
import (
"droboticflow/internal/domain"
"droboticflow/internal/productionstats"
"droboticflow/internal/workforcestats"
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestProductionQualityDates(t *testing.T) {
now := time.Date(2026, 3, 31, 12, 0, 0, 0, time.UTC)
from, to, loc, err := productionQualityDates(httptest.NewRequest("GET", "/", nil), now)
if err != nil || from.In(loc).Format("2006-01-02") != "2026-02-28" || to.In(loc).Format("2006-01-02") != "2026-03-31" {
t.Fatalf("defaults %v %v %v", from, to, err)
}
from, to, _, err = productionQualityDates(httptest.NewRequest("GET", "/?from=2026-10-01&to=2026-11-01&timeZone=UTC", nil), now)
if err != nil || to.Sub(from) != 31*24*time.Hour {
t.Fatal("end must be exclusive")
}
if _, _, _, err = productionQualityDates(httptest.NewRequest("GET", "/?from=2026-10-01&to=2026-10-01", nil), now); err == nil {
t.Fatal("empty range accepted")
}
}
func TestProductionQualityAPISelectionPaginationExportAndPermissions(t *testing.T) {
f := newCombinedDraftFixture(t)
_, err := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0)
if err != nil {
t.Fatal(err)
}
for _, spec := range []struct{ id, at string }{{"oct", "2026-10-01T00:00:00Z"}, {"sep", "2026-09-01T00:00:00Z"}} {
task := f.task
task.ID = spec.id
task.SourceIsFullVideo = true
task.SourceDurationSeconds = 100
task.RunID = ""
task.DataTaskID = ""
task.CreatedAt = spec.at
if e := f.store.UpsertAnnotationTask(f.ctx, task); e != nil {
t.Fatal(e)
}
if e := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: spec.id + "r", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, AuthorUserID: f.annotator.ID, CountsAsOutput: true}); e != nil {
t.Fatal(e)
}
if e := f.store.UpsertAnnotationWorklogEvent(f.ctx, domain.AnnotationWorklogEventRecord{ID: spec.id + "s", OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, TaskID: task.ID, UserID: f.annotator.ID, SupplierID: f.annotator.SupplierID, EventType: "combined_submitted", Revision: 1, CreatedAt: spec.at}); e != nil {
t.Fatal(e)
}
if e := f.store.UpsertAnnotationReview(f.ctx, domain.AnnotationReviewRecord{ID: spec.id + "q", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, ReviewerUserID: f.reviewer.ID, CreatedAt: "2026-10-02T00:00:00Z"}); e != nil {
t.Fatal(e)
}
}
url := "/api/dashboard/production-quality?from=2026-10-01&to=2026-10-02&timeZone=UTC&role=annotation"
r := outputReady(t, f, url, f.annotator)
var body struct {
Totals workforcestats.OutputTotals
People outputPage[workforcestats.ProductionPerson]
Snapshot struct {
ID string `json:"id"`
}
}
if err = json.Unmarshal(r.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
if body.Totals.Submitted.Count != 1 || body.Totals.Quality.Count != 1 || body.People.Total != 1 {
t.Fatalf("cohort/status %s", r.Body.String())
}
for _, person := range body.People.Items {
if person.ID != f.annotator.ID {
t.Fatal("other personnel leaked")
}
}
exported := outputReady(t, f, url+"&format=csv&snapshotId="+body.Snapshot.ID, f.annotator)
if !strings.Contains(exported.Header().Get("Content-Type"), "text/csv") || !strings.Contains(exported.Body.String(), "首轮通过率") {
t.Fatal("export contract")
}
reviewURL := strings.Replace(url, "role=annotation", "role=quality", 1)
self := outputReady(t, f, reviewURL, f.reviewer)
var selfBody struct{ Totals workforcestats.OutputTotals }
if e := json.Unmarshal(self.Body.Bytes(), &selfBody); e != nil {
t.Fatal(e)
}
selected := outputReady(t, f, reviewURL+"&userId="+f.reviewer.ID+"&snapshotId="+body.Snapshot.ID, f.admin)
var selectedBody struct {
Totals workforcestats.OutputTotals
People outputPage[workforcestats.ProductionPerson]
}
if e := json.Unmarshal(selected.Body.Bytes(), &selectedBody); e != nil {
t.Fatal(e)
}
if selfBody.Totals.Submitted.Count != 1 || selectedBody.Totals.Submitted.Count != 1 || selectedBody.People.Total != 1 || *selectedBody.Totals.Submitted.Seconds != *selfBody.Totals.Submitted.Seconds {
t.Fatal("selected reviewer summary differs from self on same snapshot")
}
invalid := f.request("GET", url+"&snapshotId=invalid", f.annotator, nil)
if invalid.Code != 409 {
t.Fatalf("snapshot %d", invalid.Code)
}
}