forked from longtao.wu/droboticflow
131 lines
4.5 KiB
Go
131 lines
4.5 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{}, RuntimeContract: map[string]any{"handler": "builtin_generic"},
|
|
},
|
|
Position: domain.Position{},
|
|
}},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
handler := newBusinessTestHandlerWithConfig(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 TestHistoricalMetricBackfillEndpointIsRemoved(t *testing.T) {
|
|
handler := newBusinessTestHandler(store.NewMemoryStore(), nil)
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/data-operations/backfill", nil, nil)
|
|
if recorder.Code != http.StatusNotFound {
|
|
t.Fatalf("retired backfill status = %d", recorder.Code)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|