droboticflow-new/internal/api/data_operations_test.go
2026-08-18 12:22:11 +08:00

212 lines
7.7 KiB
Go

package api
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"droboticflow/internal/config"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
func apiDataOperationsInt64(value int64) *int64 {
return &value
}
func TestCollectionTaskCreateAndStartCarriesTaskIntoRun(t *testing.T) {
ctx := context.Background()
dataStore := store.NewMemoryStore()
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Y1 桌面整理"})
if err != nil {
t.Fatalf("create project: %v", err)
}
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
ProjectID: project.ID,
Name: "Y1 完整链路",
Type: domain.WorkflowTypeBatch,
Nodes: []domain.WorkflowNode{{
ID: "collect-node",
Type: "default",
Data: domain.WorkflowNodeData{
Label: "数据采集",
Category: "DATA",
Description: "collect",
Inputs: []domain.PortDefinition{},
Outputs: []domain.PortDefinition{},
},
Position: domain.Position{},
}},
Edges: []domain.WorkflowEdge{},
})
if err != nil {
t.Fatalf("create workflow: %v", err)
}
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{WorkspaceMountPath: t.TempDir()})
payload := domain.CollectionTaskInput{
ProjectID: project.ID,
Name: "Y1 桌面整理数据闭环",
RobotModel: "Y1",
Scene: "桌面整理",
Modalities: []string{"RGB", "关节状态"},
Mode: domain.CollectionTaskModeCollectionOnly,
Status: domain.CollectionTaskStatusDraft,
WorkflowID: workflow.ID,
StageBindings: map[domain.DataMetricStage]string{
domain.DataMetricStageCollect: "collect-node",
},
Targets: domain.DataVolume{TrajectoryCount: apiDataOperationsInt64(100)},
Requirements: domain.CollectionTaskRequirements{
RequireTechnicalValidation: true,
RequireQuality: true,
RequireAnnotation: true,
RequireAudit: true,
},
SplitRules: map[string]int{"train": 80, "val": 10, "test": 10},
}
body, _ := json.Marshal(payload)
createRecorder := httptest.NewRecorder()
handler.ServeHTTP(createRecorder, httptest.NewRequest(http.MethodPost, "/api/data-collection-tasks", bytesReader(body)))
if createRecorder.Code != http.StatusCreated {
t.Fatalf("create status = %d body=%s", createRecorder.Code, createRecorder.Body.String())
}
var task domain.CollectionTaskRecord
if err := json.Unmarshal(createRecorder.Body.Bytes(), &task); err != nil {
t.Fatalf("decode task: %v", err)
}
if task.ID == "" || task.CreatedAt == "" || task.UpdatedAt == "" {
t.Fatalf("created task is missing persisted fields: %#v", task)
}
startRecorder := httptest.NewRecorder()
handler.ServeHTTP(startRecorder, httptest.NewRequest(http.MethodPost, "/api/data-collection-tasks/"+task.ID+"/start", nil))
if startRecorder.Code != http.StatusAccepted {
t.Fatalf("start status = %d body=%s", startRecorder.Code, startRecorder.Body.String())
}
var response struct {
Run domain.RunRecord `json:"run"`
}
if err := json.Unmarshal(startRecorder.Body.Bytes(), &response); err != nil {
t.Fatalf("decode start response: %v", err)
}
if response.Run.TaskID != task.ID {
t.Fatalf("run task id = %q, want %q", response.Run.TaskID, task.ID)
}
}
func TestWorkflowNodeBusinessStageValidation(t *testing.T) {
issues := validateWorkflowNodeResources([]domain.WorkflowNode{{
Data: domain.WorkflowNodeData{BusinessStage: domain.DataMetricStage("publish")},
}})
if len(issues) != 1 || issues[0].Field != "nodes[0].data.businessStage" {
t.Fatalf("issues = %#v", issues)
}
}
func TestCollectionTaskAcceptsFileCountOnlyTarget(t *testing.T) {
if !collectionTaskHasTarget(domain.DataVolume{FileCount: apiDataOperationsInt64(1)}) {
t.Fatal("fileCount should be accepted as a collection task target")
}
}
func TestAcceptedAnnotationGateQueuesRunOnlyAfterAllTasksAccepted(t *testing.T) {
ctx := context.Background()
dataStore := store.NewMemoryStore()
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Annotation Gate"})
if err != nil {
t.Fatalf("create project: %v", err)
}
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
ProjectID: project.ID,
Name: "Annotation Gate",
Type: domain.WorkflowTypeBatch,
Nodes: []domain.WorkflowNode{{ID: "annotation-gate", Type: "operator", Data: domain.WorkflowNodeData{Label: "逐段语义标注"}}},
})
if err != nil {
t.Fatalf("create workflow: %v", err)
}
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
if err != nil {
t.Fatalf("create run: %v", err)
}
run.Status = domain.RunStatusWaitingForAudit
run.CurrentNodeID = "annotation-gate"
if err := dataStore.UpsertRun(ctx, *run); err != nil {
t.Fatalf("upsert waiting run: %v", err)
}
tasks := []domain.AnnotationTaskRecord{
{ID: "annotation-1", OrganizationID: project.OrganizationID, ProjectID: project.ID, RunID: run.ID, NodeID: "annotation-gate", Status: domain.AnnotationTaskStatusAccepted},
{ID: "annotation-2", OrganizationID: project.OrganizationID, ProjectID: project.ID, RunID: run.ID, NodeID: "annotation-gate", Status: domain.AnnotationTaskStatusPendingAcceptance},
}
for _, item := range tasks {
if err := dataStore.UpsertAnnotationTask(ctx, item); err != nil {
t.Fatalf("upsert annotation task: %v", err)
}
}
server := &server{store: dataStore}
if err := server.queueAcceptedAnnotationGateRun(ctx, tasks[0]); err != nil {
t.Fatalf("queue with pending sibling: %v", err)
}
stillWaiting, _ := dataStore.GetRun(ctx, run.ID)
if stillWaiting.Status != domain.RunStatusWaitingForAudit {
t.Fatalf("run should wait for every task, got %s", stillWaiting.Status)
}
tasks[1].Status = domain.AnnotationTaskStatusAccepted
if err := dataStore.UpsertAnnotationTask(ctx, tasks[1]); err != nil {
t.Fatalf("accept final annotation task: %v", err)
}
if err := server.queueAcceptedAnnotationGateRun(ctx, tasks[1]); err != nil {
t.Fatalf("queue accepted gate: %v", err)
}
queued, _ := dataStore.GetRun(ctx, run.ID)
if queued.Status != domain.RunStatusQueued {
t.Fatalf("all accepted tasks should queue the run, got %s", queued.Status)
}
}
func TestLegacySourceCollectionsProduceEstimatedTrajectoryMetrics(t *testing.T) {
metrics := legacyMetricsFromExecution(
domain.RunRecord{CreatedAt: "2026-07-23T01:00:00Z"},
domain.NodeExecutionRecord{
NodeLabel: "BOS 增量扫描",
Artifacts: []domain.ExecutionArtifact{{
Name: "dataset-index.json",
Data: map[string]any{
"collections": []any{
map[string]any{"id": "trajectory-1", "path": "y1/1", "fileCount": float64(5), "totalBytes": float64(100), "valid": true},
map[string]any{"id": "trajectory-2", "path": "y1/2", "fileCount": float64(4), "totalBytes": float64(80), "valid": false},
},
},
}},
},
)
if metrics == nil || metrics.Output.TrajectoryCount == nil || *metrics.Output.TrajectoryCount != 2 {
t.Fatalf("metrics = %#v", metrics)
}
if metrics.Output.FileCount == nil || *metrics.Output.FileCount != 9 ||
metrics.Output.Bytes == nil || *metrics.Output.Bytes != 180 {
t.Fatalf("source collection volumes = %#v", metrics)
}
if len(metrics.DatasetRefs) != 2 || metrics.Quality != domain.DataMetricQualityEstimated {
t.Fatalf("source collection lineage = %#v", metrics)
}
}
func TestParseDataOperationsQueryUsesUTC8TodayByDefault(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, "/api/data-operations/summary", nil)
query, issues := parseDataOperationsQuery(request, &domain.SettingsRecord{Timezone: "Asia/Shanghai"})
if len(issues) > 0 {
t.Fatalf("issues = %#v", issues)
}
if query.Timezone != "Asia/Shanghai" || query.From == "" || query.To == "" {
t.Fatalf("query = %#v", query)
}
}
func bytesReader(value []byte) *bytes.Reader {
return bytes.NewReader(value)
}