forked from longtao.wu/droboticflow
683 lines
23 KiB
Go
683 lines
23 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// ============================================================================
|
|
// Tier 5 Adversarial Coverage Hardening Test Suite
|
|
// ============================================================================
|
|
// Stress tests boundary conditions, concurrency race conditions, security
|
|
// gate bypass attempts, and legacy migration edge cases.
|
|
|
|
func TestTier5AdversarialHardening(t *testing.T) {
|
|
t.Run("Adv-BVA: Boundary Conditions & Edge Payloads", func(t *testing.T) {
|
|
testTier5BoundaryConditions(t)
|
|
})
|
|
|
|
t.Run("Adv-RACE: Concurrency & Race Conditions", func(t *testing.T) {
|
|
testTier5ConcurrencyRaces(t)
|
|
})
|
|
|
|
t.Run("Adv-SEC: Security Gate Bypass Attempts", func(t *testing.T) {
|
|
testTier5SecurityGateBypasses(t)
|
|
})
|
|
|
|
t.Run("Adv-LEGACY: Legacy Migration & Nil State Resilience", func(t *testing.T) {
|
|
testTier5LegacyMigrationEdgeCases(t)
|
|
})
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// 1. Boundary Value Analysis (BVA) Stress Tests
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func testTier5BoundaryConditions(t *testing.T) {
|
|
h := newDualTrackTestHarness(t)
|
|
h.createProject("bva-project", "BVA Testing Project")
|
|
h.createStorageConnection("conn-bva", domain.StorageProviderBOS)
|
|
|
|
// 5-Node DAG: Root1 & Root2 -> Intermediate -> QA Gate -> Export
|
|
nodes := []domain.WorkflowNode{
|
|
{
|
|
ID: "root-1",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Root BOS Video Stream",
|
|
Category: "source",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "video_in", Name: "Video Stream", Type: domain.PortTypeVideo, Required: true},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "video_out", Name: "Video Frames", Type: domain.PortTypeVideo},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
ID: "root-2",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Root Meta Config",
|
|
Category: "source",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "meta_in", Name: "Metadata JSON", Type: domain.PortTypeJSON, Required: false},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "meta_out", Name: "Metadata Config", Type: domain.PortTypeJSON},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
ID: "inter-1",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "YOLO Detector",
|
|
Category: "algorithm",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "video_in", Name: "Video Frames", Type: domain.PortTypeVideo, Required: true},
|
|
{ID: "meta_in", Name: "Metadata Config", Type: domain.PortTypeJSON, Required: false},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "boxes_out", Name: "Detected Bounding Boxes", Type: domain.PortTypeJSON},
|
|
},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "conf_thresh", Value: "0.5"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
ID: "qa-gate",
|
|
Type: "audit",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Human QA Gate",
|
|
Category: "audit",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "boxes_in", Name: "Detected Bounding Boxes", Type: domain.PortTypeJSON, Required: true},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "approved_boxes", Name: "Approved Boxes", Type: domain.PortTypeJSON},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
ID: "export-sink",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Dataset Exporter",
|
|
Category: "sink",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "boxes_in", Name: "Approved Boxes", Type: domain.PortTypeJSON, Required: true},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
edges := []domain.WorkflowEdge{
|
|
{ID: "e1", Source: "root-1", SourceHandle: "video_out", Target: "inter-1", TargetHandle: "video_in"},
|
|
{ID: "e2", Source: "root-2", SourceHandle: "meta_out", Target: "inter-1", TargetHandle: "meta_in"},
|
|
{ID: "e3", Source: "inter-1", SourceHandle: "boxes_out", Target: "qa-gate", TargetHandle: "boxes_in"},
|
|
{ID: "e4", Source: "qa-gate", SourceHandle: "approved_boxes", Target: "export-sink", TargetHandle: "boxes_in"},
|
|
}
|
|
|
|
inputs := []domain.WorkflowInputDefinition{
|
|
{
|
|
ID: "video_stream",
|
|
Name: "BOS Video Source Stream",
|
|
Type: domain.PortTypeVideo,
|
|
Required: true,
|
|
Bindings: []domain.WorkflowInputTarget{{NodeID: "root-1", TargetPortID: "video_in"}},
|
|
},
|
|
{
|
|
ID: "meta_config",
|
|
Name: "Metadata Config",
|
|
Type: domain.PortTypeJSON,
|
|
Required: false,
|
|
Bindings: []domain.WorkflowInputTarget{{NodeID: "root-2", TargetPortID: "meta_in"}},
|
|
},
|
|
}
|
|
|
|
release := h.publishWorkflowRelease("bva-template", "BVA Release", 1, nodes, edges, inputs)
|
|
|
|
// 1. Exact Inline Payload Boundary (60KB accepted vs 65540 bytes rejected)
|
|
t.Run("Inline Payload Size Exact Thresholds", func(t *testing.T) {
|
|
// Valid payload at threshold - 5KB
|
|
dataValid := strings.Repeat("x", 60000)
|
|
bodyValid := map[string]any{
|
|
"trigger": "manual",
|
|
"inputs": map[string]any{
|
|
"video_stream": map[string]any{
|
|
"type": "video",
|
|
"mode": "ref",
|
|
"ref": map[string]any{
|
|
"connectionId": "conn-bva",
|
|
"provider": "BOS",
|
|
"kind": "object",
|
|
"bucket": "test-bucket",
|
|
"key": "camera/front.mp4",
|
|
},
|
|
},
|
|
"meta_config": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{
|
|
"payload": dataValid,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
recValid := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflow-releases/%s/runs", release.ID), bodyValid)
|
|
if recValid.Code != http.StatusAccepted {
|
|
t.Fatalf("expected 202 Accepted for 60KB inline payload, got %d: %s", recValid.Code, recValid.Body.String())
|
|
}
|
|
|
|
// Reset active run to avoid 409 conflict
|
|
_ = h.store.UpsertWorkflow(h.ctx, domain.WorkflowRecord{ID: release.ID, Status: domain.WorkflowStatusDraft})
|
|
|
|
// Oversized payload (>64KB inline bytes)
|
|
dataOversized := strings.Repeat("x", 65540)
|
|
bodyOversized := map[string]any{
|
|
"trigger": "manual",
|
|
"inputs": map[string]any{
|
|
"video_stream": map[string]any{
|
|
"type": "video",
|
|
"mode": "ref",
|
|
"ref": map[string]any{
|
|
"connectionId": "conn-bva",
|
|
"provider": "BOS",
|
|
"kind": "object",
|
|
"bucket": "test-bucket",
|
|
"key": "camera/front.mp4",
|
|
},
|
|
},
|
|
"meta_config": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{
|
|
"payload": dataOversized,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
recOversized := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflow-releases/%s/runs", release.ID), bodyOversized)
|
|
if recOversized.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400 Bad Request for >64KB inline payload, got %d: %s", recOversized.Code, recOversized.Body.String())
|
|
}
|
|
})
|
|
|
|
// 2. Dashboard Workforce Date Range Boundaries
|
|
t.Run("Workforce Date Range Upper Limit and Boundaries", func(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
|
|
// 367 days (exceeds 366-day limit)
|
|
fromTooOld := now.Add(-367 * 24 * time.Hour).Format(time.RFC3339)
|
|
toNow := now.Format(time.RFC3339)
|
|
recTooLong := h.serveHTTP(http.MethodGet, fmt.Sprintf("/api/dashboard?from=%s&to=%s", fromTooOld, toNow), nil)
|
|
if recTooLong.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400 for range > 366 days, got %d: %s", recTooLong.Code, recTooLong.Body.String())
|
|
}
|
|
|
|
// Inverted range (from > to)
|
|
fromFuture := now.Add(24 * time.Hour).Format(time.RFC3339)
|
|
recInverted := h.serveHTTP(http.MethodGet, fmt.Sprintf("/api/dashboard?from=%s&to=%s", fromFuture, toNow), nil)
|
|
if recInverted.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400 for inverted range, got %d: %s", recInverted.Code, recInverted.Body.String())
|
|
}
|
|
|
|
// Valid 30-day range
|
|
fromValid := now.Add(-30 * 24 * time.Hour).Format(time.RFC3339)
|
|
recValid := h.serveHTTP(http.MethodGet, fmt.Sprintf("/api/dashboard?from=%s&to=%s", fromValid, toNow), nil)
|
|
if recValid.Code != http.StatusOK {
|
|
t.Fatalf("expected 200 for valid range, got %d: %s", recValid.Code, recValid.Body.String())
|
|
}
|
|
})
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// 2. Concurrency & Race Condition Stress Tests
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func testTier5ConcurrencyRaces(t *testing.T) {
|
|
h := newDualTrackTestHarness(t)
|
|
h.createProject("race-project", "Race Testing Project")
|
|
h.createStorageConnection("conn-race", domain.StorageProviderBOS)
|
|
|
|
nodes := []domain.WorkflowNode{
|
|
{
|
|
ID: "src",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Source Node",
|
|
Category: "source",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "data_in", Name: "Data In", Type: domain.PortTypeJSON, Required: true},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "data_out", Name: "Data Out", Type: domain.PortTypeJSON},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
ID: "sink",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Sink Node",
|
|
Category: "sink",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "data_in", Name: "Data In", Type: domain.PortTypeJSON, Required: true},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
edges := []domain.WorkflowEdge{
|
|
{ID: "e1", Source: "src", SourceHandle: "data_out", Target: "sink", TargetHandle: "data_in"},
|
|
}
|
|
inputs := []domain.WorkflowInputDefinition{
|
|
{
|
|
ID: "data_in",
|
|
Name: "Input Data",
|
|
Type: domain.PortTypeJSON,
|
|
Required: true,
|
|
Bindings: []domain.WorkflowInputTarget{{NodeID: "src", TargetPortID: "data_in"}},
|
|
},
|
|
}
|
|
|
|
release := h.publishWorkflowRelease("race-template", "Race Release", 1, nodes, edges, inputs)
|
|
|
|
// Test 1: 50 concurrent production run launch requests on same release
|
|
t.Run("High-Concurrency Production Run Trigger (Single Active Run Lock)", func(t *testing.T) {
|
|
const numGoroutines = 50
|
|
var wg sync.WaitGroup
|
|
wg.Add(numGoroutines)
|
|
|
|
results := make([]int, numGoroutines)
|
|
for i := 0; i < numGoroutines; i++ {
|
|
go func(idx int) {
|
|
defer wg.Done()
|
|
body := map[string]any{
|
|
"trigger": "manual",
|
|
"inputs": map[string]any{
|
|
"data_in": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{"batchId": idx},
|
|
},
|
|
},
|
|
}
|
|
rec := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflow-releases/%s/runs", release.ID), body)
|
|
results[idx] = rec.Code
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
|
|
acceptedCount := 0
|
|
conflictCount := 0
|
|
for _, code := range results {
|
|
if code == http.StatusAccepted {
|
|
acceptedCount++
|
|
} else if code == http.StatusConflict {
|
|
conflictCount++
|
|
} else {
|
|
t.Errorf("unexpected HTTP response code during concurrent run trigger: %d", code)
|
|
}
|
|
}
|
|
|
|
if acceptedCount < 1 {
|
|
t.Fatalf("expected at least 1 accepted run, got %d accepted and %d conflicts", acceptedCount, conflictCount)
|
|
}
|
|
})
|
|
|
|
// Test 2: Concurrent queries with mode filters while writing runs
|
|
t.Run("Concurrent Run Queries and Writes across Modes", func(t *testing.T) {
|
|
const numWorkers = 30
|
|
var wg sync.WaitGroup
|
|
wg.Add(numWorkers)
|
|
|
|
for i := 0; i < numWorkers; i++ {
|
|
go func(idx int) {
|
|
defer wg.Done()
|
|
mode := domain.RunModeProduction
|
|
if idx%2 == 1 {
|
|
mode = domain.RunModeDebug
|
|
}
|
|
|
|
// Write run
|
|
runID := fmt.Sprintf("race-run-%d", idx)
|
|
_ = h.store.UpsertRun(h.ctx, domain.RunRecord{
|
|
ID: runID,
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
WorkflowID: "race-wf",
|
|
Mode: mode,
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: time.Now().UTC().Format(time.RFC3339),
|
|
UpdatedAt: time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
|
|
// Concurrently query
|
|
rec := h.serveHTTP(http.MethodGet, fmt.Sprintf("/api/runs?mode=%s&limit=20", mode), nil)
|
|
if rec.Code != http.StatusOK {
|
|
t.Errorf("worker %d: failed to query runs by mode %s: %d", idx, mode, rec.Code)
|
|
}
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
})
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// 3. Security Gate Bypass Stress Tests
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func testTier5SecurityGateBypasses(t *testing.T) {
|
|
h := newDualTrackTestHarness(t)
|
|
project := h.createProject("sec-project", "Security Testing Project")
|
|
h.createStorageConnection("conn-sec", domain.StorageProviderBOS)
|
|
|
|
nodes := []domain.WorkflowNode{
|
|
{
|
|
ID: "node-root",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Root Ingestion Node",
|
|
Category: "source",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "in_root", Name: "Root In", Type: domain.PortTypeJSON, Required: true},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "out_root", Name: "Root Out", Type: domain.PortTypeJSON},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
ID: "node-intermediate",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Intermediate Computation Node",
|
|
Category: "algorithm",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "in_inter", Name: "Inter In", Type: domain.PortTypeJSON, Required: true},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "out_inter", Name: "Inter Out", Type: domain.PortTypeJSON},
|
|
},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "lr", Value: "0.001"},
|
|
},
|
|
Image: "registry.example.com/models/evaluator:v1.0",
|
|
},
|
|
},
|
|
}
|
|
edges := []domain.WorkflowEdge{
|
|
{ID: "e1", Source: "node-root", SourceHandle: "out_root", Target: "node-intermediate", TargetHandle: "in_inter"},
|
|
}
|
|
inputs := []domain.WorkflowInputDefinition{
|
|
{
|
|
ID: "root_input",
|
|
Name: "Root Input",
|
|
Type: domain.PortTypeJSON,
|
|
Required: true,
|
|
Bindings: []domain.WorkflowInputTarget{{NodeID: "node-root", TargetPortID: "in_root"}},
|
|
},
|
|
}
|
|
|
|
release := h.publishWorkflowRelease("sec-template", "Security Gate Release", 1, nodes, edges, inputs)
|
|
|
|
// Attack 1: Attempt to inject nodeOverrides into POST /api/workflow-releases/:id/runs
|
|
t.Run("Security Gate: Rejection of nodeOverrides in production run", func(t *testing.T) {
|
|
body := map[string]any{
|
|
"trigger": "manual",
|
|
"inputs": map[string]any{
|
|
"root_input": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{"key": "val"},
|
|
},
|
|
},
|
|
"nodeOverrides": map[string]any{
|
|
"node-intermediate": map[string]any{
|
|
"hyperparams": []map[string]string{{"key": "lr", "value": "0.999"}},
|
|
},
|
|
},
|
|
}
|
|
rec := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflow-releases/%s/runs", release.ID), body)
|
|
if rec.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("expected 422 for nodeOverrides injection, got %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var errResp map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &errResp)
|
|
if errResp["code"] != "production_input_forbidden" {
|
|
t.Fatalf("expected error code production_input_forbidden, got %v", errResp["code"])
|
|
}
|
|
})
|
|
|
|
// Attack 2: Attempt to inject debugTarget into POST /api/workflow-releases/:id/runs
|
|
t.Run("Security Gate: Rejection of debugTarget in production run", func(t *testing.T) {
|
|
body := map[string]any{
|
|
"trigger": "manual",
|
|
"inputs": map[string]any{
|
|
"root_input": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{"key": "val"},
|
|
},
|
|
},
|
|
"debugTarget": map[string]any{
|
|
"targetNodeId": "node-intermediate",
|
|
"singleNode": true,
|
|
},
|
|
}
|
|
rec := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflow-releases/%s/runs", release.ID), body)
|
|
if rec.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("expected 422 for debugTarget injection, got %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var errResp map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &errResp)
|
|
if errResp["code"] != "production_input_forbidden" {
|
|
t.Fatalf("expected error code production_input_forbidden, got %v", errResp["code"])
|
|
}
|
|
})
|
|
|
|
// Attack 3: Attempt to inject topology nodes/edges modifications into POST /api/workflow-releases/:id/runs
|
|
t.Run("Security Gate: Rejection of topology tampering in production run", func(t *testing.T) {
|
|
body := map[string]any{
|
|
"trigger": "manual",
|
|
"nodes": []any{},
|
|
"edges": []any{},
|
|
}
|
|
rec := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflow-releases/%s/runs", release.ID), body)
|
|
if rec.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("expected 422 for topology tampering, got %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
})
|
|
|
|
// Attack 4: Attempt to inject non-root node input directly
|
|
t.Run("Security Gate: Rejection of non-root intermediate input binding", func(t *testing.T) {
|
|
body := map[string]any{
|
|
"trigger": "manual",
|
|
"inputs": map[string]any{
|
|
"root_input": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{"key": "val"},
|
|
},
|
|
"node-intermediate.in_inter": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{"tampered": true},
|
|
},
|
|
},
|
|
}
|
|
rec := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflow-releases/%s/runs", release.ID), body)
|
|
if rec.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("expected 422 for non-root node input injection, got %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var errResp map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &errResp)
|
|
if errResp["code"] != "production_input_forbidden" {
|
|
t.Fatalf("expected error code production_input_forbidden, got %v", errResp["code"])
|
|
}
|
|
})
|
|
|
|
// Attack 5: Attempt to trigger production run via /api/workflows/:id/run on draft workflow
|
|
t.Run("Security Gate: Reject explicit production mode on draft workflow", func(t *testing.T) {
|
|
draftWf := h.createWorkflow(project.ID, "draft-only-wf", nodes, edges, inputs)
|
|
body := map[string]any{
|
|
"trigger": "manual",
|
|
"mode": "production",
|
|
"inputs": map[string]any{
|
|
"root_input": map[string]any{
|
|
"type": "json",
|
|
"mode": "inline",
|
|
"inline": map[string]any{"key": "val"},
|
|
},
|
|
},
|
|
}
|
|
rec := h.serveHTTP(http.MethodPost, fmt.Sprintf("/api/workflows/%s/run", draftWf.ID), body)
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400 when triggering production run on draft workflow, got %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var errResp map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &errResp)
|
|
issues, ok := errResp["issues"].([]any)
|
|
if !ok || len(issues) == 0 {
|
|
t.Fatalf("expected validation issues in response, got %v", errResp)
|
|
}
|
|
issueMap := issues[0].(map[string]any)
|
|
if issueMap["code"] != "production_run_draft_forbidden" {
|
|
t.Fatalf("expected issue code production_run_draft_forbidden, got %v", issueMap["code"])
|
|
}
|
|
})
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// 4. Legacy Migration & Nil State Resilience Tests
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func testTier5LegacyMigrationEdgeCases(t *testing.T) {
|
|
h := newDualTrackTestHarness(t)
|
|
project := h.createProject("legacy-project", "Legacy Testing Project")
|
|
|
|
t.Run("Legacy Run Record with Empty Mode Defaults to Production in Summaries and Filters", func(t *testing.T) {
|
|
legacyRun := domain.RunRecord{
|
|
ID: "legacy-run-001",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
WorkflowID: "legacy-wf-001",
|
|
ProjectID: project.ID,
|
|
Mode: "", // Unpopulated legacy mode
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: time.Now().UTC().Format(time.RFC3339),
|
|
UpdatedAt: time.Now().UTC().Format(time.RFC3339),
|
|
}
|
|
if err := h.store.UpsertRun(h.ctx, legacyRun); err != nil {
|
|
t.Fatalf("failed to insert legacy run: %v", err)
|
|
}
|
|
|
|
// Querying mode=production must include legacy run
|
|
recProd := h.serveHTTP(http.MethodGet, "/api/runs?mode=production", nil)
|
|
if recProd.Code != http.StatusOK {
|
|
t.Fatalf("failed to query production runs: %d", recProd.Code)
|
|
}
|
|
var prodData map[string]any
|
|
_ = json.Unmarshal(recProd.Body.Bytes(), &prodData)
|
|
items := prodData["items"].([]any)
|
|
found := false
|
|
for _, itemAny := range items {
|
|
item := itemAny.(map[string]any)
|
|
if item["id"] == "legacy-run-001" {
|
|
found = true
|
|
if item["mode"] != "production" {
|
|
t.Fatalf("expected legacy run mode to be hydrated as production, got %v", item["mode"])
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("legacy run with empty mode was not returned in mode=production query")
|
|
}
|
|
|
|
// Querying mode=debug must NOT include legacy run
|
|
recDebug := h.serveHTTP(http.MethodGet, "/api/runs?mode=debug", nil)
|
|
if recDebug.Code != http.StatusOK {
|
|
t.Fatalf("failed to query debug runs: %d", recDebug.Code)
|
|
}
|
|
var debugData map[string]any
|
|
_ = json.Unmarshal(recDebug.Body.Bytes(), &debugData)
|
|
debugItems := debugData["items"].([]any)
|
|
for _, itemAny := range debugItems {
|
|
item := itemAny.(map[string]any)
|
|
if item["id"] == "legacy-run-001" {
|
|
t.Fatalf("legacy run unexpectedly returned in mode=debug query")
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("Workforce Metrics Ignores Debug Events and Tolerates Nil Aggregates", func(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
nowISO := now.Format(time.RFC3339Nano)
|
|
|
|
// Add 1 production worklog event and 1 debug worklog event
|
|
_ = h.store.UpsertAnnotationWorklogEvent(h.ctx, domain.AnnotationWorklogEventRecord{
|
|
ID: "event-prod-1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
UserID: "annotator-1",
|
|
VendorID: "vendor-1",
|
|
WorkgroupID: "workgroup-1",
|
|
EventType: "annotation_submitted",
|
|
DurationSeconds: 120,
|
|
OutputDelta: 1,
|
|
Mode: domain.RunModeProduction,
|
|
CreatedAt: nowISO,
|
|
})
|
|
_ = h.store.UpsertAnnotationWorklogEvent(h.ctx, domain.AnnotationWorklogEventRecord{
|
|
ID: "event-prod-2",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
UserID: "annotator-1",
|
|
VendorID: "vendor-1",
|
|
WorkgroupID: "workgroup-1",
|
|
EventType: "quality_approved",
|
|
DurationSeconds: 30,
|
|
OutputDelta: 0,
|
|
Mode: domain.RunModeProduction,
|
|
CreatedAt: nowISO,
|
|
})
|
|
_ = h.store.UpsertAnnotationWorklogEvent(h.ctx, domain.AnnotationWorklogEventRecord{
|
|
ID: "event-debug-1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
UserID: "annotator-1",
|
|
VendorID: "vendor-1",
|
|
WorkgroupID: "workgroup-1",
|
|
EventType: "annotation_submitted",
|
|
DurationSeconds: 9999,
|
|
OutputDelta: 100,
|
|
Mode: domain.RunModeDebug, // Should be completely excluded
|
|
CreatedAt: nowISO,
|
|
})
|
|
|
|
from := now.Add(-1 * time.Hour).Format(time.RFC3339)
|
|
to := now.Add(1 * time.Hour).Format(time.RFC3339)
|
|
rec := h.serveHTTP(http.MethodGet, fmt.Sprintf("/api/dashboard?from=%s&to=%s", from, to), nil)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("failed to query dashboard: %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var summary domain.DashboardSummary
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &summary)
|
|
|
|
// Production totals should reflect only prod events: Duration = 150s, OutputCount = 1
|
|
if summary.Workforce.Totals.DurationSeconds != 150 {
|
|
t.Fatalf("expected total duration 150s, got %d", summary.Workforce.Totals.DurationSeconds)
|
|
}
|
|
if summary.Workforce.Totals.OutputCount != 1 {
|
|
t.Fatalf("expected total output count 1, got %d", summary.Workforce.Totals.OutputCount)
|
|
}
|
|
if summary.Workforce.Totals.QualityApproved != 1 {
|
|
t.Fatalf("expected total quality approved 1, got %d", summary.Workforce.Totals.QualityApproved)
|
|
}
|
|
})
|
|
}
|