147 lines
4.7 KiB
Go
147 lines
4.7 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestDashboardWorkforceRangeDefaultsAndCaps(t *testing.T) {
|
|
now := time.Date(2026, 8, 19, 8, 0, 0, 0, time.UTC)
|
|
from, to, err := dashboardWorkforceRange(now, "", "")
|
|
if err != nil || to.Sub(from) != 30*24*time.Hour {
|
|
t.Fatalf("unexpected default range from=%v to=%v err=%v", from, to, err)
|
|
}
|
|
if _, _, err := dashboardWorkforceRange(now, "2025-01-01T00:00:00Z", "2026-08-19T08:00:00Z"); err == nil {
|
|
t.Fatal("expected range longer than 366 days to fail")
|
|
}
|
|
}
|
|
|
|
func TestDashboardWorkforceKeepsDerivedRatesDisabled(t *testing.T) {
|
|
item := domain.DashboardWorkforceMetric{SubmittedCount: 8, ReworkCount: 2, QualityApproved: 9, QualityRejected: 1}
|
|
finalizeDashboardWorkforceMetric(&item)
|
|
if item.QualityPassRate != nil || item.ReworkRate != nil || item.AcceptancePassRate != nil {
|
|
t.Fatalf("derived rates must remain disabled: %#v", item)
|
|
}
|
|
}
|
|
|
|
func TestDashboardFirstSubmissionWithMaterializationRevisionIsNotRework(t *testing.T) {
|
|
var metric domain.DashboardWorkforceMetric
|
|
addDashboardWorkforceEvent(&metric, domain.AnnotationWorklogEventRecord{
|
|
TaskID: "first-task", EventType: "combined_submitted", Revision: 4, OutputDelta: 1,
|
|
})
|
|
if metric.SubmittedCount != 1 || metric.ReworkCount != 0 {
|
|
t.Fatalf("first formal submission must not be classified by materialization revision: %#v", metric)
|
|
}
|
|
}
|
|
|
|
func TestPopulateDashboardWorkforce_SkipsDebugEvents(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
s := &server{store: dataStore}
|
|
|
|
now := time.Now().UTC()
|
|
from := now.Add(-24 * time.Hour)
|
|
to := now.Add(24 * time.Hour)
|
|
|
|
// Add a production event
|
|
prodEvent := domain.AnnotationWorklogEventRecord{
|
|
ID: "prod-event-1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-1",
|
|
TaskID: "task-1",
|
|
UserID: "user-1",
|
|
SupplierID: "supplier-1",
|
|
EventType: "annotation_submitted",
|
|
Mode: domain.RunModeProduction,
|
|
DurationSeconds: 120,
|
|
OutputDelta: 1,
|
|
CreatedAt: now.Format(time.RFC3339Nano),
|
|
}
|
|
if err := dataStore.UpsertAnnotationWorklogEvent(ctx, prodEvent); err != nil {
|
|
t.Fatalf("upsert prod event: %v", err)
|
|
}
|
|
|
|
// Add a debug event
|
|
debugEvent := domain.AnnotationWorklogEventRecord{
|
|
ID: "debug-event-1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-1",
|
|
TaskID: "task-dbg-1",
|
|
UserID: "user-1",
|
|
SupplierID: "supplier-1",
|
|
EventType: "annotation_submitted",
|
|
Mode: domain.RunModeDebug,
|
|
Temporary: true,
|
|
DurationSeconds: 999,
|
|
OutputDelta: 50,
|
|
CreatedAt: now.Format(time.RFC3339Nano),
|
|
}
|
|
if err := dataStore.UpsertAnnotationWorklogEvent(ctx, debugEvent); err != nil {
|
|
t.Fatalf("upsert debug event: %v", err)
|
|
}
|
|
|
|
summary := &domain.DashboardSummary{}
|
|
principal := &authenticatedPrincipal{
|
|
User: domain.UserRecord{
|
|
ID: "admin-user",
|
|
Role: domain.UserRoleAdmin,
|
|
},
|
|
}
|
|
|
|
if err := s.populateDashboardWorkforce(ctx, domain.DefaultOrganizationID, principal, from, to, summary); err != nil {
|
|
t.Fatalf("populateDashboardWorkforce error: %v", err)
|
|
}
|
|
|
|
if summary.Workforce.Totals.SubmittedCount != 1 {
|
|
t.Fatalf("expected Totals.SubmittedCount == 1, got %d", summary.Workforce.Totals.SubmittedCount)
|
|
}
|
|
if summary.Workforce.Totals.DurationSeconds != 120 {
|
|
t.Fatalf("expected Totals.DurationSeconds == 120, got %d", summary.Workforce.Totals.DurationSeconds)
|
|
}
|
|
if summary.Workforce.Totals.OutputCount != 1 {
|
|
t.Fatalf("expected Totals.OutputCount == 1, got %d", summary.Workforce.Totals.OutputCount)
|
|
}
|
|
}
|
|
|
|
func TestResolveTaskRunMode(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
s := &server{store: dataStore}
|
|
|
|
// 1. Empty run ID
|
|
mode, isTemp := s.resolveTaskRunMode(ctx, "")
|
|
if mode != domain.RunModeProduction || isTemp {
|
|
t.Fatalf("expected (production, false), got (%s, %v)", mode, isTemp)
|
|
}
|
|
|
|
// 2. Nonexistent run ID
|
|
mode, isTemp = s.resolveTaskRunMode(ctx, "run-not-found")
|
|
if mode != domain.RunModeProduction || isTemp {
|
|
t.Fatalf("expected (production, false), got (%s, %v)", mode, isTemp)
|
|
}
|
|
|
|
// 3. Production run
|
|
_ = dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-prod",
|
|
Mode: domain.RunModeProduction,
|
|
})
|
|
mode, isTemp = s.resolveTaskRunMode(ctx, "run-prod")
|
|
if mode != domain.RunModeProduction || isTemp {
|
|
t.Fatalf("expected (production, false), got (%s, %v)", mode, isTemp)
|
|
}
|
|
|
|
// 4. Debug run
|
|
_ = dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-dbg",
|
|
Mode: domain.RunModeDebug,
|
|
})
|
|
mode, isTemp = s.resolveTaskRunMode(ctx, "run-dbg")
|
|
if mode != domain.RunModeDebug || !isTemp {
|
|
t.Fatalf("expected (debug, true), got (%s, %v)", mode, isTemp)
|
|
}
|
|
}
|