forked from longtao.wu/droboticflow
580 lines
21 KiB
Go
580 lines
21 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestAPIHandlerSupportsDashboardCRUDAndRunQueueing(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
storageRoot := t.TempDir()
|
|
for _, bucket := range []string{"incoming", "processed"} {
|
|
if err := os.Mkdir(filepath.Join(storageRoot, bucket), 0o755); err != nil {
|
|
t.Fatalf("create bucket %s: %v", bucket, err)
|
|
}
|
|
}
|
|
settings, err := dataStore.GetSettings(ctx)
|
|
if err != nil {
|
|
t.Fatalf("get settings: %v", err)
|
|
}
|
|
settings.K8s.Kubeconfig = "apiVersion: v1"
|
|
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
|
t.Fatalf("update settings: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "Dashboard 项目",
|
|
Description: "用于 dashboard 验证",
|
|
}); err != nil {
|
|
t.Fatalf("create dashboard project: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
dashboardRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(dashboardRecorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
|
if dashboardRecorder.Code != http.StatusOK {
|
|
t.Fatalf("dashboard status: %d", dashboardRecorder.Code)
|
|
}
|
|
var dashboardBody map[string]any
|
|
if err := json.Unmarshal(dashboardRecorder.Body.Bytes(), &dashboardBody); err != nil {
|
|
t.Fatalf("decode dashboard: %v", err)
|
|
}
|
|
projects := dashboardBody["projects"].(map[string]any)
|
|
if projects["active"].(float64) <= 0 {
|
|
t.Fatalf("expected active projects")
|
|
}
|
|
|
|
projectPayload := []byte(`{"name":"API 项目","description":"通过 API 创建"}`)
|
|
projectRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectRecorder, httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader(projectPayload)))
|
|
if projectRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("project create status: %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
|
}
|
|
var createdProject map[string]any
|
|
if err := json.Unmarshal(projectRecorder.Body.Bytes(), &createdProject); err != nil {
|
|
t.Fatalf("decode created project: %v", err)
|
|
}
|
|
projectID := createdProject["id"].(string)
|
|
if projectID == "" {
|
|
t.Fatalf("expected project id")
|
|
}
|
|
|
|
workflowPayload := []byte(`{"name":"API 工作流","description":"workflow","type":"batch","nodes":[{"id":"node-source","type":"operator","data":{"label":"真实数据源","category":"IO","description":"source","inputs":[],"outputs":[{"id":"out-1","name":"file","type":"file"}],"hyperparams":[{"key":"SOURCE_TEXT","value":"api-test"}]},"position":{"x":0,"y":0}}],"edges":[]}`)
|
|
workflowRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(workflowRecorder, httptest.NewRequest(http.MethodPost, "/api/projects/"+projectID+"/workflows", bytes.NewReader(workflowPayload)))
|
|
if workflowRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("workflow create status: %d body=%s", workflowRecorder.Code, workflowRecorder.Body.String())
|
|
}
|
|
var createdWorkflow map[string]any
|
|
if err := json.Unmarshal(workflowRecorder.Body.Bytes(), &createdWorkflow); err != nil {
|
|
t.Fatalf("decode workflow: %v", err)
|
|
}
|
|
|
|
projectsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectsRecorder, httptest.NewRequest(http.MethodGet, "/api/projects", nil))
|
|
if projectsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("projects list status: %d body=%s", projectsRecorder.Code, projectsRecorder.Body.String())
|
|
}
|
|
var projectsBody struct {
|
|
Items []map[string]any `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(projectsRecorder.Body.Bytes(), &projectsBody); err != nil {
|
|
t.Fatalf("decode projects list: %v", err)
|
|
}
|
|
var countedProject map[string]any
|
|
for _, item := range projectsBody.Items {
|
|
if item["id"] == projectID {
|
|
countedProject = item
|
|
break
|
|
}
|
|
}
|
|
if countedProject == nil {
|
|
t.Fatalf("expected created project in projects list")
|
|
}
|
|
if countedProject["workflowCount"].(float64) != 1 {
|
|
t.Fatalf("expected workflowCount=1 in projects list, got %v", countedProject["workflowCount"])
|
|
}
|
|
|
|
projectGetRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectGetRecorder, httptest.NewRequest(http.MethodGet, "/api/projects/"+projectID, nil))
|
|
if projectGetRecorder.Code != http.StatusOK {
|
|
t.Fatalf("project get status: %d body=%s", projectGetRecorder.Code, projectGetRecorder.Body.String())
|
|
}
|
|
var projectGetBody map[string]any
|
|
if err := json.Unmarshal(projectGetRecorder.Body.Bytes(), &projectGetBody); err != nil {
|
|
t.Fatalf("decode project get: %v", err)
|
|
}
|
|
if projectGetBody["workflowCount"].(float64) != 1 {
|
|
t.Fatalf("expected workflowCount=1 in project get, got %v", projectGetBody["workflowCount"])
|
|
}
|
|
|
|
runPayload := []byte(`{"trigger":"manual"}`)
|
|
runRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(runRecorder, httptest.NewRequest(http.MethodPost, "/api/workflows/"+createdWorkflow["id"].(string)+"/run", bytes.NewReader(runPayload)))
|
|
if runRecorder.Code != http.StatusAccepted {
|
|
t.Fatalf("run create status: %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
var queuedRun map[string]any
|
|
if err := json.Unmarshal(runRecorder.Body.Bytes(), &queuedRun); err != nil {
|
|
t.Fatalf("decode queued run: %v", err)
|
|
}
|
|
if queuedRun["status"].(string) != "queued" {
|
|
t.Fatalf("expected queued status, got %v", queuedRun["status"])
|
|
}
|
|
|
|
userPayload := []byte(`{"name":"API User","email":"api-user@example.com","role":"admin"}`)
|
|
userRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(userRecorder, httptest.NewRequest(http.MethodPost, "/api/users", bytes.NewReader(userPayload)))
|
|
if userRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("user create status: %d body=%s", userRecorder.Code, userRecorder.Body.String())
|
|
}
|
|
|
|
runsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(runsRecorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
|
if runsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("runs list status: %d", runsRecorder.Code)
|
|
}
|
|
var runsBody struct {
|
|
Items []map[string]any `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(runsRecorder.Body.Bytes(), &runsBody); err != nil {
|
|
t.Fatalf("decode runs: %v", err)
|
|
}
|
|
foundRun := false
|
|
for _, item := range runsBody.Items {
|
|
if item["id"] == queuedRun["id"] {
|
|
foundRun = true
|
|
break
|
|
}
|
|
}
|
|
if !foundRun {
|
|
t.Fatalf("expected queued run in listing")
|
|
}
|
|
|
|
bucketsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(bucketsRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/buckets?sourcePath="+storageRoot, nil))
|
|
if bucketsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("buckets status: %d", bucketsRecorder.Code)
|
|
}
|
|
var bucketsBody struct {
|
|
Items []string `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(bucketsRecorder.Body.Bytes(), &bucketsBody); err != nil {
|
|
t.Fatalf("decode buckets: %v", err)
|
|
}
|
|
if len(bucketsBody.Items) == 0 {
|
|
t.Fatalf("expected storage buckets")
|
|
}
|
|
|
|
objectsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
objectsRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/storage/objects?sourcePath="+storageRoot+"&bucket=incoming&prefix=", nil),
|
|
)
|
|
if objectsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("objects status: %d", objectsRecorder.Code)
|
|
}
|
|
|
|
missingSourcePathRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(missingSourcePathRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/buckets", nil))
|
|
if missingSourcePathRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("missing sourcePath status: %d", missingSourcePathRecorder.Code)
|
|
}
|
|
|
|
aiRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(aiRecorder, httptest.NewRequest(http.MethodGet, "/api/ai/status", nil))
|
|
if aiRecorder.Code != http.StatusOK {
|
|
t.Fatalf("ai status: %d", aiRecorder.Code)
|
|
}
|
|
}
|
|
|
|
func TestSettingsAIConfigurationDrivesAIStatus(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
settingsPayload := []byte(`{
|
|
"k8s": {
|
|
"endpoint": "https://120.48.161.230:6443",
|
|
"namespace": "droboticflow",
|
|
"kubeconfig": "apiVersion: v1\nkind: Config"
|
|
},
|
|
"ai": {
|
|
"provider": "dashscope-openai-compatible",
|
|
"baseURL": "https://coding.dashscope.aliyuncs.com/v1",
|
|
"model": "glm-5",
|
|
"apiKey": "test-key"
|
|
},
|
|
"timezone": "Asia/Tokyo"
|
|
}`)
|
|
|
|
updateRecorder := httptest.NewRecorder()
|
|
updateRequest := httptest.NewRequest(http.MethodPut, "/api/settings", bytes.NewReader(settingsPayload))
|
|
handler.ServeHTTP(updateRecorder, updateRequest)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("settings update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
statusRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(statusRecorder, httptest.NewRequest(http.MethodGet, "/api/ai/status", nil))
|
|
if statusRecorder.Code != http.StatusOK {
|
|
t.Fatalf("ai status code: %d body=%s", statusRecorder.Code, statusRecorder.Body.String())
|
|
}
|
|
|
|
var statusBody struct {
|
|
Enabled bool `json:"enabled"`
|
|
BaseURL string `json:"baseURL"`
|
|
Model string `json:"model"`
|
|
Provider string `json:"provider"`
|
|
}
|
|
if err := json.Unmarshal(statusRecorder.Body.Bytes(), &statusBody); err != nil {
|
|
t.Fatalf("decode ai status: %v", err)
|
|
}
|
|
if !statusBody.Enabled {
|
|
t.Fatalf("expected ai status enabled")
|
|
}
|
|
if statusBody.Model != "glm-5" {
|
|
t.Fatalf("unexpected model: %s", statusBody.Model)
|
|
}
|
|
if statusBody.BaseURL != "https://coding.dashscope.aliyuncs.com/v1" {
|
|
t.Fatalf("unexpected baseURL: %s", statusBody.BaseURL)
|
|
}
|
|
|
|
settingsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(settingsRecorder, httptest.NewRequest(http.MethodGet, "/api/settings", nil))
|
|
if settingsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("settings get status: %d body=%s", settingsRecorder.Code, settingsRecorder.Body.String())
|
|
}
|
|
var settingsBody struct {
|
|
K8s struct {
|
|
Kubeconfig string `json:"kubeconfig"`
|
|
KubeconfigPreview string `json:"kubeconfigPreview"`
|
|
HasKubeconfig bool `json:"hasKubeconfig"`
|
|
} `json:"k8s"`
|
|
AI struct {
|
|
APIKey string `json:"apiKey"`
|
|
APIKeyPreview string `json:"apiKeyPreview"`
|
|
HasAPIKey bool `json:"hasAPIKey"`
|
|
} `json:"ai"`
|
|
Timezone string `json:"timezone"`
|
|
Storage any `json:"storage"`
|
|
}
|
|
if err := json.Unmarshal(settingsRecorder.Body.Bytes(), &settingsBody); err != nil {
|
|
t.Fatalf("decode settings: %v", err)
|
|
}
|
|
if settingsBody.K8s.Kubeconfig != "" {
|
|
t.Fatalf("expected settings kubeconfig to be sanitized, got %q", settingsBody.K8s.Kubeconfig)
|
|
}
|
|
if !settingsBody.K8s.HasKubeconfig {
|
|
t.Fatalf("expected hasKubeconfig to be true")
|
|
}
|
|
if settingsBody.K8s.KubeconfigPreview == "" {
|
|
t.Fatalf("expected kubeconfig preview to be returned")
|
|
}
|
|
if settingsBody.AI.APIKey != "" {
|
|
t.Fatalf("expected settings apiKey to be sanitized")
|
|
}
|
|
if !settingsBody.AI.HasAPIKey {
|
|
t.Fatalf("expected hasAPIKey to be true")
|
|
}
|
|
if settingsBody.AI.APIKeyPreview != "te****ey" {
|
|
t.Fatalf("expected apiKey preview to be masked, got %q", settingsBody.AI.APIKeyPreview)
|
|
}
|
|
if settingsBody.Timezone != "Asia/Tokyo" {
|
|
t.Fatalf("expected timezone to roundtrip, got %q", settingsBody.Timezone)
|
|
}
|
|
if settingsBody.Storage != nil {
|
|
t.Fatalf("expected storage to be omitted from settings response")
|
|
}
|
|
}
|
|
|
|
func TestTemplateExecutionConfigValidationAndPersistence(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
createPayload := []byte(`{
|
|
"name":"执行保障模板",
|
|
"description":"template execution config",
|
|
"type":"streaming",
|
|
"maxConcurrentRuns":2,
|
|
"nodeTimeoutSeconds":12,
|
|
"runTimeoutSeconds":34,
|
|
"maxRetryAttempts":1,
|
|
"retryBackoffSeconds":56,
|
|
"schedule":{
|
|
"interval":90,
|
|
"sourcePath":"oss://incoming"
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("template create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created map[string]any
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created template: %v", err)
|
|
}
|
|
if created["type"].(string) != "streaming" {
|
|
t.Fatalf("expected streaming type, got %v", created["type"])
|
|
}
|
|
if created["retryBackoffSeconds"].(float64) != 56 {
|
|
t.Fatalf("expected retryBackoffSeconds=56, got %v", created["retryBackoffSeconds"])
|
|
}
|
|
|
|
templateID := created["id"].(string)
|
|
getRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(getRecorder, httptest.NewRequest(http.MethodGet, "/api/templates/"+templateID, nil))
|
|
if getRecorder.Code != http.StatusOK {
|
|
t.Fatalf("template get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
|
|
var loaded map[string]any
|
|
if err := json.Unmarshal(getRecorder.Body.Bytes(), &loaded); err != nil {
|
|
t.Fatalf("decode loaded template: %v", err)
|
|
}
|
|
if loaded["nodeTimeoutSeconds"].(float64) != 12 {
|
|
t.Fatalf("expected nodeTimeoutSeconds=12, got %v", loaded["nodeTimeoutSeconds"])
|
|
}
|
|
schedule := loaded["schedule"].(map[string]any)
|
|
if schedule["interval"].(float64) != 90 {
|
|
t.Fatalf("expected interval=90, got %v", schedule["interval"])
|
|
}
|
|
|
|
updatePayload := []byte(`{
|
|
"type":"batch",
|
|
"maxConcurrentRuns":3,
|
|
"nodeTimeoutSeconds":6,
|
|
"runTimeoutSeconds":18,
|
|
"maxRetryAttempts":2,
|
|
"retryBackoffSeconds":9
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/templates/"+templateID, bytes.NewReader(updatePayload)),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("template update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
var updated map[string]any
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
|
t.Fatalf("decode updated template: %v", err)
|
|
}
|
|
if updated["type"].(string) != "batch" {
|
|
t.Fatalf("expected batch type after update, got %v", updated["type"])
|
|
}
|
|
if updated["maxConcurrentRuns"].(float64) != 3 {
|
|
t.Fatalf("expected maxConcurrentRuns=3 after update, got %v", updated["maxConcurrentRuns"])
|
|
}
|
|
if updated["retryBackoffSeconds"].(float64) != 9 {
|
|
t.Fatalf("expected retryBackoffSeconds=9 after update, got %v", updated["retryBackoffSeconds"])
|
|
}
|
|
|
|
invalidPayload := []byte(`{"maxRetryAttempts":-1}`)
|
|
invalidRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/templates/"+templateID, bytes.NewReader(invalidPayload)),
|
|
)
|
|
if invalidRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid template update status: %d body=%s", invalidRecorder.Code, invalidRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkflowExecutionConfigValidationAndPersistence(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "执行配置项目",
|
|
Description: "workflow execution settings",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
createPayload := []byte(`{
|
|
"name":"执行配置工作流",
|
|
"description":"workflow",
|
|
"type":"batch",
|
|
"maxConcurrentRuns":2,
|
|
"nodeTimeoutSeconds":30,
|
|
"runTimeoutSeconds":120,
|
|
"maxRetryAttempts":3,
|
|
"retryBackoffSeconds":15,
|
|
"nodes":[{"id":"node-source","type":"operator","data":{"label":"真实数据源","category":"IO","description":"source","inputs":[],"outputs":[{"id":"out-1","name":"file","type":"file"}],"hyperparams":[{"key":"SOURCE_TEXT","value":"api-test"}]},"position":{"x":0,"y":0}}],
|
|
"edges":[]
|
|
}`)
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
createRequest := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload))
|
|
handler.ServeHTTP(createRecorder, createRequest)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("workflow create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if created.RunTimeoutSeconds != 120 {
|
|
t.Fatalf("expected runTimeoutSeconds=120, got %d", created.RunTimeoutSeconds)
|
|
}
|
|
if created.NodeTimeoutSeconds != 30 {
|
|
t.Fatalf("expected nodeTimeoutSeconds=30, got %d", created.NodeTimeoutSeconds)
|
|
}
|
|
if created.MaxRetryAttempts != 3 {
|
|
t.Fatalf("expected maxRetryAttempts=3, got %d", created.MaxRetryAttempts)
|
|
}
|
|
if created.RetryBackoffSeconds != 15 {
|
|
t.Fatalf("expected retryBackoffSeconds=15, got %d", created.RetryBackoffSeconds)
|
|
}
|
|
|
|
updatePayload := []byte(`{"nodeTimeoutSeconds":12,"runTimeoutSeconds":45,"maxRetryAttempts":1,"retryBackoffSeconds":9}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
updateRequest := httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(updatePayload))
|
|
handler.ServeHTTP(updateRecorder, updateRequest)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("workflow update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
var updated domain.WorkflowRecord
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
|
t.Fatalf("decode updated workflow: %v", err)
|
|
}
|
|
if updated.RunTimeoutSeconds != 45 {
|
|
t.Fatalf("expected updated runTimeoutSeconds=45, got %d", updated.RunTimeoutSeconds)
|
|
}
|
|
if updated.NodeTimeoutSeconds != 12 {
|
|
t.Fatalf("expected updated nodeTimeoutSeconds=12, got %d", updated.NodeTimeoutSeconds)
|
|
}
|
|
if updated.MaxRetryAttempts != 1 {
|
|
t.Fatalf("expected updated maxRetryAttempts=1, got %d", updated.MaxRetryAttempts)
|
|
}
|
|
if updated.RetryBackoffSeconds != 9 {
|
|
t.Fatalf("expected updated retryBackoffSeconds=9, got %d", updated.RetryBackoffSeconds)
|
|
}
|
|
|
|
invalidCreatePayload := []byte(`{
|
|
"name":"非法工作流",
|
|
"description":"workflow",
|
|
"type":"batch",
|
|
"nodeTimeoutSeconds":-1,
|
|
"runTimeoutSeconds":-1,
|
|
"maxRetryAttempts":-2,
|
|
"retryBackoffSeconds":-3,
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
invalidCreateRecorder := httptest.NewRecorder()
|
|
invalidCreateRequest := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(invalidCreatePayload))
|
|
handler.ServeHTTP(invalidCreateRecorder, invalidCreateRequest)
|
|
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid create status: %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
|
}
|
|
|
|
invalidUpdatePayload := []byte(`{"nodeTimeoutSeconds":-1,"runTimeoutSeconds":-1,"maxRetryAttempts":-1,"retryBackoffSeconds":-1}`)
|
|
invalidUpdateRecorder := httptest.NewRecorder()
|
|
invalidUpdateRequest := httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(invalidUpdatePayload))
|
|
handler.ServeHTTP(invalidUpdateRecorder, invalidUpdateRequest)
|
|
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid update status: %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCompletedWorkflowCannotBeRunAgainManually(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "已完成项目",
|
|
Description: "禁止重复运行",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "已完成工作流",
|
|
Description: "completed",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
workflow.Status = domain.WorkflowStatusCompleted
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatalf("mark workflow completed: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
runRecorder := httptest.NewRecorder()
|
|
runRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/workflows/"+workflow.ID+"/run",
|
|
bytes.NewReader([]byte(`{"trigger":"manual"}`)),
|
|
)
|
|
handler.ServeHTTP(runRecorder, runRequest)
|
|
if runRecorder.Code != http.StatusConflict {
|
|
t.Fatalf("expected 409 for completed workflow rerun, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
|
|
var body map[string]string
|
|
if err := json.Unmarshal(runRecorder.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("decode rerun response: %v", err)
|
|
}
|
|
if body["error"] == "" {
|
|
t.Fatalf("expected rerun error message")
|
|
}
|
|
}
|
|
|
|
func TestSettingsRejectsInvalidTimezone(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
updateRecorder := httptest.NewRecorder()
|
|
updateRequest := httptest.NewRequest(
|
|
http.MethodPut,
|
|
"/api/settings",
|
|
bytes.NewReader([]byte(`{"timezone":"Mars/Base"}`)),
|
|
)
|
|
handler.ServeHTTP(updateRecorder, updateRequest)
|
|
if updateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid timezone to fail, got %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
}
|