droboticflow/internal/api/annotation_output_test.go
d-robotics 2715670828 ✨ Show QC and acceptance reviewers on the production output dashboard
The production overview and supplier output pages add QC and acceptance
sections beside annotators in the same card and table style. Reviews
count every formal decision per part, so rework re-reviews count again
and are listed as re-reviews; an aggregate acceptance counts each part
once. Every role gets video hours per person-day as efficiency, and all
durations now display in hours with one decimal.

Work facts are computed once per shared snapshot (schema v2) and only
filtered in memory per query; the loader reads three more identity
fields and no new collections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 18:57:23 +08:00

114 lines
5.2 KiB
Go

package api
import (
"droboticflow/internal/domain"
"droboticflow/internal/productionstats"
"droboticflow/internal/workforcestats"
"encoding/csv"
"encoding/json"
"fmt"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
"time"
)
func TestAnnotationOutputAPIAndCSVScope(t *testing.T) {
f := newCombinedDraftFixture(t, "output-vendor")
check := func(e error) {
t.Helper()
if e != nil {
t.Fatal(e)
}
}
_, e := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0)
check(e)
at := time.Now().Add(-time.Hour).UTC().Format(time.RFC3339Nano)
for i := 0; i < 207; i++ {
id := fmt.Sprintf("output-task-%d", i)
check(f.store.UpsertAnnotationTask(f.ctx, domain.AnnotationTaskRecord{ID: id, ProjectID: f.task.ProjectID, OrganizationID: domain.DefaultOrganizationID, SourceIsFullVideo: true, SourceDurationSeconds: 900, CreatedAt: at}))
check(f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: id + "-rev", OrganizationID: domain.DefaultOrganizationID, TaskID: id, Revision: 1, AuthorUserID: f.annotator.ID, CountsAsOutput: true}))
check(f.store.UpsertAnnotationWorklogEvent(f.ctx, domain.AnnotationWorklogEventRecord{ID: id + "-event", ProjectID: f.task.ProjectID, OrganizationID: domain.DefaultOrganizationID, TaskID: id, Revision: 1, UserID: f.annotator.ID, SupplierID: f.annotator.SupplierID, EventType: "combined_submitted", CreatedAt: at}))
}
for i := 0; i < 2; i++ {
check(f.store.UpsertAnnotationReview(f.ctx, domain.AnnotationReviewRecord{ID: fmt.Sprintf("output-review-%d", i), OrganizationID: domain.DefaultOrganizationID, TaskID: "output-task-0", Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewRejected, ReviewerUserID: f.reviewer.ID, CreatedAt: at}))
}
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
for _, user := range []domain.UserRecord{f.admin, f.annotator} {
r := outputReady(t, f, "/api/dashboard/annotation-output?tasksPage=42&tasksPageSize=5&qualityDaysPageSize=1", user)
if r.Code != 200 {
t.Fatalf("read %d %s", r.Code, r.Body.String())
}
var d struct {
Tasks outputPage[workforcestats.OutputTask] `json:"tasks"`
Totals workforcestats.OutputTotals `json:"totals"`
Quality struct {
Totals workforcestats.OutputReviewMetric `json:"totals"`
Days outputPage[workforcestats.OutputReviewDay] `json:"dailyOutput"`
} `json:"qualityReview"`
}
check(json.Unmarshal(r.Body.Bytes(), &d))
if d.Tasks.Total != 207 || len(d.Tasks.Items) != 2 || d.Totals.Submitted.Count != 207 {
t.Fatalf("pagination changed totals %s", r.Body.String())
}
// Both decisions on the same part count; an annotator never sees reviewer work.
want := 2
if user.ID == f.annotator.ID {
want = 0
}
if d.Quality.Totals.Reviews.Count != want || d.Quality.Totals.Rereviews != want/2 || d.Quality.Days.PageSize != 1 {
t.Fatalf("review output for %s: %s", user.ID, r.Body.String())
}
exported := outputReady(t, f, "/api/dashboard/annotation-output?format=csv&tasksPage=42&tasksPageSize=5", user)
if exported.Code != 200 {
t.Fatalf("export %s", exported.Body.String())
}
rows, e := csv.NewReader(strings.NewReader(strings.TrimPrefix(exported.Body.String(), "\ufeff"))).ReadAll()
check(e)
if len(rows) != 208 {
t.Fatalf("export only current page: %d", len(rows))
}
}
for _, query := range []string{"userId=" + f.reviewer.ID, "supplierId=foreign"} {
r := f.request("GET", "/api/dashboard/annotation-output?"+query, f.annotator, nil)
var body struct {
Tasks outputPage[workforcestats.OutputTask] `json:"tasks"`
}
check(json.Unmarshal(r.Body.Bytes(), &body))
if r.Code != 200 || body.Tasks.Total != 0 {
t.Fatalf("scope enlarged %s", r.Body.String())
}
}
if r := f.request("GET", "/api/dashboard/annotation-output", f.reviewer, nil); r.Code != 403 {
t.Fatal("reviewer gained author report")
}
for _, query := range []string{"tasksPage=-1", "tasksPageSize=1000", "qualityDaysPageSize=1000", "acceptanceSuppliersPage=0", "timeZone=invalid", "from=2026-10-20&to=2026-10-01", "format=unknown"} {
if r := f.request("GET", "/api/dashboard/annotation-output?"+query, f.admin, nil); r.Code != 400 {
t.Fatalf("invalid query accepted %s: %d", query, r.Code)
}
}
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if !reflect.DeepEqual(before, after) {
t.Fatal("report mutated customer task")
}
}
func TestAnnotationOutputDateBoundariesAndCSVCells(t *testing.T) {
for _, tc := range []struct {
zone, from, to string
hours time.Duration
}{{"Asia/Shanghai", "2026-10-01", "2026-10-01", 24}, {"America/New_York", "2026-03-08", "2026-03-08", 23}, {"America/New_York", "2026-11-01", "2026-11-01", 25}} {
req := httptest.NewRequest("GET", "/api/dashboard/annotation-output?"+url.Values{"timeZone": {tc.zone}, "from": {tc.from}, "to": {tc.to}}.Encode(), nil)
from, to, _, e := outputDateRange(req, time.Now())
if e != nil || to.Sub(from) != tc.hours*time.Hour {
t.Fatalf("DST range %+v: %v %v", tc, e, to.Sub(from))
}
}
for _, s := range []string{"=SUM(A1)", " +123", "@unsafe", "\t-formula"} {
if !strings.HasPrefix(outputCSVCell(s), "'") {
t.Fatal("formula passed to spreadsheet")
}
}
}