forked from longtao.wu/droboticflow
11310 lines
457 KiB
Go
11310 lines
457 KiB
Go
package api
|
||
|
||
import (
|
||
"bytes"
|
||
"compress/gzip"
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"mime/multipart"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"net/url"
|
||
"os"
|
||
"path/filepath"
|
||
"sort"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
aipkg "droboticflow/internal/ai"
|
||
"droboticflow/internal/config"
|
||
"droboticflow/internal/domain"
|
||
"droboticflow/internal/store"
|
||
|
||
bosapi "github.com/baidubce/bce-sdk-go/services/bos/api"
|
||
)
|
||
|
||
func TestListStorageObjectsFromConnectionNormalizesDirectoryPrefixes(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
prefix string
|
||
expected string
|
||
}{
|
||
{name: "empty", prefix: "", expected: ""},
|
||
{name: "trimmed root child", prefix: "algo", expected: "algo/"},
|
||
{name: "preserve nested directory", prefix: "algo/X-platform-test", expected: "algo/X-platform-test/"},
|
||
{name: "trim slashes", prefix: "/algo/X-platform-test//", expected: "algo/X-platform-test/"},
|
||
}
|
||
|
||
for _, test := range tests {
|
||
t.Run(test.name, func(t *testing.T) {
|
||
requestPrefix := storageObjectRequestPrefix(test.prefix)
|
||
if requestPrefix != test.expected {
|
||
t.Fatalf("request prefix = %q, want %q", requestPrefix, test.expected)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestArtifactContentMimeTypeKnowsRoboticsAndModelExtensions(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
expected string
|
||
}{
|
||
{name: "scene.gltf", expected: "model/gltf+json"},
|
||
{name: "scene.glb", expected: "model/gltf-binary"},
|
||
{name: "scene.obj", expected: "model/obj"},
|
||
{name: "episodes.parquet", expected: "application/vnd.apache.parquet"},
|
||
{name: "recording.mcap", expected: "application/mcap"},
|
||
{name: "trajectory.db3", expected: "application/vnd.sqlite3"},
|
||
{name: "calibration.urdf", expected: "application/xml"},
|
||
{name: "policy.onnx", expected: "application/x-onnx"},
|
||
{name: "episode.rrd", expected: "application/vnd.rerun.rrd"},
|
||
}
|
||
|
||
for _, test := range tests {
|
||
t.Run(test.name, func(t *testing.T) {
|
||
got := artifactContentMimeType(domain.ExecutionArtifact{Name: test.name}, test.name, nil)
|
||
if got != test.expected {
|
||
t.Fatalf("mime type = %q, want %q", got, test.expected)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestArtifactContentMimeTypePrefersKnownExtensionOverGenericMetadata(t *testing.T) {
|
||
got := artifactContentMimeType(
|
||
domain.ExecutionArtifact{
|
||
Name: "scene.obj",
|
||
Metadata: &domain.ArtifactMetadata{MimeType: "application/octet-stream"},
|
||
},
|
||
"scene.obj",
|
||
nil,
|
||
)
|
||
if got != "model/obj" {
|
||
t.Fatalf("mime type = %q, want model/obj", got)
|
||
}
|
||
}
|
||
|
||
func TestAppendStorageObjectItemsSkipsCurrentPrefixAndDedupesPages(t *testing.T) {
|
||
collected := make(map[string]struct{})
|
||
appendStorageObjectItems(
|
||
collected,
|
||
&bosapi.ListObjectsResult{
|
||
CommonPrefixes: []bosapi.PrefixType{
|
||
{Prefix: "algo/"},
|
||
{Prefix: "algo/X-platform-test/"},
|
||
},
|
||
Contents: []bosapi.ObjectSummaryType{
|
||
{Key: "algo/"},
|
||
{Key: "algo/manifest.json"},
|
||
},
|
||
},
|
||
"algo",
|
||
"algo/",
|
||
)
|
||
appendStorageObjectItems(
|
||
collected,
|
||
&bosapi.ListObjectsResult{
|
||
CommonPrefixes: []bosapi.PrefixType{
|
||
{Prefix: "algo/X-platform-test/"},
|
||
{Prefix: "algo/raw/"},
|
||
},
|
||
Contents: []bosapi.ObjectSummaryType{
|
||
{Key: "algo/manifest.json"},
|
||
{Key: "algo/video.mp4"},
|
||
},
|
||
},
|
||
"algo",
|
||
"algo/",
|
||
)
|
||
|
||
items := make([]string, 0, len(collected))
|
||
for item := range collected {
|
||
items = append(items, item)
|
||
}
|
||
sort.Strings(items)
|
||
|
||
expected := []string{
|
||
"algo/X-platform-test/",
|
||
"algo/manifest.json",
|
||
"algo/raw/",
|
||
"algo/video.mp4",
|
||
}
|
||
if strings.Join(items, "|") != strings.Join(expected, "|") {
|
||
t.Fatalf("items = %#v, want %#v", items, expected)
|
||
}
|
||
}
|
||
|
||
func TestAPITokenAuthProtectsBusinessEndpointsWhenConfigured(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "test-token")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
healthRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(healthRecorder, httptest.NewRequest(http.MethodGet, "/api/health", nil))
|
||
if healthRecorder.Code != http.StatusOK {
|
||
t.Fatalf("expected health endpoint to remain public, got %d", healthRecorder.Code)
|
||
}
|
||
|
||
unauthorizedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
||
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected dashboard without token to be rejected, got %d", unauthorizedRecorder.Code)
|
||
}
|
||
|
||
authorizedRequest := httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)
|
||
authorizedRequest.Header.Set("Authorization", "Bearer test-token")
|
||
authorizedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(authorizedRecorder, authorizedRequest)
|
||
if authorizedRecorder.Code != http.StatusOK {
|
||
t.Fatalf("expected dashboard with token to succeed, got %d body=%s", authorizedRecorder.Code, authorizedRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestJSONResponsesUseGzipWhenTheClientSupportsIt(t *testing.T) {
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
request := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
||
request.Header.Set("Accept-Encoding", "gzip")
|
||
recorder := httptest.NewRecorder()
|
||
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("health = %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if got := recorder.Header().Get("Content-Encoding"); got != "gzip" {
|
||
t.Fatalf("content encoding = %q, want gzip", got)
|
||
}
|
||
reader, err := gzip.NewReader(recorder.Body)
|
||
if err != nil {
|
||
t.Fatalf("open gzip response: %v", err)
|
||
}
|
||
defer reader.Close()
|
||
payload, err := io.ReadAll(reader)
|
||
if err != nil {
|
||
t.Fatalf("read gzip response: %v", err)
|
||
}
|
||
if !bytes.Contains(payload, []byte(`"status":"ok"`)) {
|
||
t.Fatalf("unexpected decompressed payload: %s", payload)
|
||
}
|
||
}
|
||
|
||
func TestLivePodLogEntriesKeepKubernetesTimestampsStable(t *testing.T) {
|
||
tail := "2026-07-10T10:20:30.123456789Z first line\nsecond line\n"
|
||
entries := livePodLogEntries(tail, "Train", "2026-07-10T10:00:00Z")
|
||
if len(entries) != 2 {
|
||
t.Fatalf("entries = %#v", entries)
|
||
}
|
||
if entries[0].Timestamp != "2026-07-10T10:20:30.123456789Z" || entries[0].Message != "first line" {
|
||
t.Fatalf("timestamped entry = %#v", entries[0])
|
||
}
|
||
if entries[1].Timestamp != "2026-07-10T10:00:00Z" || entries[1].Message != "second line" {
|
||
t.Fatalf("fallback entry = %#v", entries[1])
|
||
}
|
||
}
|
||
|
||
func TestNewHandlerReportsInvalidConfiguration(t *testing.T) {
|
||
t.Setenv("PORT", "invalid")
|
||
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/health", nil))
|
||
|
||
if recorder.Code != http.StatusInternalServerError {
|
||
t.Fatalf("expected invalid configuration to return 500, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "PORT must be an integer") {
|
||
t.Fatalf("expected invalid PORT error, got %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func sessionCookieFromRecorder(t *testing.T, recorder *httptest.ResponseRecorder) *http.Cookie {
|
||
t.Helper()
|
||
cookies := recorder.Result().Cookies()
|
||
for _, cookie := range cookies {
|
||
if cookie.Name == sessionCookieName {
|
||
if !cookie.HttpOnly {
|
||
t.Fatalf("expected HttpOnly session cookie, got %#v", cookie)
|
||
}
|
||
return cookie
|
||
}
|
||
}
|
||
t.Fatalf("expected session cookie, got %#v", cookies)
|
||
return nil
|
||
}
|
||
|
||
func serveJSONWithCookie(handler http.Handler, method string, path string, body []byte, cookie *http.Cookie) *httptest.ResponseRecorder {
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(method, path, bytes.NewReader(body))
|
||
if cookie != nil {
|
||
request.AddCookie(cookie)
|
||
if isCSRFProtectedMethod(method) && !isCSRFExemptEndpoint(request) {
|
||
request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: "test-csrf-token"})
|
||
request.Header.Set(csrfHeaderName, "test-csrf-token")
|
||
}
|
||
}
|
||
handler.ServeHTTP(recorder, request)
|
||
return recorder
|
||
}
|
||
|
||
func newHandlerWithWorkerAPIBaseURL(t *testing.T, workerURL string) http.Handler {
|
||
t.Helper()
|
||
appConfig, err := config.LoadValidated()
|
||
if err != nil {
|
||
t.Fatalf("load test config: %v", err)
|
||
}
|
||
appConfig.WorkerAPIBaseURL = workerURL
|
||
return NewHandlerWithConfig(store.NewMemoryStore(), nil, appConfig)
|
||
}
|
||
|
||
func TestResolveScanConfigUsesConfiguredGlobalBOSEndpointForConnectionWithoutEndpoint(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 全局 endpoint 连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
AccessKeyID: "ak-api-default",
|
||
SecretAccessKey: "sk-api-default",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
server := &server{
|
||
store: dataStore,
|
||
globalBOSEndpoint: "https://gz.bcebos.com",
|
||
}
|
||
|
||
resolved, err := server.resolveScanConfig(ctx, &domain.ScanConfig{
|
||
DataSource: "BOS",
|
||
ConnectionID: connection.ID,
|
||
Bucket: "datastorage",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("resolve scan config: %v", err)
|
||
}
|
||
if resolved == nil {
|
||
t.Fatalf("expected resolved scan config")
|
||
}
|
||
if resolved.Endpoint != "https://gz.bcebos.com" {
|
||
t.Fatalf("expected configured global bos endpoint, got %q", resolved.Endpoint)
|
||
}
|
||
if resolved.AccessKeyID != "ak-api-default" || resolved.SecretAccessKey != "sk-api-default" {
|
||
t.Fatalf("expected resolved connection secrets, got %+v", resolved)
|
||
}
|
||
}
|
||
|
||
func TestResolveScanConfigKeepsEmptyEndpointWithoutGlobalBOSFallback(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 无 endpoint 连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
AccessKeyID: "ak-api-empty-endpoint",
|
||
SecretAccessKey: "sk-api-empty-endpoint",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
server := &server{store: dataStore}
|
||
|
||
resolved, err := server.resolveScanConfig(ctx, &domain.ScanConfig{
|
||
DataSource: "BOS",
|
||
ConnectionID: connection.ID,
|
||
Bucket: "datastorage",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("resolve scan config: %v", err)
|
||
}
|
||
if resolved == nil {
|
||
t.Fatalf("expected resolved scan config")
|
||
}
|
||
if resolved.Endpoint != "" {
|
||
t.Fatalf("expected empty endpoint without global fallback, got %q", resolved.Endpoint)
|
||
}
|
||
}
|
||
|
||
func bootstrapSuperAdminSession(t *testing.T, handler http.Handler) *http.Cookie {
|
||
t.Helper()
|
||
body := []byte(`{"name":"Root Admin","email":"root@example.com","password":"correct-horse-battery-staple"}`)
|
||
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/auth/bootstrap-super-admin", body, nil)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("bootstrap super admin = %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
return sessionCookieFromRecorder(t, recorder)
|
||
}
|
||
|
||
func loginSession(t *testing.T, handler http.Handler, email string, password string) *http.Cookie {
|
||
t.Helper()
|
||
body, err := json.Marshal(map[string]string{"email": email, "password": password})
|
||
if err != nil {
|
||
t.Fatalf("marshal login payload: %v", err)
|
||
}
|
||
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/auth/login", body, nil)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("login %s = %d body=%s", email, recorder.Code, recorder.Body.String())
|
||
}
|
||
return sessionCookieFromRecorder(t, recorder)
|
||
}
|
||
|
||
func adminCreateOrganization(t *testing.T, handler http.Handler, cookie *http.Cookie, name string, slug string) domain.OrganizationRecord {
|
||
t.Helper()
|
||
body, err := json.Marshal(map[string]string{"name": name, "slug": slug})
|
||
if err != nil {
|
||
t.Fatalf("marshal organization payload: %v", err)
|
||
}
|
||
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/admin/organizations", body, cookie)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create organization %s = %d body=%s", name, recorder.Code, recorder.Body.String())
|
||
}
|
||
var item domain.OrganizationRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &item); err != nil {
|
||
t.Fatalf("decode organization: %v", err)
|
||
}
|
||
return item
|
||
}
|
||
|
||
func adminCreateUser(t *testing.T, handler http.Handler, cookie *http.Cookie, organizationID string, name string, email string, role domain.UserRole, password string) domain.UserRecord {
|
||
t.Helper()
|
||
body, err := json.Marshal(map[string]any{
|
||
"name": name,
|
||
"email": email,
|
||
"role": role,
|
||
"password": password,
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal user payload: %v", err)
|
||
}
|
||
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/admin/organizations/"+organizationID+"/users", body, cookie)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create user %s = %d body=%s", email, recorder.Code, recorder.Body.String())
|
||
}
|
||
var item domain.UserRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &item); err != nil {
|
||
t.Fatalf("decode user: %v", err)
|
||
}
|
||
if item.PasswordHash != "" {
|
||
t.Fatalf("admin user response leaked password hash: %s", recorder.Body.String())
|
||
}
|
||
return item
|
||
}
|
||
|
||
func TestSessionAuthBootstrapLoginAndBusinessGuard(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
unauthorizedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
||
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected dashboard without session to be rejected, got %d", unauthorizedRecorder.Code)
|
||
}
|
||
|
||
statusRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(statusRecorder, httptest.NewRequest(http.MethodGet, "/api/auth/bootstrap-status", nil))
|
||
if statusRecorder.Code != http.StatusOK {
|
||
t.Fatalf("bootstrap status = %d body=%s", statusRecorder.Code, statusRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(statusRecorder.Body.String(), `"bootstrapRequired":true`) {
|
||
t.Fatalf("expected bootstrap to be required, got %s", statusRecorder.Body.String())
|
||
}
|
||
|
||
blankPasswordBootstrapRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
blankPasswordBootstrapRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodPost,
|
||
"/api/auth/bootstrap-super-admin",
|
||
bytes.NewReader([]byte(`{"name":"Blank Password","email":"blank@example.com","password":" "}`)),
|
||
),
|
||
)
|
||
if blankPasswordBootstrapRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected whitespace bootstrap password to fail, got %d body=%s", blankPasswordBootstrapRecorder.Code, blankPasswordBootstrapRecorder.Body.String())
|
||
}
|
||
|
||
bootstrapBody := []byte(`{"name":"Root Admin","email":"root@example.com","password":"correct-horse-battery-staple"}`)
|
||
bootstrapRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(bootstrapRecorder, httptest.NewRequest(http.MethodPost, "/api/auth/bootstrap-super-admin", bytes.NewReader(bootstrapBody)))
|
||
if bootstrapRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("bootstrap super admin = %d body=%s", bootstrapRecorder.Code, bootstrapRecorder.Body.String())
|
||
}
|
||
if strings.Contains(bootstrapRecorder.Body.String(), "passwordHash") {
|
||
t.Fatalf("bootstrap response leaked password hash: %s", bootstrapRecorder.Body.String())
|
||
}
|
||
bootstrapCookie := sessionCookieFromRecorder(t, bootstrapRecorder)
|
||
|
||
bootstrapMeRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
||
bootstrapMeRequest.AddCookie(bootstrapCookie)
|
||
bootstrapMeRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(bootstrapMeRecorder, bootstrapMeRequest)
|
||
if bootstrapMeRecorder.Code != http.StatusOK {
|
||
t.Fatalf("me after bootstrap = %d body=%s", bootstrapMeRecorder.Code, bootstrapMeRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(bootstrapMeRecorder.Body.String(), `"isSuperAdmin":true`) {
|
||
t.Fatalf("expected super admin in bootstrap me response, got %s", bootstrapMeRecorder.Body.String())
|
||
}
|
||
|
||
loginBody := []byte(`{"email":"root@example.com","password":"correct-horse-battery-staple"}`)
|
||
loginRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(loginRecorder, httptest.NewRequest(http.MethodPost, "/api/auth/login", bytes.NewReader(loginBody)))
|
||
if loginRecorder.Code != http.StatusOK {
|
||
t.Fatalf("login = %d body=%s", loginRecorder.Code, loginRecorder.Body.String())
|
||
}
|
||
sessionCookie := sessionCookieFromRecorder(t, loginRecorder)
|
||
|
||
meRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
||
meRequest.AddCookie(sessionCookie)
|
||
meRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(meRecorder, meRequest)
|
||
if meRecorder.Code != http.StatusOK {
|
||
t.Fatalf("me = %d body=%s", meRecorder.Code, meRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(meRecorder.Body.String(), `"isSuperAdmin":true`) {
|
||
t.Fatalf("expected super admin in me response, got %s", meRecorder.Body.String())
|
||
}
|
||
|
||
businessRequest := httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)
|
||
businessRequest.AddCookie(sessionCookie)
|
||
businessRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(businessRecorder, businessRequest)
|
||
if businessRecorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected super admin business access to be forbidden, got %d body=%s", businessRecorder.Code, businessRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestAuditTasksRequireReviewRolesAndAllowComplianceAuditorDecision(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
|
||
org := adminCreateOrganization(t, handler, rootCookie, "Audit Tenant", "audit-tenant")
|
||
admin := adminCreateUser(t, handler, rootCookie, org.ID, "Audit Admin", "audit-admin@example.com", domain.UserRoleAdmin, "audit-admin-pass")
|
||
pipeline := adminCreateUser(t, handler, rootCookie, org.ID, "Pipeline User", "audit-pipeline@example.com", domain.UserRolePipelineEngineer, "audit-pipeline-pass")
|
||
dataOperator := adminCreateUser(t, handler, rootCookie, org.ID, "Data User", "audit-data@example.com", domain.UserRoleDataOperator, "audit-data-pass")
|
||
qa := adminCreateUser(t, handler, rootCookie, org.ID, "QA User", "audit-qa@example.com", domain.UserRoleQAReviewer, "audit-qa-pass")
|
||
compliance := adminCreateUser(t, handler, rootCookie, org.ID, "Compliance User", "audit-compliance@example.com", domain.UserRoleComplianceAuditor, "audit-compliance-pass")
|
||
|
||
task := domain.AuditTaskRecord{
|
||
ID: domain.NewID("audit"),
|
||
OrganizationID: org.ID,
|
||
RunID: "run-audit-role-test",
|
||
WorkflowID: "workflow-audit-role-test",
|
||
ProjectID: "project-audit-role-test",
|
||
NodeID: "review",
|
||
WorkflowName: "人工审核权限验证",
|
||
ProjectName: "审核项目",
|
||
OperatorName: "人工审核",
|
||
CreatedAt: nowISO(),
|
||
UpdatedAt: nowISO(),
|
||
Status: domain.AuditStatusPending,
|
||
Data: domain.AuditTaskPayload{
|
||
Reason: "role verification",
|
||
},
|
||
}
|
||
if _, err := dataStore.CreateAuditTask(context.Background(), task); err != nil {
|
||
t.Fatalf("create audit task: %v", err)
|
||
}
|
||
|
||
for _, test := range []struct {
|
||
name string
|
||
user domain.UserRecord
|
||
password string
|
||
wantStatus int
|
||
}{
|
||
{name: "admin", user: admin, password: "audit-admin-pass", wantStatus: http.StatusOK},
|
||
{name: "qa reviewer", user: qa, password: "audit-qa-pass", wantStatus: http.StatusOK},
|
||
{name: "compliance auditor", user: compliance, password: "audit-compliance-pass", wantStatus: http.StatusOK},
|
||
{name: "pipeline engineer", user: pipeline, password: "audit-pipeline-pass", wantStatus: http.StatusForbidden},
|
||
{name: "data operator", user: dataOperator, password: "audit-data-pass", wantStatus: http.StatusForbidden},
|
||
} {
|
||
t.Run(test.name, func(t *testing.T) {
|
||
cookie := loginSession(t, handler, test.user.Email, test.password)
|
||
recorder := serveJSONWithCookie(handler, http.MethodGet, "/api/audit-tasks?status=all", nil, cookie)
|
||
if recorder.Code != test.wantStatus {
|
||
t.Fatalf("list audit tasks = %d, want %d body=%s", recorder.Code, test.wantStatus, recorder.Body.String())
|
||
}
|
||
if test.wantStatus == http.StatusOK && !strings.Contains(recorder.Body.String(), task.ID) {
|
||
t.Fatalf("expected audit task %s in response, got %s", task.ID, recorder.Body.String())
|
||
}
|
||
})
|
||
}
|
||
|
||
complianceCookie := loginSession(t, handler, compliance.Email, "audit-compliance-pass")
|
||
approveRecorder := serveJSONWithCookie(handler, http.MethodPost, "/api/audit-tasks/"+task.ID+"/approve", nil, complianceCookie)
|
||
if approveRecorder.Code != http.StatusOK {
|
||
t.Fatalf("compliance auditor approve = %d body=%s", approveRecorder.Code, approveRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(approveRecorder.Body.String(), `"status":"approved"`) {
|
||
t.Fatalf("expected approved task, got %s", approveRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestAuthMeUsesStoredUserContextInPublicPreviewMode(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
organization, err := dataStore.EnsureDefaultOrganization(ctx)
|
||
if err != nil {
|
||
t.Fatalf("ensure default organization: %v", err)
|
||
}
|
||
if _, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
||
OrganizationID: organization.ID,
|
||
Name: "Root Admin",
|
||
Email: "root-public@example.com",
|
||
Role: domain.UserRoleAdmin,
|
||
IsSuperAdmin: true,
|
||
Status: domain.UserStatusActive,
|
||
}); err != nil {
|
||
t.Fatalf("create super admin: %v", err)
|
||
}
|
||
if _, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
||
OrganizationID: organization.ID,
|
||
Name: "POC Operator",
|
||
Email: "poc-operator@example.com",
|
||
Role: domain.UserRoleDataOperator,
|
||
Status: domain.UserStatusActive,
|
||
}); err != nil {
|
||
t.Fatalf("create operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/auth/me", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("public preview me = %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), `"email":"poc-operator@example.com"`) {
|
||
t.Fatalf("expected public preview to prefer business user, got %s", recorder.Body.String())
|
||
}
|
||
if strings.Contains(recorder.Body.String(), "passwordHash") {
|
||
t.Fatalf("public preview me leaked password hash: %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestAuthMeStillRequiresCookieWhenSessionAuthIsEnabled(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
organization, err := dataStore.EnsureDefaultOrganization(ctx)
|
||
if err != nil {
|
||
t.Fatalf("ensure default organization: %v", err)
|
||
}
|
||
if _, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
||
OrganizationID: organization.ID,
|
||
Name: "POC Operator",
|
||
Email: "poc-auth-required@example.com",
|
||
Role: domain.UserRoleDataOperator,
|
||
Status: domain.UserStatusActive,
|
||
}); err != nil {
|
||
t.Fatalf("create operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/auth/me", nil))
|
||
if recorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected auth me to require login with session auth enabled, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestSecureSessionCookieAndSharedTokenBrowserBypass(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-token")
|
||
t.Setenv("DROBOTICFLOW_SESSION_COOKIE_SECURE", "true")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
if !rootCookie.Secure {
|
||
t.Fatalf("expected secure bootstrap session cookie, got %#v", rootCookie)
|
||
}
|
||
org := adminCreateOrganization(t, handler, rootCookie, "Browser Tenant", "browser-tenant")
|
||
admin := adminCreateUser(t, handler, rootCookie, org.ID, "Browser Admin", "browser-admin@example.com", domain.UserRoleAdmin, "browser-pass")
|
||
|
||
adminCookie := loginSession(t, handler, admin.Email, "browser-pass")
|
||
if !adminCookie.Secure {
|
||
t.Fatalf("expected secure login session cookie, got %#v", adminCookie)
|
||
}
|
||
missingCSRFRecorder := httptest.NewRecorder()
|
||
missingCSRFRequest := httptest.NewRequest(
|
||
http.MethodPost,
|
||
"/api/projects",
|
||
bytes.NewReader([]byte(`{"name":"Missing CSRF","description":"should fail"}`)),
|
||
)
|
||
missingCSRFRequest.AddCookie(adminCookie)
|
||
handler.ServeHTTP(missingCSRFRecorder, missingCSRFRequest)
|
||
if missingCSRFRecorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected missing csrf token to be rejected, got %d body=%s", missingCSRFRecorder.Code, missingCSRFRecorder.Body.String())
|
||
}
|
||
projectRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/projects",
|
||
[]byte(`{"name":"Cookie Project","description":"created without embedded API token"}`),
|
||
adminCookie,
|
||
)
|
||
if projectRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("expected session cookie to bypass shared API token, got %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestAdminOrganizationSlugValidation(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
|
||
invalidCreateSlugs := []string{
|
||
"bad slug",
|
||
"bad/slug",
|
||
"-tenant",
|
||
"tenant-",
|
||
strings.Repeat("a", 64),
|
||
"租户",
|
||
}
|
||
for _, slug := range invalidCreateSlugs {
|
||
recorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/admin/organizations",
|
||
[]byte(fmt.Sprintf(`{"name":"Invalid Tenant","slug":%q}`, slug)),
|
||
rootCookie,
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid organization slug %q rejected, got %d body=%s", slug, recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "slug") {
|
||
t.Fatalf("expected slug validation issue for %q, body=%s", slug, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
org := adminCreateOrganization(t, handler, rootCookie, "Slug Tenant", "Slug-Tenant")
|
||
if org.Slug != "slug-tenant" {
|
||
t.Fatalf("expected organization slug normalized to lowercase, got %#v", org)
|
||
}
|
||
|
||
duplicateCreateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/admin/organizations",
|
||
[]byte(`{"name":"Duplicate Slug","slug":"slug-tenant"}`),
|
||
rootCookie,
|
||
)
|
||
if duplicateCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate organization slug rejected, got %d body=%s", duplicateCreateRecorder.Code, duplicateCreateRecorder.Body.String())
|
||
}
|
||
|
||
invalidUpdateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/admin/organizations/"+org.ID,
|
||
[]byte(`{"slug":"invalid/tenant"}`),
|
||
rootCookie,
|
||
)
|
||
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid organization slug update rejected, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
||
}
|
||
stored, err := dataStore.GetOrganization(context.Background(), org.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload organization after invalid slug update: %v", err)
|
||
}
|
||
if stored == nil || stored.Slug != "slug-tenant" {
|
||
t.Fatalf("expected invalid slug update not to persist, got %#v", stored)
|
||
}
|
||
|
||
other := adminCreateOrganization(t, handler, rootCookie, "Other Tenant", "other-tenant")
|
||
duplicateUpdateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/admin/organizations/"+other.ID,
|
||
[]byte(`{"slug":"slug-tenant"}`),
|
||
rootCookie,
|
||
)
|
||
if duplicateUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate organization slug update rejected, got %d body=%s", duplicateUpdateRecorder.Code, duplicateUpdateRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestUserEmailUniquenessValidation(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
org := adminCreateOrganization(t, handler, rootCookie, "Email Tenant", "email-tenant")
|
||
|
||
admin := adminCreateUser(t, handler, rootCookie, org.ID, "Email Admin", "email-admin@example.com", domain.UserRoleAdmin, "admin-pass")
|
||
operator := adminCreateUser(t, handler, rootCookie, org.ID, "Email Operator", "email-operator@example.com", domain.UserRoleOperator, "operator-pass")
|
||
|
||
duplicateAdminCreateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/admin/organizations/"+org.ID+"/users",
|
||
[]byte(`{"name":"Duplicate Admin","email":"EMAIL-ADMIN@example.com","role":"operator","password":"operator-pass"}`),
|
||
rootCookie,
|
||
)
|
||
if duplicateAdminCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate admin user create email rejected, got %d body=%s", duplicateAdminCreateRecorder.Code, duplicateAdminCreateRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(duplicateAdminCreateRecorder.Body.String(), "email") {
|
||
t.Fatalf("expected email validation issue, body=%s", duplicateAdminCreateRecorder.Body.String())
|
||
}
|
||
|
||
duplicateAdminUpdateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/admin/users/"+operator.ID,
|
||
[]byte(`{"email":"email-admin@example.com"}`),
|
||
rootCookie,
|
||
)
|
||
if duplicateAdminUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate admin user update email rejected, got %d body=%s", duplicateAdminUpdateRecorder.Code, duplicateAdminUpdateRecorder.Body.String())
|
||
}
|
||
storedOperator, err := dataStore.GetUser(context.Background(), operator.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload operator after invalid email update: %v", err)
|
||
}
|
||
if storedOperator == nil || storedOperator.Email != "email-operator@example.com" {
|
||
t.Fatalf("expected duplicate email update not to persist, got %#v", storedOperator)
|
||
}
|
||
|
||
operatorCookie := loginSession(t, handler, operator.Email, "operator-pass")
|
||
duplicateProfileRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/me",
|
||
[]byte(`{"email":"email-admin@example.com"}`),
|
||
operatorCookie,
|
||
)
|
||
if duplicateProfileRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate profile email rejected, got %d body=%s", duplicateProfileRecorder.Code, duplicateProfileRecorder.Body.String())
|
||
}
|
||
|
||
adminCookie := loginSession(t, handler, admin.Email, "admin-pass")
|
||
duplicateBusinessCreateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/users",
|
||
[]byte(`{"name":"Duplicate Business User","email":"email-operator@example.com","role":"operator","password":"business-pass"}`),
|
||
adminCookie,
|
||
)
|
||
if duplicateBusinessCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate business user create email rejected, got %d body=%s", duplicateBusinessCreateRecorder.Code, duplicateBusinessCreateRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestStoreDuplicateMutationErrorsRenderValidationResponses(t *testing.T) {
|
||
emailRecorder := httptest.NewRecorder()
|
||
renderUserMutationError(emailRecorder, store.ErrDuplicateUserEmail)
|
||
if emailRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate email error to render 400, got %d body=%s", emailRecorder.Code, emailRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(emailRecorder.Body.String(), `"field":"email"`) || !strings.Contains(emailRecorder.Body.String(), "email must be unique") {
|
||
t.Fatalf("expected duplicate email validation issue, got %s", emailRecorder.Body.String())
|
||
}
|
||
|
||
slugRecorder := httptest.NewRecorder()
|
||
renderOrganizationMutationError(slugRecorder, store.ErrDuplicateOrganizationSlug)
|
||
if slugRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected duplicate slug error to render 400, got %d body=%s", slugRecorder.Code, slugRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(slugRecorder.Body.String(), `"field":"slug"`) || !strings.Contains(slugRecorder.Body.String(), "slug must be unique") {
|
||
t.Fatalf("expected duplicate slug validation issue, got %s", slugRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestSessionLogoutRequiresCSRF(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
sessionCookie := bootstrapSuperAdminSession(t, handler)
|
||
|
||
missingCSRFRecorder := httptest.NewRecorder()
|
||
missingCSRFRequest := httptest.NewRequest(http.MethodPost, "/api/auth/logout", nil)
|
||
missingCSRFRequest.AddCookie(sessionCookie)
|
||
handler.ServeHTTP(missingCSRFRecorder, missingCSRFRequest)
|
||
if missingCSRFRecorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected logout without csrf token to be rejected, got %d body=%s", missingCSRFRecorder.Code, missingCSRFRecorder.Body.String())
|
||
}
|
||
|
||
meRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
||
meRequest.AddCookie(sessionCookie)
|
||
meRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(meRecorder, meRequest)
|
||
if meRecorder.Code != http.StatusOK {
|
||
t.Fatalf("expected session to survive rejected logout, got %d body=%s", meRecorder.Code, meRecorder.Body.String())
|
||
}
|
||
|
||
logoutRecorder := serveJSONWithCookie(handler, http.MethodPost, "/api/auth/logout", nil, sessionCookie)
|
||
if logoutRecorder.Code != http.StatusNoContent {
|
||
t.Fatalf("logout with csrf = %d body=%s", logoutRecorder.Code, logoutRecorder.Body.String())
|
||
}
|
||
|
||
loggedOutMeRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
||
loggedOutMeRequest.AddCookie(sessionCookie)
|
||
loggedOutMeRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(loggedOutMeRecorder, loggedOutMeRequest)
|
||
if loggedOutMeRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected logged-out session to be rejected, got %d body=%s", loggedOutMeRecorder.Code, loggedOutMeRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestExternalBusinessWriteBypassesSessionLoginRequirement(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-token")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/external/business-db/documents", bytes.NewReader([]byte(`{}`))))
|
||
if recorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected external write auth to reject missing X-API-Key, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if strings.Contains(recorder.Body.String(), "login is required") {
|
||
t.Fatalf("external write endpoint should not be blocked by session auth: %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestAPITokenCanReadRunMonitoringWhenSessionAuthIsEnabled(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-token")
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Token Read Project", Description: "read"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "Token Read Workflow",
|
||
Description: "read",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
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)
|
||
}
|
||
contentPath := filepath.Join(t.TempDir(), "clip.mp4")
|
||
if err := os.WriteFile(contentPath, []byte("token-media"), 0o600); err != nil {
|
||
t.Fatalf("write token content artifact: %v", err)
|
||
}
|
||
run.Status = domain.RunStatusCompleted
|
||
contentArtifact := domain.ExecutionArtifact{
|
||
ID: "artifact-token-content",
|
||
RunID: run.ID,
|
||
NodeID: "node-token-read",
|
||
Name: "clip.mp4",
|
||
Type: domain.PortTypeVideo,
|
||
Size: "11 B",
|
||
Path: contentPath,
|
||
Metadata: &domain.ArtifactMetadata{MimeType: "video/mp4"},
|
||
}
|
||
manifestArtifact := domain.ExecutionArtifact{
|
||
ID: "artifact-token-manifest", RunID: run.ID, NodeID: "node-token-read", Name: "directory-manifest.json", Type: domain.PortTypeJSON,
|
||
Data: map[string]any{
|
||
"schemaVersion": "droboticflow.directory-read/v1",
|
||
"entries": []any{map[string]any{"path": "pts.csv", "inlineText": "timestamp_us\\n100\\n"}},
|
||
},
|
||
}
|
||
run.NodeExecutions = []domain.NodeExecutionRecord{
|
||
{
|
||
NodeID: "node-token-read",
|
||
NodeLabel: "Token Read Node",
|
||
Status: domain.NodeExecutionStatusCompleted,
|
||
Logs: []domain.LogEntryRecord{{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: "done"}},
|
||
Artifacts: []domain.ExecutionArtifact{{ID: "artifact-token-read", RunID: run.ID, NodeID: "node-token-read", Name: "result.json", Type: domain.PortTypeJSON, Size: "1 KB", Path: "bos://bucket/result.json"}, contentArtifact, manifestArtifact},
|
||
ResourceSeries: []domain.ResourceSample{{Timestamp: "2026-04-13T01:00:00Z", CPU: 1, Memory: 64}},
|
||
},
|
||
}
|
||
run.Artifacts = append([]domain.ExecutionArtifact{}, run.NodeExecutions[0].Artifacts...)
|
||
run.Logs = append([]domain.LogEntryRecord{}, run.NodeExecutions[0].Logs...)
|
||
run.ResourceSeries = append([]domain.ResourceSample{}, run.NodeExecutions[0].ResourceSeries...)
|
||
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
tokenRequest := func(method string, path string, body []byte) *httptest.ResponseRecorder {
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(method, path, bytes.NewReader(body))
|
||
request.Header.Set("X-Droboticflow-API-Token", "shared-token")
|
||
handler.ServeHTTP(recorder, request)
|
||
return recorder
|
||
}
|
||
for _, path := range []string{
|
||
"/api/runs",
|
||
"/api/runs/" + run.ID,
|
||
"/api/runs/" + run.ID + "/logs",
|
||
"/api/runs/" + run.ID + "/resources",
|
||
"/api/runs/" + run.ID + "/artifacts",
|
||
"/api/runs/" + run.ID + "/artifacts/" + contentArtifact.ID + "/content",
|
||
"/api/runs/" + run.ID + "/artifacts/" + manifestArtifact.ID + "/members",
|
||
"/api/runs/" + run.ID + "/lineage",
|
||
"/api/artifacts/" + contentArtifact.ID + "/lineage?runId=" + run.ID,
|
||
"/api/workflows/" + workflow.ID + "/runs",
|
||
} {
|
||
recorder := tokenRequest(http.MethodGet, path, nil)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected API token read %s to succeed, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
membersRecorder := tokenRequest(http.MethodGet, "/api/runs/"+run.ID+"/artifacts/"+manifestArtifact.ID+"/members", nil)
|
||
var members struct {
|
||
Items []runArtifactManifestMember `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(membersRecorder.Body.Bytes(), &members); err != nil || len(members.Items) != 1 {
|
||
t.Fatalf("expected API token member list, err=%v body=%s", err, membersRecorder.Body.String())
|
||
}
|
||
memberContentRecorder := tokenRequest(http.MethodGet, "/api/runs/"+run.ID+"/artifacts/"+manifestArtifact.ID+"/members/"+members.Items[0].ID+"/content", nil)
|
||
if memberContentRecorder.Code != http.StatusOK || memberContentRecorder.Body.String() != "timestamp_us\\n100\\n" {
|
||
t.Fatalf("expected API token member content, got %d body=%q", memberContentRecorder.Code, memberContentRecorder.Body.String())
|
||
}
|
||
|
||
settingsRecorder := tokenRequest(http.MethodGet, "/api/settings", nil)
|
||
if settingsRecorder.Code != http.StatusUnauthorized || !strings.Contains(settingsRecorder.Body.String(), "login is required") {
|
||
t.Fatalf("expected settings read to still require login, got %d body=%s", settingsRecorder.Code, settingsRecorder.Body.String())
|
||
}
|
||
stopRecorder := tokenRequest(http.MethodPost, "/api/runs/"+run.ID+"/stop", nil)
|
||
if stopRecorder.Code != http.StatusUnauthorized || !strings.Contains(stopRecorder.Body.String(), "login is required") {
|
||
t.Fatalf("expected run stop to still require login, got %d body=%s", stopRecorder.Code, stopRecorder.Body.String())
|
||
}
|
||
projectRecorder := tokenRequest(http.MethodPost, "/api/projects", []byte(`{"name":"Token Write Project"}`))
|
||
if projectRecorder.Code != http.StatusUnauthorized || !strings.Contains(projectRecorder.Body.String(), "login is required") {
|
||
t.Fatalf("expected project write to still require login, got %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestSessionAuthTenantIsolationAndRolePermissions(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
|
||
orgA := adminCreateOrganization(t, handler, rootCookie, "Tenant A", "tenant-a")
|
||
orgB := adminCreateOrganization(t, handler, rootCookie, "Tenant B", "tenant-b")
|
||
blankPasswordAdminUserRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/admin/organizations/"+orgA.ID+"/users",
|
||
[]byte(`{"name":"Blank Password User","email":"blank-password-user@example.com","role":"operator","password":" "}`),
|
||
rootCookie,
|
||
)
|
||
if blankPasswordAdminUserRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected admin user create with whitespace password to fail, got %d body=%s", blankPasswordAdminUserRecorder.Code, blankPasswordAdminUserRecorder.Body.String())
|
||
}
|
||
adminA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Admin A", "admin-a@example.com", domain.UserRoleAdmin, "admin-a-pass")
|
||
adminB := adminCreateUser(t, handler, rootCookie, orgB.ID, "Admin B", "admin-b@example.com", domain.UserRoleAdmin, "admin-b-pass")
|
||
operatorA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Operator A", "operator-a@example.com", domain.UserRoleOperator, "operator-a-pass")
|
||
auditorA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Auditor A", "auditor-a@example.com", domain.UserRoleAuditor, "auditor-a-pass")
|
||
|
||
if adminA.OrganizationID != orgA.ID || adminB.OrganizationID != orgB.ID || operatorA.OrganizationID != orgA.ID || auditorA.OrganizationID != orgA.ID {
|
||
t.Fatalf("created users have wrong organizations: adminA=%s adminB=%s auditorA=%s", adminA.OrganizationID, adminB.OrganizationID, auditorA.OrganizationID)
|
||
}
|
||
|
||
adminACookie := loginSession(t, handler, adminA.Email, "admin-a-pass")
|
||
projectRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/projects",
|
||
[]byte(`{"name":"Tenant A Project","description":"owned by tenant A","organizationId":"org_wrong"}`),
|
||
adminACookie,
|
||
)
|
||
if projectRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("tenant A project create = %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
||
}
|
||
var createdProject domain.ProjectRecord
|
||
if err := json.Unmarshal(projectRecorder.Body.Bytes(), &createdProject); err != nil {
|
||
t.Fatalf("decode project: %v", err)
|
||
}
|
||
if createdProject.OrganizationID != orgA.ID {
|
||
t.Fatalf("expected created project tenant %s, got %s", orgA.ID, createdProject.OrganizationID)
|
||
}
|
||
blankProjectNameRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/projects/"+createdProject.ID,
|
||
[]byte(`{"name":" "}`),
|
||
adminACookie,
|
||
)
|
||
if blankProjectNameRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected project update with whitespace name to fail, got %d body=%s", blankProjectNameRecorder.Code, blankProjectNameRecorder.Body.String())
|
||
}
|
||
invalidProjectStatusRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/projects/"+createdProject.ID,
|
||
[]byte(`{"status":"paused"}`),
|
||
adminACookie,
|
||
)
|
||
if invalidProjectStatusRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected project update with invalid status to fail, got %d body=%s", invalidProjectStatusRecorder.Code, invalidProjectStatusRecorder.Body.String())
|
||
}
|
||
storedProject, err := dataStore.GetProject(context.Background(), createdProject.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload project after invalid updates: %v", err)
|
||
}
|
||
if storedProject == nil || storedProject.Name != createdProject.Name || storedProject.Status != createdProject.Status {
|
||
t.Fatalf("expected invalid project updates to preserve fields, got %#v", storedProject)
|
||
}
|
||
workflowA, err := dataStore.CreateWorkflow(context.Background(), domain.CreateWorkflowInput{
|
||
OrganizationID: createdProject.OrganizationID,
|
||
ProjectID: createdProject.ID,
|
||
Name: "Tenant A Workflow",
|
||
Description: "owned by tenant A",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("seed tenant A workflow: %v", err)
|
||
}
|
||
runA, err := dataStore.CreateRun(context.Background(), workflowA.ID, domain.RunTriggerManual)
|
||
if err != nil {
|
||
t.Fatalf("seed tenant A run: %v", err)
|
||
}
|
||
|
||
adminBCookie := loginSession(t, handler, adminB.Email, "admin-b-pass")
|
||
foreignGetRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/projects/"+createdProject.ID, nil, adminBCookie)
|
||
if foreignGetRecorder.Code != http.StatusNotFound {
|
||
t.Fatalf("expected tenant B project get to hide tenant A record, got %d body=%s", foreignGetRecorder.Code, foreignGetRecorder.Body.String())
|
||
}
|
||
|
||
foreignListRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/projects", nil, adminBCookie)
|
||
if foreignListRecorder.Code != http.StatusOK {
|
||
t.Fatalf("tenant B project list = %d body=%s", foreignListRecorder.Code, foreignListRecorder.Body.String())
|
||
}
|
||
var projectList struct {
|
||
Items []domain.ProjectRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(foreignListRecorder.Body.Bytes(), &projectList); err != nil {
|
||
t.Fatalf("decode project list: %v", err)
|
||
}
|
||
for _, item := range projectList.Items {
|
||
if item.ID == createdProject.ID {
|
||
t.Fatalf("tenant B project list leaked tenant A project: %#v", projectList.Items)
|
||
}
|
||
}
|
||
|
||
dashboardRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/dashboard", nil, adminBCookie)
|
||
if dashboardRecorder.Code != http.StatusOK {
|
||
t.Fatalf("tenant B dashboard = %d body=%s", dashboardRecorder.Code, dashboardRecorder.Body.String())
|
||
}
|
||
var dashboard struct {
|
||
Projects struct {
|
||
Active int `json:"active"`
|
||
} `json:"projects"`
|
||
}
|
||
if err := json.Unmarshal(dashboardRecorder.Body.Bytes(), &dashboard); err != nil {
|
||
t.Fatalf("decode dashboard: %v", err)
|
||
}
|
||
if dashboard.Projects.Active != 0 {
|
||
t.Fatalf("tenant B dashboard leaked tenant A active project count: %s", dashboardRecorder.Body.String())
|
||
}
|
||
|
||
foreignRunsRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/runs", nil, adminBCookie)
|
||
if foreignRunsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("tenant B run summary list = %d body=%s", foreignRunsRecorder.Code, foreignRunsRecorder.Body.String())
|
||
}
|
||
var runList struct {
|
||
Items []domain.RunSummary `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(foreignRunsRecorder.Body.Bytes(), &runList); err != nil {
|
||
t.Fatalf("decode run summary list: %v", err)
|
||
}
|
||
for _, item := range runList.Items {
|
||
if item.ID == runA.ID {
|
||
t.Fatalf("tenant B run summary list leaked tenant A run: %#v", runList.Items)
|
||
}
|
||
}
|
||
|
||
auditorCookie := loginSession(t, handler, auditorA.Email, "auditor-a-pass")
|
||
auditorProjectRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/projects",
|
||
[]byte(`{"name":"Auditor Project","description":"should fail"}`),
|
||
auditorCookie,
|
||
)
|
||
if auditorProjectRecorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected auditor project create to be forbidden, got %d body=%s", auditorProjectRecorder.Code, auditorProjectRecorder.Body.String())
|
||
}
|
||
for _, path := range []string{"/api/ai/operators/generate", "/api/ai/workflows/generate"} {
|
||
recorder := serveJSONWithCookie(handler, http.MethodPost, path, []byte(`{"description":"generate test"}`), auditorCookie)
|
||
if recorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected auditor POST %s to be forbidden, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
for _, path := range []string{
|
||
"/api/storage/connections",
|
||
"/api/mongo/connections",
|
||
"/api/storage/buckets",
|
||
"/api/storage/objects",
|
||
} {
|
||
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, auditorCookie)
|
||
if recorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected auditor GET %s to be forbidden, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
publicSettingsRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(publicSettingsRecorder, httptest.NewRequest(http.MethodGet, "/api/public/settings", nil))
|
||
if publicSettingsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("public settings = %d body=%s", publicSettingsRecorder.Code, publicSettingsRecorder.Body.String())
|
||
}
|
||
var publicSettings map[string]string
|
||
if err := json.Unmarshal(publicSettingsRecorder.Body.Bytes(), &publicSettings); err != nil {
|
||
t.Fatalf("decode public settings: %v", err)
|
||
}
|
||
if len(publicSettings) != 1 || publicSettings["timezone"] != domain.DefaultWorkflowScheduleTimezone {
|
||
t.Fatalf("expected public settings to expose only default timezone, got %#v", publicSettings)
|
||
}
|
||
|
||
adminUsersRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/users", nil, adminACookie)
|
||
if adminUsersRecorder.Code != http.StatusOK {
|
||
t.Fatalf("admin user list = %d body=%s", adminUsersRecorder.Code, adminUsersRecorder.Body.String())
|
||
}
|
||
|
||
adminSettingsRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/settings", nil, adminACookie)
|
||
if adminSettingsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("admin settings get = %d body=%s", adminSettingsRecorder.Code, adminSettingsRecorder.Body.String())
|
||
}
|
||
|
||
userWithoutPasswordRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/users",
|
||
[]byte(`{"name":"No Password","email":"no-password@example.com","role":"operator"}`),
|
||
adminACookie,
|
||
)
|
||
if userWithoutPasswordRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected session user create without password to fail, got %d body=%s", userWithoutPasswordRecorder.Code, userWithoutPasswordRecorder.Body.String())
|
||
}
|
||
|
||
userWithInvalidStatusRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/users",
|
||
[]byte(`{"name":"Invalid Status","email":"invalid-status@example.com","role":"operator","status":"pending","password":"invalid-status-pass"}`),
|
||
adminACookie,
|
||
)
|
||
if userWithInvalidStatusRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected session user create with invalid status to fail, got %d body=%s", userWithInvalidStatusRecorder.Code, userWithInvalidStatusRecorder.Body.String())
|
||
}
|
||
|
||
userWithPasswordRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/users",
|
||
[]byte(`{"name":"Created Operator","email":"created-operator@example.com","role":"operator","password":"created-operator-pass"}`),
|
||
adminACookie,
|
||
)
|
||
if userWithPasswordRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("session user create with password = %d body=%s", userWithPasswordRecorder.Code, userWithPasswordRecorder.Body.String())
|
||
}
|
||
var createdSessionUser domain.UserRecord
|
||
if err := json.Unmarshal(userWithPasswordRecorder.Body.Bytes(), &createdSessionUser); err != nil {
|
||
t.Fatalf("decode created session user: %v", err)
|
||
}
|
||
if createdSessionUser.PasswordHash != "" {
|
||
t.Fatalf("session user create leaked password hash: %s", userWithPasswordRecorder.Body.String())
|
||
}
|
||
storedSessionUser, err := dataStore.GetUser(context.Background(), createdSessionUser.ID)
|
||
if err != nil {
|
||
t.Fatalf("load created session user: %v", err)
|
||
}
|
||
if storedSessionUser == nil || strings.TrimSpace(storedSessionUser.PasswordHash) == "" {
|
||
t.Fatalf("expected created session user to store password hash, got %#v", storedSessionUser)
|
||
}
|
||
originalCreatedUserPasswordHash := storedSessionUser.PasswordHash
|
||
blankPasswordUpdateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/"+createdSessionUser.ID,
|
||
[]byte(`{"password":" "}`),
|
||
adminACookie,
|
||
)
|
||
if blankPasswordUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected user update with whitespace password to fail, got %d body=%s", blankPasswordUpdateRecorder.Code, blankPasswordUpdateRecorder.Body.String())
|
||
}
|
||
storedSessionUser, err = dataStore.GetUser(context.Background(), createdSessionUser.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload created session user after blank password update: %v", err)
|
||
}
|
||
if storedSessionUser == nil || storedSessionUser.PasswordHash != originalCreatedUserPasswordHash {
|
||
t.Fatalf("expected blank password update to preserve hash, got %#v", storedSessionUser)
|
||
}
|
||
blankNameUpdateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/"+createdSessionUser.ID,
|
||
[]byte(`{"name":" "}`),
|
||
adminACookie,
|
||
)
|
||
if blankNameUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected user update with whitespace name to fail, got %d body=%s", blankNameUpdateRecorder.Code, blankNameUpdateRecorder.Body.String())
|
||
}
|
||
invalidStatusUpdateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/"+createdSessionUser.ID,
|
||
[]byte(`{"status":"pending"}`),
|
||
adminACookie,
|
||
)
|
||
if invalidStatusUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected user update with invalid status to fail, got %d body=%s", invalidStatusUpdateRecorder.Code, invalidStatusUpdateRecorder.Body.String())
|
||
}
|
||
storedSessionUser, err = dataStore.GetUser(context.Background(), createdSessionUser.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload created session user after invalid updates: %v", err)
|
||
}
|
||
if storedSessionUser == nil || storedSessionUser.Name != createdSessionUser.Name || storedSessionUser.Status != createdSessionUser.Status {
|
||
t.Fatalf("expected invalid updates to preserve user fields, got %#v", storedSessionUser)
|
||
}
|
||
_ = loginSession(t, handler, createdSessionUser.Email, "created-operator-pass")
|
||
|
||
operatorCookie := loginSession(t, handler, operatorA.Email, "operator-a-pass")
|
||
for _, path := range []string{"/api/storage/connections", "/api/mongo/connections"} {
|
||
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, operatorCookie)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected operator GET %s to be allowed, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
for _, path := range []string{"/api/storage/buckets", "/api/storage/objects"} {
|
||
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, operatorCookie)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected operator GET %s to reach storage browse validation, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
adminOnlyPaths := []string{
|
||
"/api/users",
|
||
"/api/database",
|
||
"/api/object-storage",
|
||
"/api/object-storage/objects",
|
||
"/api/object-storage/content",
|
||
"/api/settings",
|
||
"/api/external/business-db/tokens",
|
||
}
|
||
for _, path := range adminOnlyPaths {
|
||
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, operatorCookie)
|
||
if recorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected operator GET %s to be forbidden, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
mongoTestRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/mongo/connections/test",
|
||
[]byte(`{"uri":"mongodb://example.invalid:27017","database":"business"}`),
|
||
operatorCookie,
|
||
)
|
||
if mongoTestRecorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected operator mongo connection test to be forbidden, got %d body=%s", mongoTestRecorder.Code, mongoTestRecorder.Body.String())
|
||
}
|
||
|
||
objectStorageCreateRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPost,
|
||
"/api/object-storage/objects",
|
||
[]byte(`{"bucket":"datastorage","key":"blocked.json","content":"{}"}`),
|
||
operatorCookie,
|
||
)
|
||
if objectStorageCreateRecorder.Code != http.StatusForbidden {
|
||
t.Fatalf("expected operator object storage create to be forbidden, got %d body=%s", objectStorageCreateRecorder.Code, objectStorageCreateRecorder.Body.String())
|
||
}
|
||
|
||
profileRecorder := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/me",
|
||
[]byte(`{"name":"Renamed Operator","email":"RENAMED-OPERATOR@example.com","role":"admin"}`),
|
||
operatorCookie,
|
||
)
|
||
if profileRecorder.Code != http.StatusOK {
|
||
t.Fatalf("operator profile update = %d body=%s", profileRecorder.Code, profileRecorder.Body.String())
|
||
}
|
||
var updatedOperator domain.UserRecord
|
||
if err := json.Unmarshal(profileRecorder.Body.Bytes(), &updatedOperator); err != nil {
|
||
t.Fatalf("decode updated operator: %v", err)
|
||
}
|
||
if updatedOperator.Name != "Renamed Operator" || updatedOperator.Email != "renamed-operator@example.com" {
|
||
t.Fatalf("expected profile fields to update, got %#v", updatedOperator)
|
||
}
|
||
if updatedOperator.Role != domain.UserRoleOperator {
|
||
t.Fatalf("expected profile update to preserve operator role, got %s", updatedOperator.Role)
|
||
}
|
||
storedOperator, err := dataStore.GetUser(context.Background(), operatorA.ID)
|
||
if err != nil {
|
||
t.Fatalf("load stored operator: %v", err)
|
||
}
|
||
if storedOperator == nil || storedOperator.Role != domain.UserRoleOperator {
|
||
t.Fatalf("expected stored operator role to remain operator, got %#v", storedOperator)
|
||
}
|
||
}
|
||
|
||
func TestSessionAuthShowsGlobalBuiltinOperatorsForEachTenant(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
|
||
orgA := adminCreateOrganization(t, handler, rootCookie, "Operator Tenant A", "operator-tenant-a")
|
||
orgB := adminCreateOrganization(t, handler, rootCookie, "Operator Tenant B", "operator-tenant-b")
|
||
adminA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Operator Admin A", "operator-admin-a@example.com", domain.UserRoleAdmin, "operator-admin-a-pass")
|
||
adminB := adminCreateUser(t, handler, rootCookie, orgB.ID, "Operator Admin B", "operator-admin-b@example.com", domain.UserRoleAdmin, "operator-admin-b-pass")
|
||
adminACookie := loginSession(t, handler, adminA.Email, "operator-admin-a-pass")
|
||
adminBCookie := loginSession(t, handler, adminB.Email, "operator-admin-b-pass")
|
||
|
||
catalog, err := builtinOperatorRecords("2026-06-23T00:00:00Z", "runner:test")
|
||
if err != nil {
|
||
t.Fatalf("load builtin catalog: %v", err)
|
||
}
|
||
|
||
listARecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators", nil, adminACookie)
|
||
if listARecorder.Code != http.StatusOK {
|
||
t.Fatalf("tenant A operators list = %d body=%s", listARecorder.Code, listARecorder.Body.String())
|
||
}
|
||
var listA struct {
|
||
Items []domain.OperatorRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(listARecorder.Body.Bytes(), &listA); err != nil {
|
||
t.Fatalf("decode tenant A operators: %v", err)
|
||
}
|
||
if got := countOperatorsByType(listA.Items, "builtin"); got != len(catalog) {
|
||
t.Fatalf("tenant A builtin count = %d, want %d", got, len(catalog))
|
||
}
|
||
|
||
customA := createCustomOperatorForTest(t, handler, adminACookie, "Tenant A Custom Operator", "registry.example/operators/a:v1")
|
||
customB := createCustomOperatorForTest(t, handler, adminBCookie, "Tenant B Custom Operator", "registry.example/operators/b:v1")
|
||
|
||
listBRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators", nil, adminBCookie)
|
||
if listBRecorder.Code != http.StatusOK {
|
||
t.Fatalf("tenant B operators list = %d body=%s", listBRecorder.Code, listBRecorder.Body.String())
|
||
}
|
||
var listB struct {
|
||
Items []domain.OperatorRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(listBRecorder.Body.Bytes(), &listB); err != nil {
|
||
t.Fatalf("decode tenant B operators: %v", err)
|
||
}
|
||
if got := countOperatorsByType(listB.Items, "builtin"); got != len(catalog) {
|
||
t.Fatalf("tenant B builtin count = %d, want %d", got, len(catalog))
|
||
}
|
||
if !containsOperatorID(listB.Items, customB.ID) {
|
||
t.Fatalf("tenant B custom operator missing from tenant B list")
|
||
}
|
||
if containsOperatorID(listB.Items, customA.ID) {
|
||
t.Fatalf("tenant B operators leaked tenant A custom operator")
|
||
}
|
||
|
||
builtinGetRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators/"+catalog[0].ID, nil, adminBCookie)
|
||
if builtinGetRecorder.Code != http.StatusOK {
|
||
t.Fatalf("tenant B builtin get = %d body=%s", builtinGetRecorder.Code, builtinGetRecorder.Body.String())
|
||
}
|
||
foreignCustomGetRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators/"+customA.ID, nil, adminBCookie)
|
||
if foreignCustomGetRecorder.Code != http.StatusNotFound {
|
||
t.Fatalf("expected tenant B custom get to hide tenant A operator, got %d body=%s", foreignCustomGetRecorder.Code, foreignCustomGetRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func createCustomOperatorForTest(t *testing.T, handler http.Handler, cookie *http.Cookie, name string, image string) domain.OperatorRecord {
|
||
t.Helper()
|
||
payload, err := json.Marshal(map[string]any{
|
||
"name": name,
|
||
"type": "custom",
|
||
"category": "DATA",
|
||
"description": "tenant scoped custom operator",
|
||
"inputs": []map[string]string{
|
||
{"id": "input", "name": "Input", "type": "json"},
|
||
},
|
||
"outputs": []map[string]string{
|
||
{"id": "output", "name": "Output", "type": "json"},
|
||
},
|
||
"image": image,
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal custom operator: %v", err)
|
||
}
|
||
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/operators", payload, cookie)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create custom operator %s = %d body=%s", name, recorder.Code, recorder.Body.String())
|
||
}
|
||
var item domain.OperatorRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &item); err != nil {
|
||
t.Fatalf("decode custom operator: %v", err)
|
||
}
|
||
return item
|
||
}
|
||
|
||
func countOperatorsByType(items []domain.OperatorRecord, operatorType string) int {
|
||
count := 0
|
||
for _, item := range items {
|
||
if strings.EqualFold(strings.TrimSpace(item.Type), operatorType) {
|
||
count++
|
||
}
|
||
}
|
||
return count
|
||
}
|
||
|
||
func containsOperatorID(items []domain.OperatorRecord, id string) bool {
|
||
for _, item := range items {
|
||
if item.ID == id {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func containsString(items []string, target string) bool {
|
||
for _, item := range items {
|
||
if item == target {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
func TestWorkerAPIHealthIsProxiedThroughAPI(t *testing.T) {
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.URL.Path != "/worker-api/health" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
_, _ = writer.Write([]byte(`{"status":"ok","service":"droboticflow-worker"}`))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/worker-api/health", nil))
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if got := strings.TrimSpace(recorder.Body.String()); got != `{"status":"ok","service":"droboticflow-worker"}` {
|
||
t.Fatalf("unexpected worker health payload: %s", got)
|
||
}
|
||
}
|
||
|
||
func TestWorkerAPIBaseURLUsesConfiguredEnv(t *testing.T) {
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.URL.Path != "/worker-api/health" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
_, _ = writer.Write([]byte(`{"status":"configured"}`))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
t.Setenv("WORKER_API_BASE_URL", worker.URL)
|
||
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/worker-api/health", nil))
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if got := strings.TrimSpace(recorder.Body.String()); got != `{"status":"configured"}` {
|
||
t.Fatalf("unexpected configured worker payload: %s", got)
|
||
}
|
||
}
|
||
|
||
func TestWorkerAPIProxyTimesOutSlowHealthChecks(t *testing.T) {
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
time.Sleep(100 * time.Millisecond)
|
||
writer.WriteHeader(http.StatusOK)
|
||
}))
|
||
defer worker.Close()
|
||
|
||
originalTimeout := workerAPIShortProxyTimeout
|
||
workerAPIShortProxyTimeout = 10 * time.Millisecond
|
||
t.Cleanup(func() {
|
||
workerAPIShortProxyTimeout = originalTimeout
|
||
})
|
||
|
||
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/worker-api/health", nil))
|
||
|
||
if recorder.Code != http.StatusBadGateway {
|
||
t.Fatalf("expected proxy timeout to return 502, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkerAPIProcessOnceIsProxiedThroughAPI(t *testing.T) {
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.Method != http.MethodPost {
|
||
t.Fatalf("unexpected upstream method: %s", request.Method)
|
||
}
|
||
if request.URL.Path != "/worker-api/process-once" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
writer.WriteHeader(http.StatusOK)
|
||
_, _ = writer.Write([]byte(`{"run":{"id":"run-1","status":"running"}}`))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
||
unauthorizedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodPost, "/worker-api/process-once", nil))
|
||
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected unauthenticated process-once to be rejected, got %d body=%s", unauthorizedRecorder.Code, unauthorizedRecorder.Body.String())
|
||
}
|
||
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
org := adminCreateOrganization(t, handler, rootCookie, "Worker Tenant", "worker-tenant")
|
||
admin := adminCreateUser(t, handler, rootCookie, org.ID, "Worker Admin", "worker-admin@example.com", domain.UserRoleAdmin, "worker-pass")
|
||
adminCookie := loginSession(t, handler, admin.Email, "worker-pass")
|
||
|
||
recorder := serveJSONWithCookie(handler, http.MethodPost, "/worker-api/process-once", nil, adminCookie)
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if got := strings.TrimSpace(recorder.Body.String()); got != `{"run":{"id":"run-1","status":"running"}}` {
|
||
t.Fatalf("unexpected worker process-once payload: %s", got)
|
||
}
|
||
}
|
||
|
||
func TestWorkerAPIMetricsIsProxiedThroughAPI(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "worker-metrics-token")
|
||
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.Method != http.MethodGet {
|
||
t.Fatalf("unexpected upstream method: %s", request.Method)
|
||
}
|
||
if request.URL.Path != "/worker-api/metrics" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "text/plain; version=0.0.4")
|
||
writer.WriteHeader(http.StatusOK)
|
||
_, _ = writer.Write([]byte("droboticflow_worker_runs{status=\"queued\"} 1\n"))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
||
unauthorizedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/worker-api/metrics", nil))
|
||
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected metrics without API token to be rejected, got %d body=%s", unauthorizedRecorder.Code, unauthorizedRecorder.Body.String())
|
||
}
|
||
|
||
request := httptest.NewRequest(http.MethodGet, "/worker-api/metrics", nil)
|
||
request.Header.Set("Authorization", "Bearer worker-metrics-token")
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "droboticflow_worker_runs") {
|
||
t.Fatalf("unexpected worker metrics payload: %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkerAPICapacityIsProxiedThroughAPI(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "worker-capacity-token")
|
||
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.Method != http.MethodGet {
|
||
t.Fatalf("unexpected upstream method: %s", request.Method)
|
||
}
|
||
if request.URL.Path != "/worker-api/capacity" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
writer.WriteHeader(http.StatusOK)
|
||
_, _ = writer.Write([]byte(`{"gpu":{"resourceName":"nvidia.com/gpu","available":false,"nodes":[{"name":"cpu-node","allocatable":0}]}}`))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
||
unauthorizedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/worker-api/capacity", nil))
|
||
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected capacity without API token to be rejected, got %d body=%s", unauthorizedRecorder.Code, unauthorizedRecorder.Body.String())
|
||
}
|
||
|
||
request := httptest.NewRequest(http.MethodGet, "/worker-api/capacity", nil)
|
||
request.Header.Set("Authorization", "Bearer worker-capacity-token")
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), `"available":false`) || !strings.Contains(recorder.Body.String(), `"cpu-node"`) {
|
||
t.Fatalf("unexpected worker capacity payload: %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestDashboardIncludesPOCGatesFromAcceptanceArtifactAndWorkerCapacity(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.URL.Path != "/worker-api/capacity" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
_, _ = writer.Write([]byte(`{"gpu":{"resourceName":"nvidia.com/gpu","available":false,"schedulableAllocatable":0,"maxAllocatable":0,"schedulableNodeCount":0,"selectorSummary":"<none>","nodes":[{"name":"192.168.80.249","allocatable":0,"selectorMatched":true},{"name":"192.168.81.47","allocatable":0,"selectorMatched":true}]}}`))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
acceptancePayload := map[string]any{
|
||
"runId": "droboticflow-strict-acceptance-20260626041734",
|
||
"status": "failed",
|
||
"acceptanceScope": "full",
|
||
"fullExternalAcceptance": false,
|
||
"preflightFailure": true,
|
||
"preflightStage": "runner-image-readiness",
|
||
"preflightStages": []any{"runner-image-readiness"},
|
||
"requiredExternals": []any{"bos", "mongo"},
|
||
"externalVerificationPassed": []any{},
|
||
"externalVerificationMissing": []any{"bos", "mongo"},
|
||
"operatorCount": 86,
|
||
"operatorPassedCount": 0,
|
||
"coveredOperatorCount": 0,
|
||
"allOperatorCoverageComplete": false,
|
||
"blockingOperatorCoverageComplete": false,
|
||
"missingCoverage": []any{"model_inference"},
|
||
"blockingMissingCoverage": []any{"model_inference"},
|
||
"failedCount": 1,
|
||
"requiredSkippedCount": 0,
|
||
"operatorRepositoryRequiredCount": 86,
|
||
"operatorRepositoryCount": 86,
|
||
"operatorRepositoryCoverageComplete": true,
|
||
"operatorRepositoryPolicyClean": true,
|
||
"realFixtureCount": 6,
|
||
"generatedFixtureCount": 0,
|
||
"allFixturesReal": true,
|
||
"failures": []any{"OPERATOR_RUNNER_IMAGE is not configured"},
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-poc-operator-acceptance-blocked",
|
||
WorkflowID: "workflow-poc",
|
||
WorkflowName: "POC strict acceptance",
|
||
ProjectID: "project-poc",
|
||
ProjectName: "POC",
|
||
Trigger: domain.RunTriggerManual,
|
||
Status: domain.RunStatusFailed,
|
||
Artifacts: []domain.ExecutionArtifact{
|
||
{
|
||
ID: "artifact-poc-acceptance",
|
||
RunID: "run-poc-operator-acceptance-blocked",
|
||
NodeID: "poc-acceptance",
|
||
Name: "poc-operator-full-acceptance-result.json",
|
||
Type: domain.PortTypeJSON,
|
||
Path: "/workspace/system/acceptance/general/poc-operator-full-acceptance-result.json",
|
||
Data: acceptancePayload,
|
||
},
|
||
},
|
||
NodeExecutions: []domain.NodeExecutionRecord{},
|
||
Logs: []domain.LogEntryRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
K8sRefs: []domain.K8sRef{},
|
||
CreatedAt: "2026-06-26T04:17:34Z",
|
||
UpdatedAt: "2026-06-26T04:17:39Z",
|
||
}); err != nil {
|
||
t.Fatalf("upsert POC acceptance run: %v", err)
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-poc-gpu-workflow-preflight",
|
||
WorkflowID: "workflow-poc-gpu",
|
||
WorkflowName: pocGPUWorkflowAcceptanceWorkflowName,
|
||
ProjectID: "project-poc-gpu",
|
||
ProjectName: pocGPUWorkflowAcceptanceProjectName,
|
||
Trigger: domain.RunTriggerManual,
|
||
Status: domain.RunStatusFailed,
|
||
NodeExecutions: []domain.NodeExecutionRecord{
|
||
{
|
||
NodeID: "source-1",
|
||
NodeLabel: "真实数据源",
|
||
Status: domain.NodeExecutionStatusFailed,
|
||
Logs: []domain.LogEntryRecord{
|
||
{Level: "ERROR", Message: "gpu request cannot be scheduled: requested nvidia.com/gpu=1", Operator: "真实数据源"},
|
||
},
|
||
Error: "gpu request cannot be scheduled: requested nvidia.com/gpu=1",
|
||
},
|
||
},
|
||
Logs: []domain.LogEntryRecord{
|
||
{Level: "ERROR", Message: "gpu request cannot be scheduled: requested nvidia.com/gpu=1", Operator: "真实数据源"},
|
||
},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
Artifacts: []domain.ExecutionArtifact{},
|
||
K8sRefs: []domain.K8sRef{},
|
||
Error: "gpu request cannot be scheduled: requested nvidia.com/gpu=1",
|
||
CreatedAt: "2026-06-26T04:18:01Z",
|
||
UpdatedAt: "2026-06-26T04:18:02Z",
|
||
}); err != nil {
|
||
t.Fatalf("upsert POC GPU workflow preflight run: %v", err)
|
||
}
|
||
|
||
appConfig, err := config.LoadValidated()
|
||
if err != nil {
|
||
t.Fatalf("load test config: %v", err)
|
||
}
|
||
appConfig.WorkerAPIBaseURL = worker.URL
|
||
handler := NewHandlerWithConfig(dataStore, nil, appConfig)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("dashboard status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var body struct {
|
||
POC domain.DashboardPOCReadiness `json:"poc"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||
t.Fatalf("decode dashboard: %v", err)
|
||
}
|
||
if body.POC.Status != "blocked" {
|
||
t.Fatalf("expected POC dashboard to be blocked, got %#v", body.POC)
|
||
}
|
||
if body.POC.LatestAcceptanceRunID != "run-poc-operator-acceptance-blocked" {
|
||
t.Fatalf("latest acceptance run = %q", body.POC.LatestAcceptanceRunID)
|
||
}
|
||
if body.POC.GPU.Status != "blocked" || body.POC.StrictAcceptance.Status != "unknown" {
|
||
t.Fatalf("unexpected POC gate statuses: gpu=%s strict=%s", body.POC.GPU.Status, body.POC.StrictAcceptance.Status)
|
||
}
|
||
if body.POC.Freshness != "stale" || body.POC.StaleReason == "" {
|
||
t.Fatalf("expected historical acceptance artifact to be stale: %#v", body.POC)
|
||
}
|
||
if body.POC.ServiceAvailability.Status != "passed" || body.POC.GPUCapacity.Status != body.POC.GPU.Status || body.POC.BusinessAcceptance.Status != "unknown" || body.POC.OperatorCoverage.Status != "unknown" {
|
||
t.Fatalf("unexpected split readiness gates: %#v", body.POC)
|
||
}
|
||
if body.POC.GPUWorkflowPreflight.Status != "passed" {
|
||
t.Fatalf("expected GPU workflow preflight gate to pass, got %#v", body.POC.GPUWorkflowPreflight)
|
||
}
|
||
if body.POC.GPUWorkflowPreflight.Evidence["runId"] != "run-poc-gpu-workflow-preflight" {
|
||
t.Fatalf("unexpected GPU workflow preflight evidence: %#v", body.POC.GPUWorkflowPreflight.Evidence)
|
||
}
|
||
if body.POC.OperatorRepositoryCount != 86 || !body.POC.OperatorRepositoryCoverageComplete {
|
||
t.Fatalf("operator repository evidence missing from POC dashboard: %#v", body.POC)
|
||
}
|
||
if !containsString(body.POC.ExternalVerificationMissing, "mongo") || !containsString(body.POC.BlockingMissingCoverage, "model_inference") {
|
||
t.Fatalf("expected strict blocking evidence, got %#v", body.POC)
|
||
}
|
||
if len(body.POC.NextActions) < 2 {
|
||
t.Fatalf("expected actionable next steps, got %#v", body.POC.NextActions)
|
||
}
|
||
}
|
||
|
||
func TestDashboardPOCReadinessSerializesEmptyLists(t *testing.T) {
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.URL.Path != "/worker-api/capacity" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
_, _ = writer.Write([]byte(`{"gpu":{"resourceName":"nvidia.com/gpu","available":false,"schedulableAllocatable":0,"maxAllocatable":0,"schedulableNodeCount":0,"selectorSummary":"<none>","nodes":[]}}`))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
appConfig, err := config.LoadValidated()
|
||
if err != nil {
|
||
t.Fatalf("load test config: %v", err)
|
||
}
|
||
appConfig.WorkerAPIBaseURL = worker.URL
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, appConfig)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("dashboard status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
bodyText := recorder.Body.String()
|
||
for _, unexpected := range []string{
|
||
`"preflightStages":null`,
|
||
`"requiredExternals":null`,
|
||
`"externalVerificationPassed":null`,
|
||
`"externalVerificationMissing":null`,
|
||
`"missingCoverage":null`,
|
||
`"blockingMissingCoverage":null`,
|
||
`"nextActions":null`,
|
||
} {
|
||
if strings.Contains(bodyText, unexpected) {
|
||
t.Fatalf("dashboard POC readiness should serialize empty lists, found %s in %s", unexpected, bodyText)
|
||
}
|
||
}
|
||
|
||
var body struct {
|
||
POC domain.DashboardPOCReadiness `json:"poc"`
|
||
}
|
||
if err := json.Unmarshal([]byte(bodyText), &body); err != nil {
|
||
t.Fatalf("decode dashboard: %v", err)
|
||
}
|
||
if body.POC.PreflightStages == nil ||
|
||
body.POC.RequiredExternals == nil ||
|
||
body.POC.ExternalVerificationPassed == nil ||
|
||
body.POC.ExternalVerificationMissing == nil ||
|
||
body.POC.MissingCoverage == nil ||
|
||
body.POC.BlockingMissingCoverage == nil ||
|
||
body.POC.NextActions == nil {
|
||
t.Fatalf("expected non-nil empty POC lists, got %#v", body.POC)
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceReadinessAllowsSharedAPITokenWhenSessionAuthIsEnabled(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-readiness-token")
|
||
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
if request.URL.Path != "/worker-api/capacity" {
|
||
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
||
}
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
_, _ = writer.Write([]byte(`{"gpu":{"resourceName":"nvidia.com/gpu","available":false,"schedulableAllocatable":0,"maxAllocatable":0,"schedulableNodeCount":0,"selectorSummary":"<none>","nodes":[{"name":"192.168.80.249","allocatable":0,"selectorMatched":true},{"name":"192.168.81.47","allocatable":0,"selectorMatched":true}]}}`))
|
||
}))
|
||
defer worker.Close()
|
||
|
||
acceptancePayload := map[string]any{
|
||
"runId": "droboticflow-strict-acceptance-20260626043724",
|
||
"status": "failed",
|
||
"acceptanceScope": "full",
|
||
"fullExternalAcceptance": false,
|
||
"preflightFailure": true,
|
||
"preflightStages": []any{"runner-image-readiness"},
|
||
"requiredExternals": []any{"bos", "mongo"},
|
||
"externalVerificationPassed": []any{},
|
||
"externalVerificationMissing": []any{"bos", "mongo"},
|
||
"operatorCount": 86,
|
||
"operatorPassedCount": 0,
|
||
"coveredOperatorCount": 0,
|
||
"allOperatorCoverageComplete": false,
|
||
"blockingOperatorCoverageComplete": false,
|
||
"blockingMissingCoverage": []any{"model_inference"},
|
||
"failedCount": 1,
|
||
"requiredSkippedCount": 0,
|
||
"operatorRepositoryRequiredCount": 86,
|
||
"operatorRepositoryCount": 86,
|
||
"operatorRepositoryCoverageComplete": true,
|
||
"operatorRepositoryPolicyClean": true,
|
||
"realFixtureCount": 6,
|
||
"generatedFixtureCount": 0,
|
||
"allFixturesReal": true,
|
||
"failures": []any{"OPERATOR_RUNNER_IMAGE is not configured"},
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-poc-operator-acceptance-readiness-blocked",
|
||
WorkflowID: "workflow-poc",
|
||
WorkflowName: "POC strict acceptance",
|
||
ProjectID: "project-poc",
|
||
ProjectName: "POC",
|
||
Trigger: domain.RunTriggerManual,
|
||
Status: domain.RunStatusFailed,
|
||
Artifacts: []domain.ExecutionArtifact{
|
||
{
|
||
ID: "artifact-poc-readiness",
|
||
RunID: "run-poc-operator-acceptance-readiness-blocked",
|
||
NodeID: "poc-acceptance",
|
||
Name: "poc-operator-full-acceptance-result.json",
|
||
Type: domain.PortTypeJSON,
|
||
Path: "/workspace/system/acceptance/general/poc-operator-full-acceptance-result.json",
|
||
Data: acceptancePayload,
|
||
},
|
||
},
|
||
CreatedAt: "2026-06-26T04:37:24Z",
|
||
UpdatedAt: "2026-06-26T04:37:29Z",
|
||
}); err != nil {
|
||
t.Fatalf("upsert POC acceptance run: %v", err)
|
||
}
|
||
|
||
appConfig, err := config.LoadValidated()
|
||
if err != nil {
|
||
t.Fatalf("load test config: %v", err)
|
||
}
|
||
appConfig.WorkerAPIBaseURL = worker.URL
|
||
handler := NewHandlerWithConfig(dataStore, nil, appConfig)
|
||
|
||
unauthorizedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/api/acceptance/poc-readiness", nil))
|
||
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected readiness without token to be rejected, got %d body=%s", unauthorizedRecorder.Code, unauthorizedRecorder.Body.String())
|
||
}
|
||
|
||
dashboardRequest := httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)
|
||
dashboardRequest.Header.Set("Authorization", "Bearer shared-readiness-token")
|
||
dashboardRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(dashboardRecorder, dashboardRequest)
|
||
if dashboardRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("API token should not read full dashboard, got %d body=%s", dashboardRecorder.Code, dashboardRecorder.Body.String())
|
||
}
|
||
|
||
readinessRequest := httptest.NewRequest(http.MethodGet, "/api/acceptance/poc-readiness", nil)
|
||
readinessRequest.Header.Set("Authorization", "Bearer shared-readiness-token")
|
||
readinessRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(readinessRecorder, readinessRequest)
|
||
if readinessRecorder.Code != http.StatusOK {
|
||
t.Fatalf("readiness status: %d body=%s", readinessRecorder.Code, readinessRecorder.Body.String())
|
||
}
|
||
var readiness domain.DashboardPOCReadiness
|
||
if err := json.Unmarshal(readinessRecorder.Body.Bytes(), &readiness); err != nil {
|
||
t.Fatalf("decode readiness: %v", err)
|
||
}
|
||
if readiness.Status != "blocked" || readiness.LatestAcceptanceRunID != "run-poc-operator-acceptance-readiness-blocked" {
|
||
t.Fatalf("unexpected readiness summary: %#v", readiness)
|
||
}
|
||
if readiness.GPU.Status != "blocked" || readiness.GPUWorkflowPreflight.Status != "unknown" || readiness.StrictAcceptance.Status != "unknown" {
|
||
t.Fatalf("unexpected readiness gates: gpu=%s gpuWorkflow=%s strict=%s", readiness.GPU.Status, readiness.GPUWorkflowPreflight.Status, readiness.StrictAcceptance.Status)
|
||
}
|
||
if readiness.Freshness != "stale" || readiness.BusinessAcceptance.Status != "unknown" || readiness.OperatorCoverage.Status != "unknown" {
|
||
t.Fatalf("expected historical readiness evidence to be stale, got %#v", readiness)
|
||
}
|
||
if !containsString(readiness.ExternalVerificationMissing, "mongo") || readiness.RealFixtureCount != 6 || readiness.GeneratedFixtureCount != 0 {
|
||
t.Fatalf("readiness evidence missing: %#v", readiness)
|
||
}
|
||
if !containsString(readiness.NextActions, "运行 npm run acceptance:poc-gpu-workflow,确认无 GPU 时 worker 会在创建 Job 前失败并留下日志证据。") {
|
||
t.Fatalf("expected GPU workflow preflight next action, got %#v", readiness.NextActions)
|
||
}
|
||
}
|
||
|
||
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","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)
|
||
}
|
||
if createdWorkflow["type"].(string) != "batch" {
|
||
t.Fatalf("expected workflow type to default to batch, got %v", createdWorkflow["type"])
|
||
}
|
||
|
||
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"])
|
||
}
|
||
listWorkflowStatusCounts := countedProject["workflowStatusCounts"].(map[string]any)
|
||
if listWorkflowStatusCounts["draft"].(float64) != 1 ||
|
||
listWorkflowStatusCounts["running"].(float64) != 0 ||
|
||
listWorkflowStatusCounts["failed"].(float64) != 0 ||
|
||
listWorkflowStatusCounts["completed"].(float64) != 0 {
|
||
t.Fatalf("unexpected workflowStatusCounts in projects list: %#v", listWorkflowStatusCounts)
|
||
}
|
||
|
||
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"])
|
||
}
|
||
getWorkflowStatusCounts := projectGetBody["workflowStatusCounts"].(map[string]any)
|
||
if getWorkflowStatusCounts["draft"].(float64) != 1 ||
|
||
getWorkflowStatusCounts["running"].(float64) != 0 ||
|
||
getWorkflowStatusCounts["failed"].(float64) != 0 ||
|
||
getWorkflowStatusCounts["completed"].(float64) != 0 {
|
||
t.Fatalf("unexpected workflowStatusCounts in project get: %#v", getWorkflowStatusCounts)
|
||
}
|
||
|
||
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 TestOperatorsListEnsuresMongoWriteBuiltin(t *testing.T) {
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var response struct {
|
||
Items []domain.OperatorRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode operators list: %v", err)
|
||
}
|
||
|
||
var mongoWrite *domain.OperatorRecord
|
||
for index := range response.Items {
|
||
item := response.Items[index]
|
||
if item.Name == "MongoDB 写入" {
|
||
mongoWrite = &item
|
||
break
|
||
}
|
||
}
|
||
if mongoWrite == nil {
|
||
t.Fatalf("expected MongoDB 写入 builtin operator to be present")
|
||
}
|
||
if mongoWrite.Category != "DATA" {
|
||
t.Fatalf("expected MongoDB 写入 category DATA, got %q", mongoWrite.Category)
|
||
}
|
||
if len(mongoWrite.Inputs) == 0 || len(mongoWrite.Outputs) == 0 {
|
||
t.Fatalf("expected MongoDB 写入 ports to be defined")
|
||
}
|
||
foundTarget := false
|
||
for _, param := range mongoWrite.Hyperparams {
|
||
if param.Key == "MONGO_TARGET" && param.Value == "business" {
|
||
foundTarget = true
|
||
break
|
||
}
|
||
}
|
||
if !foundTarget {
|
||
t.Fatalf("expected MongoDB 写入 to default MONGO_TARGET=business")
|
||
}
|
||
}
|
||
|
||
func TestOperatorsListEnsuresRegressionBuiltinCatalog(t *testing.T) {
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var response struct {
|
||
Items []domain.OperatorRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode operators list: %v", err)
|
||
}
|
||
|
||
byName := make(map[string]domain.OperatorRecord, len(response.Items))
|
||
for _, item := range response.Items {
|
||
byName[item.Name] = item
|
||
}
|
||
|
||
for _, name := range []string{
|
||
"真实数据源",
|
||
"BOS 增量扫描",
|
||
"目录读取",
|
||
"模型推理",
|
||
"X 平台数据集索引",
|
||
"采集目录校验",
|
||
"JSON 字段提取",
|
||
"文本模板渲染",
|
||
"JSON 过滤",
|
||
"JSON 映射重命名",
|
||
"JSON 转 CSV",
|
||
"CSV 转 JSON",
|
||
"条件分流",
|
||
"数据合并",
|
||
"视频质检",
|
||
"数据过滤",
|
||
"视频切片",
|
||
"视频标注",
|
||
"成品打包",
|
||
"抽检采样",
|
||
"人工审核",
|
||
"MongoDB 写入",
|
||
"LeRobot 数据集概览",
|
||
"ROSBag 数据概览",
|
||
"结果上传",
|
||
"Shell 命令执行",
|
||
} {
|
||
if _, ok := byName[name]; !ok {
|
||
t.Fatalf("expected builtin operator %q to be present", name)
|
||
}
|
||
}
|
||
|
||
sampling := byName["抽检采样"]
|
||
foundRules := false
|
||
for _, param := range sampling.Hyperparams {
|
||
if param.Key == "RULES_JSON" {
|
||
foundRules = true
|
||
break
|
||
}
|
||
}
|
||
if !foundRules {
|
||
t.Fatalf("expected 抽检采样 to expose RULES_JSON")
|
||
}
|
||
}
|
||
|
||
func TestBuiltinOperatorCatalogLoadsDeclarativeRecords(t *testing.T) {
|
||
records, err := builtinOperatorRecords("2026-06-16T00:00:00Z", "ccr.example/ns/droboticflow-operator-runner:test")
|
||
if err != nil {
|
||
t.Fatalf("load builtin operator catalog: %v", err)
|
||
}
|
||
if len(records) < 20 {
|
||
t.Fatalf("expected builtin catalog to have real operator coverage, got %d", len(records))
|
||
}
|
||
|
||
seenIDs := map[string]struct{}{}
|
||
seenNames := map[string]struct{}{}
|
||
foundMongoWrite := false
|
||
for _, item := range records {
|
||
if item.ID == "" || item.Name == "" || item.Category == "" || item.Description == "" {
|
||
t.Fatalf("expected required catalog fields to be populated: %+v", item)
|
||
}
|
||
if item.Type != "builtin" {
|
||
t.Fatalf("expected catalog item %s to be builtin, got %q", item.ID, item.Type)
|
||
}
|
||
if item.Image == "runner:test" {
|
||
t.Fatalf("expected catalog image for %s to remain independent, got runner image", item.ID)
|
||
}
|
||
if !strings.Contains(item.Image, ":test") {
|
||
t.Fatalf("expected catalog image for %s to inherit runner tag, got %q", item.ID, item.Image)
|
||
}
|
||
if item.ImageID == "" {
|
||
t.Fatalf("expected imageId for %s", item.ID)
|
||
}
|
||
if len(item.ContainerCommand) == 0 {
|
||
t.Fatalf("expected container command for %s", item.ID)
|
||
}
|
||
if item.CreatedAt != "2026-06-16T00:00:00Z" || item.UpdatedAt != "2026-06-16T00:00:00Z" {
|
||
t.Fatalf("expected timestamp injection for %s", item.ID)
|
||
}
|
||
if _, exists := seenIDs[item.ID]; exists {
|
||
t.Fatalf("duplicate builtin id %q", item.ID)
|
||
}
|
||
seenIDs[item.ID] = struct{}{}
|
||
if _, exists := seenNames[item.Name]; exists {
|
||
t.Fatalf("duplicate builtin name %q", item.Name)
|
||
}
|
||
seenNames[item.Name] = struct{}{}
|
||
if item.Name == "MongoDB 写入" {
|
||
foundMongoWrite = true
|
||
}
|
||
}
|
||
if !foundMongoWrite {
|
||
t.Fatalf("expected declarative catalog to include MongoDB 写入")
|
||
}
|
||
}
|
||
|
||
func TestOperatorsListRefreshesBuiltinOperatorImages(t *testing.T) {
|
||
t.Setenv("OPERATOR_RUNNER_IMAGE", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260416-workspacefix-amd64")
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
if err := dataStore.UpsertOperator(ctx, domain.OperatorRecord{
|
||
ID: "operator-builtin-bos-incremental-scan",
|
||
Name: "BOS 增量扫描",
|
||
Type: "builtin",
|
||
Category: "IO",
|
||
Description: "source",
|
||
Image: "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:old",
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
||
}); err != nil {
|
||
t.Fatalf("upsert operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
stored, err := dataStore.GetOperator(ctx, "operator-builtin-bos-incremental-scan")
|
||
if err != nil {
|
||
t.Fatalf("get operator: %v", err)
|
||
}
|
||
if stored == nil {
|
||
t.Fatalf("expected operator to exist")
|
||
}
|
||
if !strings.Contains(stored.Image, "droboticflow-operator-bos-incremental-scan") {
|
||
t.Fatalf("expected builtin operator to refresh to independent operator image, got %q", stored.Image)
|
||
}
|
||
if !strings.HasSuffix(stored.Image, ":20260416-workspacefix-amd64") {
|
||
t.Fatalf("expected builtin operator to inherit runner tag, got %q", stored.Image)
|
||
}
|
||
if stored.ImageID == "" || len(stored.ContainerCommand) == 0 {
|
||
t.Fatalf("expected builtin operator imageId and container command to refresh, got %#v", stored)
|
||
}
|
||
}
|
||
|
||
func TestOperatorsListPreservesDigestPinnedBuiltinImageID(t *testing.T) {
|
||
t.Setenv("OPERATOR_RUNNER_IMAGE", "ccr.example/dataflow-poc/droboticflow-operator-runner:live")
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
digestImageID := "ccr.example/dataflow-poc/droboticflow-operator-real-source@sha256:" + strings.Repeat("a", 64)
|
||
if err := dataStore.UpsertOperator(ctx, domain.OperatorRecord{
|
||
ID: "operator-builtin-real-source",
|
||
Name: "真实数据源",
|
||
Type: "builtin",
|
||
Category: "IO",
|
||
Description: "source",
|
||
Image: "ccr.example/dataflow-poc/droboticflow-operator-real-source:live",
|
||
ImageID: digestImageID,
|
||
ContainerCommand: []string{"python", "/app/backend/operator-runner/main.py"},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
||
}); err != nil {
|
||
t.Fatalf("upsert operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
stored, err := dataStore.GetOperator(ctx, "operator-builtin-real-source")
|
||
if err != nil {
|
||
t.Fatalf("get operator: %v", err)
|
||
}
|
||
if stored.ImageID != digestImageID {
|
||
t.Fatalf("expected digest-pinned imageId to be preserved, got %q", stored.ImageID)
|
||
}
|
||
if stored.Image != "ccr.example/dataflow-poc/droboticflow-operator-real-source:live" {
|
||
t.Fatalf("expected image to remain on live tag, got %q", stored.Image)
|
||
}
|
||
}
|
||
|
||
func TestOperatorsListPreservesLiveDigestOverCatalogContractDigest(t *testing.T) {
|
||
t.Setenv("OPERATOR_RUNNER_IMAGE", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-runner:live")
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
liveDigest := "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-xplatform-dataset-index@sha256:" + strings.Repeat("d", 64)
|
||
if err := dataStore.UpsertOperator(ctx, domain.OperatorRecord{
|
||
ID: "operator-builtin-xplatform-dataset-index",
|
||
Name: "X 平台数据集索引",
|
||
Type: "builtin",
|
||
Category: "DATASET",
|
||
Description: "live",
|
||
Image: "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-xplatform-dataset-index:live",
|
||
ImageID: liveDigest,
|
||
ContainerCommand: []string{"python", "/app/backend/operator-runner/main.py"},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "dataset", Type: domain.PortTypeJSON}},
|
||
}); err != nil {
|
||
t.Fatalf("upsert operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
stored, err := dataStore.GetOperator(ctx, "operator-builtin-xplatform-dataset-index")
|
||
if err != nil {
|
||
t.Fatalf("get operator: %v", err)
|
||
}
|
||
if stored.ImageID != liveDigest {
|
||
t.Fatalf("expected live registry digest to be preserved, got %q", stored.ImageID)
|
||
}
|
||
if stored.Image != "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-xplatform-dataset-index:live" {
|
||
t.Fatalf("expected live registry image tag to be preserved, got %q", stored.Image)
|
||
}
|
||
}
|
||
|
||
func TestBuiltinOperatorCatalogPreservesTaggedDigestImageID(t *testing.T) {
|
||
records, err := builtinOperatorRecords("2026-06-30T00:00:00Z", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-runner:live")
|
||
if err != nil {
|
||
t.Fatalf("builtin catalog: %v", err)
|
||
}
|
||
var found *domain.OperatorRecord
|
||
for index := range records {
|
||
if records[index].ID == "operator-builtin-openvla-finetune" {
|
||
found = &records[index]
|
||
break
|
||
}
|
||
}
|
||
if found == nil {
|
||
t.Fatalf("expected openvla finetune operator in builtin catalog")
|
||
}
|
||
if !strings.Contains(found.ImageID, ":1442552-poc-20260630062516@sha256:dda74e65fa48fd5d33afcece7779455e95569ce036ac74a07789718e69e67b79") {
|
||
t.Fatalf("expected tagged digest image id to be preserved, got %q", found.ImageID)
|
||
}
|
||
if !sameImageRepository(found.Image, found.ImageID) {
|
||
t.Fatalf("expected image and tagged digest image id to share repository: image=%q imageId=%q", found.Image, found.ImageID)
|
||
}
|
||
}
|
||
|
||
func TestOperatorsListDoesNotReplaceLiveDigestWithCatalogDigest(t *testing.T) {
|
||
t.Setenv("OPERATOR_RUNNER_IMAGE", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-runner:live")
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
oldDigest := "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-openvla-finetune:old@sha256:" + strings.Repeat("b", 64)
|
||
if err := dataStore.UpsertOperator(ctx, domain.OperatorRecord{
|
||
ID: "operator-builtin-openvla-finetune",
|
||
Name: "OpenVLA 微调训练",
|
||
Type: "builtin",
|
||
Category: "AI",
|
||
Description: "old",
|
||
Image: "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-openvla-finetune:old",
|
||
ImageID: oldDigest,
|
||
ContainerCommand: []string{"python", "/app/backend/operator-runner/main.py"},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "model", Type: domain.PortTypeFile}},
|
||
}); err != nil {
|
||
t.Fatalf("upsert operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
stored, err := dataStore.GetOperator(ctx, "operator-builtin-openvla-finetune")
|
||
if err != nil {
|
||
t.Fatalf("get operator: %v", err)
|
||
}
|
||
if stored.ImageID != oldDigest {
|
||
t.Fatalf("expected live digest to be preserved instead of catalog digest, got %q", stored.ImageID)
|
||
}
|
||
if stored.Image != "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-operator-openvla-finetune:old" {
|
||
t.Fatalf("expected live image tag to be preserved, got %q", stored.Image)
|
||
}
|
||
}
|
||
|
||
func TestOperatorsDeleteKeepsBuiltinOperatorDeleted(t *testing.T) {
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
listRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if listRecorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
||
}
|
||
|
||
var listResponse struct {
|
||
Items []domain.OperatorRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listResponse); err != nil {
|
||
t.Fatalf("decode operators list: %v", err)
|
||
}
|
||
|
||
var builtin *domain.OperatorRecord
|
||
for index := range listResponse.Items {
|
||
item := listResponse.Items[index]
|
||
if item.Type == "builtin" && item.Name == "真实数据源" {
|
||
builtin = &item
|
||
break
|
||
}
|
||
}
|
||
if builtin == nil {
|
||
t.Fatalf("expected builtin operator to exist before delete")
|
||
}
|
||
|
||
deleteRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteRecorder,
|
||
httptest.NewRequest(http.MethodDelete, "/api/operators/"+builtin.ID, nil),
|
||
)
|
||
if deleteRecorder.Code != http.StatusNoContent {
|
||
t.Fatalf("delete builtin status: %d body=%s", deleteRecorder.Code, deleteRecorder.Body.String())
|
||
}
|
||
|
||
listAfterDeleteRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listAfterDeleteRecorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
||
if listAfterDeleteRecorder.Code != http.StatusOK {
|
||
t.Fatalf("operators list after delete status: %d body=%s", listAfterDeleteRecorder.Code, listAfterDeleteRecorder.Body.String())
|
||
}
|
||
|
||
var listAfterDeleteResponse struct {
|
||
Items []domain.OperatorRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(listAfterDeleteRecorder.Body.Bytes(), &listAfterDeleteResponse); err != nil {
|
||
t.Fatalf("decode operators list after delete: %v", err)
|
||
}
|
||
for _, item := range listAfterDeleteResponse.Items {
|
||
if item.Name == builtin.Name {
|
||
t.Fatalf("expected builtin operator %q to stay deleted", builtin.Name)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserListsCollectionsAndQueriesDocuments(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
if _, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "数据库浏览项目",
|
||
Description: "用于数据库浏览测试",
|
||
}); err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: "JSON 测试算子",
|
||
Type: "builtin",
|
||
Category: "DATA",
|
||
Description: "用于数据库过滤查询测试",
|
||
Inputs: []domain.PortDefinition{{ID: "input", Name: "input", Type: domain.PortTypeJSON}},
|
||
Outputs: []domain.PortDefinition{{ID: "output", Name: "output", Type: domain.PortTypeJSON}},
|
||
}); err != nil {
|
||
t.Fatalf("create operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
overviewRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database?target=platform", nil))
|
||
if overviewRecorder.Code != http.StatusOK {
|
||
t.Fatalf("database overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
||
}
|
||
|
||
var overview struct {
|
||
Database string `json:"database"`
|
||
Collections []struct {
|
||
Name string `json:"name"`
|
||
DocumentCount int64 `json:"documentCount"`
|
||
} `json:"collections"`
|
||
}
|
||
if err := json.Unmarshal(overviewRecorder.Body.Bytes(), &overview); err != nil {
|
||
t.Fatalf("decode database overview: %v", err)
|
||
}
|
||
if overview.Database == "" {
|
||
t.Fatalf("expected database name in overview response")
|
||
}
|
||
if len(overview.Collections) == 0 {
|
||
t.Fatalf("expected collections in overview response")
|
||
}
|
||
|
||
foundOperatorsCollection := false
|
||
for _, item := range overview.Collections {
|
||
if item.Name == "operators" {
|
||
foundOperatorsCollection = true
|
||
if item.DocumentCount < 1 {
|
||
t.Fatalf("expected operators collection to have documents, got %d", item.DocumentCount)
|
||
}
|
||
}
|
||
}
|
||
if !foundOperatorsCollection {
|
||
t.Fatalf("expected operators collection in overview")
|
||
}
|
||
|
||
filter := url.QueryEscape(`{"category":"DATA"}`)
|
||
queryRecorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&filter="+filter+"&limit=10", nil)
|
||
handler.ServeHTTP(queryRecorder, request)
|
||
if queryRecorder.Code != http.StatusOK {
|
||
t.Fatalf("database documents status: %d body=%s", queryRecorder.Code, queryRecorder.Body.String())
|
||
}
|
||
|
||
var result struct {
|
||
Collection string `json:"collection"`
|
||
Documents []map[string]any `json:"documents"`
|
||
Limit int `json:"limit"`
|
||
Total int64 `json:"total"`
|
||
Filter map[string]any `json:"filter"`
|
||
}
|
||
if err := json.Unmarshal(queryRecorder.Body.Bytes(), &result); err != nil {
|
||
t.Fatalf("decode database documents: %v", err)
|
||
}
|
||
if result.Collection != "operators" {
|
||
t.Fatalf("expected operators collection, got %q", result.Collection)
|
||
}
|
||
if result.Total < 1 || len(result.Documents) < 1 {
|
||
t.Fatalf("expected at least one operator document, total=%d len=%d", result.Total, len(result.Documents))
|
||
}
|
||
if result.Filter["category"] != "DATA" {
|
||
t.Fatalf("expected filter to round-trip, got %#v", result.Filter)
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserRejectsInvalidDocumentPagination(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: "分页测试算子",
|
||
Type: "builtin",
|
||
Category: "DATA",
|
||
Description: "用于数据库分页参数测试",
|
||
}); err != nil {
|
||
t.Fatalf("create operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=0&skip=-1", nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("invalid pagination status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be greater than or equal to 1") {
|
||
t.Fatalf("expected limit validation issue, got %s", body)
|
||
}
|
||
if !strings.Contains(body, `"field":"skip"`) || !strings.Contains(body, "skip must be zero or greater") {
|
||
t.Fatalf("expected skip validation issue, got %s", body)
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserRejectsNonIntegerDocumentLimit(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: "分页非整数测试算子",
|
||
Type: "builtin",
|
||
Category: "DATA",
|
||
Description: "用于数据库分页非整数参数测试",
|
||
}); err != nil {
|
||
t.Fatalf("create operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=1.5", nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("non-integer pagination status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be an integer") {
|
||
t.Fatalf("expected limit integer validation issue, got %s", body)
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserRejectsNegativeDocumentLimitAsPositiveRequired(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: "分页负数测试算子",
|
||
Type: "builtin",
|
||
Category: "DATA",
|
||
Description: "用于数据库分页负数参数测试",
|
||
}); err != nil {
|
||
t.Fatalf("create operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=-1", nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("negative limit status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be greater than or equal to 1") {
|
||
t.Fatalf("expected positive limit validation issue, got %s", body)
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserClampsDocumentPaginationBounds(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: "分页上限测试算子",
|
||
Type: "builtin",
|
||
Category: "DATA",
|
||
Description: "用于数据库分页上限测试",
|
||
}); err != nil {
|
||
t.Fatalf("create operator: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=999&skip=9999", nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("clamped pagination status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var result struct {
|
||
Limit int `json:"limit"`
|
||
Skip int `json:"skip"`
|
||
Total int `json:"total"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
|
||
t.Fatalf("decode clamped pagination response: %v", err)
|
||
}
|
||
if result.Limit != 200 {
|
||
t.Fatalf("expected limit to clamp to 200, got %d", result.Limit)
|
||
}
|
||
if result.Skip != result.Total {
|
||
t.Fatalf("expected skip to clamp to available total after store query, got skip=%d total=%d", result.Skip, result.Total)
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserDefaultsToBusinessTargetWhenUnspecified(t *testing.T) {
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
overviewRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database", nil))
|
||
if overviewRecorder.Code != http.StatusOK {
|
||
t.Fatalf("database overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(overviewRecorder.Body.String(), `"target":"business"`) {
|
||
t.Fatalf("expected default target=business, got %s", overviewRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(overviewRecorder.Body.String(), `"configured":false`) {
|
||
t.Fatalf("expected configured=false for unconfigured business mongo, got %s", overviewRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserUsesMongoConnectionWhenProvided(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
||
Name: "业务库",
|
||
URI: "mongodb://business.example:27017",
|
||
Database: "business",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create mongo connection: %v", err)
|
||
}
|
||
|
||
originalResolver := mongoConnectionStoreResolver
|
||
t.Cleanup(func() {
|
||
mongoConnectionStoreResolver = originalResolver
|
||
})
|
||
businessStore := store.NewMemoryStore()
|
||
if _, err := businessStore.CreateProject(ctx, domain.CreateProjectInput{Name: "业务项目", Description: "业务库"}); err != nil {
|
||
t.Fatalf("create business project: %v", err)
|
||
}
|
||
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
||
if item == nil || item.ID != connection.ID || item.URI != "mongodb://business.example:27017" || item.Database != "business" {
|
||
t.Fatalf("unexpected mongo connection: %#v", item)
|
||
}
|
||
return businessStore, func() {}, nil
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
overviewRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database?connectionId="+connection.ID, nil))
|
||
if overviewRecorder.Code != http.StatusOK {
|
||
t.Fatalf("business database overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(overviewRecorder.Body.String(), `"target":"business"`) {
|
||
t.Fatalf("expected business target in overview, got %s", overviewRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(overviewRecorder.Body.String(), `"connectionId":"`+connection.ID+`"`) {
|
||
t.Fatalf("expected connection id in overview, got %s", overviewRecorder.Body.String())
|
||
}
|
||
|
||
queryRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(queryRecorder, httptest.NewRequest(http.MethodGet, "/api/database/collections/projects/documents?connectionId="+connection.ID+"&limit=5", nil))
|
||
if queryRecorder.Code != http.StatusOK {
|
||
t.Fatalf("business database documents status: %d body=%s", queryRecorder.Code, queryRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(queryRecorder.Body.String(), `"configured":true`) {
|
||
t.Fatalf("expected configured=true, got %s", queryRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(queryRecorder.Body.String(), `"target":"business"`) {
|
||
t.Fatalf("expected business target in query result, got %s", queryRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserBusinessCollectionAndDocumentCRUD(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
||
Name: "业务写入库",
|
||
URI: "mongodb://business.example:27017",
|
||
Database: "business",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create mongo connection: %v", err)
|
||
}
|
||
|
||
originalResolver := mongoConnectionStoreResolver
|
||
t.Cleanup(func() {
|
||
mongoConnectionStoreResolver = originalResolver
|
||
})
|
||
businessStore := store.NewMemoryStore()
|
||
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected mongo connection: %#v", item)
|
||
}
|
||
return businessStore, func() {}, nil
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
connectionQuery := "?connectionId=" + connection.ID
|
||
|
||
createCollectionRecorder := httptest.NewRecorder()
|
||
createCollectionBody := bytes.NewBufferString(`{"name":"orders"}`)
|
||
handler.ServeHTTP(
|
||
createCollectionRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections"+connectionQuery, createCollectionBody),
|
||
)
|
||
if createCollectionRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create collection status: %d body=%s", createCollectionRecorder.Code, createCollectionRecorder.Body.String())
|
||
}
|
||
|
||
createDocumentRecorder := httptest.NewRecorder()
|
||
createDocumentBody := bytes.NewBufferString(`{"document":{"orderNo":"SO-001","status":"new","amount":12}}`)
|
||
handler.ServeHTTP(
|
||
createDocumentRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections/orders/documents"+connectionQuery, createDocumentBody),
|
||
)
|
||
if createDocumentRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create document status: %d body=%s", createDocumentRecorder.Code, createDocumentRecorder.Body.String())
|
||
}
|
||
var createdDocument struct {
|
||
Collection string `json:"collection"`
|
||
Document map[string]any `json:"document"`
|
||
}
|
||
if err := json.Unmarshal(createDocumentRecorder.Body.Bytes(), &createdDocument); err != nil {
|
||
t.Fatalf("decode created document: %v", err)
|
||
}
|
||
documentID, _ := createdDocument.Document["_id"].(string)
|
||
if documentID == "" {
|
||
t.Fatalf("expected created document id, got %#v", createdDocument.Document["_id"])
|
||
}
|
||
|
||
queryRecorder := httptest.NewRecorder()
|
||
filter := url.QueryEscape(`{"orderNo":"SO-001"}`)
|
||
handler.ServeHTTP(
|
||
queryRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/database/collections/orders/documents"+connectionQuery+"&filter="+filter, nil),
|
||
)
|
||
if queryRecorder.Code != http.StatusOK {
|
||
t.Fatalf("query documents status: %d body=%s", queryRecorder.Code, queryRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(queryRecorder.Body.String(), `"total":1`) {
|
||
t.Fatalf("expected one query result, got %s", queryRecorder.Body.String())
|
||
}
|
||
|
||
updateDocumentRecorder := httptest.NewRecorder()
|
||
updateDocumentBody := bytes.NewBufferString(`{"document":{"orderNo":"SO-001","status":"done","amount":18}}`)
|
||
handler.ServeHTTP(
|
||
updateDocumentRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/database/collections/orders/documents/"+documentID+connectionQuery, updateDocumentBody),
|
||
)
|
||
if updateDocumentRecorder.Code != http.StatusOK {
|
||
t.Fatalf("update document status: %d body=%s", updateDocumentRecorder.Code, updateDocumentRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(updateDocumentRecorder.Body.String(), `"status":"done"`) {
|
||
t.Fatalf("expected updated status in response, got %s", updateDocumentRecorder.Body.String())
|
||
}
|
||
|
||
renameCollectionRecorder := httptest.NewRecorder()
|
||
renameCollectionBody := bytes.NewBufferString(`{"newName":"ordersArchive"}`)
|
||
handler.ServeHTTP(
|
||
renameCollectionRecorder,
|
||
httptest.NewRequest(http.MethodPatch, "/api/database/collections/orders"+connectionQuery, renameCollectionBody),
|
||
)
|
||
if renameCollectionRecorder.Code != http.StatusOK {
|
||
t.Fatalf("rename collection status: %d body=%s", renameCollectionRecorder.Code, renameCollectionRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(renameCollectionRecorder.Body.String(), `"collection":"ordersArchive"`) {
|
||
t.Fatalf("expected renamed collection in response, got %s", renameCollectionRecorder.Body.String())
|
||
}
|
||
|
||
deleteDocumentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteDocumentRecorder,
|
||
httptest.NewRequest(http.MethodDelete, "/api/database/collections/ordersArchive/documents/"+documentID+connectionQuery, nil),
|
||
)
|
||
if deleteDocumentRecorder.Code != http.StatusOK {
|
||
t.Fatalf("delete document status: %d body=%s", deleteDocumentRecorder.Code, deleteDocumentRecorder.Body.String())
|
||
}
|
||
|
||
deleteCollectionRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteCollectionRecorder,
|
||
httptest.NewRequest(http.MethodDelete, "/api/database/collections/ordersArchive"+connectionQuery, nil),
|
||
)
|
||
if deleteCollectionRecorder.Code != http.StatusOK {
|
||
t.Fatalf("delete collection status: %d body=%s", deleteCollectionRecorder.Code, deleteCollectionRecorder.Body.String())
|
||
}
|
||
|
||
overviewRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database"+connectionQuery, nil))
|
||
if overviewRecorder.Code != http.StatusOK {
|
||
t.Fatalf("overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
||
}
|
||
if strings.Contains(overviewRecorder.Body.String(), "ordersArchive") {
|
||
t.Fatalf("expected deleted collection to be absent, got %s", overviewRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserBusinessIndexCRUD(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
||
Name: "业务索引库",
|
||
URI: "mongodb://business.example:27017",
|
||
Database: "business",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create mongo connection: %v", err)
|
||
}
|
||
|
||
originalResolver := mongoConnectionStoreResolver
|
||
t.Cleanup(func() {
|
||
mongoConnectionStoreResolver = originalResolver
|
||
})
|
||
businessStore := store.NewMemoryStore()
|
||
if _, err := businessStore.CreateDatabaseCollection(ctx, "events"); err != nil {
|
||
t.Fatalf("create collection: %v", err)
|
||
}
|
||
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected mongo connection: %#v", item)
|
||
}
|
||
return businessStore, func() {}, nil
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
connectionQuery := "?connectionId=" + connection.ID
|
||
|
||
createIndexRecorder := httptest.NewRecorder()
|
||
createIndexBody := bytes.NewBufferString(`{"name":"idx-record-key","keys":{"recordKey":1},"unique":true}`)
|
||
handler.ServeHTTP(
|
||
createIndexRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/indexes"+connectionQuery, createIndexBody),
|
||
)
|
||
if createIndexRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create index status: %d body=%s", createIndexRecorder.Code, createIndexRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(createIndexRecorder.Body.String(), `"name":"idx-record-key"`) {
|
||
t.Fatalf("expected created index name, got %s", createIndexRecorder.Body.String())
|
||
}
|
||
|
||
listIndexesRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
listIndexesRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/database/collections/events/indexes"+connectionQuery, nil),
|
||
)
|
||
if listIndexesRecorder.Code != http.StatusOK {
|
||
t.Fatalf("list indexes status: %d body=%s", listIndexesRecorder.Code, listIndexesRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(listIndexesRecorder.Body.String(), `"idx-record-key"`) {
|
||
t.Fatalf("expected created index in list, got %s", listIndexesRecorder.Body.String())
|
||
}
|
||
|
||
deleteIndexRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteIndexRecorder,
|
||
httptest.NewRequest(http.MethodDelete, "/api/database/collections/events/indexes/idx-record-key"+connectionQuery, nil),
|
||
)
|
||
if deleteIndexRecorder.Code != http.StatusOK {
|
||
t.Fatalf("delete index status: %d body=%s", deleteIndexRecorder.Code, deleteIndexRecorder.Body.String())
|
||
}
|
||
if strings.Contains(deleteIndexRecorder.Body.String(), `"deleted":false`) {
|
||
t.Fatalf("expected index deletion success, got %s", deleteIndexRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserRejectsInvalidBusinessWriteInputsBeforeStoreAccess(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
||
Name: "业务校验库",
|
||
URI: "mongodb://business.example:27017",
|
||
Database: "business",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create mongo connection: %v", err)
|
||
}
|
||
|
||
originalResolver := mongoConnectionStoreResolver
|
||
t.Cleanup(func() {
|
||
mongoConnectionStoreResolver = originalResolver
|
||
})
|
||
businessStore := store.NewMemoryStore()
|
||
if _, err := businessStore.CreateDatabaseCollection(ctx, "events"); err != nil {
|
||
t.Fatalf("create collection: %v", err)
|
||
}
|
||
resolverCalls := 0
|
||
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
||
resolverCalls++
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected mongo connection: %#v", item)
|
||
}
|
||
return businessStore, func() {}, nil
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
connectionQuery := "?connectionId=" + connection.ID
|
||
|
||
invalidCollectionRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidCollectionRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections"+connectionQuery, bytes.NewBufferString(`{"name":"bad/name"}`)),
|
||
)
|
||
if invalidCollectionRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid collection name rejected, got %d body=%s", invalidCollectionRecorder.Code, invalidCollectionRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidCollectionRecorder.Body.String(), `"field":"name"`) || !strings.Contains(invalidCollectionRecorder.Body.String(), "path separators") {
|
||
t.Fatalf("expected collection name validation issue, got %s", invalidCollectionRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid collection name to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
|
||
invalidIndexRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidIndexRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/indexes"+connectionQuery, bytes.NewBufferString(`{"keys":{"$where":1}}`)),
|
||
)
|
||
if invalidIndexRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid index keys rejected, got %d body=%s", invalidIndexRecorder.Code, invalidIndexRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidIndexRecorder.Body.String(), `"field":"keys.$where"`) || !strings.Contains(invalidIndexRecorder.Body.String(), "Mongo operator") {
|
||
t.Fatalf("expected index key validation issue, got %s", invalidIndexRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid index keys to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
|
||
invalidDirectionRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidDirectionRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/indexes"+connectionQuery, bytes.NewBufferString(`{"keys":{"recordKey":"ascending"}}`)),
|
||
)
|
||
if invalidDirectionRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid index direction rejected, got %d body=%s", invalidDirectionRecorder.Code, invalidDirectionRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidDirectionRecorder.Body.String(), `"field":"keys.recordKey"`) || !strings.Contains(invalidDirectionRecorder.Body.String(), "index key direction") {
|
||
t.Fatalf("expected index direction validation issue, got %s", invalidDirectionRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid index direction to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
|
||
invalidDocumentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidDocumentRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/documents"+connectionQuery, bytes.NewBufferString(`{"document":{"profile":{"$set":"admin"}}}`)),
|
||
)
|
||
if invalidDocumentRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid document rejected, got %d body=%s", invalidDocumentRecorder.Code, invalidDocumentRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidDocumentRecorder.Body.String(), `"field":"document.profile.$set"`) || !strings.Contains(invalidDocumentRecorder.Body.String(), "Mongo operator") {
|
||
t.Fatalf("expected document Mongo operator validation issue, got %s", invalidDocumentRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid document to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
|
||
invalidNULDocumentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidNULDocumentRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/documents"+connectionQuery, bytes.NewBufferString(`{"document":{"profile":{"bad\u0000key":"admin"}}}`)),
|
||
)
|
||
if invalidNULDocumentRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected NUL document key rejected, got %d body=%s", invalidNULDocumentRecorder.Code, invalidNULDocumentRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidNULDocumentRecorder.Body.String(), "NUL bytes") {
|
||
t.Fatalf("expected document NUL validation issue, got %s", invalidNULDocumentRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected NUL document key to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
}
|
||
|
||
func TestDatabaseBrowserRejectsInvalidReadInputsBeforeStoreAccess(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
||
Name: "业务读取校验库",
|
||
URI: "mongodb://business.example:27017",
|
||
Database: "business",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create mongo connection: %v", err)
|
||
}
|
||
|
||
originalResolver := mongoConnectionStoreResolver
|
||
t.Cleanup(func() {
|
||
mongoConnectionStoreResolver = originalResolver
|
||
})
|
||
businessStore := store.NewMemoryStore()
|
||
if _, err := businessStore.CreateDatabaseCollection(ctx, "events"); err != nil {
|
||
t.Fatalf("create collection: %v", err)
|
||
}
|
||
resolverCalls := 0
|
||
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
||
resolverCalls++
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected mongo connection: %#v", item)
|
||
}
|
||
return businessStore, func() {}, nil
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
connectionQuery := "?connectionId=" + connection.ID
|
||
|
||
invalidIndexesRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidIndexesRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/database/collections/system.profile/indexes"+connectionQuery, nil),
|
||
)
|
||
if invalidIndexesRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid index collection rejected, got %d body=%s", invalidIndexesRecorder.Code, invalidIndexesRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidIndexesRecorder.Body.String(), `"field":"name"`) || !strings.Contains(invalidIndexesRecorder.Body.String(), "reserved system prefix") {
|
||
t.Fatalf("expected collection validation issue, got %s", invalidIndexesRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid index collection to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
|
||
invalidCollectionRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidCollectionRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/database/collections/bad$name/documents"+connectionQuery, nil),
|
||
)
|
||
if invalidCollectionRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid document collection rejected, got %d body=%s", invalidCollectionRecorder.Code, invalidCollectionRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidCollectionRecorder.Body.String(), `"field":"name"`) || !strings.Contains(invalidCollectionRecorder.Body.String(), "cannot contain $") {
|
||
t.Fatalf("expected document collection validation issue, got %s", invalidCollectionRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid document collection to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
|
||
invalidFilter := url.QueryEscape(`{"profile":{"$where":"return true"}}`)
|
||
invalidFilterRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidFilterRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/database/collections/events/documents"+connectionQuery+"&filter="+invalidFilter, nil),
|
||
)
|
||
if invalidFilterRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid filter rejected, got %d body=%s", invalidFilterRecorder.Code, invalidFilterRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidFilterRecorder.Body.String(), `"field":"filter.profile.$where"`) || !strings.Contains(invalidFilterRecorder.Body.String(), "$where") {
|
||
t.Fatalf("expected filter $where validation issue, got %s", invalidFilterRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid filter to be rejected before store access, resolver calls=%d", resolverCalls)
|
||
}
|
||
}
|
||
|
||
func TestMongoConnectionsTestEndpointUsesDraftOrStoredConnection(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
storedConnection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
||
Name: "业务测试库",
|
||
URI: "mongodb://business.example:27017",
|
||
Database: "business",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create mongo connection: %v", err)
|
||
}
|
||
|
||
originalResolver := mongoConnectionStoreResolver
|
||
t.Cleanup(func() {
|
||
mongoConnectionStoreResolver = originalResolver
|
||
})
|
||
probeStore := store.NewMemoryStore()
|
||
if _, err := probeStore.CreateDatabaseCollection(ctx, "probeCollection"); err != nil {
|
||
t.Fatalf("create probe collection: %v", err)
|
||
}
|
||
resolverCalls := 0
|
||
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
||
resolverCalls++
|
||
if item == nil {
|
||
t.Fatalf("expected mongo connection")
|
||
}
|
||
return probeStore, func() {}, nil
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
invalidDraftRecorder := httptest.NewRecorder()
|
||
invalidDraftBody := bytes.NewBufferString(`{"uri":"mongodb://<user>:<password>@draft.example:27017","database":"draft-db"}`)
|
||
handler.ServeHTTP(invalidDraftRecorder, httptest.NewRequest(http.MethodPost, "/api/mongo/connections/test", invalidDraftBody))
|
||
if invalidDraftRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid draft connection test rejected, got %d body=%s", invalidDraftRecorder.Code, invalidDraftRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidDraftRecorder.Body.String(), "placeholder markers") {
|
||
t.Fatalf("expected draft URI validation issue, got %s", invalidDraftRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 0 {
|
||
t.Fatalf("expected invalid draft URI to be rejected before resolver, resolver calls=%d", resolverCalls)
|
||
}
|
||
|
||
draftRecorder := httptest.NewRecorder()
|
||
draftBody := bytes.NewBufferString(`{"uri":"mongodb://draft.example:27017","database":"draft-db"}`)
|
||
handler.ServeHTTP(draftRecorder, httptest.NewRequest(http.MethodPost, "/api/mongo/connections/test", draftBody))
|
||
if draftRecorder.Code != http.StatusOK {
|
||
t.Fatalf("draft connection test status: %d body=%s", draftRecorder.Code, draftRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(draftRecorder.Body.String(), `"probeCollection"`) {
|
||
t.Fatalf("expected probe collection in test response, got %s", draftRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 1 {
|
||
t.Fatalf("expected valid draft to call resolver once, got %d", resolverCalls)
|
||
}
|
||
|
||
storedRecorder := httptest.NewRecorder()
|
||
storedBody := bytes.NewBufferString(`{"id":"` + storedConnection.ID + `"}`)
|
||
handler.ServeHTTP(storedRecorder, httptest.NewRequest(http.MethodPost, "/api/mongo/connections/test", storedBody))
|
||
if storedRecorder.Code != http.StatusOK {
|
||
t.Fatalf("stored connection test status: %d body=%s", storedRecorder.Code, storedRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(storedRecorder.Body.String(), `"connectionId":"`+storedConnection.ID+`"`) {
|
||
t.Fatalf("expected stored connection id in response, got %s", storedRecorder.Body.String())
|
||
}
|
||
if resolverCalls != 2 {
|
||
t.Fatalf("expected stored connection test to call resolver, got %d", resolverCalls)
|
||
}
|
||
}
|
||
|
||
func TestRunsListReturnsSummaryPayload(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
run := domain.RunRecord{
|
||
ID: "run-summary-1",
|
||
WorkflowID: "workflow-1",
|
||
WorkflowName: "Summary Workflow",
|
||
ProjectID: "project-1",
|
||
ProjectName: "Summary Project",
|
||
Trigger: domain.RunTriggerScheduled,
|
||
Status: domain.RunStatusRunning,
|
||
CurrentNodeID: "node-quality",
|
||
NodeExecutions: []domain.NodeExecutionRecord{
|
||
{
|
||
NodeID: "node-quality",
|
||
NodeLabel: "视频质检",
|
||
Status: domain.NodeExecutionStatusRunning,
|
||
Logs: []domain.LogEntryRecord{
|
||
{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: "heavy node log"},
|
||
},
|
||
Artifacts: []domain.ExecutionArtifact{
|
||
{ID: "artifact-1", RunID: "run-summary-1", NodeID: "node-quality", Name: "quality-report.json", Type: domain.PortTypeJSON, Size: "2 KB", Path: "/tmp/quality-report.json"},
|
||
},
|
||
ResourceSeries: []domain.ResourceSample{
|
||
{Timestamp: "2026-04-13T01:00:00Z", CPU: 90, Memory: 1024},
|
||
},
|
||
},
|
||
},
|
||
Logs: []domain.LogEntryRecord{
|
||
{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: "top level log"},
|
||
},
|
||
ResourceSeries: []domain.ResourceSample{
|
||
{Timestamp: "2026-04-13T01:00:00Z", CPU: 80, Memory: 2048},
|
||
},
|
||
Artifacts: []domain.ExecutionArtifact{
|
||
{ID: "artifact-2", RunID: "run-summary-1", NodeID: "node-quality", Name: "clip.mp4", Type: domain.PortTypeVideo, Size: "120 MB", Path: "/tmp/clip.mp4"},
|
||
},
|
||
K8sRefs: []domain.K8sRef{
|
||
{NodeID: "node-quality", JobName: "quality-job", Namespace: "droboticflow"},
|
||
},
|
||
CreatedAt: "2026-04-13T01:00:00Z",
|
||
UpdatedAt: "2026-04-13T01:00:00Z",
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("runs list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
body := recorder.Body.String()
|
||
for _, forbidden := range []string{"\"logs\"", "\"artifacts\"", "\"resourceSeries\"", "\"nodeExecutions\"", "\"k8sRefs\""} {
|
||
if strings.Contains(body, forbidden) {
|
||
t.Fatalf("runs list should not include %s in summary payload: %s", forbidden, body)
|
||
}
|
||
}
|
||
|
||
var response struct {
|
||
Items []map[string]any `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode runs response: %v", err)
|
||
}
|
||
if len(response.Items) != 1 {
|
||
t.Fatalf("expected 1 run summary, got %d", len(response.Items))
|
||
}
|
||
if response.Items[0]["id"] != run.ID {
|
||
t.Fatalf("summary id = %v, want %s", response.Items[0]["id"], run.ID)
|
||
}
|
||
if response.Items[0]["currentNodeId"] != run.CurrentNodeID {
|
||
t.Fatalf("summary currentNodeId = %v, want %s", response.Items[0]["currentNodeId"], run.CurrentNodeID)
|
||
}
|
||
}
|
||
|
||
func TestRunsLineageReturnsOperatorAndArtifactEdges(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
sourceArtifact := domain.ExecutionArtifact{
|
||
ID: "artifact-source",
|
||
RunID: "run-lineage-1",
|
||
NodeID: "node-source",
|
||
PortID: "frames",
|
||
PortName: "frames",
|
||
Name: "frames.json",
|
||
Type: domain.PortTypeJSON,
|
||
Size: "1 KB",
|
||
Path: "bos://bucket/run-lineage-1/frames.json",
|
||
}
|
||
resultArtifact := domain.ExecutionArtifact{
|
||
ID: "artifact-result",
|
||
RunID: "run-lineage-1",
|
||
NodeID: "node-transform",
|
||
Name: "result.json",
|
||
Type: domain.PortTypeJSON,
|
||
Size: "2 KB",
|
||
Path: "bos://bucket/run-lineage-1/result.json",
|
||
}
|
||
run := domain.RunRecord{
|
||
ID: "run-lineage-1",
|
||
WorkflowID: "workflow-lineage",
|
||
WorkflowName: "Lineage Workflow",
|
||
ProjectID: "project-lineage",
|
||
ProjectName: "Lineage Project",
|
||
Trigger: domain.RunTriggerManual,
|
||
Status: domain.RunStatusCompleted,
|
||
NodeExecutions: []domain.NodeExecutionRecord{
|
||
{
|
||
NodeID: "node-source",
|
||
NodeLabel: "数据读取",
|
||
OperatorID: "operator-source",
|
||
Status: domain.NodeExecutionStatusCompleted,
|
||
Logs: []domain.LogEntryRecord{},
|
||
Artifacts: []domain.ExecutionArtifact{sourceArtifact},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
},
|
||
{
|
||
NodeID: "node-transform",
|
||
NodeLabel: "目标检测",
|
||
OperatorID: "operator-detector",
|
||
OperatorVersion: "2.1.0",
|
||
OperatorImage: "registry.example/detector:2.1.0",
|
||
OperatorImageID: "registry.example/detector@sha256:abc123",
|
||
Status: domain.NodeExecutionStatusCompleted,
|
||
InputArtifacts: []domain.ExecutionArtifact{sourceArtifact},
|
||
InputLineage: []domain.InputLineageRecord{
|
||
{
|
||
WorkflowEdgeID: "edge-source-transform",
|
||
SourceNodeID: "node-source",
|
||
SourceHandle: "frames",
|
||
TargetNodeID: "node-transform",
|
||
TargetHandle: "detector-input",
|
||
Artifact: sourceArtifact,
|
||
},
|
||
},
|
||
Logs: []domain.LogEntryRecord{},
|
||
Artifacts: []domain.ExecutionArtifact{resultArtifact},
|
||
DataRefs: []domain.DataRefRecord{{ArtifactID: "artifact-result", Kind: "json", PayloadPath: "/workspace/result.json"}},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
},
|
||
},
|
||
Artifacts: []domain.ExecutionArtifact{sourceArtifact, resultArtifact},
|
||
Logs: []domain.LogEntryRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
K8sRefs: []domain.K8sRef{},
|
||
CreatedAt: "2026-04-13T01:00:00Z",
|
||
UpdatedAt: "2026-04-13T01:01:00Z",
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-lineage-1/lineage", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("lineage status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var response domain.RunLineageRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode lineage response: %v", err)
|
||
}
|
||
if response.RunID != run.ID || response.WorkflowID != run.WorkflowID {
|
||
t.Fatalf("unexpected lineage identity: %+v", response)
|
||
}
|
||
if len(response.Nodes) != 2 {
|
||
t.Fatalf("expected 2 lineage nodes, got %#v", response.Nodes)
|
||
}
|
||
transform := response.Nodes[1]
|
||
if transform.OperatorVersion != "2.1.0" || transform.OperatorImageID != "registry.example/detector@sha256:abc123" {
|
||
t.Fatalf("unexpected operator snapshot: %+v", transform)
|
||
}
|
||
if len(transform.InputArtifacts) != 1 || transform.InputArtifacts[0].ID != sourceArtifact.ID {
|
||
t.Fatalf("unexpected input artifacts: %+v", transform.InputArtifacts)
|
||
}
|
||
if len(transform.OutputArtifacts) != 1 || transform.OutputArtifacts[0].ID != resultArtifact.ID {
|
||
t.Fatalf("unexpected output artifacts: %+v", transform.OutputArtifacts)
|
||
}
|
||
if len(response.Edges) != 1 {
|
||
t.Fatalf("expected 1 lineage edge, got %#v", response.Edges)
|
||
}
|
||
edge := response.Edges[0]
|
||
if edge.SourceNodeID != "node-source" || edge.SourceNodeLabel != "数据读取" || edge.SourceArtifactID != sourceArtifact.ID || edge.TargetNodeID != "node-transform" {
|
||
t.Fatalf("unexpected lineage edge: %+v", edge)
|
||
}
|
||
if edge.WorkflowEdgeID != "edge-source-transform" || edge.SourceHandle != "frames" || edge.TargetHandle != "detector-input" {
|
||
t.Fatalf("unexpected lineage edge handles: %+v", edge)
|
||
}
|
||
|
||
artifactRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(artifactRecorder, httptest.NewRequest(http.MethodGet, "/api/artifacts/"+sourceArtifact.ID+"/lineage?runId="+run.ID, nil))
|
||
if artifactRecorder.Code != http.StatusOK {
|
||
t.Fatalf("artifact lineage status: %d body=%s", artifactRecorder.Code, artifactRecorder.Body.String())
|
||
}
|
||
var artifactResponse domain.ArtifactLineageRecord
|
||
if err := json.Unmarshal(artifactRecorder.Body.Bytes(), &artifactResponse); err != nil {
|
||
t.Fatalf("decode artifact lineage response: %v", err)
|
||
}
|
||
if artifactResponse.ArtifactID != sourceArtifact.ID || artifactResponse.Artifact.Name != sourceArtifact.Name {
|
||
t.Fatalf("unexpected artifact identity: %+v", artifactResponse)
|
||
}
|
||
if artifactResponse.Producer == nil || artifactResponse.Producer.NodeID != "node-source" {
|
||
t.Fatalf("expected source artifact producer node-source, got %+v", artifactResponse.Producer)
|
||
}
|
||
if len(artifactResponse.Consumers) != 1 || artifactResponse.Consumers[0].NodeID != "node-transform" {
|
||
t.Fatalf("expected source artifact consumer node-transform, got %+v", artifactResponse.Consumers)
|
||
}
|
||
if len(artifactResponse.DownstreamEdges) != 1 || artifactResponse.DownstreamEdges[0].TargetHandle != "detector-input" {
|
||
t.Fatalf("unexpected source artifact downstream edges: %+v", artifactResponse.DownstreamEdges)
|
||
}
|
||
if len(artifactResponse.Nodes) != 2 || len(artifactResponse.Edges) != 1 {
|
||
t.Fatalf("unexpected source artifact related graph: nodes=%+v edges=%+v", artifactResponse.Nodes, artifactResponse.Edges)
|
||
}
|
||
|
||
resultRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(resultRecorder, httptest.NewRequest(http.MethodGet, "/api/artifacts/"+resultArtifact.ID+"/lineage?runId="+run.ID, nil))
|
||
if resultRecorder.Code != http.StatusOK {
|
||
t.Fatalf("result artifact lineage status: %d body=%s", resultRecorder.Code, resultRecorder.Body.String())
|
||
}
|
||
var resultResponse domain.ArtifactLineageRecord
|
||
if err := json.Unmarshal(resultRecorder.Body.Bytes(), &resultResponse); err != nil {
|
||
t.Fatalf("decode result artifact lineage response: %v", err)
|
||
}
|
||
if resultResponse.Producer == nil || resultResponse.Producer.NodeID != "node-transform" {
|
||
t.Fatalf("expected result artifact producer node-transform, got %+v", resultResponse.Producer)
|
||
}
|
||
if len(resultResponse.Consumers) != 0 {
|
||
t.Fatalf("expected result artifact to have no consumers, got %+v", resultResponse.Consumers)
|
||
}
|
||
if len(resultResponse.UpstreamEdges) != 1 || resultResponse.UpstreamEdges[0].SourceArtifactID != sourceArtifact.ID {
|
||
t.Fatalf("unexpected result artifact upstream edges: %+v", resultResponse.UpstreamEdges)
|
||
}
|
||
|
||
missingRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(missingRecorder, httptest.NewRequest(http.MethodGet, "/api/artifacts/missing-artifact/lineage?runId="+run.ID, nil))
|
||
if missingRecorder.Code != http.StatusNotFound {
|
||
t.Fatalf("expected missing artifact lineage to be 404, got %d body=%s", missingRecorder.Code, missingRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportCreatesPlatformRunArtifact(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-operator-acceptance-20260623010101",
|
||
"status": "passed",
|
||
"workspace": "/workspace/system/acceptance/general/poc-operator-acceptance-20260623010101",
|
||
"acceptanceScope": "dependency-isolation",
|
||
"fullExternalAcceptance": false,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 26,
|
||
"coveredOperatorCount": 26,
|
||
"allOperatorCoverageComplete": false,
|
||
"blockingOperatorCoverageComplete": true,
|
||
"missingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
||
"blockingMissingCoverage": []string{},
|
||
"requiredExternals": []string{"bos", "mongo"},
|
||
"statusCounts": map[string]any{"passed": 28, "skipped": 2},
|
||
"failedCount": 0,
|
||
"skippedCount": 2,
|
||
"optionalSkippedCount": 2,
|
||
"requiredSkippedCount": 0,
|
||
"externalVerificationPassed": []string{"bos", "mongo"},
|
||
"externalVerificationMissing": []string{},
|
||
"externalVerificationPassedCount": 2,
|
||
"externalVerificationRequiredCount": 2,
|
||
},
|
||
"source": map[string]any{
|
||
"environment": "poc",
|
||
"namespace": "droboticflow",
|
||
"jobName": "droboticflow-operator-acceptance-20260623010101",
|
||
"image": "ccr.example/droboticflow-operator-runner:tag",
|
||
"imageId": "ccr.example/droboticflow-operator-runner@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("import status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var imported struct {
|
||
Run domain.RunRecord `json:"run"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &imported); err != nil {
|
||
t.Fatalf("decode import response: %v", err)
|
||
}
|
||
if imported.Run.Status != domain.RunStatusCompleted {
|
||
t.Fatalf("run status = %s, want completed", imported.Run.Status)
|
||
}
|
||
if imported.Run.WorkflowID != pocAcceptanceWorkflowID || imported.Run.ProjectID != pocAcceptanceProjectID {
|
||
t.Fatalf("unexpected platform linkage: workflow=%s project=%s", imported.Run.WorkflowID, imported.Run.ProjectID)
|
||
}
|
||
|
||
listRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
||
if listRecorder.Code != http.StatusOK {
|
||
t.Fatalf("runs list status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(listRecorder.Body.String(), imported.Run.ID) {
|
||
t.Fatalf("runs list should contain imported run: %s", listRecorder.Body.String())
|
||
}
|
||
|
||
artifactRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(artifactRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/"+imported.Run.ID+"/artifacts", nil))
|
||
if artifactRecorder.Code != http.StatusOK {
|
||
t.Fatalf("artifacts status: %d body=%s", artifactRecorder.Code, artifactRecorder.Body.String())
|
||
}
|
||
var artifactResponse struct {
|
||
Items []domain.ExecutionArtifact `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(artifactRecorder.Body.Bytes(), &artifactResponse); err != nil {
|
||
t.Fatalf("decode artifacts: %v", err)
|
||
}
|
||
if len(artifactResponse.Items) != 1 || artifactResponse.Items[0].Name != "poc-operator-full-acceptance-result.json" {
|
||
t.Fatalf("unexpected artifacts: %+v", artifactResponse.Items)
|
||
}
|
||
if artifactResponse.Items[0].Data["acceptanceScope"] != "dependency-isolation" {
|
||
t.Fatalf("artifact data missing acceptance summary: %+v", artifactResponse.Items[0].Data)
|
||
}
|
||
importSource, ok := artifactResponse.Items[0].Data["importSource"].(map[string]any)
|
||
if !ok {
|
||
t.Fatalf("artifact data missing import source: %+v", artifactResponse.Items[0].Data)
|
||
}
|
||
if importSource["image"] != "ccr.example/droboticflow-operator-runner:tag" || importSource["imageId"] != "ccr.example/droboticflow-operator-runner@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" {
|
||
t.Fatalf("unexpected import source: %+v", importSource)
|
||
}
|
||
|
||
secondRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(secondRecorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if secondRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("second import status: %d body=%s", secondRecorder.Code, secondRecorder.Body.String())
|
||
}
|
||
listAgainRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listAgainRecorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
||
var listAgain struct {
|
||
Items []domain.RunSummary `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(listAgainRecorder.Body.Bytes(), &listAgain); err != nil {
|
||
t.Fatalf("decode runs after second import: %v", err)
|
||
}
|
||
if len(listAgain.Items) != 1 {
|
||
t.Fatalf("reimport should update the same run, got %d runs", len(listAgain.Items))
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsWeakDependencyIsolationSourceDigest(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-operator-acceptance-weak-source-digest",
|
||
"status": "passed",
|
||
"acceptanceScope": "dependency-isolation",
|
||
"fullExternalAcceptance": false,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 26,
|
||
"externalVerificationPassed": []string{"mongo"},
|
||
},
|
||
"source": map[string]any{
|
||
"environment": "poc",
|
||
"namespace": "droboticflow",
|
||
"jobName": "droboticflow-operator-acceptance-weak-source-digest",
|
||
"image": "ccr.example/droboticflow-operator-runner:tag",
|
||
"imageId": "ccr.example/droboticflow-operator-runner@sha256:abc",
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "source.imageId") {
|
||
t.Fatalf("expected source.imageId validation issue in %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsSourceDigestRepositoryMismatch(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-operator-acceptance-mismatched-source-digest",
|
||
"status": "passed",
|
||
"acceptanceScope": "dependency-isolation",
|
||
"fullExternalAcceptance": false,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 26,
|
||
"externalVerificationPassed": []string{"mongo"},
|
||
},
|
||
"source": map[string]any{
|
||
"environment": "poc",
|
||
"namespace": "droboticflow",
|
||
"jobName": "droboticflow-operator-acceptance-mismatched-source-digest",
|
||
"image": "ccr.example/dataflow-poc/droboticflow-operator-runner:tag",
|
||
"imageId": "ccr.example/dataflow-poc/other-runner@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, "source.imageId") || !strings.Contains(body, "must match source.image repository") {
|
||
t.Fatalf("expected source.imageId repository validation issue in %s", body)
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportAllowsSharedAPITokenWhenSessionAuthIsEnabled(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-acceptance-token")
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-strict-preflight-token",
|
||
"status": "failed",
|
||
"acceptanceScope": "full",
|
||
"fullExternalAcceptance": false,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 0,
|
||
"coveredOperatorCount": 0,
|
||
"allOperatorCoverageComplete": false,
|
||
"blockingOperatorCoverageComplete": false,
|
||
"missingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
||
"blockingMissingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
||
"failedCount": 1,
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload))
|
||
request.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
||
handler.ServeHTTP(recorder, request)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("import status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
projectRecorder := httptest.NewRecorder()
|
||
projectRequest := httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader([]byte(`{"name":"Token Should Not Create Project"}`)))
|
||
projectRequest.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
||
handler.ServeHTTP(projectRecorder, projectRequest)
|
||
if projectRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected regular business writes to still require login, got %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocGPUWorkflowAcceptanceAllowsSharedAPITokenWhenSessionAuthIsEnabled(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-acceptance-token")
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
rawPayload := []byte(`{"sourcePath":"/workspace/datasets/fixtures/poc-operator-acceptance/xplatform"}`)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-gpu-workflow", bytes.NewReader(rawPayload))
|
||
request.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
||
handler.ServeHTTP(recorder, request)
|
||
if recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("GPU workflow acceptance status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var body struct {
|
||
Project domain.ProjectRecord `json:"project"`
|
||
Workflow domain.WorkflowRecord `json:"workflow"`
|
||
Run domain.RunRecord `json:"run"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||
t.Fatalf("decode response: %v", err)
|
||
}
|
||
if body.Project.Name != pocGPUWorkflowAcceptanceProjectName {
|
||
t.Fatalf("project name = %q", body.Project.Name)
|
||
}
|
||
if body.Workflow.Name != pocGPUWorkflowAcceptanceWorkflowName {
|
||
t.Fatalf("workflow name = %q", body.Workflow.Name)
|
||
}
|
||
if body.Run.Status != domain.RunStatusQueued || body.Run.Trigger != domain.RunTriggerManual {
|
||
t.Fatalf("run should be queued manual acceptance run, got status=%q trigger=%q", body.Run.Status, body.Run.Trigger)
|
||
}
|
||
if body.Run.ProjectID != body.Project.ID || body.Run.WorkflowID != body.Workflow.ID {
|
||
t.Fatalf("run links project=%q workflow=%q, want project=%q workflow=%q", body.Run.ProjectID, body.Run.WorkflowID, body.Project.ID, body.Workflow.ID)
|
||
}
|
||
if len(body.Workflow.Nodes) != 2 {
|
||
t.Fatalf("workflow node count = %d", len(body.Workflow.Nodes))
|
||
}
|
||
sourceNode := body.Workflow.Nodes[0]
|
||
if sourceNode.ID != "source-1" || sourceNode.Data.GPU != "1" {
|
||
t.Fatalf("source node should request one GPU, got %+v", sourceNode)
|
||
}
|
||
params := map[string]string{}
|
||
for _, param := range sourceNode.Data.Hyperparams {
|
||
params[param.Key] = param.Value
|
||
}
|
||
if params["DATA_SOURCE"] != "LOCAL" || params["SOURCE_PATH"] != "/workspace/datasets/fixtures/poc-operator-acceptance/xplatform" {
|
||
t.Fatalf("source hyperparams = %+v", params)
|
||
}
|
||
|
||
secondRecorder := httptest.NewRecorder()
|
||
secondRequest := httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-gpu-workflow", bytes.NewReader([]byte(`{"sourcePath":"/workspace/datasets/fixtures/poc-operator-acceptance/xplatform-v2"}`)))
|
||
secondRequest.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
||
handler.ServeHTTP(secondRecorder, secondRequest)
|
||
if secondRecorder.Code != http.StatusConflict {
|
||
t.Fatalf("expected active acceptance run conflict, got %d body=%s", secondRecorder.Code, secondRecorder.Body.String())
|
||
}
|
||
var conflictBody map[string]string
|
||
if err := json.Unmarshal(secondRecorder.Body.Bytes(), &conflictBody); err != nil {
|
||
t.Fatalf("decode conflict response: %v", err)
|
||
}
|
||
if conflictBody["code"] != "workflow_run_active" {
|
||
t.Fatalf("expected workflow_run_active, got %#v", conflictBody)
|
||
}
|
||
projects, err := dataStore.ListProjectsByOrganization(context.Background(), domain.DefaultOrganizationID)
|
||
if err != nil {
|
||
t.Fatalf("list projects: %v", err)
|
||
}
|
||
pocProjectCount := 0
|
||
for _, project := range projects {
|
||
if project.Name == pocGPUWorkflowAcceptanceProjectName {
|
||
pocProjectCount++
|
||
}
|
||
}
|
||
if pocProjectCount != 1 {
|
||
t.Fatalf("expected one reusable POC GPU acceptance project, got %d", pocProjectCount)
|
||
}
|
||
workflows, err := dataStore.ListWorkflowsByProject(context.Background(), body.Project.ID)
|
||
if err != nil {
|
||
t.Fatalf("list workflows: %v", err)
|
||
}
|
||
if len(workflows) != 1 {
|
||
t.Fatalf("expected one reusable POC GPU acceptance workflow, got %d", len(workflows))
|
||
}
|
||
|
||
projectRecorder := httptest.NewRecorder()
|
||
projectRequest := httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader([]byte(`{"name":"Token Should Not Create Project"}`)))
|
||
projectRequest.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
||
handler.ServeHTTP(projectRecorder, projectRequest)
|
||
if projectRecorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("expected regular business writes to still require login, got %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestLatestPocGPUWorkflowRunRecognizesLiveWorkflowAcceptanceNames(t *testing.T) {
|
||
runs := []domain.RunRecord{
|
||
{
|
||
ID: "run-old-dedicated",
|
||
WorkflowName: pocGPUWorkflowAcceptanceWorkflowName,
|
||
ProjectName: pocGPUWorkflowAcceptanceProjectName,
|
||
Status: domain.RunStatusCompleted,
|
||
UpdatedAt: "2026-06-30T01:00:00Z",
|
||
},
|
||
{
|
||
ID: "run-new-live-workflow",
|
||
WorkflowName: liveWorkflowAcceptanceWorkflowName,
|
||
ProjectName: liveWorkflowAcceptanceProjectName,
|
||
Status: domain.RunStatusCompleted,
|
||
UpdatedAt: "2026-06-30T02:00:00Z",
|
||
},
|
||
}
|
||
|
||
run, found := latestPocGPUWorkflowRun(runs)
|
||
if !found {
|
||
t.Fatalf("expected GPU workflow acceptance run to be found")
|
||
}
|
||
if run.ID != "run-new-live-workflow" {
|
||
t.Fatalf("latest GPU workflow run = %q, want run-new-live-workflow", run.ID)
|
||
}
|
||
}
|
||
|
||
func TestPocGPUWorkflowAcceptanceRejectsSourceOutsideWorkspace(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-acceptance-token")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-gpu-workflow", bytes.NewReader([]byte(`{"sourcePath":"/tmp/xplatform"}`)))
|
||
request.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
||
handler.ServeHTTP(recorder, request)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected source path validation error, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "sourcePath") || !strings.Contains(recorder.Body.String(), "workspace mount") {
|
||
t.Fatalf("expected sourcePath workspace validation issue, got %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRequiresLoginOrAPITokenWhenSessionAuthIsEnabled(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-acceptance-token")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader([]byte(`{"result":{"status":"passed"}}`))),
|
||
)
|
||
if recorder.Code != http.StatusUnauthorized {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "API token is required") {
|
||
t.Fatalf("expected API token error, got %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportValidatesRequiredSummaryFields(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader([]byte(`{"result":{"status":"passed"}}`))),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, field := range []string{"result.runId", "result.acceptanceScope", "result.fullExternalAcceptance", "result.operatorCount", "result.operatorPassedCount"} {
|
||
if !strings.Contains(body, field) {
|
||
t.Fatalf("expected validation field %s in %s", field, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsInconsistentFullAcceptance(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-operator-acceptance-fake-full",
|
||
"status": "passed",
|
||
"acceptanceScope": "dependency-isolation",
|
||
"fullExternalAcceptance": true,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 26,
|
||
"coveredOperatorCount": 26,
|
||
"allOperatorCoverageComplete": false,
|
||
"blockingOperatorCoverageComplete": true,
|
||
"missingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
||
"blockingMissingCoverage": []string{},
|
||
"failedCount": 0,
|
||
"requiredSkippedCount": 0,
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, field := range []string{
|
||
"result.acceptanceScope",
|
||
"result.requiredExternals",
|
||
"result.externalVerificationPassed",
|
||
"result.externalVerificationPassedCount",
|
||
"result.externalVerificationRequiredCount",
|
||
"result.operatorPassedCount",
|
||
"result.coveredOperatorCount",
|
||
"result.allOperatorCoverageComplete",
|
||
"result.missingCoverage",
|
||
} {
|
||
if !strings.Contains(body, field) {
|
||
t.Fatalf("expected validation field %s in %s", field, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func pocAcceptanceStaticEvidence(operatorCount int) map[string]any {
|
||
runtimeSources := make([]string, 0, operatorCount)
|
||
repositoryManifests := make([]string, 0, operatorCount)
|
||
for index := 0; index < operatorCount; index++ {
|
||
slug := fmt.Sprintf("operator_%02d", index)
|
||
runtimeSources = append(runtimeSources, "backend/operators/builtin/"+slug+"/operator.py")
|
||
repositoryManifests = append(repositoryManifests, "backend/operators/builtin/"+slug+"/repository.json")
|
||
}
|
||
return map[string]any{
|
||
"operatorFileCount": operatorCount,
|
||
"repositoryManifestCount": operatorCount,
|
||
"repositoryManifestErrors": []string{},
|
||
"duplicateOperatorIds": []string{},
|
||
"duplicateOperatorNames": []string{},
|
||
"legacyOperators": []string{},
|
||
"builtinHelperCallOperators": []string{},
|
||
"hardcodedExternalApiSources": []string{},
|
||
"helperBuiltinKeys": []string{},
|
||
"runtimeSources": runtimeSources,
|
||
"repositoryManifests": repositoryManifests,
|
||
}
|
||
}
|
||
|
||
func pocAcceptanceFixtureSources() map[string]any {
|
||
return map[string]any{
|
||
"allowGeneratedFixtures": false,
|
||
"items": map[string]any{
|
||
"csv": map[string]any{"path": "/workspace/datasets/fixtures/poc-operator-acceptance/csv/records.csv", "generated": false},
|
||
"json": map[string]any{"path": "/workspace/datasets/fixtures/poc-operator-acceptance/json/records.json", "generated": false},
|
||
"lerobot": map[string]any{"path": "/workspace/datasets/fixtures/poc-operator-acceptance/lerobot", "generated": false},
|
||
"model": map[string]any{"path": "/workspace/datasets/fixtures/poc-operator-acceptance/model/quality-gate.json", "generated": false},
|
||
"rosbag": map[string]any{"path": "/workspace/datasets/fixtures/poc-operator-acceptance/rosbag", "generated": false},
|
||
"xplatform": map[string]any{"path": "/workspace/datasets/fixtures/poc-operator-acceptance/xplatform", "generated": false},
|
||
},
|
||
}
|
||
}
|
||
|
||
func pocAcceptanceFullResult(runID string) map[string]any {
|
||
return map[string]any{
|
||
"runId": runID,
|
||
"status": "passed",
|
||
"acceptanceScope": "full",
|
||
"fullExternalAcceptance": true,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 28,
|
||
"coveredOperatorCount": 28,
|
||
"allOperatorCoverageComplete": true,
|
||
"blockingOperatorCoverageComplete": true,
|
||
"missingCoverage": []string{},
|
||
"blockingMissingCoverage": []string{},
|
||
"requiredExternals": []string{"bos", "dcloud", "mongo"},
|
||
"externalVerificationPassed": []string{"bos", "dcloud", "mongo"},
|
||
"externalVerificationMissing": []string{},
|
||
"externalVerificationPassedCount": 3,
|
||
"externalVerificationRequiredCount": 3,
|
||
"operatorRepositoryRequiredCount": 28,
|
||
"operatorRepositoryCount": 28,
|
||
"operatorRepositoryCoverageComplete": true,
|
||
"operatorRepositoryPolicyClean": true,
|
||
"operatorRepositoryPolicyViolations": []string{},
|
||
"static": pocAcceptanceStaticEvidence(28),
|
||
"failedCount": 0,
|
||
"requiredSkippedCount": 0,
|
||
"generatedFixtureCount": 0,
|
||
"realFixtureCount": 6,
|
||
"allFixturesReal": true,
|
||
"fixtureSources": pocAcceptanceFixtureSources(),
|
||
}
|
||
}
|
||
|
||
func pocAcceptanceImportSource() map[string]any {
|
||
return map[string]any{
|
||
"environment": "poc",
|
||
"namespace": "droboticflow",
|
||
"jobName": "droboticflow-op-accept-full",
|
||
"image": "ccr.example/droboticflow-operator-runner:full",
|
||
"imageId": "ccr.example/droboticflow-operator-runner@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportAcceptsFullAcceptanceWithK8sSourceEvidence(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": pocAcceptanceFullResult("poc-operator-acceptance-full-source"),
|
||
"source": pocAcceptanceImportSource(),
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("import status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var imported struct {
|
||
Run domain.RunRecord `json:"run"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &imported); err != nil {
|
||
t.Fatalf("decode import response: %v", err)
|
||
}
|
||
if imported.Run.Status != domain.RunStatusCompleted {
|
||
t.Fatalf("run status = %s, want completed", imported.Run.Status)
|
||
}
|
||
if len(imported.Run.K8sRefs) != 1 || imported.Run.K8sRefs[0].JobName != "droboticflow-op-accept-full" {
|
||
t.Fatalf("unexpected k8s refs: %+v", imported.Run.K8sRefs)
|
||
}
|
||
if len(imported.Run.Artifacts) != 1 {
|
||
t.Fatalf("expected 1 artifact, got %d", len(imported.Run.Artifacts))
|
||
}
|
||
importSource, ok := imported.Run.Artifacts[0].Data["importSource"].(map[string]any)
|
||
if !ok {
|
||
t.Fatalf("artifact data missing import source: %+v", imported.Run.Artifacts[0].Data)
|
||
}
|
||
if importSource["image"] != "ccr.example/droboticflow-operator-runner:full" || importSource["imageId"] != "ccr.example/droboticflow-operator-runner@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" {
|
||
t.Fatalf("unexpected import source: %+v", importSource)
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsFullAcceptanceWithoutK8sSourceEvidence(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": pocAcceptanceFullResult("poc-operator-acceptance-full-missing-source"),
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, field := range []string{"source.namespace", "source.jobName", "source.image", "source.imageId"} {
|
||
if !strings.Contains(body, field) {
|
||
t.Fatalf("expected validation field %s in %s", field, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsFullAcceptanceWithoutSha256ImageDigest(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
source := pocAcceptanceImportSource()
|
||
source["imageId"] = "ccr.example/droboticflow-operator-runner@sha256:not-a-digest"
|
||
payload := map[string]any{
|
||
"result": pocAcceptanceFullResult("poc-operator-acceptance-full-invalid-image-id"),
|
||
"source": source,
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "source.imageId") {
|
||
t.Fatalf("expected source.imageId validation issue in %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsFullAcceptanceWithoutFixtureSourceEvidence(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
result := pocAcceptanceFullResult("poc-operator-acceptance-full-missing-fixture-sources")
|
||
delete(result, "fixtureSources")
|
||
payload := map[string]any{
|
||
"result": result,
|
||
"source": pocAcceptanceImportSource(),
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "result.fixtureSources") {
|
||
t.Fatalf("expected result.fixtureSources validation issue in %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsMalformedFullFixtureSources(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
result := pocAcceptanceFullResult("poc-operator-acceptance-full-malformed-fixture-sources")
|
||
result["fixtureSources"] = map[string]any{
|
||
"allowGeneratedFixtures": false,
|
||
"items": map[string]any{
|
||
"csv": map[string]any{"generated": false},
|
||
"xplatform": "not-a-fixture-source-object",
|
||
},
|
||
}
|
||
payload := map[string]any{
|
||
"result": result,
|
||
"source": pocAcceptanceImportSource(),
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, field := range []string{"result.fixtureSources.items.csv.path", "result.fixtureSources.items.xplatform", "result.realFixtureCount"} {
|
||
if !strings.Contains(body, field) {
|
||
t.Fatalf("expected validation field %s in %s", field, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsFullAcceptanceWithoutRequiredFixtureSource(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
result := pocAcceptanceFullResult("poc-operator-acceptance-full-missing-required-fixture-source")
|
||
fixtureSources := pocAcceptanceFixtureSources()
|
||
items := fixtureSources["items"].(map[string]any)
|
||
delete(items, "rosbag")
|
||
result["fixtureSources"] = fixtureSources
|
||
result["realFixtureCount"] = 5
|
||
payload := map[string]any{
|
||
"result": result,
|
||
"source": pocAcceptanceImportSource(),
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "result.fixtureSources.items.rosbag") {
|
||
t.Fatalf("expected missing rosbag fixture source validation issue in %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsFullFixtureSourceCountMismatch(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
result := pocAcceptanceFullResult("poc-operator-acceptance-full-fixture-count-mismatch")
|
||
result["realFixtureCount"] = 5
|
||
payload := map[string]any{
|
||
"result": result,
|
||
"source": pocAcceptanceImportSource(),
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "result.realFixtureCount") {
|
||
t.Fatalf("expected result.realFixtureCount validation issue in %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsIncompleteExternalVerification(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-operator-acceptance-missing-external-proof",
|
||
"status": "passed",
|
||
"acceptanceScope": "full",
|
||
"fullExternalAcceptance": true,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 28,
|
||
"coveredOperatorCount": 28,
|
||
"allOperatorCoverageComplete": true,
|
||
"blockingOperatorCoverageComplete": true,
|
||
"missingCoverage": []string{},
|
||
"blockingMissingCoverage": []string{},
|
||
"requiredExternals": []string{"bos", "dcloud", "mongo"},
|
||
"externalVerificationPassed": []string{"bos", "mongo"},
|
||
"externalVerificationMissing": []string{"dcloud"},
|
||
"externalVerificationPassedCount": 2,
|
||
"externalVerificationRequiredCount": 3,
|
||
"operatorRepositoryRequiredCount": 28,
|
||
"operatorRepositoryCount": 28,
|
||
"operatorRepositoryCoverageComplete": true,
|
||
"operatorRepositoryPolicyClean": true,
|
||
"operatorRepositoryPolicyViolations": []string{},
|
||
"static": pocAcceptanceStaticEvidence(28),
|
||
"failedCount": 0,
|
||
"requiredSkippedCount": 0,
|
||
"generatedFixtureCount": 0,
|
||
"realFixtureCount": 6,
|
||
"allFixturesReal": true,
|
||
"fixtureSources": pocAcceptanceFixtureSources(),
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, field := range []string{
|
||
"result.externalVerificationPassed",
|
||
"result.externalVerificationMissing",
|
||
"result.externalVerificationPassedCount",
|
||
} {
|
||
if !strings.Contains(body, field) {
|
||
t.Fatalf("expected validation field %s in %s", field, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsMissingRepositoryEvidenceForFullAcceptance(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-operator-acceptance-missing-repository-evidence",
|
||
"status": "passed",
|
||
"acceptanceScope": "full",
|
||
"fullExternalAcceptance": true,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 28,
|
||
"coveredOperatorCount": 28,
|
||
"allOperatorCoverageComplete": true,
|
||
"blockingOperatorCoverageComplete": true,
|
||
"missingCoverage": []string{},
|
||
"blockingMissingCoverage": []string{},
|
||
"requiredExternals": []string{"bos", "dcloud", "mongo"},
|
||
"externalVerificationPassed": []string{"bos", "dcloud", "mongo"},
|
||
"externalVerificationMissing": []string{},
|
||
"externalVerificationPassedCount": 3,
|
||
"externalVerificationRequiredCount": 3,
|
||
"failedCount": 0,
|
||
"requiredSkippedCount": 0,
|
||
"generatedFixtureCount": 0,
|
||
"realFixtureCount": 6,
|
||
"allFixturesReal": true,
|
||
"fixtureSources": pocAcceptanceFixtureSources(),
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, field := range []string{
|
||
"result.static",
|
||
} {
|
||
if !strings.Contains(body, field) {
|
||
t.Fatalf("expected validation field %s in %s", field, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestPocAcceptanceImportRejectsGeneratedFixtureFullAcceptance(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := map[string]any{
|
||
"result": map[string]any{
|
||
"runId": "poc-operator-acceptance-generated-fixtures",
|
||
"status": "passed",
|
||
"acceptanceScope": "full",
|
||
"fullExternalAcceptance": true,
|
||
"operatorCount": 28,
|
||
"operatorPassedCount": 28,
|
||
"coveredOperatorCount": 28,
|
||
"allOperatorCoverageComplete": true,
|
||
"blockingOperatorCoverageComplete": true,
|
||
"missingCoverage": []string{},
|
||
"blockingMissingCoverage": []string{},
|
||
"requiredExternals": []string{"bos", "dcloud", "mongo"},
|
||
"externalVerificationPassed": []string{"bos", "dcloud", "mongo"},
|
||
"externalVerificationMissing": []string{},
|
||
"externalVerificationPassedCount": 3,
|
||
"externalVerificationRequiredCount": 3,
|
||
"operatorRepositoryRequiredCount": 28,
|
||
"operatorRepositoryCount": 28,
|
||
"operatorRepositoryCoverageComplete": true,
|
||
"operatorRepositoryPolicyClean": true,
|
||
"operatorRepositoryPolicyViolations": []string{},
|
||
"static": pocAcceptanceStaticEvidence(28),
|
||
"failedCount": 0,
|
||
"requiredSkippedCount": 0,
|
||
"generatedFixtureCount": 1,
|
||
"realFixtureCount": 5,
|
||
"allFixturesReal": false,
|
||
"fixtureSources": map[string]any{
|
||
"allowGeneratedFixtures": true,
|
||
"items": map[string]any{
|
||
"xplatform": map[string]any{"path": "/workspace/system/acceptance/general/generated/xplatform", "generated": true},
|
||
"csv": map[string]any{"path": "/workspace/datasets/fixtures/records.csv", "generated": false},
|
||
},
|
||
},
|
||
},
|
||
}
|
||
rawPayload, err := json.Marshal(payload)
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, field := range []string{
|
||
"result.generatedFixtureCount",
|
||
"result.allFixturesReal",
|
||
"result.fixtureSources.allowGeneratedFixtures",
|
||
"result.fixtureSources.items",
|
||
} {
|
||
if !strings.Contains(body, field) {
|
||
t.Fatalf("expected validation field %s in %s", field, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestRunLogsEndpointPaginatesAndDefaultsToTail(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
logs := make([]domain.LogEntryRecord, 0, 1100)
|
||
for index := 0; index < 1100; index++ {
|
||
logs = append(logs, domain.LogEntryRecord{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: fmt.Sprintf("line-%04d", index)})
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-logs-page",
|
||
WorkflowID: "workflow-logs",
|
||
WorkflowName: "Logs",
|
||
ProjectID: "project-logs",
|
||
ProjectName: "Logs",
|
||
Trigger: domain.RunTriggerManual,
|
||
Status: domain.RunStatusCompleted,
|
||
Logs: logs,
|
||
NodeExecutions: []domain.NodeExecutionRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
Artifacts: []domain.ExecutionArtifact{},
|
||
K8sRefs: []domain.K8sRef{},
|
||
CreatedAt: "2026-04-13T01:00:00Z",
|
||
UpdatedAt: "2026-04-13T01:00:00Z",
|
||
}); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-logs-page/logs", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("logs status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var response struct {
|
||
Items []domain.LogEntryRecord `json:"items"`
|
||
Total int `json:"total"`
|
||
Limit int `json:"limit"`
|
||
Offset int `json:"offset"`
|
||
Truncated bool `json:"truncated"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode logs response: %v", err)
|
||
}
|
||
if response.Total != 1100 || response.Limit != 1000 || response.Offset != 100 || !response.Truncated {
|
||
t.Fatalf("unexpected default page metadata: %+v", response)
|
||
}
|
||
if len(response.Items) != 1000 || response.Items[0].Message != "line-0100" {
|
||
t.Fatalf("expected tail page starting at line-0100, got len=%d first=%q", len(response.Items), response.Items[0].Message)
|
||
}
|
||
|
||
pageRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(pageRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-logs-page/logs?limit=2&offset=3", nil))
|
||
if pageRecorder.Code != http.StatusOK {
|
||
t.Fatalf("explicit page status: %d body=%s", pageRecorder.Code, pageRecorder.Body.String())
|
||
}
|
||
var explicitPage struct {
|
||
Items []domain.LogEntryRecord `json:"items"`
|
||
Total int `json:"total"`
|
||
Limit int `json:"limit"`
|
||
Offset int `json:"offset"`
|
||
}
|
||
if err := json.Unmarshal(pageRecorder.Body.Bytes(), &explicitPage); err != nil {
|
||
t.Fatalf("decode explicit page: %v", err)
|
||
}
|
||
if explicitPage.Total != 1100 || explicitPage.Limit != 2 || explicitPage.Offset != 3 || len(explicitPage.Items) != 2 || explicitPage.Items[0].Message != "line-0003" {
|
||
t.Fatalf("unexpected explicit page: %+v", explicitPage)
|
||
}
|
||
|
||
zeroLimitRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(zeroLimitRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-logs-page/logs?limit=0", nil))
|
||
if zeroLimitRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("zero limit status: %d body=%s", zeroLimitRecorder.Code, zeroLimitRecorder.Body.String())
|
||
}
|
||
if body := zeroLimitRecorder.Body.String(); !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be greater than or equal to 1") {
|
||
t.Fatalf("expected limit validation error, got %s", body)
|
||
}
|
||
}
|
||
|
||
func TestRunPagedEndpointsRejectInvalidPagination(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-invalid-pages",
|
||
WorkflowID: "workflow-pages",
|
||
WorkflowName: "Pages",
|
||
ProjectID: "project-pages",
|
||
ProjectName: "Pages",
|
||
Trigger: domain.RunTriggerManual,
|
||
Status: domain.RunStatusCompleted,
|
||
Logs: []domain.LogEntryRecord{{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: "hello"}},
|
||
NodeExecutions: []domain.NodeExecutionRecord{},
|
||
ResourceSeries: []domain.ResourceSample{{Timestamp: "2026-04-13T01:00:00Z", CPU: 1, Memory: 64}},
|
||
Artifacts: []domain.ExecutionArtifact{{ID: "artifact-1", Name: "result.json", Type: domain.PortTypeJSON}},
|
||
K8sRefs: []domain.K8sRef{},
|
||
CreatedAt: "2026-04-13T01:00:00Z",
|
||
UpdatedAt: "2026-04-13T01:00:00Z",
|
||
}); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
cases := []struct {
|
||
name string
|
||
path string
|
||
field string
|
||
messagePart string
|
||
}{
|
||
{
|
||
name: "logs non integer limit",
|
||
path: "/api/runs/run-invalid-pages/logs?limit=abc",
|
||
field: "limit",
|
||
messagePart: "limit must be an integer",
|
||
},
|
||
{
|
||
name: "resources negative offset",
|
||
path: "/api/runs/run-invalid-pages/resources?offset=-1",
|
||
field: "offset",
|
||
messagePart: "offset must be zero or greater",
|
||
},
|
||
{
|
||
name: "artifacts decimal skip alias",
|
||
path: "/api/runs/run-invalid-pages/artifacts?skip=1.5",
|
||
field: "skip",
|
||
messagePart: "skip must be an integer",
|
||
},
|
||
}
|
||
for _, tc := range cases {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, tc.path, nil))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, fmt.Sprintf(`"field":"%s"`, tc.field)) || !strings.Contains(body, tc.messagePart) {
|
||
t.Fatalf("expected %s validation issue containing %q, got %s", tc.field, tc.messagePart, body)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestRunArtifactContentStreamsLocalMediaWithRangeAndPreviewURL(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
videoPath := filepath.Join(t.TempDir(), "clip.mp4")
|
||
if err := os.WriteFile(videoPath, []byte("0123456789"), 0o644); err != nil {
|
||
t.Fatalf("write video artifact: %v", err)
|
||
}
|
||
artifact := domain.ExecutionArtifact{
|
||
ID: "artifact-video",
|
||
RunID: "run-media",
|
||
NodeID: "slice",
|
||
Name: "clip.mp4",
|
||
Type: domain.PortTypeVideo,
|
||
Size: "10 B",
|
||
Path: videoPath,
|
||
Metadata: &domain.ArtifactMetadata{
|
||
MimeType: "video/mp4",
|
||
},
|
||
Data: map[string]any{
|
||
"preview": map[string]any{
|
||
"kind": "video",
|
||
"url": "data:video/mp4;base64,MDEyMzQ1Njc4OQ==",
|
||
},
|
||
},
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-media",
|
||
WorkflowID: "workflow-media",
|
||
WorkflowName: "Media",
|
||
ProjectID: "project-media",
|
||
ProjectName: "Media",
|
||
Trigger: domain.RunTriggerManual,
|
||
Status: domain.RunStatusCompleted,
|
||
NodeExecutions: []domain.NodeExecutionRecord{
|
||
{NodeID: "slice", NodeLabel: "视频切片", Status: domain.NodeExecutionStatusCompleted, Artifacts: []domain.ExecutionArtifact{artifact}},
|
||
},
|
||
Logs: []domain.LogEntryRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
Artifacts: []domain.ExecutionArtifact{artifact},
|
||
K8sRefs: []domain.K8sRef{},
|
||
CreatedAt: "2026-04-13T01:00:00Z",
|
||
UpdatedAt: "2026-04-13T01:00:00Z",
|
||
}); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
artifactsRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(artifactsRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-media/artifacts", nil))
|
||
if artifactsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("artifacts status: %d body=%s", artifactsRecorder.Code, artifactsRecorder.Body.String())
|
||
}
|
||
artifactsBody := artifactsRecorder.Body.String()
|
||
if strings.Contains(artifactsBody, "MDEyMzQ1Njc4OQ==") {
|
||
t.Fatalf("artifacts response should not inline video data URL: %s", artifactsBody)
|
||
}
|
||
if !strings.Contains(artifactsBody, "/api/runs/run-media/artifacts/artifact-video/content") {
|
||
t.Fatalf("artifacts response should include content URL, got %s", artifactsBody)
|
||
}
|
||
|
||
runRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(runRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-media", nil))
|
||
if runRecorder.Code != http.StatusOK {
|
||
t.Fatalf("run status: %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||
}
|
||
if strings.Contains(runRecorder.Body.String(), "MDEyMzQ1Njc4OQ==") {
|
||
t.Fatalf("run response should not inline video data URL: %s", runRecorder.Body.String())
|
||
}
|
||
|
||
contentRequest := httptest.NewRequest(http.MethodGet, "/api/runs/run-media/artifacts/artifact-video/content", nil)
|
||
contentRequest.Header.Set("Range", "bytes=2-5")
|
||
contentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(contentRecorder, contentRequest)
|
||
if contentRecorder.Code != http.StatusPartialContent {
|
||
t.Fatalf("content status: %d body=%s", contentRecorder.Code, contentRecorder.Body.String())
|
||
}
|
||
if got := contentRecorder.Body.String(); got != "2345" {
|
||
t.Fatalf("range body = %q, want 2345", got)
|
||
}
|
||
if contentType := contentRecorder.Header().Get("Content-Type"); !strings.HasPrefix(contentType, "video/mp4") {
|
||
t.Fatalf("content type = %q, want video/mp4", contentType)
|
||
}
|
||
|
||
downloadRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
downloadRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/runs/run-media/artifacts/artifact-video/content?download=1", nil),
|
||
)
|
||
if downloadRecorder.Code != http.StatusOK {
|
||
t.Fatalf("download status: %d body=%s", downloadRecorder.Code, downloadRecorder.Body.String())
|
||
}
|
||
if got := downloadRecorder.Header().Get("Content-Disposition"); got != `attachment; filename=clip.mp4` {
|
||
t.Fatalf("download content disposition = %q", got)
|
||
}
|
||
if got := downloadRecorder.Body.String(); got != "0123456789" {
|
||
t.Fatalf("download body = %q, want full artifact", got)
|
||
}
|
||
|
||
previewPath := filepath.Join(t.TempDir(), "artifact-browser-preview.mp4")
|
||
if err := os.WriteFile(previewPath, []byte("artifact-browser-preview"), 0o644); err != nil {
|
||
t.Fatalf("write browser preview: %v", err)
|
||
}
|
||
originalPreviewResolver := sourceBrowserVideoPreviewPath
|
||
sourceBrowserVideoPreviewPath = func(ctx context.Context, request sourceBrowserPreviewRequest) (string, error) {
|
||
if request.RedirectURL != videoPath {
|
||
t.Fatalf("unexpected artifact preview input: %s", request.RedirectURL)
|
||
}
|
||
if request.FileName != "clip.mp4" {
|
||
t.Fatalf("unexpected artifact preview file name: %s", request.FileName)
|
||
}
|
||
if len(request.Payload) != 0 {
|
||
t.Fatalf("local artifact preview should not materialize payload bytes")
|
||
}
|
||
return previewPath, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
sourceBrowserVideoPreviewPath = originalPreviewResolver
|
||
})
|
||
|
||
transcodedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
transcodedRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/runs/run-media/artifacts/artifact-video/content?transcode=browser", nil),
|
||
)
|
||
if transcodedRecorder.Code != http.StatusOK {
|
||
t.Fatalf("browser preview status: %d body=%s", transcodedRecorder.Code, transcodedRecorder.Body.String())
|
||
}
|
||
if contentType := transcodedRecorder.Header().Get("Content-Type"); contentType != "video/mp4" {
|
||
t.Fatalf("browser preview content type = %q, want video/mp4", contentType)
|
||
}
|
||
if got := transcodedRecorder.Body.String(); got != "artifact-browser-preview" {
|
||
t.Fatalf("browser preview body = %q", got)
|
||
}
|
||
}
|
||
|
||
func TestRunArtifactContentStreamsBOSReportThroughWorkflowConnection(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "Report storage",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "testxyz",
|
||
AccessKeyID: "ak-report",
|
||
SecretAccessKey: "sk-report",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
Name: "Quality report",
|
||
Nodes: []domain.WorkflowNode{
|
||
{
|
||
ID: "upload",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "结果上传",
|
||
ConnectionID: connection.ID,
|
||
Bucket: "testxyz",
|
||
},
|
||
},
|
||
},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
reportPayload := []byte("<!doctype html><html><body><h1>Quality report</h1></body></html>")
|
||
reportKey := "projects/quality/run-1/report.html"
|
||
artifact := domain.ExecutionArtifact{
|
||
ID: "artifact-report",
|
||
RunID: "run-report",
|
||
NodeID: "upload",
|
||
Name: "report.html",
|
||
Type: domain.PortTypeFile,
|
||
Path: "bos://testxyz/" + reportKey,
|
||
Metadata: &domain.ArtifactMetadata{
|
||
MimeType: "text/html",
|
||
},
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-report",
|
||
WorkflowID: workflow.ID,
|
||
WorkflowName: workflow.Name,
|
||
Status: domain.RunStatusCompleted,
|
||
Artifacts: []domain.ExecutionArtifact{artifact},
|
||
CreatedAt: "2026-07-27T00:00:00Z",
|
||
UpdatedAt: "2026-07-27T00:00:00Z",
|
||
}); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
objectID := "testxyz|" + reportKey
|
||
fakeBrowser.objects[objectID] = domain.ObjectStorageObjectSummary{
|
||
Bucket: "testxyz",
|
||
Key: reportKey,
|
||
Name: "report.html",
|
||
Kind: "file",
|
||
SizeBytes: int64(len(reportPayload)),
|
||
ContentType: "application/octet-stream",
|
||
}
|
||
fakeBrowser.content[objectID] = reportPayload
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(_ context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection: %#v", item)
|
||
}
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
storageConnectionBrowserResolver = originalResolver
|
||
})
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
contentPath := "/api/runs/run-report/artifacts/artifact-report/content"
|
||
headRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(headRecorder, httptest.NewRequest(http.MethodHead, contentPath, nil))
|
||
if headRecorder.Code != http.StatusOK {
|
||
t.Fatalf("head status: %d body=%s", headRecorder.Code, headRecorder.Body.String())
|
||
}
|
||
if headRecorder.Body.Len() != 0 {
|
||
t.Fatalf("HEAD should not return a body, got %q", headRecorder.Body.String())
|
||
}
|
||
if contentType := headRecorder.Header().Get("Content-Type"); contentType != "text/html" {
|
||
t.Fatalf("HEAD content type = %q, want text/html", contentType)
|
||
}
|
||
|
||
rangeRecorder := httptest.NewRecorder()
|
||
rangeRequest := httptest.NewRequest(http.MethodGet, contentPath, nil)
|
||
rangeRequest.Header.Set("Range", "bytes=0-14")
|
||
handler.ServeHTTP(rangeRecorder, rangeRequest)
|
||
if rangeRecorder.Code != http.StatusPartialContent {
|
||
t.Fatalf("range status: %d body=%s", rangeRecorder.Code, rangeRecorder.Body.String())
|
||
}
|
||
if body := rangeRecorder.Body.String(); body != string(reportPayload[:15]) {
|
||
t.Fatalf("range body = %q, want %q", body, reportPayload[:15])
|
||
}
|
||
if contentType := rangeRecorder.Header().Get("Content-Type"); contentType != "text/html" {
|
||
t.Fatalf("range content type = %q, want text/html", contentType)
|
||
}
|
||
|
||
contentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(contentRecorder, httptest.NewRequest(http.MethodGet, contentPath, nil))
|
||
if contentRecorder.Code != http.StatusOK {
|
||
t.Fatalf("content status: %d body=%s", contentRecorder.Code, contentRecorder.Body.String())
|
||
}
|
||
if got := contentRecorder.Body.Bytes(); !bytes.Equal(got, reportPayload) {
|
||
t.Fatalf("content body = %q, want %q", got, reportPayload)
|
||
}
|
||
if contentType := contentRecorder.Header().Get("Content-Type"); contentType != "text/html" {
|
||
t.Fatalf("content type = %q, want text/html", contentType)
|
||
}
|
||
}
|
||
|
||
func TestLegacyRunArtifactsReceiveStableResponseIDsAndRemainPreviewable(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
tempDir := t.TempDir()
|
||
firstPath := filepath.Join(tempDir, "inspect_error_log.log")
|
||
secondPath := filepath.Join(tempDir, "inspect_full_log.log")
|
||
if err := os.WriteFile(firstPath, []byte("first legacy log"), 0o644); err != nil {
|
||
t.Fatalf("write first legacy artifact: %v", err)
|
||
}
|
||
if err := os.WriteFile(secondPath, []byte("second legacy log"), 0o644); err != nil {
|
||
t.Fatalf("write second legacy artifact: %v", err)
|
||
}
|
||
firstArtifact := domain.ExecutionArtifact{
|
||
Name: "inspect_error_log.log",
|
||
Type: domain.PortTypeFile,
|
||
PortID: "checker-results",
|
||
Path: firstPath,
|
||
}
|
||
secondArtifact := domain.ExecutionArtifact{
|
||
Name: "inspect_full_log.log",
|
||
Type: domain.PortTypeFile,
|
||
PortID: "checker-results",
|
||
Path: secondPath,
|
||
}
|
||
run := domain.RunRecord{
|
||
ID: "run-legacy-artifacts",
|
||
WorkflowID: "workflow-legacy-artifacts",
|
||
WorkflowName: "Legacy artifact preview",
|
||
Status: domain.RunStatusCompleted,
|
||
NodeExecutions: []domain.NodeExecutionRecord{
|
||
{
|
||
NodeID: "check",
|
||
NodeLabel: "质检",
|
||
Status: domain.NodeExecutionStatusCompleted,
|
||
Artifacts: []domain.ExecutionArtifact{firstArtifact, secondArtifact},
|
||
Logs: []domain.LogEntryRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
},
|
||
},
|
||
Artifacts: []domain.ExecutionArtifact{firstArtifact, secondArtifact},
|
||
Logs: []domain.LogEntryRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
K8sRefs: []domain.K8sRef{},
|
||
CreatedAt: "2026-07-27T00:00:00Z",
|
||
UpdatedAt: "2026-07-27T00:01:00Z",
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
||
t.Fatalf("upsert legacy run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
runRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(runRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/"+run.ID, nil))
|
||
if runRecorder.Code != http.StatusOK {
|
||
t.Fatalf("run status: %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||
}
|
||
var runResponse domain.RunRecord
|
||
if err := json.Unmarshal(runRecorder.Body.Bytes(), &runResponse); err != nil {
|
||
t.Fatalf("decode run response: %v", err)
|
||
}
|
||
responseArtifacts := runResponse.NodeExecutions[0].Artifacts
|
||
if len(responseArtifacts) != 2 {
|
||
t.Fatalf("response artifacts = %#v", responseArtifacts)
|
||
}
|
||
if responseArtifacts[0].ID == "" || responseArtifacts[1].ID == "" ||
|
||
responseArtifacts[0].ID == responseArtifacts[1].ID {
|
||
t.Fatalf("legacy response ids should be distinct and non-empty: %#v", responseArtifacts)
|
||
}
|
||
for _, artifact := range responseArtifacts {
|
||
if artifact.RunID != run.ID || artifact.NodeID != "check" {
|
||
t.Fatalf("legacy response identity was not hydrated: %#v", artifact)
|
||
}
|
||
}
|
||
|
||
artifactsRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
artifactsRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/runs/"+run.ID+"/artifacts", nil),
|
||
)
|
||
if artifactsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("artifacts status: %d body=%s", artifactsRecorder.Code, artifactsRecorder.Body.String())
|
||
}
|
||
var artifactsResponse struct {
|
||
Items []domain.ExecutionArtifact `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(artifactsRecorder.Body.Bytes(), &artifactsResponse); err != nil {
|
||
t.Fatalf("decode artifacts response: %v", err)
|
||
}
|
||
if len(artifactsResponse.Items) != 2 ||
|
||
artifactsResponse.Items[0].ID != responseArtifacts[0].ID ||
|
||
artifactsResponse.Items[1].ID != responseArtifacts[1].ID {
|
||
t.Fatalf("run and artifact-list ids should match: run=%#v list=%#v", responseArtifacts, artifactsResponse.Items)
|
||
}
|
||
|
||
contentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
contentRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodGet,
|
||
"/api/runs/"+run.ID+"/artifacts/"+url.PathEscape(responseArtifacts[1].ID)+"/content",
|
||
nil,
|
||
),
|
||
)
|
||
if contentRecorder.Code != http.StatusOK {
|
||
t.Fatalf("legacy content status: %d body=%s", contentRecorder.Code, contentRecorder.Body.String())
|
||
}
|
||
if got := contentRecorder.Body.String(); got != "second legacy log" {
|
||
t.Fatalf("legacy content = %q, want second legacy log", got)
|
||
}
|
||
|
||
storedRun, err := dataStore.GetRun(ctx, run.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload stored run: %v", err)
|
||
}
|
||
if storedRun == nil || storedRun.NodeExecutions[0].Artifacts[0].ID != "" ||
|
||
storedRun.NodeExecutions[0].Artifacts[1].ID != "" {
|
||
t.Fatalf("response compatibility must not rewrite historical runs: %#v", storedRun)
|
||
}
|
||
}
|
||
|
||
func TestWorkflowRunsListReturnsWorkflowOnlySummaries(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "历史项目", Description: "history"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflowA, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "周期工作流 A",
|
||
Description: "a",
|
||
Type: domain.WorkflowTypeStreaming,
|
||
Schedule: &domain.WorkflowSchedule{IntervalHours: 2, Mode: domain.WorkflowScheduleModeEveryHours},
|
||
Nodes: []domain.WorkflowNode{},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflowA: %v", err)
|
||
}
|
||
workflowB, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "周期工作流 B",
|
||
Description: "b",
|
||
Type: domain.WorkflowTypeStreaming,
|
||
Schedule: &domain.WorkflowSchedule{IntervalHours: 3, Mode: domain.WorkflowScheduleModeEveryHours},
|
||
Nodes: []domain.WorkflowNode{},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflowB: %v", err)
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-a1",
|
||
WorkflowID: workflowA.ID,
|
||
WorkflowName: workflowA.Name,
|
||
ProjectID: project.ID,
|
||
ProjectName: project.Name,
|
||
Trigger: domain.RunTriggerScheduled,
|
||
Status: domain.RunStatusCompleted,
|
||
CreatedAt: "2026-04-22T01:00:00Z",
|
||
UpdatedAt: "2026-04-22T01:00:00Z",
|
||
NodeExecutions: []domain.NodeExecutionRecord{},
|
||
Logs: []domain.LogEntryRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
Artifacts: []domain.ExecutionArtifact{},
|
||
K8sRefs: []domain.K8sRef{},
|
||
}); err != nil {
|
||
t.Fatalf("upsert workflowA run: %v", err)
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-b1",
|
||
WorkflowID: workflowB.ID,
|
||
WorkflowName: workflowB.Name,
|
||
ProjectID: project.ID,
|
||
ProjectName: project.Name,
|
||
Trigger: domain.RunTriggerScheduled,
|
||
Status: domain.RunStatusFailed,
|
||
CreatedAt: "2026-04-22T02:00:00Z",
|
||
UpdatedAt: "2026-04-22T02:00:00Z",
|
||
NodeExecutions: []domain.NodeExecutionRecord{},
|
||
Logs: []domain.LogEntryRecord{},
|
||
ResourceSeries: []domain.ResourceSample{},
|
||
Artifacts: []domain.ExecutionArtifact{},
|
||
K8sRefs: []domain.K8sRef{},
|
||
}); err != nil {
|
||
t.Fatalf("upsert workflowB run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/workflows/"+workflowA.ID+"/runs", nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("workflow runs list status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var response struct {
|
||
Items []domain.RunSummary `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode workflow runs: %v", err)
|
||
}
|
||
if len(response.Items) != 1 {
|
||
t.Fatalf("expected 1 workflow run summary, got %d", len(response.Items))
|
||
}
|
||
if response.Items[0].ID != "run-a1" {
|
||
t.Fatalf("expected workflowA run summary, got %#v", response.Items[0])
|
||
}
|
||
}
|
||
|
||
func TestRunsRerunSubtreeCreatesQueuedChildRunWithBaseline(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "补跑项目", Description: "rerun"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "失败补跑工作流",
|
||
Description: "rerun subtree",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{
|
||
{ID: "a", Type: "operator", Data: domain.WorkflowNodeData{Label: "A", Category: "DATA", Description: "a"}},
|
||
{ID: "b", Type: "operator", Data: domain.WorkflowNodeData{Label: "B", Category: "DATA", Description: "b"}},
|
||
{ID: "c", Type: "operator", Data: domain.WorkflowNodeData{Label: "C", Category: "DATA", Description: "c"}},
|
||
{ID: "d", Type: "operator", Data: domain.WorkflowNodeData{Label: "D", Category: "DATA", Description: "d"}},
|
||
},
|
||
Edges: []domain.WorkflowEdge{
|
||
{ID: "a-b", Source: "a", Target: "b"},
|
||
{ID: "b-d", Source: "b", Target: "d"},
|
||
{ID: "c-d", Source: "c", Target: "d"},
|
||
},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
sourceRun := domain.RunRecord{
|
||
ID: "run-failed-1",
|
||
WorkflowID: workflow.ID,
|
||
WorkflowName: workflow.Name,
|
||
ProjectID: project.ID,
|
||
ProjectName: project.Name,
|
||
Trigger: domain.RunTriggerScheduled,
|
||
Status: domain.RunStatusFailed,
|
||
CurrentNodeID: "b",
|
||
NodeExecutions: []domain.NodeExecutionRecord{
|
||
{NodeID: "a", NodeLabel: "A", Status: domain.NodeExecutionStatusCompleted, Logs: []domain.LogEntryRecord{{Message: "a ok"}}, Artifacts: []domain.ExecutionArtifact{{ID: "artifact-a", Name: "a.json"}}},
|
||
{NodeID: "c", NodeLabel: "C", Status: domain.NodeExecutionStatusCompleted, Logs: []domain.LogEntryRecord{{Message: "c ok"}}, Artifacts: []domain.ExecutionArtifact{{ID: "artifact-c", Name: "c.json"}}},
|
||
{NodeID: "b", NodeLabel: "B", Status: domain.NodeExecutionStatusFailed, Error: "boom"},
|
||
},
|
||
CreatedAt: "2026-04-22T03:00:00Z",
|
||
UpdatedAt: "2026-04-22T03:05:00Z",
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, sourceRun); err != nil {
|
||
t.Fatalf("upsert source run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
body := bytes.NewBufferString(`{"nodeId":"b"}`)
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/runs/"+sourceRun.ID+"/rerun-subtree", body))
|
||
if recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("rerun subtree status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var child domain.RunRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &child); err != nil {
|
||
t.Fatalf("decode child run: %v", err)
|
||
}
|
||
if child.Trigger != domain.RunTriggerRerunSubtree {
|
||
t.Fatalf("expected rerun_subtree trigger, got %s", child.Trigger)
|
||
}
|
||
if child.ParentRunID != sourceRun.ID {
|
||
t.Fatalf("expected parent run %s, got %s", sourceRun.ID, child.ParentRunID)
|
||
}
|
||
if child.RestartFromNodeID != "b" {
|
||
t.Fatalf("expected restartFromNodeId=b, got %s", child.RestartFromNodeID)
|
||
}
|
||
if child.Status != domain.RunStatusQueued {
|
||
t.Fatalf("expected queued child run, got %s", child.Status)
|
||
}
|
||
if child.Failure != nil || child.Progress != nil {
|
||
t.Fatalf("expected rerun child to start without stale failure/progress, failure=%#v progress=%#v", child.Failure, child.Progress)
|
||
}
|
||
if len(child.NodeExecutions) != 2 {
|
||
t.Fatalf("expected 2 baseline executions, got %#v", child.NodeExecutions)
|
||
}
|
||
nodeIDs := []string{child.NodeExecutions[0].NodeID, child.NodeExecutions[1].NodeID}
|
||
sort.Strings(nodeIDs)
|
||
if strings.Join(nodeIDs, ",") != "a,c" {
|
||
t.Fatalf("expected baseline nodes a,c, got %v", nodeIDs)
|
||
}
|
||
}
|
||
|
||
func TestRunsRerunSubtreeRebindsHumanReleasePartition(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "交付补跑项目"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID, Name: "交付补跑工作流", Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{{ID: "package", Type: "operator", Data: domain.WorkflowNodeData{Label: "Package", Category: "VIDEO", Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeFile, Required: true}}}}},
|
||
WorkflowInputs: []domain.WorkflowInputDefinition{{ID: "sourceDirectory", Name: "sourceDirectory", Type: domain.PortTypeFile, Required: true, Bindings: []domain.WorkflowInputTarget{{NodeID: "package", TargetPortID: "in-1"}}}},
|
||
})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
partition := domain.ReleasePartitionRecord{
|
||
ID: "partition-human-retry", OrganizationID: project.OrganizationID, BatchID: "batch-human-retry",
|
||
UniqueKey: "gate:1:accepted", ChildRunID: "run-human-failed", Status: domain.ReleasePartitionDispatched,
|
||
}
|
||
if err := dataStore.UpsertReleasePartition(ctx, partition); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
sourceRun := domain.RunRecord{
|
||
ID: "run-human-failed", OrganizationID: project.OrganizationID, WorkflowID: workflow.ID, WorkflowName: workflow.Name,
|
||
ProjectID: project.ID, ProjectName: project.Name, Trigger: domain.RunTriggerHumanRelease, Status: domain.RunStatusFailed,
|
||
ParentRunID: "run-human-parent-waiting", CurrentNodeID: "package", Metadata: &domain.RunMetadata{HumanReleasePartitionID: partition.ID, HumanReleaseUniqueKey: partition.UniqueKey},
|
||
NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "package", NodeLabel: "Package", Status: domain.NodeExecutionStatusFailed, Error: "duration mismatch"}},
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, sourceRun); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
||
ID: "run-human-parent-waiting", OrganizationID: project.OrganizationID, WorkflowID: workflow.ID,
|
||
ProjectID: project.ID, Trigger: domain.RunTriggerRerunSubtree, Status: domain.RunStatusWaitingForHuman,
|
||
Inputs: map[string]domain.ArtifactPayload{"sourceDirectory": {Mode: domain.ArtifactTransferModeRef, Type: domain.PortTypeFile, Ref: &domain.ArtifactReference{Provider: domain.StorageProviderBOS, ConnectionID: "storage-test", Bucket: "testxyz", Prefix: "capture", Kind: domain.ArtifactRefKindDirectory}}},
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/runs/"+sourceRun.ID+"/rerun-subtree", bytes.NewBufferString(`{"nodeId":"package"}`)))
|
||
if recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("rerun subtree status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var child domain.RunRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &child); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if child.Metadata == nil || child.Metadata.HumanReleasePartitionID != partition.ID || child.Metadata.HumanReleaseUniqueKey != partition.UniqueKey {
|
||
t.Fatalf("human release metadata was not retained: %#v", child.Metadata)
|
||
}
|
||
if child.Trigger != domain.RunTriggerHumanRelease {
|
||
t.Fatalf("human release partition retry must preserve its scheduler trigger: %s", child.Trigger)
|
||
}
|
||
if len(child.Inputs) != 1 || child.Inputs["sourceDirectory"].Ref == nil || child.Inputs["sourceDirectory"].Ref.Prefix != "capture" {
|
||
t.Fatalf("human release retry did not recover parent workflow inputs: %#v", child.Inputs)
|
||
}
|
||
rebound, err := dataStore.GetReleasePartitionByUniqueKey(ctx, project.OrganizationID, partition.UniqueKey)
|
||
if err != nil || rebound == nil || rebound.ChildRunID != child.ID || rebound.Status != domain.ReleasePartitionDispatched {
|
||
t.Fatalf("human release partition was not rebound: partition=%#v err=%v", rebound, err)
|
||
}
|
||
}
|
||
|
||
func TestRunsRerunSubtreeRejectsMissingCompletedBaseline(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "补跑项目", Description: "rerun"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "失败补跑工作流",
|
||
Description: "rerun subtree",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{
|
||
{ID: "a", Type: "operator", Data: domain.WorkflowNodeData{Label: "A", Category: "DATA", Description: "a"}},
|
||
{ID: "b", Type: "operator", Data: domain.WorkflowNodeData{Label: "B", Category: "DATA", Description: "b"}},
|
||
{ID: "c", Type: "operator", Data: domain.WorkflowNodeData{Label: "C", Category: "DATA", Description: "c"}},
|
||
{ID: "d", Type: "operator", Data: domain.WorkflowNodeData{Label: "D", Category: "DATA", Description: "d"}},
|
||
},
|
||
Edges: []domain.WorkflowEdge{
|
||
{ID: "a-b", Source: "a", Target: "b"},
|
||
{ID: "b-d", Source: "b", Target: "d"},
|
||
{ID: "c-d", Source: "c", Target: "d"},
|
||
},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
sourceRun := domain.RunRecord{
|
||
ID: "run-failed-2",
|
||
WorkflowID: workflow.ID,
|
||
WorkflowName: workflow.Name,
|
||
ProjectID: project.ID,
|
||
ProjectName: project.Name,
|
||
Trigger: domain.RunTriggerScheduled,
|
||
Status: domain.RunStatusFailed,
|
||
CurrentNodeID: "b",
|
||
NodeExecutions: []domain.NodeExecutionRecord{
|
||
{NodeID: "a", NodeLabel: "A", Status: domain.NodeExecutionStatusCompleted},
|
||
{NodeID: "c", NodeLabel: "C", Status: domain.NodeExecutionStatusFailed, Error: "side branch failed"},
|
||
{NodeID: "b", NodeLabel: "B", Status: domain.NodeExecutionStatusFailed, Error: "boom"},
|
||
},
|
||
CreatedAt: "2026-04-22T03:00:00Z",
|
||
UpdatedAt: "2026-04-22T03:05:00Z",
|
||
}
|
||
if err := dataStore.UpsertRun(ctx, sourceRun); err != nil {
|
||
t.Fatalf("upsert source run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
body := bytes.NewBufferString(`{"nodeId":"b"}`)
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/runs/"+sourceRun.ID+"/rerun-subtree", body))
|
||
if recorder.Code != http.StatusConflict {
|
||
t.Fatalf("expected 409 for invalid baseline, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowNodeSourceStateEndpointsExposeAndResetCommittedState(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
Name: "扫描状态工作流",
|
||
Description: "scan source state",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Kind: domain.WorkflowKindScan,
|
||
ScanConfig: &domain.ScanConfig{
|
||
DataSource: "BOS",
|
||
Endpoint: "https://bos.example.test",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test/source-a",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test/source-a",
|
||
ScanMode: "INCREMENTAL",
|
||
},
|
||
Nodes: []domain.WorkflowNode{
|
||
{
|
||
ID: "scan-source",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "BOS 增量扫描",
|
||
Category: "IO",
|
||
Description: "source",
|
||
},
|
||
},
|
||
},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
|
||
currentFingerprint := domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
|
||
ScanMode: "INCREMENTAL",
|
||
IncrementalUnit: "OBJECT",
|
||
DirectoryDepth: 1,
|
||
DataSource: "BOS",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test/source-a",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test/source-a",
|
||
Endpoint: "https://bos.example.test",
|
||
})
|
||
legacyFingerprint := domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
|
||
ScanMode: "INCREMENTAL",
|
||
IncrementalUnit: "OBJECT",
|
||
DirectoryDepth: 1,
|
||
DataSource: "BOS",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test/source-legacy",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test/source-legacy",
|
||
Endpoint: "https://bos.example.test",
|
||
})
|
||
|
||
if err := dataStore.UpsertSourceState(ctx, domain.SourceStateRecord{
|
||
ID: workflow.ID + ":scan-source:" + legacyFingerprint,
|
||
WorkflowID: workflow.ID,
|
||
NodeID: "scan-source",
|
||
SourceFingerprint: legacyFingerprint,
|
||
DataSource: "BOS",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test/source-legacy",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test/source-legacy",
|
||
ScanMode: "INCREMENTAL",
|
||
IncrementalUnit: "OBJECT",
|
||
DirectoryDepth: 1,
|
||
SeenFingerprints: []string{"legacy-1"},
|
||
LastRunID: "run-legacy",
|
||
LastListedCount: 3,
|
||
LastNewCount: 1,
|
||
LastScanCompletedAt: "2026-04-12T00:00:00Z",
|
||
UpdatedAt: "2026-04-12T00:00:01Z",
|
||
}); err != nil {
|
||
t.Fatalf("seed legacy source state: %v", err)
|
||
}
|
||
if err := dataStore.UpsertSourceState(ctx, domain.SourceStateRecord{
|
||
ID: workflow.ID + ":scan-source:" + currentFingerprint,
|
||
WorkflowID: workflow.ID,
|
||
NodeID: "scan-source",
|
||
SourceFingerprint: currentFingerprint,
|
||
DataSource: "BOS",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test/source-a",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test/source-a",
|
||
ScanMode: "INCREMENTAL",
|
||
IncrementalUnit: "OBJECT",
|
||
DirectoryDepth: 1,
|
||
SeenFingerprints: []string{"entry-1", "entry-2"},
|
||
LastRunID: "run-current",
|
||
LastListedCount: 8,
|
||
LastNewCount: 2,
|
||
LastScanCompletedAt: "2026-04-12T01:00:00Z",
|
||
UpdatedAt: "2026-04-12T01:00:01Z",
|
||
}); err != nil {
|
||
t.Fatalf("seed current source state: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
getRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(getRecorder, httptest.NewRequest(http.MethodGet, "/api/workflows/"+workflow.ID+"/nodes/scan-source/source-state", nil))
|
||
if getRecorder.Code != http.StatusOK {
|
||
t.Fatalf("source state get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
||
}
|
||
|
||
var body workflowNodeSourceStateResponse
|
||
if err := json.Unmarshal(getRecorder.Body.Bytes(), &body); err != nil {
|
||
t.Fatalf("decode source state response: %v", err)
|
||
}
|
||
if !body.Supported || !body.Stateful {
|
||
t.Fatalf("expected supported stateful response, got %+v", body)
|
||
}
|
||
if body.Current == nil || body.Current.LastRunID != "run-current" {
|
||
t.Fatalf("expected current source state, got %+v", body.Current)
|
||
}
|
||
if body.CurrentFingerprint != currentFingerprint {
|
||
t.Fatalf("expected current fingerprint %s, got %s", currentFingerprint, body.CurrentFingerprint)
|
||
}
|
||
if len(body.Versions) != 2 {
|
||
t.Fatalf("expected 2 source state versions, got %+v", body.Versions)
|
||
}
|
||
if body.Versions[0].LastRunID != "run-current" {
|
||
t.Fatalf("expected latest source state first, got %+v", body.Versions)
|
||
}
|
||
|
||
deleteRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(deleteRecorder, httptest.NewRequest(http.MethodDelete, "/api/workflows/"+workflow.ID+"/nodes/scan-source/source-state", nil))
|
||
if deleteRecorder.Code != http.StatusNoContent {
|
||
t.Fatalf("source state delete status: %d body=%s", deleteRecorder.Code, deleteRecorder.Body.String())
|
||
}
|
||
|
||
stored, err := dataStore.GetSourceState(ctx, workflow.ID, "scan-source", currentFingerprint)
|
||
if err != nil {
|
||
t.Fatalf("get deleted current state: %v", err)
|
||
}
|
||
if stored != nil {
|
||
t.Fatalf("expected current source state removed, got %+v", stored)
|
||
}
|
||
legacyStored, err := dataStore.GetSourceState(ctx, workflow.ID, "scan-source", legacyFingerprint)
|
||
if err != nil {
|
||
t.Fatalf("get legacy source state: %v", err)
|
||
}
|
||
if legacyStored == nil {
|
||
t.Fatalf("expected legacy source state to remain")
|
||
}
|
||
}
|
||
|
||
func TestWorkflowNodeSourceStateEndpointsSupportDedicatedBosScanOperatorNodes(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
Name: "BOS 扫描状态工作流",
|
||
Description: "dedicated bos scan state",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Kind: domain.WorkflowKindScan,
|
||
ScanConfig: &domain.ScanConfig{
|
||
DataSource: "BOS",
|
||
Endpoint: "https://bos.example.test",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test",
|
||
ScanMode: "INCREMENTAL",
|
||
},
|
||
Nodes: []domain.WorkflowNode{
|
||
{
|
||
ID: "node-bos-scan",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "BOS 增量扫描",
|
||
Category: "IO",
|
||
Description: "source",
|
||
},
|
||
},
|
||
},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
|
||
currentFingerprint := domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
|
||
ScanMode: "INCREMENTAL",
|
||
IncrementalUnit: "OBJECT",
|
||
DirectoryDepth: 1,
|
||
DataSource: "BOS",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test",
|
||
Endpoint: "https://bos.example.test",
|
||
})
|
||
|
||
if err := dataStore.UpsertSourceState(ctx, domain.SourceStateRecord{
|
||
ID: workflow.ID + ":node-bos-scan:" + currentFingerprint,
|
||
WorkflowID: workflow.ID,
|
||
NodeID: "node-bos-scan",
|
||
SourceFingerprint: currentFingerprint,
|
||
DataSource: "BOS",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test",
|
||
ScanMode: "INCREMENTAL",
|
||
IncrementalUnit: "OBJECT",
|
||
DirectoryDepth: 1,
|
||
SeenFingerprints: []string{"entry-1"},
|
||
LastRunID: "run-bos-current",
|
||
LastListedCount: 6,
|
||
LastNewCount: 1,
|
||
LastScanCompletedAt: "2026-04-12T02:00:00Z",
|
||
UpdatedAt: "2026-04-12T02:00:01Z",
|
||
}); err != nil {
|
||
t.Fatalf("seed bos source state: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
getRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(getRecorder, httptest.NewRequest(http.MethodGet, "/api/workflows/"+workflow.ID+"/nodes/node-bos-scan/source-state", nil))
|
||
if getRecorder.Code != http.StatusOK {
|
||
t.Fatalf("source state get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
||
}
|
||
|
||
var body workflowNodeSourceStateResponse
|
||
if err := json.Unmarshal(getRecorder.Body.Bytes(), &body); err != nil {
|
||
t.Fatalf("decode source state response: %v", err)
|
||
}
|
||
if !body.Supported || !body.Stateful {
|
||
t.Fatalf("expected supported stateful response, got %+v", body)
|
||
}
|
||
if body.Current == nil || body.Current.LastRunID != "run-bos-current" {
|
||
t.Fatalf("expected current bos source state, got %+v", body.Current)
|
||
}
|
||
if body.CurrentFingerprint != currentFingerprint {
|
||
t.Fatalf("expected current fingerprint %s, got %s", currentFingerprint, body.CurrentFingerprint)
|
||
}
|
||
}
|
||
|
||
func TestUserStoragePathFavoritesAPIStoresNormalizedFavoritesWithoutLeakingToUsersList(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
user, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
||
Name: "收藏用户",
|
||
Email: "favorite-user@example.com",
|
||
Role: domain.UserRoleOperator,
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create user: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
updatePayload := []byte(`{
|
||
"items": [
|
||
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test","label":" "},
|
||
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test/","label":"重复"},
|
||
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"processed"},
|
||
{"scopeKey":"","bucket":"datastorage","prefix":"invalid"},
|
||
{"scopeKey":"source:bos://datastorage/root","bucket":"archive","prefix":" clips "}
|
||
]
|
||
}`)
|
||
updateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
updateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/users/"+user.ID+"/storage-path-favorites", bytes.NewReader(updatePayload)),
|
||
)
|
||
if updateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("favorites update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||
}
|
||
var updateBody struct {
|
||
Items []domain.StoragePathFavoriteRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updateBody); err != nil {
|
||
t.Fatalf("decode favorites update: %v", err)
|
||
}
|
||
expected := []domain.StoragePathFavoriteRecord{
|
||
{
|
||
ScopeKey: "connection:storage-a",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test",
|
||
Label: "datastorage/algo/X-platform-test",
|
||
},
|
||
{
|
||
ScopeKey: "connection:storage-a",
|
||
Bucket: "datastorage",
|
||
Prefix: "processed",
|
||
Label: "datastorage/processed",
|
||
},
|
||
{
|
||
ScopeKey: "source:bos://datastorage/root",
|
||
Bucket: "archive",
|
||
Prefix: "clips",
|
||
Label: "archive/clips",
|
||
},
|
||
}
|
||
if got, want := len(updateBody.Items), len(expected); got != want {
|
||
t.Fatalf("favorites count = %d, want %d", got, want)
|
||
}
|
||
for index := range expected {
|
||
if updateBody.Items[index] != expected[index] {
|
||
t.Fatalf("favorite[%d] = %#v, want %#v", index, updateBody.Items[index], expected[index])
|
||
}
|
||
}
|
||
|
||
getRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
getRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/users/"+user.ID+"/storage-path-favorites", nil),
|
||
)
|
||
if getRecorder.Code != http.StatusOK {
|
||
t.Fatalf("favorites get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
||
}
|
||
var getBody struct {
|
||
Items []domain.StoragePathFavoriteRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(getRecorder.Body.Bytes(), &getBody); err != nil {
|
||
t.Fatalf("decode favorites get: %v", err)
|
||
}
|
||
if strings.Join(func() []string {
|
||
values := make([]string, 0, len(getBody.Items))
|
||
for _, item := range getBody.Items {
|
||
values = append(values, item.ScopeKey+"|"+item.Bucket+"|"+item.Prefix+"|"+item.Label)
|
||
}
|
||
return values
|
||
}(), "\n") != strings.Join(func() []string {
|
||
values := make([]string, 0, len(expected))
|
||
for _, item := range expected {
|
||
values = append(values, item.ScopeKey+"|"+item.Bucket+"|"+item.Prefix+"|"+item.Label)
|
||
}
|
||
return values
|
||
}(), "\n") {
|
||
t.Fatalf("favorites get body = %#v, want %#v", getBody.Items, expected)
|
||
}
|
||
|
||
usersRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(usersRecorder, httptest.NewRequest(http.MethodGet, "/api/users", nil))
|
||
if usersRecorder.Code != http.StatusOK {
|
||
t.Fatalf("users list status: %d body=%s", usersRecorder.Code, usersRecorder.Body.String())
|
||
}
|
||
if strings.Contains(usersRecorder.Body.String(), "storagePathFavorites") {
|
||
t.Fatalf("expected users list to omit storagePathFavorites, got %s", usersRecorder.Body.String())
|
||
}
|
||
|
||
storedUser, err := dataStore.GetUser(ctx, user.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored user: %v", err)
|
||
}
|
||
if storedUser == nil || len(storedUser.StoragePathFavorites) != len(expected) {
|
||
t.Fatalf("stored favorites = %#v, want %#v", storedUser, expected)
|
||
}
|
||
}
|
||
|
||
func TestUserStoragePathFavoritesSessionAuthRequiresSelfOrAdmin(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
organization := adminCreateOrganization(t, handler, rootCookie, "Favorites Tenant", "favorites-tenant")
|
||
admin := adminCreateUser(t, handler, rootCookie, organization.ID, "Favorites Admin", "favorites-admin@example.com", domain.UserRoleAdmin, "admin-pass")
|
||
operatorA := adminCreateUser(t, handler, rootCookie, organization.ID, "Favorites Operator A", "favorites-a@example.com", domain.UserRoleOperator, "operator-a-pass")
|
||
operatorB := adminCreateUser(t, handler, rootCookie, organization.ID, "Favorites Operator B", "favorites-b@example.com", domain.UserRoleOperator, "operator-b-pass")
|
||
adminCookie := loginSession(t, handler, admin.Email, "admin-pass")
|
||
operatorCookie := loginSession(t, handler, operatorA.Email, "operator-a-pass")
|
||
|
||
payload := []byte(`{"items":[{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"owned"}]}`)
|
||
forbiddenUpdate := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/"+operatorB.ID+"/storage-path-favorites",
|
||
payload,
|
||
operatorCookie,
|
||
)
|
||
if forbiddenUpdate.Code != http.StatusForbidden {
|
||
t.Fatalf("expected operator update of another user's favorites to be forbidden, got %d body=%s", forbiddenUpdate.Code, forbiddenUpdate.Body.String())
|
||
}
|
||
|
||
forbiddenGet := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodGet,
|
||
"/api/users/"+operatorB.ID+"/storage-path-favorites",
|
||
nil,
|
||
operatorCookie,
|
||
)
|
||
if forbiddenGet.Code != http.StatusForbidden {
|
||
t.Fatalf("expected operator read of another user's favorites to be forbidden, got %d body=%s", forbiddenGet.Code, forbiddenGet.Body.String())
|
||
}
|
||
|
||
selfUpdate := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/"+operatorA.ID+"/storage-path-favorites",
|
||
payload,
|
||
operatorCookie,
|
||
)
|
||
if selfUpdate.Code != http.StatusOK {
|
||
t.Fatalf("expected self favorites update to succeed, got %d body=%s", selfUpdate.Code, selfUpdate.Body.String())
|
||
}
|
||
|
||
adminUpdate := serveJSONWithCookie(
|
||
handler,
|
||
http.MethodPut,
|
||
"/api/users/"+operatorB.ID+"/storage-path-favorites",
|
||
payload,
|
||
adminCookie,
|
||
)
|
||
if adminUpdate.Code != http.StatusOK {
|
||
t.Fatalf("expected admin favorites update to succeed, got %d body=%s", adminUpdate.Code, adminUpdate.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestProjectStoragePathFavoritesAPIStoresNormalizedFavoritesWithoutLeakingToProjectResponses(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "共享收藏项目",
|
||
Description: "project favorites",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
updatePayload := []byte(`{
|
||
"items": [
|
||
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test"},
|
||
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test/"},
|
||
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"processed"},
|
||
{"scopeKey":"source:bos://datastorage/root","bucket":"archive","prefix":" clips "}
|
||
]
|
||
}`)
|
||
updateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
updateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/projects/"+project.ID+"/storage-path-favorites", bytes.NewReader(updatePayload)),
|
||
)
|
||
if updateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("project favorites update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||
}
|
||
var updateBody struct {
|
||
Items []domain.StoragePathFavoriteRecord `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updateBody); err != nil {
|
||
t.Fatalf("decode project favorites update: %v", err)
|
||
}
|
||
if len(updateBody.Items) != 3 {
|
||
t.Fatalf("expected 3 normalized project favorites, got %#v", updateBody.Items)
|
||
}
|
||
|
||
getRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
getRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/projects/"+project.ID+"/storage-path-favorites", nil),
|
||
)
|
||
if getRecorder.Code != http.StatusOK {
|
||
t.Fatalf("project favorites get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
||
}
|
||
if strings.Contains(getRecorder.Body.String(), "\"prefix\":\"clips \"") {
|
||
t.Fatalf("expected project favorites prefix to be normalized, got %s", getRecorder.Body.String())
|
||
}
|
||
|
||
projectGetRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(projectGetRecorder, httptest.NewRequest(http.MethodGet, "/api/projects/"+project.ID, nil))
|
||
if projectGetRecorder.Code != http.StatusOK {
|
||
t.Fatalf("project get status: %d body=%s", projectGetRecorder.Code, projectGetRecorder.Body.String())
|
||
}
|
||
if strings.Contains(projectGetRecorder.Body.String(), "storagePathFavorites") {
|
||
t.Fatalf("expected project get to omit storagePathFavorites, got %s", projectGetRecorder.Body.String())
|
||
}
|
||
|
||
projectsListRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(projectsListRecorder, httptest.NewRequest(http.MethodGet, "/api/projects", nil))
|
||
if projectsListRecorder.Code != http.StatusOK {
|
||
t.Fatalf("projects list status: %d body=%s", projectsListRecorder.Code, projectsListRecorder.Body.String())
|
||
}
|
||
if strings.Contains(projectsListRecorder.Body.String(), "storagePathFavorites") {
|
||
t.Fatalf("expected projects list to omit storagePathFavorites, got %s", projectsListRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestSettingsAIConfigurationDrivesAIStatus(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
settingsPayload := []byte(`{
|
||
"k8s": {
|
||
"endpoint": "https://192.0.2.10:6443",
|
||
"namespace": "droboticflow",
|
||
"workspacePVCName": "droboticflow-workspace",
|
||
"workspaceMountPath": "/workspace",
|
||
"kubeconfig": "apiVersion: v1\nkind: Config"
|
||
},
|
||
"ai": {
|
||
"provider": "openai-compatible",
|
||
"baseURL": "https://ai.example/v1",
|
||
"model": "model-x",
|
||
"apiKey": "test-key"
|
||
},
|
||
"operatorGeneration": {
|
||
"enabled": true,
|
||
"giteaBaseURL": "https://gitea.example",
|
||
"giteaOrganization": "drobotics-operators",
|
||
"giteaToken": "gitea-secret-token",
|
||
"repositoryMode": "per_operator_repo",
|
||
"repositoryPrefix": "operator-external-",
|
||
"defaultBranch": "main",
|
||
"imageRegistry": "ccr.example/dataflow/custom-operators",
|
||
"buildStrategy": "kaniko",
|
||
"kanikoNamespace": "droboticflow",
|
||
"kanikoServiceAccount": "droboticflow-worker",
|
||
"registrySecretName": "droboticflow-registry",
|
||
"requireHumanApproval": true
|
||
},
|
||
"businessMongo": {
|
||
"uri": "mongodb://business-user:business-pass@business.example:27017/?authSource=admin",
|
||
"database": "business_data"
|
||
},
|
||
"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"`
|
||
OperatorGeneration struct {
|
||
Ready bool `json:"ready"`
|
||
} `json:"operatorGeneration"`
|
||
}
|
||
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 != "model-x" {
|
||
t.Fatalf("unexpected model: %s", statusBody.Model)
|
||
}
|
||
if statusBody.BaseURL != "https://ai.example/v1" {
|
||
t.Fatalf("unexpected baseURL: %s", statusBody.BaseURL)
|
||
}
|
||
if !statusBody.OperatorGeneration.Ready {
|
||
t.Fatalf("expected operator generation status to be ready: %#v", statusBody.OperatorGeneration)
|
||
}
|
||
|
||
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 {
|
||
WorkspacePVCName string `json:"workspacePVCName"`
|
||
WorkspaceMountPath string `json:"workspaceMountPath"`
|
||
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"`
|
||
OperatorGeneration struct {
|
||
GiteaBaseURL string `json:"giteaBaseURL"`
|
||
GiteaToken string `json:"giteaToken"`
|
||
GiteaTokenPreview string `json:"giteaTokenPreview"`
|
||
HasGiteaToken bool `json:"hasGiteaToken"`
|
||
ImageRegistry string `json:"imageRegistry"`
|
||
} `json:"operatorGeneration"`
|
||
BusinessMongo struct {
|
||
URI string `json:"uri"`
|
||
URIPreview string `json:"uriPreview"`
|
||
HasURI bool `json:"hasUri"`
|
||
Database string `json:"database"`
|
||
} `json:"businessMongo"`
|
||
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.K8s.WorkspacePVCName != "droboticflow-workspace" {
|
||
t.Fatalf("expected workspace pvc name to roundtrip, got %q", settingsBody.K8s.WorkspacePVCName)
|
||
}
|
||
if settingsBody.K8s.WorkspaceMountPath != "/workspace" {
|
||
t.Fatalf("expected workspace mount path to roundtrip, got %q", settingsBody.K8s.WorkspaceMountPath)
|
||
}
|
||
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.OperatorGeneration.GiteaBaseURL != "https://gitea.example" {
|
||
t.Fatalf("expected operator generation gitea base url to roundtrip, got %q", settingsBody.OperatorGeneration.GiteaBaseURL)
|
||
}
|
||
if settingsBody.OperatorGeneration.GiteaToken != "" {
|
||
t.Fatalf("expected gitea token to be sanitized")
|
||
}
|
||
if !settingsBody.OperatorGeneration.HasGiteaToken {
|
||
t.Fatalf("expected hasGiteaToken to be true")
|
||
}
|
||
if settingsBody.OperatorGeneration.GiteaTokenPreview == "" {
|
||
t.Fatalf("expected gitea token preview")
|
||
}
|
||
if settingsBody.OperatorGeneration.ImageRegistry != "ccr.example/dataflow/custom-operators" {
|
||
t.Fatalf("expected image registry to roundtrip, got %q", settingsBody.OperatorGeneration.ImageRegistry)
|
||
}
|
||
if settingsBody.BusinessMongo.URI != "" {
|
||
t.Fatalf("expected business mongo uri to be sanitized")
|
||
}
|
||
if !settingsBody.BusinessMongo.HasURI {
|
||
t.Fatalf("expected hasUri to be true for business mongo")
|
||
}
|
||
if settingsBody.BusinessMongo.URIPreview == "" {
|
||
t.Fatalf("expected business mongo uri preview to be returned")
|
||
}
|
||
if settingsBody.BusinessMongo.Database != "business_data" {
|
||
t.Fatalf("expected business mongo database to roundtrip, got %q", settingsBody.BusinessMongo.Database)
|
||
}
|
||
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 TestArtifactStorageSettingsAreScopedByOrganization(t *testing.T) {
|
||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
rootCookie := bootstrapSuperAdminSession(t, handler)
|
||
organizationA := adminCreateOrganization(t, handler, rootCookie, "Artifact Tenant A", "artifact-tenant-a")
|
||
organizationB := adminCreateOrganization(t, handler, rootCookie, "Artifact Tenant B", "artifact-tenant-b")
|
||
adminCreateUser(t, handler, rootCookie, organizationA.ID, "Artifact Admin A", "artifact-admin-a@example.com", domain.UserRoleAdmin, "artifact-admin-a-pass")
|
||
adminCreateUser(t, handler, rootCookie, organizationB.ID, "Artifact Admin B", "artifact-admin-b@example.com", domain.UserRoleAdmin, "artifact-admin-b-pass")
|
||
|
||
connectionA, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
OrganizationID: organizationA.ID,
|
||
Name: "Artifact Storage A",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bj.bcebos.com",
|
||
Bucket: "tenant-a",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create tenant A storage connection: %v", err)
|
||
}
|
||
connectionB, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
OrganizationID: organizationB.ID,
|
||
Name: "Artifact Storage B",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bj.bcebos.com",
|
||
Bucket: "tenant-b",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create tenant B storage connection: %v", err)
|
||
}
|
||
|
||
updateSettings := func(cookie *http.Cookie, connectionID string, bucket string) {
|
||
t.Helper()
|
||
payload, err := json.Marshal(map[string]any{
|
||
"k8s": map[string]any{"workspacePVCName": "workspace", "workspaceMountPath": "/workspace"},
|
||
"artifactStorage": map[string]any{
|
||
"connectionId": connectionID,
|
||
"bucket": bucket,
|
||
"prefixTemplate": "artifacts/{organizationId}/{workflowId}/{runId}/{nodeId}",
|
||
},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal settings payload: %v", err)
|
||
}
|
||
recorder := serveJSONWithCookie(handler, http.MethodPut, "/api/settings", payload, cookie)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("settings update = %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
cookieA := loginSession(t, handler, "artifact-admin-a@example.com", "artifact-admin-a-pass")
|
||
cookieB := loginSession(t, handler, "artifact-admin-b@example.com", "artifact-admin-b-pass")
|
||
updateSettings(cookieA, connectionA.ID, "tenant-a")
|
||
updateSettings(cookieB, connectionB.ID, "tenant-b")
|
||
|
||
settingsA, err := dataStore.GetArtifactStorageSettings(ctx, organizationA.ID)
|
||
if err != nil {
|
||
t.Fatalf("get tenant A artifact settings: %v", err)
|
||
}
|
||
settingsB, err := dataStore.GetArtifactStorageSettings(ctx, organizationB.ID)
|
||
if err != nil {
|
||
t.Fatalf("get tenant B artifact settings: %v", err)
|
||
}
|
||
if settingsA.ConnectionID != connectionA.ID || settingsA.Bucket != "tenant-a" {
|
||
t.Fatalf("unexpected tenant A artifact settings: %#v", settingsA)
|
||
}
|
||
if settingsB.ConnectionID != connectionB.ID || settingsB.Bucket != "tenant-b" {
|
||
t.Fatalf("unexpected tenant B artifact settings: %#v", settingsB)
|
||
}
|
||
|
||
getA := serveJSONWithCookie(handler, http.MethodGet, "/api/settings", nil, cookieA)
|
||
getB := serveJSONWithCookie(handler, http.MethodGet, "/api/settings", nil, cookieB)
|
||
if !strings.Contains(getA.Body.String(), connectionA.ID) || strings.Contains(getA.Body.String(), connectionB.ID) {
|
||
t.Fatalf("tenant A settings leaked or lost artifact storage: %s", getA.Body.String())
|
||
}
|
||
if !strings.Contains(getB.Body.String(), connectionB.ID) || strings.Contains(getB.Body.String(), connectionA.ID) {
|
||
t.Fatalf("tenant B settings leaked or lost artifact storage: %s", getB.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestAIOperatorGenerateRequiresOperatorGenerationSettings(t *testing.T) {
|
||
calledAI := false
|
||
aiServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
calledAI = true
|
||
t.Fatalf("AI provider should not be called when operator generation settings are incomplete")
|
||
}))
|
||
defer aiServer.Close()
|
||
|
||
handler := NewHandler(store.NewMemoryStore(), aipkg.NewService(aiServer.URL, "test-key", "model-x"))
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(
|
||
http.MethodPost,
|
||
"/api/ai/operators/generate",
|
||
bytes.NewReader([]byte(`{"description":"生成外部算子"}`)),
|
||
)
|
||
request.Header.Set("Content-Type", "application/json")
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusServiceUnavailable {
|
||
t.Fatalf("expected incomplete operator generation settings to block generation, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if calledAI {
|
||
t.Fatalf("AI provider was called despite incomplete operator generation settings")
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "operatorGeneration.giteaBaseURL") ||
|
||
!strings.Contains(recorder.Body.String(), "operatorGeneration.giteaToken") {
|
||
t.Fatalf("expected missing operator generation fields in response, got %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestAIOperatorGenerateAppliesOperatorGenerationSettings(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
settings, err := dataStore.GetSettings(ctx)
|
||
if err != nil {
|
||
t.Fatalf("get settings: %v", err)
|
||
}
|
||
settings.OperatorGeneration = domain.OperatorGenerationSettings{
|
||
Enabled: true,
|
||
GiteaBaseURL: "https://gitea.example",
|
||
GiteaOrganization: "drobotics-operators",
|
||
GiteaToken: "gitea-token",
|
||
RepositoryMode: "per_operator_repo",
|
||
RepositoryPrefix: "operator-external-",
|
||
DefaultBranch: "main",
|
||
ImageRegistry: "ccr.example/dataflow/custom-operators",
|
||
BuildStrategy: "kaniko",
|
||
KanikoNamespace: "droboticflow",
|
||
KanikoServiceAccount: "droboticflow-worker",
|
||
RegistrySecretName: "droboticflow-registry",
|
||
RequireHumanApproval: true,
|
||
}
|
||
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
||
t.Fatalf("upsert settings: %v", err)
|
||
}
|
||
|
||
aiServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||
writer.Header().Set("Content-Type", "application/json")
|
||
fmt.Fprint(writer, `{
|
||
"choices": [
|
||
{
|
||
"message": {
|
||
"content": "{\"name\":\"CSV to Parquet\",\"category\":\"IO\",\"description\":\"Convert CSV to Parquet\",\"inputs\":[{\"name\":\"csv\",\"type\":\"file\",\"transmission\":\"oss\"}],\"outputs\":[{\"name\":\"parquet\",\"type\":\"file\",\"transmission\":\"oss\"}],\"hyperparams\":[{\"key\":\"BATCH_SIZE\",\"value\":1000,\"type\":\"integer\"}],\"skillDefinition\":\"Convert CSV files to Parquet and metrics.\"}"
|
||
}
|
||
}
|
||
]
|
||
}`)
|
||
}))
|
||
defer aiServer.Close()
|
||
|
||
handler := NewHandler(dataStore, aipkg.NewService(aiServer.URL, "test-key", "model-x"))
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(
|
||
http.MethodPost,
|
||
"/api/ai/operators/generate",
|
||
bytes.NewReader([]byte(`{"description":"生成 CSV 转 Parquet 外部算子"}`)),
|
||
)
|
||
request.Header.Set("Content-Type", "application/json")
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected operator generation to succeed, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var response struct {
|
||
Name string `json:"name"`
|
||
RuntimeContract map[string]any `json:"runtimeContract"`
|
||
PublicationStatus domain.OperatorPublicationStatus `json:"publicationStatus"`
|
||
GenerationSource domain.OperatorGenerationSource `json:"generationSource"`
|
||
Implementation struct {
|
||
RepositoryName string `json:"repositoryName"`
|
||
RepositoryURL string `json:"repositoryURL"`
|
||
BuildCommands []string `json:"buildCommands"`
|
||
} `json:"implementation"`
|
||
Hyperparams []domain.HyperParam `json:"hyperparams"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode response: %v", err)
|
||
}
|
||
if response.Implementation.RepositoryName != "operator-external-csv-to-parquet" {
|
||
t.Fatalf("unexpected repository name: %#v", response.Implementation)
|
||
}
|
||
if response.Implementation.RepositoryURL != "https://gitea.example/drobotics-operators/operator-external-csv-to-parquet" {
|
||
t.Fatalf("unexpected repository url: %#v", response.Implementation)
|
||
}
|
||
if len(response.Implementation.BuildCommands) < 3 ||
|
||
!strings.Contains(strings.Join(response.Implementation.BuildCommands, "\n"), "ccr.example/dataflow/custom-operators/csv-to-parquet:v1.0.0") {
|
||
t.Fatalf("expected build commands to use configured image registry, got %#v", response.Implementation.BuildCommands)
|
||
}
|
||
if len(response.Hyperparams) != 1 || response.Hyperparams[0].Value != "1000" {
|
||
t.Fatalf("expected generated numeric hyperparam to be stringified, got %#v", response.Hyperparams)
|
||
}
|
||
codeRepository, ok := response.RuntimeContract["codeRepository"].(map[string]any)
|
||
if !ok || codeRepository["url"] != response.Implementation.RepositoryURL {
|
||
t.Fatalf("expected runtime contract codeRepository to reference generated repo, got %#v", response.RuntimeContract["codeRepository"])
|
||
}
|
||
if response.RuntimeContract["implementationStatus"] != "draft-requires-real-logic" {
|
||
t.Fatalf("expected generated operator to be draft-only, got %#v", response.RuntimeContract)
|
||
}
|
||
if response.GenerationSource != domain.OperatorGenerationSourceAI || response.PublicationStatus != domain.OperatorPublicationStatusDraft {
|
||
t.Fatalf("expected generated operator lifecycle to default to ai/draft, got source=%q status=%q", response.GenerationSource, response.PublicationStatus)
|
||
}
|
||
}
|
||
|
||
func TestSettingsRejectsAIKeyWithoutExplicitRuntimeConfig(t *testing.T) {
|
||
handler := NewHandler(store.NewMemoryStore(), nil)
|
||
|
||
for _, tc := range []struct {
|
||
name string
|
||
aiPatch string
|
||
field string
|
||
message string
|
||
}{
|
||
{
|
||
name: "missing base url",
|
||
aiPatch: `"baseURL":"","model":"model-x","apiKey":"test-key"`,
|
||
field: "ai.baseURL",
|
||
message: "baseURL is required when apiKey is configured",
|
||
},
|
||
{
|
||
name: "missing model",
|
||
aiPatch: `"baseURL":"https://ai.example/v1","model":"","apiKey":"test-key"`,
|
||
field: "ai.model",
|
||
message: "model is required when apiKey is configured",
|
||
},
|
||
{
|
||
name: "invalid base url",
|
||
aiPatch: `"baseURL":"ai.example/v1","model":"model-x"`,
|
||
field: "ai.baseURL",
|
||
message: "baseURL must be an absolute http(s) URL without whitespace",
|
||
},
|
||
{
|
||
name: "invalid model",
|
||
aiPatch: `"baseURL":"https://ai.example/v1","model":"model x"`,
|
||
field: "ai.model",
|
||
message: "model must not contain whitespace",
|
||
},
|
||
} {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
payload := `{"k8s":{"workspacePVCName":"droboticflow-workspace","workspaceMountPath":"/workspace"},"ai":{` + tc.aiPatch + `}}`
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/settings", strings.NewReader(payload)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected 400, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), tc.field) || !strings.Contains(recorder.Body.String(), tc.message) {
|
||
t.Fatalf("expected validation error for %s/%q, got %s", tc.field, tc.message, recorder.Body.String())
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestSettingsUpdateRejectsInvalidRuntimeDefaults(t *testing.T) {
|
||
for _, tc := range []struct {
|
||
name string
|
||
payload string
|
||
field string
|
||
messagePart string
|
||
}{
|
||
{
|
||
name: "worker poll interval must be positive",
|
||
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"worker":{"pollIntervalMs":0}}`,
|
||
field: "worker.pollIntervalMs",
|
||
messagePart: "greater than zero",
|
||
},
|
||
{
|
||
name: "worker concurrency must be positive",
|
||
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"worker":{"maxConcurrentRuns":-1}}`,
|
||
field: "worker.maxConcurrentRuns",
|
||
messagePart: "greater than zero",
|
||
},
|
||
{
|
||
name: "cpu request must be a kubernetes quantity",
|
||
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"defaults":{"cpuRequest":"one-core"}}`,
|
||
field: "defaults.cpuRequest",
|
||
messagePart: "valid Kubernetes resource quantity",
|
||
},
|
||
{
|
||
name: "memory limit must be positive",
|
||
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"defaults":{"memoryLimit":"0"}}`,
|
||
field: "defaults.memoryLimit",
|
||
messagePart: "greater than zero",
|
||
},
|
||
{
|
||
name: "gpu limit must be positive integer",
|
||
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"defaults":{"gpuLimit":"half"}}`,
|
||
field: "defaults.gpuLimit",
|
||
messagePart: "positive integer",
|
||
},
|
||
} {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(http.MethodPut, "/api/settings", strings.NewReader(tc.payload))
|
||
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, `"field":"`+tc.field+`"`) || !strings.Contains(body, tc.messagePart) {
|
||
t.Fatalf("expected %s validation issue containing %q, got %s", tc.field, tc.messagePart, body)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestTemplateExecutionConfigValidationAndPersistence(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
createPayload := []byte(`{
|
||
"name":"执行保障模板",
|
||
"description":"template execution config",
|
||
"type":"streaming",
|
||
"hyperparams":[{"key":"GLOBAL_ENV","value":"template-create"}],
|
||
"maxConcurrentRuns":2,
|
||
"nodeTimeoutSeconds":12,
|
||
"runTimeoutSeconds":34,
|
||
"maxRetryAttempts":1,
|
||
"retryBackoffSeconds":56,
|
||
"schedule":{
|
||
"interval":90,
|
||
"sourcePath":"oss://incoming"
|
||
},
|
||
"nodes":[],
|
||
"edges":[{"id":"edge-runner-custom","source":"runner-node","sourceHandle":"out-1","target":"custom-node","targetHandle":"in-1"}]
|
||
}`)
|
||
|
||
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"])
|
||
}
|
||
createdHyperparams := created["hyperparams"].([]any)
|
||
if len(createdHyperparams) != 1 || createdHyperparams[0].(map[string]any)["value"].(string) != "template-create" {
|
||
t.Fatalf("expected template hyperparams to persist on create, got %#v", created["hyperparams"])
|
||
}
|
||
|
||
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"])
|
||
}
|
||
loadedHyperparams := loaded["hyperparams"].([]any)
|
||
if len(loadedHyperparams) != 1 || loadedHyperparams[0].(map[string]any)["key"].(string) != "GLOBAL_ENV" {
|
||
t.Fatalf("expected template hyperparams to survive load, got %#v", loaded["hyperparams"])
|
||
}
|
||
|
||
updatePayload := []byte(`{
|
||
"type":"batch",
|
||
"hyperparams":[{"key":"GLOBAL_ENV","value":"template-update"},{"key":"SHARED_MODE","value":"true"}],
|
||
"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"])
|
||
}
|
||
updatedHyperparams := updated["hyperparams"].([]any)
|
||
if len(updatedHyperparams) != 2 || updatedHyperparams[1].(map[string]any)["key"].(string) != "SHARED_MODE" {
|
||
t.Fatalf("expected template hyperparams to update, got %#v", updated["hyperparams"])
|
||
}
|
||
|
||
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",
|
||
"hyperparams":[{"key":"GLOBAL_ENV","value":"workflow-create"}],
|
||
"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":[{"id":"edge-runner-custom","source":"runner-node","sourceHandle":"out-1","target":"custom-node","targetHandle":"in-1"}]
|
||
}`)
|
||
|
||
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)
|
||
}
|
||
if len(created.Hyperparams) != 1 || created.Hyperparams[0].Value != "workflow-create" {
|
||
t.Fatalf("expected workflow hyperparams persisted on create, got %#v", created.Hyperparams)
|
||
}
|
||
|
||
updatePayload := []byte(`{
|
||
"hyperparams":[{"key":"GLOBAL_ENV","value":"workflow-update"},{"key":"SHARED_MODE","value":"true"}],
|
||
"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)
|
||
}
|
||
if len(updated.Hyperparams) != 2 || updated.Hyperparams[1].Key != "SHARED_MODE" {
|
||
t.Fatalf("expected workflow hyperparams updated, got %#v", updated.Hyperparams)
|
||
}
|
||
|
||
invalidCreatePayload := []byte(`{
|
||
"name":"非法工作流",
|
||
"description":"workflow",
|
||
"type":"batch",
|
||
"nodeTimeoutSeconds":-1,
|
||
"runTimeoutSeconds":-1,
|
||
"maxRetryAttempts":-2,
|
||
"retryBackoffSeconds":-3,
|
||
"nodes":[],
|
||
"edges":[{"id":"edge-runner-custom","source":"runner-node","sourceHandle":"out-1","target":"custom-node","targetHandle":"in-1"}]
|
||
}`)
|
||
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())
|
||
}
|
||
|
||
invalidSHMCreateRecorder := httptest.NewRecorder()
|
||
invalidSHMCreatePayload := []byte(`{
|
||
"name":"非法共享内存工作流",
|
||
"description":"workflow",
|
||
"type":"batch",
|
||
"nodes":[{"id":"node-source","type":"operator","data":{"label":"source","inputs":[],"outputs":[],"sharedMemorySize":"0"},"position":{"x":0,"y":0}}],
|
||
"edges":[]
|
||
}`)
|
||
handler.ServeHTTP(invalidSHMCreateRecorder, httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(invalidSHMCreatePayload)))
|
||
if invalidSHMCreateRecorder.Code != http.StatusBadRequest || !strings.Contains(invalidSHMCreateRecorder.Body.String(), "nodes[0].data.sharedMemorySize") {
|
||
t.Fatalf("invalid shared memory workflow status/body: %d %s", invalidSHMCreateRecorder.Code, invalidSHMCreateRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowAndTemplateMetadataValidationAndNormalization(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "元数据校验项目",
|
||
Description: "metadata validation",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
|
||
workflowCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
workflowCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader([]byte(`{
|
||
"name":" Trimmed Workflow ",
|
||
"description":"workflow metadata",
|
||
"type":" batch ",
|
||
"kind":" standard ",
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`))),
|
||
)
|
||
if workflowCreateRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("workflow create status: %d body=%s", workflowCreateRecorder.Code, workflowCreateRecorder.Body.String())
|
||
}
|
||
var createdWorkflow domain.WorkflowRecord
|
||
if err := json.Unmarshal(workflowCreateRecorder.Body.Bytes(), &createdWorkflow); err != nil {
|
||
t.Fatalf("decode workflow: %v", err)
|
||
}
|
||
if createdWorkflow.Name != "Trimmed Workflow" || createdWorkflow.Type != domain.WorkflowTypeBatch || createdWorkflow.Kind != domain.WorkflowKindStandard {
|
||
t.Fatalf("expected normalized workflow metadata, got %#v", createdWorkflow)
|
||
}
|
||
|
||
for _, tc := range []struct {
|
||
name string
|
||
payload string
|
||
}{
|
||
{name: "blank name", payload: `{"name":" "}`},
|
||
{name: "invalid type", payload: `{"type":"realtime"}`},
|
||
{name: "invalid kind", payload: `{"kind":"custom"}`},
|
||
{name: "invalid status", payload: `{"status":"paused"}`},
|
||
} {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/workflows/"+createdWorkflow.ID, bytes.NewReader([]byte(tc.payload))),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected %s to fail, got %d body=%s", tc.name, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
storedWorkflow, err := dataStore.GetWorkflow(ctx, createdWorkflow.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload workflow: %v", err)
|
||
}
|
||
if storedWorkflow == nil || storedWorkflow.Name != createdWorkflow.Name || storedWorkflow.Type != createdWorkflow.Type || storedWorkflow.Kind != createdWorkflow.Kind || storedWorkflow.Status != createdWorkflow.Status {
|
||
t.Fatalf("expected invalid workflow updates to preserve metadata, got %#v", storedWorkflow)
|
||
}
|
||
|
||
workflowUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
workflowUpdateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/workflows/"+createdWorkflow.ID, bytes.NewReader([]byte(`{
|
||
"name":" Ready Workflow ",
|
||
"type":" streaming ",
|
||
"kind":" standard ",
|
||
"status":" ready "
|
||
}`))),
|
||
)
|
||
if workflowUpdateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("workflow update status: %d body=%s", workflowUpdateRecorder.Code, workflowUpdateRecorder.Body.String())
|
||
}
|
||
var updatedWorkflow domain.WorkflowRecord
|
||
if err := json.Unmarshal(workflowUpdateRecorder.Body.Bytes(), &updatedWorkflow); err != nil {
|
||
t.Fatalf("decode updated workflow: %v", err)
|
||
}
|
||
if updatedWorkflow.Name != "Ready Workflow" || updatedWorkflow.Type != domain.WorkflowTypeStreaming || updatedWorkflow.Kind != domain.WorkflowKindStandard || updatedWorkflow.Status != domain.WorkflowStatusReady {
|
||
t.Fatalf("expected normalized workflow update, got %#v", updatedWorkflow)
|
||
}
|
||
|
||
workflowInvalidCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
workflowInvalidCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader([]byte(`{
|
||
"name":"Invalid Kind Workflow",
|
||
"description":"workflow metadata",
|
||
"type":"batch",
|
||
"kind":"custom",
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`))),
|
||
)
|
||
if workflowInvalidCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("invalid workflow create status: %d body=%s", workflowInvalidCreateRecorder.Code, workflowInvalidCreateRecorder.Body.String())
|
||
}
|
||
|
||
templateCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
templateCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader([]byte(`{
|
||
"name":" Trimmed Template ",
|
||
"description":"template metadata",
|
||
"type":" batch ",
|
||
"kind":" standard ",
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`))),
|
||
)
|
||
if templateCreateRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("template create status: %d body=%s", templateCreateRecorder.Code, templateCreateRecorder.Body.String())
|
||
}
|
||
var createdTemplate domain.TemplateRecord
|
||
if err := json.Unmarshal(templateCreateRecorder.Body.Bytes(), &createdTemplate); err != nil {
|
||
t.Fatalf("decode template: %v", err)
|
||
}
|
||
if createdTemplate.Name != "Trimmed Template" || createdTemplate.Type != domain.WorkflowTypeBatch || createdTemplate.Kind != domain.WorkflowKindStandard {
|
||
t.Fatalf("expected normalized template metadata, got %#v", createdTemplate)
|
||
}
|
||
|
||
for _, tc := range []struct {
|
||
name string
|
||
payload string
|
||
}{
|
||
{name: "blank name", payload: `{"name":" "}`},
|
||
{name: "invalid type", payload: `{"type":"realtime"}`},
|
||
{name: "invalid kind", payload: `{"kind":"custom"}`},
|
||
} {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/templates/"+createdTemplate.ID, bytes.NewReader([]byte(tc.payload))),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected template %s to fail, got %d body=%s", tc.name, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
templateUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
templateUpdateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/templates/"+createdTemplate.ID, bytes.NewReader([]byte(`{
|
||
"name":" Streaming Template ",
|
||
"type":" streaming ",
|
||
"kind":" standard "
|
||
}`))),
|
||
)
|
||
if templateUpdateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("template update status: %d body=%s", templateUpdateRecorder.Code, templateUpdateRecorder.Body.String())
|
||
}
|
||
var updatedTemplate domain.TemplateRecord
|
||
if err := json.Unmarshal(templateUpdateRecorder.Body.Bytes(), &updatedTemplate); err != nil {
|
||
t.Fatalf("decode updated template: %v", err)
|
||
}
|
||
if updatedTemplate.Name != "Streaming Template" || updatedTemplate.Type != domain.WorkflowTypeStreaming || updatedTemplate.Kind != domain.WorkflowKindStandard {
|
||
t.Fatalf("expected normalized template update, got %#v", updatedTemplate)
|
||
}
|
||
}
|
||
|
||
func TestOperatorMetadataValidationAndNormalization(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader([]byte(`{
|
||
"name":" Custom Operator ",
|
||
"category":" CV ",
|
||
"description":" Normalize metadata ",
|
||
"image":" registry.example.com/custom/operator:v1 ",
|
||
"inputs":[{"id":" in-1 ","name":" input ","type":" JSON ","description":" source ","transmission":" BUS "}],
|
||
"outputs":[{"id":"out-1","name":" result ","type":" FILE ","transmission":" OSS "}],
|
||
"resources":{"cpuLimit":" 500m ","memoryLimit":" 1Gi ","gpuType":" NVIDIA A800 80GB ","gpuLimit":" 1 ","sharedMemorySize":" 16Gi "}
|
||
}`))),
|
||
)
|
||
if createRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("operator create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
||
}
|
||
|
||
var created domain.OperatorRecord
|
||
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatalf("decode operator: %v", err)
|
||
}
|
||
if created.Name != "Custom Operator" || created.Type != "custom" || created.Category != "CV" || created.Description != "Normalize metadata" {
|
||
t.Fatalf("expected normalized operator metadata, got %#v", created)
|
||
}
|
||
if created.Image != "registry.example.com/custom/operator:v1" {
|
||
t.Fatalf("expected trimmed image, got %q", created.Image)
|
||
}
|
||
if created.Version != "v1" {
|
||
t.Fatalf("expected default operator version v1, got %q", created.Version)
|
||
}
|
||
if created.ImageID != created.Image {
|
||
t.Fatalf("expected imageId to default to image, got %q", created.ImageID)
|
||
}
|
||
if len(created.Inputs) != 1 || created.Inputs[0].ID != "in-1" || created.Inputs[0].Name != "input" || created.Inputs[0].Type != domain.PortTypeJSON || created.Inputs[0].Transmission != "bus" {
|
||
t.Fatalf("expected normalized input port, got %#v", created.Inputs)
|
||
}
|
||
if len(created.Outputs) != 1 || created.Outputs[0].Type != domain.PortTypeFile || created.Outputs[0].Transmission != "oss" {
|
||
t.Fatalf("expected normalized output port, got %#v", created.Outputs)
|
||
}
|
||
if created.Resources == nil || created.Resources.CPULimit != "500m" || created.Resources.MemoryLimit != "1Gi" || created.Resources.GPUType != "NVIDIA A800 80GB" || created.Resources.GPULimit != "1" || created.Resources.SharedMemorySize != "16Gi" {
|
||
t.Fatalf("expected normalized resources, got %#v", created.Resources)
|
||
}
|
||
|
||
lightweightCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
lightweightCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader([]byte(`{
|
||
"name":"Lightweight Operator",
|
||
"type":"lightweight",
|
||
"category":"AI",
|
||
"description":"Base image plus workspace code",
|
||
"image":"python:3.11-slim",
|
||
"containerCommand":["python","/opt/droboticflow/lightweight/operator/operator.py"],
|
||
"containerArgs":[],
|
||
"runtimeContract":{"lightweightOperator":{"codeSubPath":" /lightweight-operators/demo/ ","mountPath":" /opt/droboticflow/lightweight/operator "}},
|
||
"inputs":[{"id":"in-1","name":"input","type":"json"}],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}],
|
||
"hyperparams":[{"key":"MODE","value":"smoke"}]
|
||
}`))),
|
||
)
|
||
if lightweightCreateRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("lightweight operator create status: %d body=%s", lightweightCreateRecorder.Code, lightweightCreateRecorder.Body.String())
|
||
}
|
||
var lightweightCreated domain.OperatorRecord
|
||
if err := json.Unmarshal(lightweightCreateRecorder.Body.Bytes(), &lightweightCreated); err != nil {
|
||
t.Fatalf("decode lightweight operator: %v", err)
|
||
}
|
||
if lightweightCreated.Type != "lightweight" || lightweightCreated.Image != "python:3.11-slim" {
|
||
t.Fatalf("expected lightweight metadata, got %#v", lightweightCreated)
|
||
}
|
||
lightweightRuntime, _ := lightweightCreated.RuntimeContract["lightweightOperator"].(map[string]any)
|
||
if lightweightRuntime["enabled"] != true || lightweightRuntime["codeSubPath"] != "lightweight-operators/demo" || lightweightRuntime["mountPath"] != "/opt/droboticflow/lightweight/operator" || lightweightRuntime["containerArgsConfigured"] != true {
|
||
t.Fatalf("expected normalized lightweight contract, got %#v", lightweightRuntime)
|
||
}
|
||
if strings.Join(lightweightCreated.ContainerCommand, " ") != "python /opt/droboticflow/lightweight/operator/operator.py" || len(lightweightCreated.ContainerArgs) != 0 {
|
||
t.Fatalf("expected explicit lightweight command and empty args, got command=%#v args=%#v", lightweightCreated.ContainerCommand, lightweightCreated.ContainerArgs)
|
||
}
|
||
|
||
builtinCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
builtinCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader([]byte(`{
|
||
"name":"伪内置算子",
|
||
"type":"builtin",
|
||
"category":"IO",
|
||
"description":"should fail",
|
||
"inputs":[],
|
||
"outputs":[]
|
||
}`))),
|
||
)
|
||
if builtinCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected builtin operator registration to fail, got %d body=%s", builtinCreateRecorder.Code, builtinCreateRecorder.Body.String())
|
||
}
|
||
|
||
for _, tc := range []struct {
|
||
name string
|
||
payload string
|
||
}{
|
||
{name: "blank name", payload: `{"name":" "}`},
|
||
{name: "blank category", payload: `{"category":" "}`},
|
||
{name: "blank description", payload: `{"description":" "}`},
|
||
{name: "type change", payload: `{"type":"builtin"}`},
|
||
{name: "invalid type", payload: `{"type":"system"}`},
|
||
{name: "invalid input type", payload: `{"inputs":[{"id":"in-1","name":"input","type":"audio"}]}`},
|
||
{name: "invalid output transmission", payload: `{"outputs":[{"id":"out-1","name":"result","type":"json","transmission":"mailbox"}]}`},
|
||
{name: "duplicate output ids", payload: `{"outputs":[{"id":"dup","name":"one","type":"json"},{"id":"dup","name":"two","type":"json"}]}`},
|
||
{name: "invalid resources", payload: `{"resources":{"gpuLimit":"half"}}`},
|
||
{name: "invalid shared memory", payload: `{"resources":{"sharedMemorySize":"0"}}`},
|
||
{name: "invalid version", payload: `{"version":"bad version"}`},
|
||
} {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/operators/"+created.ID, bytes.NewReader([]byte(tc.payload))),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected %s to fail, got %d body=%s", tc.name, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
stored, err := dataStore.GetOperator(ctx, created.ID)
|
||
if err != nil {
|
||
t.Fatalf("reload operator: %v", err)
|
||
}
|
||
if stored == nil || stored.Name != created.Name || stored.Type != created.Type || stored.Category != created.Category || stored.Description != created.Description {
|
||
t.Fatalf("expected invalid operator updates to preserve metadata, got %#v", stored)
|
||
}
|
||
|
||
updateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
updateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/operators/"+created.ID, bytes.NewReader([]byte(`{
|
||
"name":" Updated Operator ",
|
||
"type":" CUSTOM ",
|
||
"category":" AI ",
|
||
"description":" Updated description ",
|
||
"version":" 2.0.0 ",
|
||
"outputs":[{"id":"out-2","name":" tensor result ","type":" TENSOR ","transmission":" STREAM "}],
|
||
"resources":{"cpuLimit":" 750m ","memoryLimit":" 2Gi "}
|
||
}`))),
|
||
)
|
||
if updateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("operator update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||
}
|
||
var updated domain.OperatorRecord
|
||
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
||
t.Fatalf("decode updated operator: %v", err)
|
||
}
|
||
if updated.Name != "Updated Operator" || updated.Type != "custom" || updated.Category != "AI" || updated.Description != "Updated description" {
|
||
t.Fatalf("expected normalized operator update, got %#v", updated)
|
||
}
|
||
if updated.Version != "2.0.0" {
|
||
t.Fatalf("expected normalized operator version, got %q", updated.Version)
|
||
}
|
||
if len(updated.Outputs) != 1 || updated.Outputs[0].ID != "out-2" || updated.Outputs[0].Name != "tensor result" || updated.Outputs[0].Type != domain.PortTypeTensor || updated.Outputs[0].Transmission != "stream" {
|
||
t.Fatalf("expected normalized updated output port, got %#v", updated.Outputs)
|
||
}
|
||
if updated.Resources == nil || updated.Resources.CPULimit != "750m" || updated.Resources.MemoryLimit != "2Gi" || updated.Resources.GPULimit != "" || updated.Resources.SharedMemorySize != "" {
|
||
t.Fatalf("expected normalized updated resources, got %#v", updated.Resources)
|
||
}
|
||
}
|
||
|
||
func TestWorkflowRunRecordsLightweightCodeManifest(t *testing.T) {
|
||
ctx := context.Background()
|
||
workspaceRoot := t.TempDir()
|
||
codeDir := filepath.Join(workspaceRoot, "lightweight-operators", "demo")
|
||
if err := os.MkdirAll(codeDir, 0o755); err != nil {
|
||
t.Fatalf("create code dir: %v", err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(codeDir, "operator.py"), []byte("print('ok')\n"), 0o644); err != nil {
|
||
t.Fatalf("write operator: %v", err)
|
||
}
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{WorkspaceMountPath: workspaceRoot})
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Lightweight", Description: "manifest"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "Lightweight workflow",
|
||
Description: "manifest",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{{
|
||
ID: "light",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "Lightweight node",
|
||
Category: "AI",
|
||
Description: "manifest",
|
||
Image: "python:3.11-slim",
|
||
ContainerCommand: []string{"python", "/opt/droboticflow/lightweight/operator/operator.py"},
|
||
ContainerArgs: []string{},
|
||
RuntimeContract: map[string]any{
|
||
"lightweightOperator": map[string]any{
|
||
"enabled": true,
|
||
"codeSubPath": "lightweight-operators/demo",
|
||
"mountPath": "/opt/droboticflow/lightweight/operator",
|
||
"containerArgsConfigured": true,
|
||
},
|
||
},
|
||
},
|
||
Position: domain.Position{X: 0, Y: 0},
|
||
}},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", nil))
|
||
if recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("run create status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var run domain.RunRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &run); err != nil {
|
||
t.Fatalf("decode run: %v", err)
|
||
}
|
||
if run.Metadata == nil || len(run.Metadata.LightweightOperatorCodeManifests) != 1 {
|
||
t.Fatalf("expected lightweight code manifest metadata, got %#v", run.Metadata)
|
||
}
|
||
manifest := run.Metadata.LightweightOperatorCodeManifests["light"]
|
||
if manifest.FileCount != 1 || manifest.TotalBytes == 0 || manifest.SHA256 == "" || manifest.ReadOnly {
|
||
t.Fatalf("unexpected manifest: %#v", manifest)
|
||
}
|
||
if len(manifest.Files) != 1 || manifest.Files[0].Path != "operator.py" || manifest.Files[0].SHA256 == "" {
|
||
t.Fatalf("unexpected manifest files: %#v", manifest.Files)
|
||
}
|
||
}
|
||
|
||
func TestLightweightOperatorRequiresExplicitRuntimeFields(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader([]byte(`{
|
||
"name":"Lightweight Operator",
|
||
"type":"lightweight",
|
||
"category":"AI",
|
||
"description":"missing explicit runtime fields",
|
||
"image":"python:3.11-slim",
|
||
"runtimeContract":{"lightweightOperator":{"codeSubPath":"lightweight-operators/demo"}},
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
||
}`))),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected bad request, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
for _, expected := range []string{"mountPath is required", "containerCommand is required", "containerArgs must be explicitly configured"} {
|
||
if !strings.Contains(body, expected) {
|
||
t.Fatalf("expected %q in validation body, got %s", expected, body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func createLightweightOperatorForFileEditorTest(t *testing.T, handler http.Handler, codeSubPath string) domain.OperatorRecord {
|
||
t.Helper()
|
||
payload := []byte(`{
|
||
"name":"Lightweight File Editor",
|
||
"type":"lightweight",
|
||
"category":"AI",
|
||
"description":"Edit workspace code",
|
||
"image":"python:3.11-slim",
|
||
"containerCommand":["python","/opt/droboticflow/lightweight/operator/operator.py"],
|
||
"containerArgs":[],
|
||
"runtimeContract":{"lightweightOperator":{"codeSubPath":"` + codeSubPath + `","mountPath":"/opt/droboticflow/lightweight/operator"}},
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
||
}`)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader(payload)))
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("operator create status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var created domain.OperatorRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatalf("decode operator: %v", err)
|
||
}
|
||
return created
|
||
}
|
||
|
||
func TestLightweightOperatorFileEditorListsReadsAndWritesCodeDirectory(t *testing.T) {
|
||
workspaceRoot := t.TempDir()
|
||
codeDir := filepath.Join(workspaceRoot, "lightweight-operators", "editor")
|
||
if err := os.MkdirAll(filepath.Join(codeDir, "src"), 0o755); err != nil {
|
||
t.Fatalf("create code dir: %v", err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(codeDir, "operator.py"), []byte("print('old')\n"), 0o644); err != nil {
|
||
t.Fatalf("write operator: %v", err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(codeDir, "src", "helper.py"), []byte("VALUE = 1\n"), 0o644); err != nil {
|
||
t.Fatalf("write helper: %v", err)
|
||
}
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{WorkspaceMountPath: workspaceRoot})
|
||
created := createLightweightOperatorForFileEditorTest(t, handler, "lightweight-operators/editor")
|
||
|
||
listRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/operators/"+created.ID+"/lightweight-files", nil))
|
||
if listRecorder.Code != http.StatusOK {
|
||
t.Fatalf("list status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
||
}
|
||
var listResponse lightweightOperatorFilesResponse
|
||
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listResponse); err != nil {
|
||
t.Fatalf("decode list: %v", err)
|
||
}
|
||
if !listResponse.Exists || listResponse.CodeSubPath != "lightweight-operators/editor" || listResponse.MountPath != "/opt/droboticflow/lightweight/operator" {
|
||
t.Fatalf("unexpected list response: %#v", listResponse)
|
||
}
|
||
paths := make([]string, 0, len(listResponse.Items))
|
||
for _, item := range listResponse.Items {
|
||
paths = append(paths, item.Path)
|
||
}
|
||
sort.Strings(paths)
|
||
if strings.Join(paths, ",") != "operator.py,src,src/helper.py" {
|
||
t.Fatalf("unexpected file tree paths: %#v", paths)
|
||
}
|
||
|
||
contentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
contentRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/operators/"+created.ID+"/lightweight-files/content?path="+url.QueryEscape("operator.py"), nil),
|
||
)
|
||
if contentRecorder.Code != http.StatusOK {
|
||
t.Fatalf("content status: %d body=%s", contentRecorder.Code, contentRecorder.Body.String())
|
||
}
|
||
var contentResponse lightweightOperatorFileContentResponse
|
||
if err := json.Unmarshal(contentRecorder.Body.Bytes(), &contentResponse); err != nil {
|
||
t.Fatalf("decode content: %v", err)
|
||
}
|
||
if contentResponse.Content != "print('old')\n" || contentResponse.Path != "operator.py" {
|
||
t.Fatalf("unexpected content response: %#v", contentResponse)
|
||
}
|
||
|
||
updateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
updateRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodPut,
|
||
"/api/operators/"+created.ID+"/lightweight-files/content",
|
||
bytes.NewReader([]byte(`{"path":"src/new.py","content":"print('new')\n"}`)),
|
||
),
|
||
)
|
||
if updateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||
}
|
||
written, err := os.ReadFile(filepath.Join(codeDir, "src", "new.py"))
|
||
if err != nil {
|
||
t.Fatalf("read written file: %v", err)
|
||
}
|
||
if string(written) != "print('new')\n" {
|
||
t.Fatalf("unexpected written file content: %q", string(written))
|
||
}
|
||
}
|
||
|
||
func TestLightweightOperatorFileEditorPaginatesCompleteCodeDirectory(t *testing.T) {
|
||
workspaceRoot := t.TempDir()
|
||
codeDir := filepath.Join(workspaceRoot, "lightweight-operators", "large-editor")
|
||
if err := os.MkdirAll(codeDir, 0o755); err != nil {
|
||
t.Fatalf("create code dir: %v", err)
|
||
}
|
||
for index := 0; index < lightweightOperatorFileListLimit+5; index++ {
|
||
name := filepath.Join(codeDir, fmt.Sprintf("file-%04d.py", index))
|
||
if err := os.WriteFile(name, []byte("VALUE = 1\n"), 0o644); err != nil {
|
||
t.Fatalf("write %s: %v", name, err)
|
||
}
|
||
}
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{WorkspaceMountPath: workspaceRoot})
|
||
created := createLightweightOperatorForFileEditorTest(t, handler, "lightweight-operators/large-editor")
|
||
|
||
firstRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(firstRecorder, httptest.NewRequest(http.MethodGet, "/api/operators/"+created.ID+"/lightweight-files", nil))
|
||
if firstRecorder.Code != http.StatusOK {
|
||
t.Fatalf("first page status: %d body=%s", firstRecorder.Code, firstRecorder.Body.String())
|
||
}
|
||
var first lightweightOperatorFilesResponse
|
||
if err := json.Unmarshal(firstRecorder.Body.Bytes(), &first); err != nil {
|
||
t.Fatalf("decode first page: %v", err)
|
||
}
|
||
if !first.Truncated || first.NextOffset != lightweightOperatorFileListLimit || len(first.Items) != lightweightOperatorFileListLimit {
|
||
t.Fatalf("unexpected first page: %#v", first)
|
||
}
|
||
if first.FileCount != lightweightOperatorFileListLimit+5 || first.TotalItems != lightweightOperatorFileListLimit+5 {
|
||
t.Fatalf("unexpected first page totals: %#v", first)
|
||
}
|
||
|
||
secondRecorder := httptest.NewRecorder()
|
||
secondPath := fmt.Sprintf("/api/operators/%s/lightweight-files?offset=%d", created.ID, first.NextOffset)
|
||
handler.ServeHTTP(secondRecorder, httptest.NewRequest(http.MethodGet, secondPath, nil))
|
||
if secondRecorder.Code != http.StatusOK {
|
||
t.Fatalf("second page status: %d body=%s", secondRecorder.Code, secondRecorder.Body.String())
|
||
}
|
||
var second lightweightOperatorFilesResponse
|
||
if err := json.Unmarshal(secondRecorder.Body.Bytes(), &second); err != nil {
|
||
t.Fatalf("decode second page: %v", err)
|
||
}
|
||
if second.Truncated || second.NextOffset != 0 || second.PageOffset != lightweightOperatorFileListLimit || len(second.Items) != 5 {
|
||
t.Fatalf("unexpected second page: %#v", second)
|
||
}
|
||
}
|
||
|
||
func TestLightweightOperatorFileEditorCreatesMissingCodeDirectoryAndRejectsTraversal(t *testing.T) {
|
||
workspaceRoot := t.TempDir()
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{WorkspaceMountPath: workspaceRoot})
|
||
created := createLightweightOperatorForFileEditorTest(t, handler, "lightweight-operators/new-editor")
|
||
|
||
listRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/operators/"+created.ID+"/lightweight-files", nil))
|
||
if listRecorder.Code != http.StatusOK {
|
||
t.Fatalf("missing-root list status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
||
}
|
||
var listResponse lightweightOperatorFilesResponse
|
||
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listResponse); err != nil {
|
||
t.Fatalf("decode missing-root list: %v", err)
|
||
}
|
||
if listResponse.Exists || len(listResponse.Items) != 0 {
|
||
t.Fatalf("expected missing root to return an empty editable directory response, got %#v", listResponse)
|
||
}
|
||
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodPut,
|
||
"/api/operators/"+created.ID+"/lightweight-files/content",
|
||
bytes.NewReader([]byte(`{"path":"operator.py","content":"print('created')\n"}`)),
|
||
),
|
||
)
|
||
if createRecorder.Code != http.StatusOK {
|
||
t.Fatalf("create file status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
||
}
|
||
if _, err := os.Stat(filepath.Join(workspaceRoot, "lightweight-operators", "new-editor", "operator.py")); err != nil {
|
||
t.Fatalf("expected file to be created in code directory: %v", err)
|
||
}
|
||
|
||
traversalRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
traversalRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodPut,
|
||
"/api/operators/"+created.ID+"/lightweight-files/content",
|
||
bytes.NewReader([]byte(`{"path":"../escape.py","content":"bad"}`)),
|
||
),
|
||
)
|
||
if traversalRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected traversal to fail, got %d body=%s", traversalRecorder.Code, traversalRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(traversalRecorder.Body.String(), "path must stay within the code directory") {
|
||
t.Fatalf("expected traversal validation message, got %s", traversalRecorder.Body.String())
|
||
}
|
||
if _, err := os.Stat(filepath.Join(workspaceRoot, "lightweight-operators", "escape.py")); !errors.Is(err, os.ErrNotExist) {
|
||
t.Fatalf("expected escape file to be absent, stat err=%v", err)
|
||
}
|
||
}
|
||
|
||
func TestLightweightOperatorFileEditorSelectsWorkspacePVCMountAndEnforcesReadOnly(t *testing.T) {
|
||
workspaceRoot := t.TempDir()
|
||
if err := os.MkdirAll(filepath.Join(workspaceRoot, "lightweight-operators", "editor"), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.MkdirAll(filepath.Join(workspaceRoot, "datasets", "robotwin"), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(workspaceRoot, "lightweight-operators", "editor", "operator.py"), []byte("print('ok')\n"), 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(workspaceRoot, "datasets", "robotwin", "state.json"), []byte("{}\n"), 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{WorkspaceMountPath: workspaceRoot, WorkspacePVCName: "workspace-pvc"})
|
||
payload := []byte(`{
|
||
"name":"Multi Mount Editor","type":"lightweight","category":"AI","description":"Edit mounted PVC directories",
|
||
"image":"python:3.11-slim","containerCommand":["python","/opt/operator/operator.py"],"containerArgs":[],
|
||
"runtimeContract":{"lightweightOperator":{"codeSubPath":"lightweight-operators/editor","mountPath":"/opt/operator","mounts":[
|
||
{"name":"dataset","pvcName":"workspace-pvc","subPath":"datasets/robotwin","mountPath":"/data","readOnly":true,"ideEnabled":true},
|
||
{"name":"models","pvcName":"model-pvc","subPath":"models","mountPath":"/models","readOnly":true,"ideEnabled":true}
|
||
]}},"inputs":[],"outputs":[]
|
||
}`)
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(createRecorder, httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader(payload)))
|
||
if createRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create status=%d body=%s", createRecorder.Code, createRecorder.Body.String())
|
||
}
|
||
var created domain.OperatorRecord
|
||
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
listRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/operators/"+created.ID+"/lightweight-files?mount=dataset", nil))
|
||
if listRecorder.Code != http.StatusOK {
|
||
t.Fatalf("list status=%d body=%s", listRecorder.Code, listRecorder.Body.String())
|
||
}
|
||
var listed lightweightOperatorFilesResponse
|
||
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listed); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if !listed.ReadOnly || listed.CodeSubPath != "datasets/robotwin" || len(listed.Items) != 1 || listed.Items[0].Path != "state.json" {
|
||
t.Fatalf("unexpected mounted file tree: %#v", listed)
|
||
}
|
||
|
||
writeRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(writeRecorder, httptest.NewRequest(http.MethodPut, "/api/operators/"+created.ID+"/lightweight-files/content?mount=dataset", bytes.NewReader([]byte(`{"path":"new.txt","content":"blocked"}`))))
|
||
if writeRecorder.Code != http.StatusBadRequest || !strings.Contains(writeRecorder.Body.String(), "read-only") {
|
||
t.Fatalf("expected read-only failure, status=%d body=%s", writeRecorder.Code, writeRecorder.Body.String())
|
||
}
|
||
|
||
externalRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(externalRecorder, httptest.NewRequest(http.MethodGet, "/api/operators/"+created.ID+"/lightweight-files?mount=models", nil))
|
||
if externalRecorder.Code != http.StatusBadRequest || !strings.Contains(externalRecorder.Body.String(), "not mounted into the file management service") {
|
||
t.Fatalf("expected explicit external PVC boundary, status=%d body=%s", externalRecorder.Code, externalRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestLightweightOperatorFileEditorManagesDirectoriesUploadsMovesAndDeletes(t *testing.T) {
|
||
workspaceRoot := t.TempDir()
|
||
codeDir := filepath.Join(workspaceRoot, "lightweight-operators", "manager")
|
||
if err := os.MkdirAll(codeDir, 0o755); err != nil {
|
||
t.Fatalf("create code dir: %v", err)
|
||
}
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{WorkspaceMountPath: workspaceRoot})
|
||
created := createLightweightOperatorForFileEditorTest(t, handler, "lightweight-operators/manager")
|
||
|
||
dirRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
dirRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodPost,
|
||
"/api/operators/"+created.ID+"/lightweight-files/directories",
|
||
bytes.NewReader([]byte(`{"path":"pkg/data"}`)),
|
||
),
|
||
)
|
||
if dirRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("directory create status: %d body=%s", dirRecorder.Code, dirRecorder.Body.String())
|
||
}
|
||
if info, err := os.Stat(filepath.Join(codeDir, "pkg", "data")); err != nil || !info.IsDir() {
|
||
t.Fatalf("expected directory to be created, info=%#v err=%v", info, err)
|
||
}
|
||
|
||
var uploadBody bytes.Buffer
|
||
uploadWriter := multipart.NewWriter(&uploadBody)
|
||
if err := uploadWriter.WriteField("path", "pkg/data/blob.bin"); err != nil {
|
||
t.Fatalf("write upload path field: %v", err)
|
||
}
|
||
part, err := uploadWriter.CreateFormFile("file", "blob.bin")
|
||
if err != nil {
|
||
t.Fatalf("create upload file field: %v", err)
|
||
}
|
||
if _, err := part.Write([]byte{0, 1, 2, 3, 255}); err != nil {
|
||
t.Fatalf("write upload file field: %v", err)
|
||
}
|
||
if err := uploadWriter.Close(); err != nil {
|
||
t.Fatalf("close upload body: %v", err)
|
||
}
|
||
uploadRecorder := httptest.NewRecorder()
|
||
uploadRequest := httptest.NewRequest(http.MethodPost, "/api/operators/"+created.ID+"/lightweight-files/upload", &uploadBody)
|
||
uploadRequest.Header.Set("Content-Type", uploadWriter.FormDataContentType())
|
||
handler.ServeHTTP(uploadRecorder, uploadRequest)
|
||
if uploadRecorder.Code != http.StatusOK {
|
||
t.Fatalf("upload status: %d body=%s", uploadRecorder.Code, uploadRecorder.Body.String())
|
||
}
|
||
uploaded, err := os.ReadFile(filepath.Join(codeDir, "pkg", "data", "blob.bin"))
|
||
if err != nil {
|
||
t.Fatalf("read uploaded file: %v", err)
|
||
}
|
||
if !bytes.Equal(uploaded, []byte{0, 1, 2, 3, 255}) {
|
||
t.Fatalf("unexpected uploaded content: %#v", uploaded)
|
||
}
|
||
|
||
moveRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
moveRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodPatch,
|
||
"/api/operators/"+created.ID+"/lightweight-files",
|
||
bytes.NewReader([]byte(`{"path":"pkg/data/blob.bin","newPath":"pkg/data/renamed.bin"}`)),
|
||
),
|
||
)
|
||
if moveRecorder.Code != http.StatusOK {
|
||
t.Fatalf("move status: %d body=%s", moveRecorder.Code, moveRecorder.Body.String())
|
||
}
|
||
if _, err := os.Stat(filepath.Join(codeDir, "pkg", "data", "blob.bin")); !errors.Is(err, os.ErrNotExist) {
|
||
t.Fatalf("expected old path to be absent, stat err=%v", err)
|
||
}
|
||
if _, err := os.Stat(filepath.Join(codeDir, "pkg", "data", "renamed.bin")); err != nil {
|
||
t.Fatalf("expected renamed file to exist: %v", err)
|
||
}
|
||
|
||
deleteFileRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteFileRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodDelete,
|
||
"/api/operators/"+created.ID+"/lightweight-files?path="+url.QueryEscape("pkg/data/renamed.bin"),
|
||
nil,
|
||
),
|
||
)
|
||
if deleteFileRecorder.Code != http.StatusOK {
|
||
t.Fatalf("delete file status: %d body=%s", deleteFileRecorder.Code, deleteFileRecorder.Body.String())
|
||
}
|
||
if _, err := os.Stat(filepath.Join(codeDir, "pkg", "data", "renamed.bin")); !errors.Is(err, os.ErrNotExist) {
|
||
t.Fatalf("expected renamed file to be deleted, stat err=%v", err)
|
||
}
|
||
|
||
deleteDirRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteDirRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodDelete,
|
||
"/api/operators/"+created.ID+"/lightweight-files?path="+url.QueryEscape("pkg"),
|
||
nil,
|
||
),
|
||
)
|
||
if deleteDirRecorder.Code != http.StatusOK {
|
||
t.Fatalf("delete directory status: %d body=%s", deleteDirRecorder.Code, deleteDirRecorder.Body.String())
|
||
}
|
||
if _, err := os.Stat(filepath.Join(codeDir, "pkg")); !errors.Is(err, os.ErrNotExist) {
|
||
t.Fatalf("expected directory to be deleted recursively, stat err=%v", err)
|
||
}
|
||
|
||
traversalRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
traversalRecorder,
|
||
httptest.NewRequest(
|
||
http.MethodPost,
|
||
"/api/operators/"+created.ID+"/lightweight-files/directories",
|
||
bytes.NewReader([]byte(`{"path":"../escape-dir"}`)),
|
||
),
|
||
)
|
||
if traversalRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected traversal directory create to fail, got %d body=%s", traversalRecorder.Code, traversalRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowRunRejectsMissingLightweightCodeDirectory(t *testing.T) {
|
||
ctx := context.Background()
|
||
workspaceRoot := t.TempDir()
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{WorkspaceMountPath: workspaceRoot})
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Lightweight", Description: "missing dir"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "Lightweight workflow",
|
||
Description: "missing dir",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{{
|
||
ID: "light",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "Lightweight node",
|
||
Category: "AI",
|
||
Description: "missing dir",
|
||
Image: "python:3.11-slim",
|
||
ContainerCommand: []string{"python", "/opt/droboticflow/lightweight/operator/operator.py"},
|
||
RuntimeContract: map[string]any{
|
||
"lightweightOperator": map[string]any{
|
||
"enabled": true,
|
||
"codeSubPath": "lightweight-operators/missing",
|
||
"mountPath": "/opt/droboticflow/lightweight/operator",
|
||
"containerArgsConfigured": true,
|
||
},
|
||
},
|
||
},
|
||
Position: domain.Position{X: 0, Y: 0},
|
||
}},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", nil))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected bad request for missing code directory, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "code directory does not exist") {
|
||
t.Fatalf("expected missing code directory error, got %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestCreateAndUpdatePreserveOperatorRunnerImage(t *testing.T) {
|
||
t.Setenv("OPERATOR_RUNNER_IMAGE", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260412151842-amd64")
|
||
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
oldRunnerImage := "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260410181157-amd64"
|
||
customImage := "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/custom-operator:1"
|
||
|
||
operatorCreatePayload := []byte(`{
|
||
"name":"镜像归一化算子",
|
||
"type":"custom",
|
||
"category":"IO",
|
||
"description":"normalize operator image",
|
||
"image":"` + oldRunnerImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
||
}`)
|
||
operatorCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(operatorCreateRecorder, httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader(operatorCreatePayload)))
|
||
if operatorCreateRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("operator create status: %d body=%s", operatorCreateRecorder.Code, operatorCreateRecorder.Body.String())
|
||
}
|
||
|
||
var createdOperator domain.OperatorRecord
|
||
if err := json.Unmarshal(operatorCreateRecorder.Body.Bytes(), &createdOperator); err != nil {
|
||
t.Fatalf("decode created operator: %v", err)
|
||
}
|
||
if createdOperator.Image != oldRunnerImage {
|
||
t.Fatalf("expected created operator image %q, got %q", oldRunnerImage, createdOperator.Image)
|
||
}
|
||
|
||
storedOperator, err := dataStore.GetOperator(ctx, createdOperator.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored operator: %v", err)
|
||
}
|
||
if storedOperator == nil || storedOperator.Image != oldRunnerImage {
|
||
t.Fatalf("expected stored operator image %q, got %+v", oldRunnerImage, storedOperator)
|
||
}
|
||
|
||
operatorUpdatePayload := []byte(`{"image":"` + oldRunnerImage + `"}`)
|
||
operatorUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(operatorUpdateRecorder, httptest.NewRequest(http.MethodPut, "/api/operators/"+createdOperator.ID, bytes.NewReader(operatorUpdatePayload)))
|
||
if operatorUpdateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("operator update status: %d body=%s", operatorUpdateRecorder.Code, operatorUpdateRecorder.Body.String())
|
||
}
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "镜像归一化项目",
|
||
Description: "normalize workflow images",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
|
||
workflowCreatePayload := []byte(`{
|
||
"name":"镜像归一化工作流",
|
||
"description":"normalize workflow node image",
|
||
"type":"batch",
|
||
"nodes":[
|
||
{
|
||
"id":"runner-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Runner Node",
|
||
"category":"IO",
|
||
"description":"runner node",
|
||
"image":"` + oldRunnerImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
},
|
||
{
|
||
"id":"custom-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Custom Node",
|
||
"category":"IO",
|
||
"description":"custom node",
|
||
"image":"` + customImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":180,"y":0}
|
||
}
|
||
],
|
||
"edges":[{"id":"edge-runner-custom","source":"runner-node","sourceHandle":"out-1","target":"custom-node","targetHandle":"in-1"}]
|
||
}`)
|
||
workflowCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(workflowCreateRecorder, httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(workflowCreatePayload)))
|
||
if workflowCreateRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("workflow create status: %d body=%s", workflowCreateRecorder.Code, workflowCreateRecorder.Body.String())
|
||
}
|
||
|
||
var createdWorkflow domain.WorkflowRecord
|
||
if err := json.Unmarshal(workflowCreateRecorder.Body.Bytes(), &createdWorkflow); err != nil {
|
||
t.Fatalf("decode created workflow: %v", err)
|
||
}
|
||
if createdWorkflow.Nodes[0].Data.Image != oldRunnerImage {
|
||
t.Fatalf("expected created workflow runner image %q, got %q", oldRunnerImage, createdWorkflow.Nodes[0].Data.Image)
|
||
}
|
||
if createdWorkflow.Nodes[1].Data.Image != customImage {
|
||
t.Fatalf("expected custom workflow image to remain %q, got %q", customImage, createdWorkflow.Nodes[1].Data.Image)
|
||
}
|
||
|
||
storedWorkflow, err := dataStore.GetWorkflow(ctx, createdWorkflow.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored workflow: %v", err)
|
||
}
|
||
if storedWorkflow == nil || storedWorkflow.Nodes[0].Data.Image != oldRunnerImage {
|
||
t.Fatalf("expected stored workflow runner image %q, got %+v", oldRunnerImage, storedWorkflow)
|
||
}
|
||
if storedWorkflow.Nodes[1].Data.Image != customImage {
|
||
t.Fatalf("expected stored custom workflow image to remain %q, got %q", customImage, storedWorkflow.Nodes[1].Data.Image)
|
||
}
|
||
|
||
workflowUpdatePayload := []byte(`{
|
||
"nodes":[
|
||
{
|
||
"id":"runner-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Runner Node",
|
||
"category":"IO",
|
||
"description":"runner node",
|
||
"image":"` + oldRunnerImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
},
|
||
{
|
||
"id":"custom-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Custom Node",
|
||
"category":"IO",
|
||
"description":"custom node",
|
||
"image":"` + customImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":180,"y":0}
|
||
}
|
||
]
|
||
}`)
|
||
workflowUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(workflowUpdateRecorder, httptest.NewRequest(http.MethodPut, "/api/workflows/"+createdWorkflow.ID, bytes.NewReader(workflowUpdatePayload)))
|
||
if workflowUpdateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("workflow update status: %d body=%s", workflowUpdateRecorder.Code, workflowUpdateRecorder.Body.String())
|
||
}
|
||
|
||
templateCreatePayload := []byte(`{
|
||
"name":"镜像归一化模板",
|
||
"description":"normalize template node image",
|
||
"type":"batch",
|
||
"nodes":[
|
||
{
|
||
"id":"runner-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Runner Node",
|
||
"category":"IO",
|
||
"description":"runner node",
|
||
"image":"` + oldRunnerImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
},
|
||
{
|
||
"id":"custom-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Custom Node",
|
||
"category":"IO",
|
||
"description":"custom node",
|
||
"image":"` + customImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":180,"y":0}
|
||
}
|
||
],
|
||
"edges":[{"id":"edge-runner-custom","source":"runner-node","sourceHandle":"out-1","target":"custom-node","targetHandle":"in-1"}]
|
||
}`)
|
||
templateCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(templateCreateRecorder, httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(templateCreatePayload)))
|
||
if templateCreateRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("template create status: %d body=%s", templateCreateRecorder.Code, templateCreateRecorder.Body.String())
|
||
}
|
||
|
||
var createdTemplate domain.TemplateRecord
|
||
if err := json.Unmarshal(templateCreateRecorder.Body.Bytes(), &createdTemplate); err != nil {
|
||
t.Fatalf("decode created template: %v", err)
|
||
}
|
||
if createdTemplate.Nodes[0].Data.Image != oldRunnerImage {
|
||
t.Fatalf("expected created template runner image %q, got %q", oldRunnerImage, createdTemplate.Nodes[0].Data.Image)
|
||
}
|
||
if createdTemplate.Nodes[1].Data.Image != customImage {
|
||
t.Fatalf("expected custom template image to remain %q, got %q", customImage, createdTemplate.Nodes[1].Data.Image)
|
||
}
|
||
|
||
storedTemplate, err := dataStore.GetTemplate(ctx, createdTemplate.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored template: %v", err)
|
||
}
|
||
if storedTemplate == nil || storedTemplate.Nodes[0].Data.Image != oldRunnerImage {
|
||
t.Fatalf("expected stored template runner image %q, got %+v", oldRunnerImage, storedTemplate)
|
||
}
|
||
if storedTemplate.Nodes[1].Data.Image != customImage {
|
||
t.Fatalf("expected stored custom template image to remain %q, got %q", customImage, storedTemplate.Nodes[1].Data.Image)
|
||
}
|
||
|
||
templateUpdatePayload := []byte(`{
|
||
"nodes":[
|
||
{
|
||
"id":"runner-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Runner Node",
|
||
"category":"IO",
|
||
"description":"runner node",
|
||
"image":"` + oldRunnerImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
},
|
||
{
|
||
"id":"custom-node",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"Custom Node",
|
||
"category":"IO",
|
||
"description":"custom node",
|
||
"image":"` + customImage + `",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
||
},
|
||
"position":{"x":180,"y":0}
|
||
}
|
||
]
|
||
}`)
|
||
templateUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(templateUpdateRecorder, httptest.NewRequest(http.MethodPut, "/api/templates/"+createdTemplate.ID, bytes.NewReader(templateUpdatePayload)))
|
||
if templateUpdateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("template update status: %d body=%s", templateUpdateRecorder.Code, templateUpdateRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
type fakeOperatorImageArchiveImporter struct {
|
||
t *testing.T
|
||
digest string
|
||
input *operatorImageArchiveImportJobInput
|
||
}
|
||
|
||
func (importer *fakeOperatorImageArchiveImporter) Import(ctx context.Context, input operatorImageArchiveImportJobInput) (*operatorImageArchiveImportJobResult, error) {
|
||
importer.t.Helper()
|
||
importer.input = &input
|
||
payload, err := os.ReadFile(input.ArchivePath)
|
||
if err != nil {
|
||
importer.t.Fatalf("expected uploaded archive to be readable: %v", err)
|
||
}
|
||
if string(payload) != "fake docker archive" {
|
||
importer.t.Fatalf("unexpected uploaded archive payload %q", string(payload))
|
||
}
|
||
if input.ArchiveFormat != "docker" {
|
||
importer.t.Fatalf("expected docker archive format, got %q", input.ArchiveFormat)
|
||
}
|
||
if input.TimeoutSeconds != 600 {
|
||
importer.t.Fatalf("expected import timeout 600, got %d", input.TimeoutSeconds)
|
||
}
|
||
if input.ImporterImage != "ccr.example/tools/skopeo:1" {
|
||
importer.t.Fatalf("expected importer image to be passed through, got %q", input.ImporterImage)
|
||
}
|
||
return &operatorImageArchiveImportJobResult{Digest: importer.digest}, nil
|
||
}
|
||
|
||
func buildOperatorImageArchiveImportBody(t *testing.T, fields map[string]string) (*bytes.Buffer, string) {
|
||
t.Helper()
|
||
body := &bytes.Buffer{}
|
||
writer := multipart.NewWriter(body)
|
||
for key, value := range fields {
|
||
if err := writer.WriteField(key, value); err != nil {
|
||
t.Fatalf("write field %s: %v", key, err)
|
||
}
|
||
}
|
||
part, err := writer.CreateFormFile("file", "operator.tar")
|
||
if err != nil {
|
||
t.Fatalf("create archive form file: %v", err)
|
||
}
|
||
if _, err := part.Write([]byte("fake docker archive")); err != nil {
|
||
t.Fatalf("write archive: %v", err)
|
||
}
|
||
if err := writer.Close(); err != nil {
|
||
t.Fatalf("close multipart writer: %v", err)
|
||
}
|
||
return body, writer.FormDataContentType()
|
||
}
|
||
|
||
func TestOperatorImageArchiveImportCreatesOperatorWithDigestImage(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
workspace := t.TempDir()
|
||
digest := "sha256:" + strings.Repeat("a", 64)
|
||
fakeImporter := &fakeOperatorImageArchiveImporter{t: t, digest: digest}
|
||
previousFactory := operatorImageArchiveImporterFactory
|
||
operatorImageArchiveImporterFactory = func(server *server) (operatorImageArchiveImporter, error) {
|
||
return fakeImporter, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
operatorImageArchiveImporterFactory = previousFactory
|
||
})
|
||
|
||
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{
|
||
K8sNamespace: "droboticflow",
|
||
K8sImagePullSecretName: "droboticflow-registry",
|
||
WorkspacePVCName: "droboticflow-workspace",
|
||
WorkspaceMountPath: workspace,
|
||
CustomOperatorImageRegistry: "ccr.example/dataflow-poc/custom-operators",
|
||
CustomOperatorImageImporterImage: "ccr.example/tools/skopeo:1",
|
||
CustomOperatorImageImportTimeoutSeconds: 600,
|
||
CustomOperatorImageArchiveMaxMiB: 16,
|
||
})
|
||
body, contentType := buildOperatorImageArchiveImportBody(t, map[string]string{
|
||
"name": " Archive Import Operator ",
|
||
"type": " custom ",
|
||
"category": " AI ",
|
||
"description": " imported from docker archive ",
|
||
"repository": "Team Vision/Segment Operator",
|
||
"tag": "v1_test",
|
||
"outputs": `[{"id":"out-1","name":" result ","type":" JSON ","transmission":" BUS "}]`,
|
||
"hyperparams": `[{"key":"THRESHOLD","value":"0.5"}]`,
|
||
"containerCommand": `["python","/app/src/operator.py"]`,
|
||
"containerArgs": `["--mode","batch"]`,
|
||
"runtimeContract": `{"protocol":"droboticflow.external-operator/v1","resultOutputPathEnv":"RESULT_OUTPUT_PATH"}`,
|
||
"archiveFormat": "docker",
|
||
})
|
||
request := httptest.NewRequest(http.MethodPost, "/api/operators/image-archives/import", body)
|
||
request.Header.Set("Content-Type", contentType)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("operator image archive import status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var response struct {
|
||
Repository string `json:"repository"`
|
||
Tag string `json:"tag"`
|
||
Version string `json:"version"`
|
||
Image string `json:"image"`
|
||
ImageID string `json:"imageId"`
|
||
Digest string `json:"digest"`
|
||
Operator domain.OperatorRecord `json:"operator"`
|
||
}
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
||
t.Fatalf("decode import response: %v", err)
|
||
}
|
||
if response.Repository != "team-vision/segment-operator" || response.Tag != "v1_test" {
|
||
t.Fatalf("unexpected repository/tag: %#v", response)
|
||
}
|
||
if response.Version != "v1_test" {
|
||
t.Fatalf("expected version to default to tag, got %#v", response)
|
||
}
|
||
expectedTaggedImage := "ccr.example/dataflow-poc/custom-operators/team-vision/segment-operator:v1_test"
|
||
expectedImageID := "ccr.example/dataflow-poc/custom-operators/team-vision/segment-operator@" + digest
|
||
if response.Image != expectedTaggedImage || response.ImageID != expectedImageID || response.Digest != digest {
|
||
t.Fatalf("unexpected image refs: %#v", response)
|
||
}
|
||
if fakeImporter.input == nil || fakeImporter.input.DestinationImage != expectedTaggedImage {
|
||
t.Fatalf("expected importer to receive destination %q, got %#v", expectedTaggedImage, fakeImporter.input)
|
||
}
|
||
stored, err := dataStore.GetOperator(ctx, response.Operator.ID)
|
||
if err != nil {
|
||
t.Fatalf("get imported operator: %v", err)
|
||
}
|
||
if stored == nil || stored.Image != expectedImageID {
|
||
t.Fatalf("expected imported operator to use digest image %q, got %#v", expectedImageID, stored)
|
||
}
|
||
if stored.ImageID != expectedImageID || stored.Version != "v1_test" {
|
||
t.Fatalf("expected imported operator to persist imageId/version, got %#v", stored)
|
||
}
|
||
if stored.Name != "Archive Import Operator" || stored.Type != "custom" || stored.Category != "AI" {
|
||
t.Fatalf("expected normalized operator metadata, got %#v", stored)
|
||
}
|
||
if strings.Join(stored.ContainerCommand, " ") != "python /app/src/operator.py" || strings.Join(stored.ContainerArgs, " ") != "--mode batch" {
|
||
t.Fatalf("expected imported operator to preserve container command/args, got command=%#v args=%#v", stored.ContainerCommand, stored.ContainerArgs)
|
||
}
|
||
if stored.RuntimeContract == nil || stored.RuntimeContract["protocol"] != "droboticflow.external-operator/v1" {
|
||
t.Fatalf("expected imported operator to preserve runtime contract, got %#v", stored.RuntimeContract)
|
||
}
|
||
if len(stored.Outputs) != 1 || stored.Outputs[0].Name != "result" || stored.Outputs[0].Type != domain.PortTypeJSON || stored.Outputs[0].Transmission != "bus" {
|
||
t.Fatalf("expected normalized output contract, got %#v", stored.Outputs)
|
||
}
|
||
}
|
||
|
||
func TestOperatorImageArchiveImportRequiresConfiguredRegistry(t *testing.T) {
|
||
calledImporter := false
|
||
previousFactory := operatorImageArchiveImporterFactory
|
||
operatorImageArchiveImporterFactory = func(server *server) (operatorImageArchiveImporter, error) {
|
||
calledImporter = true
|
||
return nil, fmt.Errorf("should not be called")
|
||
}
|
||
t.Cleanup(func() {
|
||
operatorImageArchiveImporterFactory = previousFactory
|
||
})
|
||
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{
|
||
WorkspaceMountPath: t.TempDir(),
|
||
CustomOperatorImageImporterImage: "ccr.example/tools/skopeo:1",
|
||
CustomOperatorImageImportTimeoutSeconds: 600,
|
||
CustomOperatorImageArchiveMaxMiB: 16,
|
||
})
|
||
body, contentType := buildOperatorImageArchiveImportBody(t, map[string]string{
|
||
"name": "Archive Import Operator",
|
||
"type": "custom",
|
||
"category": "AI",
|
||
"description": "imported from docker archive",
|
||
"outputs": `[{"id":"out-1","name":"result","type":"json"}]`,
|
||
})
|
||
request := httptest.NewRequest(http.MethodPost, "/api/operators/image-archives/import", body)
|
||
request.Header.Set("Content-Type", contentType)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected missing registry validation status 400, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if calledImporter {
|
||
t.Fatalf("importer should not be called when registry is missing")
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "CUSTOM_OPERATOR_IMAGE_REGISTRY is required") {
|
||
t.Fatalf("expected registry validation message, got %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestOperatorImageArchiveImportAllowsAPITokenWhenSessionAuthRequired(t *testing.T) {
|
||
digest := "sha256:" + strings.Repeat("b", 64)
|
||
fakeImporter := &fakeOperatorImageArchiveImporter{t: t, digest: digest}
|
||
previousFactory := operatorImageArchiveImporterFactory
|
||
operatorImageArchiveImporterFactory = func(server *server) (operatorImageArchiveImporter, error) {
|
||
return fakeImporter, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
operatorImageArchiveImporterFactory = previousFactory
|
||
})
|
||
|
||
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{
|
||
APIToken: "archive-token",
|
||
RequireSessionAuth: true,
|
||
K8sNamespace: "droboticflow",
|
||
K8sImagePullSecretName: "droboticflow-registry",
|
||
WorkspacePVCName: "droboticflow-workspace",
|
||
WorkspaceMountPath: t.TempDir(),
|
||
CustomOperatorImageRegistry: "ccr.example/dataflow-poc/custom-operators",
|
||
CustomOperatorImageImporterImage: "ccr.example/tools/skopeo:1",
|
||
CustomOperatorImageImportTimeoutSeconds: 600,
|
||
CustomOperatorImageArchiveMaxMiB: 16,
|
||
})
|
||
body, contentType := buildOperatorImageArchiveImportBody(t, map[string]string{
|
||
"name": "Archive Token Operator",
|
||
"type": "custom",
|
||
"category": "IO",
|
||
"description": "imported with api token",
|
||
"repository": "archive-token/operator",
|
||
"tag": "v1",
|
||
"outputs": `[{"id":"out-1","name":"result","type":"json"}]`,
|
||
})
|
||
request := httptest.NewRequest(http.MethodPost, "/api/operators/image-archives/import", body)
|
||
request.Header.Set("Content-Type", contentType)
|
||
request.Header.Set("Authorization", "Bearer archive-token")
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, request)
|
||
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("expected API token import to succeed, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestCompletedWorkflowCanBeRunAgainManually(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.StatusAccepted {
|
||
t.Fatalf("expected 202 for completed workflow rerun, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||
}
|
||
|
||
var body domain.RunRecord
|
||
if err := json.Unmarshal(runRecorder.Body.Bytes(), &body); err != nil {
|
||
t.Fatalf("decode rerun response: %v", err)
|
||
}
|
||
if body.Status != domain.RunStatusQueued || body.WorkflowID != workflow.ID {
|
||
t.Fatalf("expected queued rerun for workflow %q, got %#v", workflow.ID, body)
|
||
}
|
||
|
||
activeRecorder := httptest.NewRecorder()
|
||
activeRequest := httptest.NewRequest(
|
||
http.MethodPost,
|
||
"/api/workflows/"+workflow.ID+"/run",
|
||
bytes.NewReader([]byte(`{"trigger":"manual"}`)),
|
||
)
|
||
handler.ServeHTTP(activeRecorder, activeRequest)
|
||
if activeRecorder.Code != http.StatusConflict || !strings.Contains(activeRecorder.Body.String(), "workflow_run_active") {
|
||
t.Fatalf("expected active rerun conflict, got %d body=%s", activeRecorder.Code, activeRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestProjectlessScanWorkflowCanRun(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
Name: "无项目扫描工作流",
|
||
Description: "projectless scan workflow",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Kind: domain.WorkflowKindScan,
|
||
ScanConfig: &domain.ScanConfig{
|
||
DataSource: "BOS",
|
||
SourcePath: "bos://datastorage/algo/X-platform-test",
|
||
Bucket: "datastorage",
|
||
Prefix: "algo/X-platform-test",
|
||
Endpoint: "https://bos.example.test",
|
||
AccessKeyID: "ak-projectless-1234",
|
||
SecretAccessKey: "sk-projectless-5678",
|
||
Enabled: false,
|
||
},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %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.StatusAccepted {
|
||
t.Fatalf("expected 202 for projectless scan workflow run, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||
}
|
||
|
||
var run domain.RunRecord
|
||
if err := json.Unmarshal(runRecorder.Body.Bytes(), &run); err != nil {
|
||
t.Fatalf("decode run response: %v", err)
|
||
}
|
||
if run.ProjectID != "" || run.ProjectName != "" {
|
||
t.Fatalf("expected blank project scope for projectless run, got %+v", run)
|
||
}
|
||
}
|
||
|
||
func TestRunningWorkflowCanBeStoppedGracefully(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "停止项目",
|
||
Description: "graceful stop",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "停止工作流",
|
||
Description: "stoppable",
|
||
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)
|
||
}
|
||
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
||
if err != nil {
|
||
t.Fatalf("create run: %v", err)
|
||
}
|
||
run.Status = domain.RunStatusRunning
|
||
run.StartedAt = nowISO()
|
||
run.UpdatedAt = nowISO()
|
||
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
||
t.Fatalf("upsert running run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(http.MethodPost, "/api/runs/"+run.ID+"/stop", nil)
|
||
handler.ServeHTTP(recorder, request)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected 200 from graceful stop, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
stored, err := dataStore.GetRun(ctx, run.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored run: %v", err)
|
||
}
|
||
if stored == nil || stored.Status != domain.RunStatus("stopping") {
|
||
t.Fatalf("expected run to enter stopping state, got %#v", stored)
|
||
}
|
||
}
|
||
|
||
func TestRunningWorkflowCanBeForceStopped(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
originalFactory := runJobTerminatorFactory
|
||
runJobTerminatorFactory = func(namespace string, imagePullSecretName string, workspacePVCName string, workspaceMountPath string) (runJobTerminator, error) {
|
||
return testRunJobTerminator{}, nil
|
||
}
|
||
defer func() {
|
||
runJobTerminatorFactory = originalFactory
|
||
}()
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "强制停止项目",
|
||
Description: "force stop",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "强制停止工作流",
|
||
Description: "force stoppable",
|
||
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)
|
||
}
|
||
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
||
if err != nil {
|
||
t.Fatalf("create run: %v", err)
|
||
}
|
||
run.Status = domain.RunStatusRunning
|
||
run.StartedAt = nowISO()
|
||
run.UpdatedAt = nowISO()
|
||
run.K8sRefs = []domain.K8sRef{{NodeID: "source", JobName: "run-source-1", Namespace: "droboticflow"}}
|
||
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
||
t.Fatalf("upsert running run: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(http.MethodPost, "/api/runs/"+run.ID+"/force-stop", nil)
|
||
handler.ServeHTTP(recorder, request)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("expected 200 from force stop, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
stored, err := dataStore.GetRun(ctx, run.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored run: %v", err)
|
||
}
|
||
if stored == nil || stored.Status != domain.RunStatusCancelled {
|
||
t.Fatalf("expected run to be cancelled, got %#v", stored)
|
||
}
|
||
if stored.EndedAt == "" {
|
||
t.Fatalf("expected cancelled run to be ended")
|
||
}
|
||
}
|
||
|
||
type testRunJobTerminator struct{}
|
||
|
||
func (testRunJobTerminator) TerminateRun(context.Context, string, string) error { return nil }
|
||
|
||
func (testRunJobTerminator) TerminateJob(context.Context, string, string) error { return nil }
|
||
|
||
func TestProjectWorkflowRejectsInvalidScanSourceIncrementalUnit(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "非法增量单位项目",
|
||
Description: "invalid incremental unit",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := []byte(`{
|
||
"name":"非法增量单位工作流",
|
||
"description":"invalid incremental unit",
|
||
"type":"batch",
|
||
"nodes":[{
|
||
"id":"scan-source",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"BOS 增量扫描",
|
||
"hyperparams":[
|
||
{"key":"DATA_SOURCE","value":"BOS"},
|
||
{"key":"SOURCE_PATH","value":"/data/source"},
|
||
{"key":"SCAN_MODE","value":"INCREMENTAL"},
|
||
{"key":"INCREMENTAL_UNIT","value":"ROW"}
|
||
]
|
||
}
|
||
}],
|
||
"edges":[]
|
||
}`)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, `"field":"incrementalUnit"`) || !strings.Contains(body, "INCREMENTAL_UNIT must be OBJECT or DIRECTORY") {
|
||
t.Fatalf("expected incrementalUnit validation issue, got %s", body)
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowRejectsInvalidScanSourceScanMode(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "非法扫描模式项目",
|
||
Description: "invalid scan mode",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := []byte(`{
|
||
"name":"非法扫描模式工作流",
|
||
"description":"invalid scan mode",
|
||
"type":"batch",
|
||
"nodes":[{
|
||
"id":"scan-source",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"BOS 增量扫描",
|
||
"hyperparams":[
|
||
{"key":"DATA_SOURCE","value":"BOS"},
|
||
{"key":"SOURCE_PATH","value":"/data/source"},
|
||
{"key":"SCAN_MODE","value":"ROW"}
|
||
]
|
||
}
|
||
}],
|
||
"edges":[]
|
||
}`)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, `"field":"scanMode"`) || !strings.Contains(body, "SCAN_MODE must be FULL or INCREMENTAL") {
|
||
t.Fatalf("expected scanMode validation issue, got %s", body)
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowRejectsInvalidScanSourceDataSource(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "非法扫描数据源项目",
|
||
Description: "invalid scan data source",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
payload := []byte(`{
|
||
"name":"非法扫描数据源工作流",
|
||
"description":"invalid scan data source",
|
||
"type":"batch",
|
||
"nodes":[{
|
||
"id":"scan-source",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"BOS 增量扫描",
|
||
"hyperparams":[
|
||
{"key":"DATA_SOURCE","value":"ROW"},
|
||
{"key":"SOURCE_PATH","value":"bos://datastorage/algo/X-platform-test"},
|
||
{"key":"SCAN_MODE","value":"INCREMENTAL"}
|
||
]
|
||
}
|
||
}],
|
||
"edges":[]
|
||
}`)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
body := recorder.Body.String()
|
||
if !strings.Contains(body, `"field":"dataSource"`) || !strings.Contains(body, "DATA_SOURCE must be BOS for scan workflows") {
|
||
t.Fatalf("expected dataSource validation issue, got %s", body)
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowAPISupportsNativeScanWorkflows(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
for _, name := range []string{"BOS 增量扫描", "X 平台数据集索引", "采集目录校验"} {
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: name,
|
||
Type: "builtin",
|
||
Category: "IO",
|
||
Description: name,
|
||
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "output", Type: domain.PortTypeJSON}},
|
||
}); err != nil {
|
||
t.Fatalf("create operator %s: %v", name, err)
|
||
}
|
||
}
|
||
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "扫描项目",
|
||
Description: "native workflow scan api",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
createPayload := []byte(`{
|
||
"name":"X-platform BOS 扫描",
|
||
"description":"扫描 workflow 原生创建",
|
||
"type":"batch",
|
||
"kind":"scan",
|
||
"schedule":{"interval":900},
|
||
"scanConfig":{
|
||
"dataSource":"BOS",
|
||
"bucket":"datastorage",
|
||
"prefix":"algo/X-platform-test/",
|
||
"endpoint":"https://bos.example.test",
|
||
"accessKeyId":"ak-native-1234",
|
||
"secretAccessKey":"sk-native-5678",
|
||
"scanMode":"INCREMENTAL",
|
||
"incrementalUnit":"DIRECTORY",
|
||
"directoryDepth":2,
|
||
"pattern":"*.mp4",
|
||
"enabled":true
|
||
},
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`)
|
||
invalidCreatePayload := []byte(strings.Replace(string(createPayload), `"endpoint":"https://bos.example.test"`, `"endpoint":"ftp://bos.example.test"`, 1))
|
||
invalidCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(invalidCreatePayload)),
|
||
)
|
||
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid scan endpoint create failure, got %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
||
}
|
||
invalidCreateBody := invalidCreateRecorder.Body.String()
|
||
if !strings.Contains(invalidCreateBody, `"field":"endpoint"`) || !strings.Contains(invalidCreateBody, "absolute http(s) URL") {
|
||
t.Fatalf("expected endpoint validation issue, got %s", invalidCreateBody)
|
||
}
|
||
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
||
)
|
||
if createRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create scan workflow 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.Kind != domain.WorkflowKindScan {
|
||
t.Fatalf("expected scan workflow kind, got %s", created.Kind)
|
||
}
|
||
if created.Type != domain.WorkflowTypeStreaming {
|
||
t.Fatalf("expected streaming scan workflow, got %s", created.Type)
|
||
}
|
||
if created.ScanConfig == nil {
|
||
t.Fatalf("expected scan config in response")
|
||
}
|
||
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
||
t.Fatalf("expected masked secrets in response: %+v", created.ScanConfig)
|
||
}
|
||
if !created.ScanConfig.HasAccessKeyID || !created.ScanConfig.HasSecretAccessKey {
|
||
t.Fatalf("expected secret presence flags in response: %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.AccessKeyPreview == "" || created.ScanConfig.SecretAccessKeyPreview == "" {
|
||
t.Fatalf("expected secret previews in response: %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.ScanMode != "INCREMENTAL" {
|
||
t.Fatalf("expected incremental scan mode, got %s", created.ScanConfig.ScanMode)
|
||
}
|
||
if created.ScanConfig.IncrementalUnit != "DIRECTORY" || created.ScanConfig.DirectoryDepth != 2 {
|
||
t.Fatalf("expected directory incremental config, got %+v", created.ScanConfig)
|
||
}
|
||
if len(created.Nodes) != 3 || len(created.Edges) != 2 {
|
||
t.Fatalf("expected default scan graph scaffold, got %d nodes and %d edges", len(created.Nodes), len(created.Edges))
|
||
}
|
||
|
||
stored, err := dataStore.GetWorkflow(ctx, created.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored workflow: %v", err)
|
||
}
|
||
if stored == nil || stored.ScanConfig == nil {
|
||
t.Fatalf("expected stored workflow scan config")
|
||
}
|
||
if stored.ScanConfig.AccessKeyID != "ak-native-1234" || stored.ScanConfig.SecretAccessKey != "sk-native-5678" {
|
||
t.Fatalf("expected secrets preserved in store: %+v", stored.ScanConfig)
|
||
}
|
||
if stored.ScanConfig.IncrementalUnit != "DIRECTORY" || stored.ScanConfig.DirectoryDepth != 2 {
|
||
t.Fatalf("expected stored directory incremental config, got %+v", stored.ScanConfig)
|
||
}
|
||
if stored.Schedule == nil || stored.Schedule.Interval != 900 {
|
||
t.Fatalf("expected schedule interval persisted, got %+v", stored.Schedule)
|
||
}
|
||
if stored.Schedule.SourcePath != "bos://datastorage/algo/X-platform-test" {
|
||
t.Fatalf("unexpected stored source path: %+v", stored.Schedule)
|
||
}
|
||
|
||
invalidUpdatePayload := []byte(`{
|
||
"scanConfig":{
|
||
"dataSource":"BOS",
|
||
"bucket":"datastorage",
|
||
"prefix":"algo/X-platform-test",
|
||
"endpoint":"https://bos.example.test/bad path",
|
||
"accessKeyId":"",
|
||
"secretAccessKey":"",
|
||
"scanMode":"INCREMENTAL",
|
||
"incrementalUnit":"DIRECTORY",
|
||
"directoryDepth":2,
|
||
"pattern":"*.mp4",
|
||
"enabled":true
|
||
}
|
||
}`)
|
||
invalidUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidUpdateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(invalidUpdatePayload)),
|
||
)
|
||
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid scan endpoint update failure, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
||
}
|
||
invalidUpdateBody := invalidUpdateRecorder.Body.String()
|
||
if !strings.Contains(invalidUpdateBody, `"field":"endpoint"`) || !strings.Contains(invalidUpdateBody, "without whitespace") {
|
||
t.Fatalf("expected endpoint whitespace validation issue, got %s", invalidUpdateBody)
|
||
}
|
||
afterInvalidUpdate, err := dataStore.GetWorkflow(ctx, created.ID)
|
||
if err != nil {
|
||
t.Fatalf("get workflow after invalid update: %v", err)
|
||
}
|
||
if afterInvalidUpdate == nil || afterInvalidUpdate.ScanConfig == nil || afterInvalidUpdate.ScanConfig.Endpoint != "https://bos.example.test" {
|
||
t.Fatalf("expected invalid update to leave scan endpoint unchanged, got %+v", afterInvalidUpdate)
|
||
}
|
||
|
||
updatePayload := []byte(`{
|
||
"schedule":{"interval":0},
|
||
"scanConfig":{
|
||
"dataSource":"BOS",
|
||
"bucket":"datastorage",
|
||
"prefix":"algo/X-platform-test",
|
||
"endpoint":"https://bos.example.test",
|
||
"accessKeyId":"",
|
||
"secretAccessKey":"",
|
||
"scanMode":"FULL",
|
||
"incrementalUnit":"OBJECT",
|
||
"directoryDepth":1,
|
||
"pattern":"*.json",
|
||
"enabled":false
|
||
}
|
||
}`)
|
||
updateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
updateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(updatePayload)),
|
||
)
|
||
if updateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("update scan workflow status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||
}
|
||
|
||
updated, err := dataStore.GetWorkflow(ctx, created.ID)
|
||
if err != nil {
|
||
t.Fatalf("get updated workflow: %v", err)
|
||
}
|
||
if updated == nil || updated.ScanConfig == nil {
|
||
t.Fatalf("expected updated scan config")
|
||
}
|
||
if updated.Type != domain.WorkflowTypeBatch {
|
||
t.Fatalf("expected batch after disabling scan schedule, got %s", updated.Type)
|
||
}
|
||
if updated.ScanConfig.AccessKeyID != "ak-native-1234" || updated.ScanConfig.SecretAccessKey != "sk-native-5678" {
|
||
t.Fatalf("expected secrets preserved on generic workflow update: %+v", updated.ScanConfig)
|
||
}
|
||
if updated.ScanConfig.ScanMode != "FULL" {
|
||
t.Fatalf("expected full scan mode after update, got %s", updated.ScanConfig.ScanMode)
|
||
}
|
||
if updated.ScanConfig.IncrementalUnit != "OBJECT" || updated.ScanConfig.DirectoryDepth != 1 {
|
||
t.Fatalf("expected updated incremental config, got %+v", updated.ScanConfig)
|
||
}
|
||
if updated.Schedule == nil || updated.Schedule.Interval != 0 {
|
||
t.Fatalf("expected schedule interval reset to 0, got %+v", updated.Schedule)
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowCreateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
||
Name: "增量项目",
|
||
Description: "generic incremental dag",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
createPayload := []byte(`{
|
||
"name":"增量全链路",
|
||
"description":"generic incremental dag",
|
||
"type":"streaming",
|
||
"schedule":{"interval":45},
|
||
"nodes":[
|
||
{
|
||
"id":"source",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"BOS 增量扫描",
|
||
"category":"IO",
|
||
"description":"source",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
||
"bucket":"wjzh-data-migration",
|
||
"prefix":"wjzh-0330-Data-migration",
|
||
"pattern":"*.mp4",
|
||
"hyperparams":[
|
||
{"key":"DATA_SOURCE","value":"BOS"},
|
||
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
||
{"key":"BOS_ENDPOINT","value":"https://bos.example.test"},
|
||
{"key":"BOS_ACCESS_KEY_ID","value":"ak-node-123"},
|
||
{"key":"BOS_SECRET_ACCESS_KEY","value":"sk-node-456"},
|
||
{"key":"SCAN_MODE","value":"INCREMENTAL"}
|
||
]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
},
|
||
{
|
||
"id":"quality",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"视频质检",
|
||
"category":"VIDEO",
|
||
"description":"quality",
|
||
"inputs":[{"id":"in-1","name":"input","type":"json"}],
|
||
"outputs":[{"id":"out-1","name":"quality","type":"json"}]
|
||
},
|
||
"position":{"x":200,"y":0}
|
||
}
|
||
],
|
||
"edges":[
|
||
{"id":"source-quality","source":"source","sourceHandle":"out-1","target":"quality","targetHandle":"in-1"}
|
||
]
|
||
}`)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
||
)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create incremental workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var created domain.WorkflowRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatalf("decode created workflow: %v", err)
|
||
}
|
||
if created.Kind != domain.WorkflowKindScan {
|
||
t.Fatalf("expected scan kind, got %s", created.Kind)
|
||
}
|
||
if created.Type != domain.WorkflowTypeStreaming {
|
||
t.Fatalf("expected streaming workflow, got %s", created.Type)
|
||
}
|
||
if created.ScanConfig == nil {
|
||
t.Fatalf("expected inferred scan config")
|
||
}
|
||
if created.ScanConfig.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
||
t.Fatalf("unexpected inferred source path: %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.Bucket != "wjzh-data-migration" || created.ScanConfig.Prefix != "wjzh-0330-Data-migration" {
|
||
t.Fatalf("unexpected inferred bucket/prefix: %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
||
t.Fatalf("expected inferred secrets to be masked, got %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.ScanMode != "INCREMENTAL" {
|
||
t.Fatalf("expected inferred incremental scan mode, got %+v", created.ScanConfig)
|
||
}
|
||
if created.MaxConcurrentRuns != 1 {
|
||
t.Fatalf("expected inferred scan workflow maxConcurrentRuns=1, got %d", created.MaxConcurrentRuns)
|
||
}
|
||
if created.Schedule == nil || created.Schedule.Interval != 45 || created.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
||
t.Fatalf("unexpected inferred schedule: %+v", created.Schedule)
|
||
}
|
||
if len(created.Nodes) != 2 || len(created.Edges) != 1 {
|
||
t.Fatalf("expected original graph to be preserved, got %d nodes %d edges", len(created.Nodes), len(created.Edges))
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowCreateRejectsInvalidIncrementalDirectoryDepthHyperparam(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
||
Name: "非法增量项目",
|
||
Description: "invalid incremental depth",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
createPayload := []byte(`{
|
||
"name":"非法目录深度工作流",
|
||
"description":"invalid incremental depth",
|
||
"type":"streaming",
|
||
"schedule":{"interval":45},
|
||
"nodes":[
|
||
{
|
||
"id":"source",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"BOS 增量扫描",
|
||
"category":"IO",
|
||
"description":"source",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
||
"bucket":"wjzh-data-migration",
|
||
"prefix":"wjzh-0330-Data-migration",
|
||
"hyperparams":[
|
||
{"key":"DATA_SOURCE","value":"BOS"},
|
||
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
||
{"key":"BOS_ENDPOINT","value":"https://bos.example.test"},
|
||
{"key":"BOS_ACCESS_KEY_ID","value":"ak-node-123"},
|
||
{"key":"BOS_SECRET_ACCESS_KEY","value":"sk-node-456"},
|
||
{"key":"SCAN_MODE","value":"INCREMENTAL"},
|
||
{"key":"INCREMENTAL_DIRECTORY_DEPTH","value":"abc"}
|
||
]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
}
|
||
],
|
||
"edges":[]
|
||
}`)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid depth to be rejected, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "INCREMENTAL_DIRECTORY_DEPTH must be a positive integer") {
|
||
t.Fatalf("expected directory depth validation error, got %s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowCreatePreservesBOSSourcePathWhenUsingConnectionReference(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "连接引用扫描项目",
|
||
Description: "connection + sourcePath",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "WJZH Connection",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "wjzh-data-migration",
|
||
AccessKeyID: "ak-conn",
|
||
SecretAccessKey: "sk-conn",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
createPayload := []byte(fmt.Sprintf(`{
|
||
"name":"连接引用增量烟测",
|
||
"description":"scan config should keep explicit sourcePath",
|
||
"type":"batch",
|
||
"nodes":[
|
||
{
|
||
"id":"source",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"BOS 增量扫描",
|
||
"category":"IO",
|
||
"description":"source",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
||
"connectionId":"%s",
|
||
"hyperparams":[
|
||
{"key":"DATA_SOURCE","value":"BOS"},
|
||
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
||
{"key":"SCAN_MODE","value":"FULL"}
|
||
]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
}
|
||
],
|
||
"edges":[]
|
||
}`, connection.ID))
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
||
)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var created domain.WorkflowRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatalf("decode created workflow: %v", err)
|
||
}
|
||
if created.ScanConfig == nil {
|
||
t.Fatalf("expected scan config")
|
||
}
|
||
if created.ScanConfig.ConnectionID != connection.ID {
|
||
t.Fatalf("expected connection reference to be preserved, got %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
||
t.Fatalf("expected explicit bos sourcePath to be preserved, got %+v", created.ScanConfig)
|
||
}
|
||
|
||
runRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
runRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/workflows/"+created.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
||
)
|
||
if runRecorder.Code != http.StatusCreated && runRecorder.Code != http.StatusAccepted {
|
||
t.Fatalf("expected connectionId + sourcePath workflow to run, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowUpdateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "增量更新项目",
|
||
Description: "generic incremental dag",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "待升级增量工作流",
|
||
Description: "incremental update",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Schedule: &domain.WorkflowSchedule{Interval: 0},
|
||
Nodes: []domain.WorkflowNode{
|
||
{
|
||
ID: "source",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "BOS 增量扫描",
|
||
Category: "IO",
|
||
Description: "source",
|
||
Bucket: "wjzh-data-migration",
|
||
Prefix: "wjzh-0330-Data-migration",
|
||
Pattern: "*.json",
|
||
Hyperparams: []domain.HyperParam{
|
||
{Key: "DATA_SOURCE", Value: "BOS"},
|
||
{Key: "SOURCE_PATH", Value: "bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
||
{Key: "BOS_ENDPOINT", Value: "https://bos.example.test"},
|
||
{Key: "BOS_ACCESS_KEY_ID", Value: "ak-update-123"},
|
||
{Key: "BOS_SECRET_ACCESS_KEY", Value: "sk-update-456"},
|
||
{Key: "SCAN_MODE", Value: "FULL"},
|
||
},
|
||
},
|
||
Position: domain.Position{X: 0, Y: 0},
|
||
},
|
||
},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
updatePayload := []byte(`{
|
||
"type":"streaming",
|
||
"schedule":{"interval":75}
|
||
}`)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID, bytes.NewReader(updatePayload)),
|
||
)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("update incremental workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
updated, err := dataStore.GetWorkflow(ctx, workflow.ID)
|
||
if err != nil {
|
||
t.Fatalf("get updated workflow: %v", err)
|
||
}
|
||
if updated == nil || updated.ScanConfig == nil {
|
||
t.Fatalf("expected inferred scan config after update")
|
||
}
|
||
if updated.Kind != domain.WorkflowKindScan {
|
||
t.Fatalf("expected scan kind after update, got %s", updated.Kind)
|
||
}
|
||
if updated.Type != domain.WorkflowTypeStreaming {
|
||
t.Fatalf("expected streaming type after update, got %s", updated.Type)
|
||
}
|
||
if updated.ScanConfig.AccessKeyID != "ak-update-123" || updated.ScanConfig.SecretAccessKey != "sk-update-456" {
|
||
t.Fatalf("expected secrets derived from node hyperparams, got %+v", updated.ScanConfig)
|
||
}
|
||
if updated.ScanConfig.ScanMode != "FULL" {
|
||
t.Fatalf("expected scan mode derived from node hyperparams, got %+v", updated.ScanConfig)
|
||
}
|
||
if updated.MaxConcurrentRuns != 1 {
|
||
t.Fatalf("expected inferred scan workflow maxConcurrentRuns=1 after update, got %d", updated.MaxConcurrentRuns)
|
||
}
|
||
if updated.Schedule == nil || updated.Schedule.Interval != 75 || updated.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
||
t.Fatalf("unexpected updated schedule: %+v", updated.Schedule)
|
||
}
|
||
}
|
||
|
||
func TestTemplateCreateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
createPayload := []byte(`{
|
||
"name":"增量模板",
|
||
"description":"template incremental dag",
|
||
"type":"streaming",
|
||
"schedule":{"interval":30},
|
||
"nodes":[
|
||
{
|
||
"id":"source",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"BOS 增量扫描",
|
||
"category":"IO",
|
||
"description":"source",
|
||
"inputs":[],
|
||
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
||
"bucket":"wjzh-data-migration",
|
||
"prefix":"wjzh-0330-Data-migration",
|
||
"pattern":"*.mp4",
|
||
"hyperparams":[
|
||
{"key":"DATA_SOURCE","value":"BOS"},
|
||
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
||
{"key":"BOS_ENDPOINT","value":"https://bos.example.test"},
|
||
{"key":"BOS_ACCESS_KEY_ID","value":"ak-template-123"},
|
||
{"key":"BOS_SECRET_ACCESS_KEY","value":"sk-template-456"},
|
||
{"key":"SCAN_MODE","value":"INCREMENTAL"}
|
||
]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
}
|
||
],
|
||
"edges":[]
|
||
}`)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(createPayload)),
|
||
)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create incremental template status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var created domain.TemplateRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatalf("decode created template: %v", err)
|
||
}
|
||
if created.Kind != domain.WorkflowKindScan {
|
||
t.Fatalf("expected scan template kind, got %s", created.Kind)
|
||
}
|
||
if created.Type != domain.WorkflowTypeStreaming {
|
||
t.Fatalf("expected streaming template type, got %s", created.Type)
|
||
}
|
||
if created.ScanConfig == nil {
|
||
t.Fatalf("expected inferred template scan config")
|
||
}
|
||
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
||
t.Fatalf("expected inferred template secrets to be masked, got %+v", created.ScanConfig)
|
||
}
|
||
if created.MaxConcurrentRuns != 1 {
|
||
t.Fatalf("expected inferred scan template maxConcurrentRuns=1, got %d", created.MaxConcurrentRuns)
|
||
}
|
||
if created.Schedule == nil || created.Schedule.Interval != 30 || created.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
||
t.Fatalf("unexpected template schedule: %+v", created.Schedule)
|
||
}
|
||
}
|
||
|
||
func TestTemplateUpdateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
template, err := dataStore.CreateTemplate(ctx, domain.CreateTemplateInput{
|
||
Name: "待升级增量模板",
|
||
Description: "incremental template update",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Schedule: &domain.WorkflowSchedule{Interval: 0},
|
||
Nodes: []domain.WorkflowNode{
|
||
{
|
||
ID: "source",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "BOS 增量扫描",
|
||
Category: "IO",
|
||
Description: "source",
|
||
Bucket: "wjzh-data-migration",
|
||
Prefix: "wjzh-0330-Data-migration",
|
||
Pattern: "*.json",
|
||
Hyperparams: []domain.HyperParam{
|
||
{Key: "DATA_SOURCE", Value: "BOS"},
|
||
{Key: "SOURCE_PATH", Value: "bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
||
{Key: "BOS_ENDPOINT", Value: "https://bos.example.test"},
|
||
{Key: "BOS_ACCESS_KEY_ID", Value: "ak-template-update-123"},
|
||
{Key: "BOS_SECRET_ACCESS_KEY", Value: "sk-template-update-456"},
|
||
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
||
},
|
||
},
|
||
Position: domain.Position{X: 0, Y: 0},
|
||
},
|
||
},
|
||
Edges: []domain.WorkflowEdge{},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create template: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
updatePayload := []byte(`{
|
||
"type":"streaming",
|
||
"schedule":{"interval":90}
|
||
}`)
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/templates/"+template.ID, bytes.NewReader(updatePayload)),
|
||
)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("update incremental template status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
updated, err := dataStore.GetTemplate(ctx, template.ID)
|
||
if err != nil {
|
||
t.Fatalf("get updated template: %v", err)
|
||
}
|
||
if updated == nil || updated.ScanConfig == nil {
|
||
t.Fatalf("expected inferred template scan config after update")
|
||
}
|
||
if updated.Kind != domain.WorkflowKindScan {
|
||
t.Fatalf("expected scan template kind after update, got %s", updated.Kind)
|
||
}
|
||
if updated.Type != domain.WorkflowTypeStreaming {
|
||
t.Fatalf("expected streaming template type after update, got %s", updated.Type)
|
||
}
|
||
if updated.ScanConfig.AccessKeyID != "ak-template-update-123" || updated.ScanConfig.SecretAccessKey != "sk-template-update-456" {
|
||
t.Fatalf("expected template secrets derived from node hyperparams, got %+v", updated.ScanConfig)
|
||
}
|
||
if updated.ScanConfig.ScanMode != "INCREMENTAL" {
|
||
t.Fatalf("expected template scan mode derived from node hyperparams, got %+v", updated.ScanConfig)
|
||
}
|
||
if updated.MaxConcurrentRuns != 1 {
|
||
t.Fatalf("expected inferred scan template maxConcurrentRuns=1 after update, got %d", updated.MaxConcurrentRuns)
|
||
}
|
||
if updated.Schedule == nil || updated.Schedule.Interval != 90 || updated.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
||
t.Fatalf("unexpected updated template schedule: %+v", updated.Schedule)
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowCreatePersistsEveryHoursSchedule(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
||
Name: "小时调度项目",
|
||
Description: "hourly schedule",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
payload := []byte(`{
|
||
"name":"小时调度工作流",
|
||
"description":"hourly schedule",
|
||
"type":"streaming",
|
||
"schedule":{
|
||
"mode":"every_hours",
|
||
"intervalHours":4
|
||
},
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create hourly workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var created domain.WorkflowRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatalf("decode created workflow: %v", err)
|
||
}
|
||
if created.Schedule == nil {
|
||
t.Fatalf("expected schedule persisted")
|
||
}
|
||
if created.Schedule.Mode != domain.WorkflowScheduleModeEveryHours {
|
||
t.Fatalf("expected every_hours mode, got %+v", created.Schedule)
|
||
}
|
||
if created.Schedule.IntervalHours != 4 {
|
||
t.Fatalf("expected intervalHours=4, got %+v", created.Schedule)
|
||
}
|
||
if strings.TrimSpace(created.Schedule.AnchorAt) == "" {
|
||
t.Fatalf("expected anchorAt populated, got %+v", created.Schedule)
|
||
}
|
||
}
|
||
|
||
func TestTemplateCreatePersistsEveryDaysScheduleDefaultsTimezone(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
payload := []byte(`{
|
||
"name":"按天模板",
|
||
"description":"daily schedule",
|
||
"type":"streaming",
|
||
"schedule":{
|
||
"mode":"every_days_at_time",
|
||
"dayInterval":2,
|
||
"timeOfDay":"18:30"
|
||
},
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("create daily template status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
|
||
var created domain.TemplateRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
||
t.Fatalf("decode created template: %v", err)
|
||
}
|
||
if created.Schedule == nil {
|
||
t.Fatalf("expected schedule persisted")
|
||
}
|
||
if created.Schedule.Mode != domain.WorkflowScheduleModeEveryDaysAtTime {
|
||
t.Fatalf("expected every_days_at_time mode, got %+v", created.Schedule)
|
||
}
|
||
if created.Schedule.DayInterval != 2 || created.Schedule.TimeOfDay != "18:30" {
|
||
t.Fatalf("unexpected daily schedule payload: %+v", created.Schedule)
|
||
}
|
||
if created.Schedule.Timezone != domain.DefaultWorkflowScheduleTimezone {
|
||
t.Fatalf("expected default timezone %q, got %+v", domain.DefaultWorkflowScheduleTimezone, created.Schedule)
|
||
}
|
||
if strings.TrimSpace(created.Schedule.AnchorAt) == "" {
|
||
t.Fatalf("expected anchorAt populated, got %+v", created.Schedule)
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowCreateRejectsInvalidEveryDaysSchedule(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
||
Name: "非法调度项目",
|
||
Description: "invalid schedule",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
payload := []byte(`{
|
||
"name":"非法按天工作流",
|
||
"description":"invalid daily schedule",
|
||
"type":"streaming",
|
||
"schedule":{
|
||
"mode":"every_days_at_time",
|
||
"dayInterval":1,
|
||
"timeOfDay":"25:61"
|
||
},
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`)
|
||
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid daily schedule rejected, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "schedule.timeOfDay") {
|
||
t.Fatalf("expected timeOfDay validation issue, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestStorageConnectionsAPIStoresSecretsMasked(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
appConfig, err := config.LoadValidated()
|
||
if err != nil {
|
||
t.Fatalf("load test config: %v", err)
|
||
}
|
||
appConfig.GlobalBOSEndpoint = ""
|
||
handler := NewHandlerWithConfig(dataStore, nil, appConfig)
|
||
|
||
invalidCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/storage/connections", bytes.NewReader([]byte(`{
|
||
"name":"无效 BOS",
|
||
"provider":"BOS",
|
||
"endpoint":"bos.example.test",
|
||
"accessKeyId":"ak-invalid",
|
||
"secretAccessKey":"sk-invalid"
|
||
}`))),
|
||
)
|
||
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid storage endpoint rejected, got %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidCreateRecorder.Body.String(), "endpoint") {
|
||
t.Fatalf("expected endpoint validation issue, body=%s", invalidCreateRecorder.Body.String())
|
||
}
|
||
|
||
createPayload := []byte(`{
|
||
"name":"BOS 主连接",
|
||
"description":"扫描用 BOS 连接",
|
||
"provider":"BOS",
|
||
"endpoint":"https://bos.example.test",
|
||
"bucket":"datastorage",
|
||
"accessKeyId":"ak-connection-1234",
|
||
"secretAccessKey":"sk-connection-5678"
|
||
}`)
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/storage/connections", bytes.NewReader(createPayload)),
|
||
)
|
||
if createRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create storage connection 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 connection: %v", err)
|
||
}
|
||
if _, ok := created["accessKeyId"]; ok {
|
||
t.Fatalf("expected masked accessKeyId to be omitted: %#v", created)
|
||
}
|
||
if _, ok := created["secretAccessKey"]; ok {
|
||
t.Fatalf("expected masked secretAccessKey to be omitted: %#v", created)
|
||
}
|
||
if created["hasAccessKeyId"] != true || created["hasSecretAccessKey"] != true {
|
||
t.Fatalf("expected secret presence flags in response: %#v", created)
|
||
}
|
||
if created["accessKeyPreview"] == "" || created["secretAccessKeyPreview"] == "" {
|
||
t.Fatalf("expected secret previews in response: %#v", created)
|
||
}
|
||
|
||
stored, err := dataStore.GetStorageConnection(ctx, created["id"].(string))
|
||
if err != nil {
|
||
t.Fatalf("get stored storage connection: %v", err)
|
||
}
|
||
if stored == nil {
|
||
t.Fatalf("expected stored connection")
|
||
}
|
||
if stored.Bucket != "datastorage" {
|
||
t.Fatalf("expected bucket preserved in store: %#v", stored)
|
||
}
|
||
if stored.AccessKeyID != "ak-connection-1234" || stored.SecretAccessKey != "sk-connection-5678" {
|
||
t.Fatalf("expected real secrets preserved in store: %#v", stored)
|
||
}
|
||
|
||
updatePayload := []byte(`{
|
||
"description":"更新后的连接描述",
|
||
"endpoint":"https://gz.bcebos.com",
|
||
"bucket":"archive",
|
||
"accessKeyId":"",
|
||
"secretAccessKey":""
|
||
}`)
|
||
updateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
updateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/storage/connections/"+created["id"].(string), bytes.NewReader(updatePayload)),
|
||
)
|
||
if updateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("update storage connection status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||
}
|
||
|
||
updated, err := dataStore.GetStorageConnection(ctx, created["id"].(string))
|
||
if err != nil {
|
||
t.Fatalf("get updated storage connection: %v", err)
|
||
}
|
||
if updated == nil {
|
||
t.Fatalf("expected updated connection")
|
||
}
|
||
if updated.Description != "更新后的连接描述" || updated.Endpoint != "https://gz.bcebos.com" || updated.Bucket != "archive" {
|
||
t.Fatalf("unexpected updated connection: %#v", updated)
|
||
}
|
||
if updated.AccessKeyID != "ak-connection-1234" || updated.SecretAccessKey != "sk-connection-5678" {
|
||
t.Fatalf("expected blank update payload to preserve secrets: %#v", updated)
|
||
}
|
||
|
||
invalidUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidUpdateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/storage/connections/"+created["id"].(string), bytes.NewReader([]byte(`{"endpoint":"ftp://gz.bcebos.com"}`))),
|
||
)
|
||
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid storage endpoint update rejected, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidUpdateRecorder.Body.String(), "endpoint") {
|
||
t.Fatalf("expected endpoint validation issue, body=%s", invalidUpdateRecorder.Body.String())
|
||
}
|
||
|
||
clearEndpointRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
clearEndpointRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/storage/connections/"+created["id"].(string), bytes.NewReader([]byte(`{"endpoint":""}`))),
|
||
)
|
||
if clearEndpointRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected clearing endpoint without global fallback rejected, got %d body=%s", clearEndpointRecorder.Code, clearEndpointRecorder.Body.String())
|
||
}
|
||
|
||
persisted, err := dataStore.GetStorageConnection(ctx, created["id"].(string))
|
||
if err != nil {
|
||
t.Fatalf("get storage connection after invalid updates: %v", err)
|
||
}
|
||
if persisted == nil || persisted.Endpoint != "https://gz.bcebos.com" {
|
||
t.Fatalf("expected invalid updates not to persist endpoint changes: %#v", persisted)
|
||
}
|
||
|
||
listRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/connections", nil))
|
||
if listRecorder.Code != http.StatusOK {
|
||
t.Fatalf("list storage connections status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
||
}
|
||
var listBody struct {
|
||
Items []map[string]any `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listBody); err != nil {
|
||
t.Fatalf("decode storage connection list: %v", err)
|
||
}
|
||
if len(listBody.Items) != 1 {
|
||
t.Fatalf("expected 1 storage connection, got %d", len(listBody.Items))
|
||
}
|
||
}
|
||
|
||
func TestMongoConnectionsAPIStoresSecretsMasked(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
invalidCreateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidCreateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/mongo/connections", bytes.NewReader([]byte(`{
|
||
"name":"占位符 MongoDB",
|
||
"uri":"mongodb://<user>:<password>@business.example:27017",
|
||
"database":"business_data"
|
||
}`))),
|
||
)
|
||
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected placeholder mongo URI rejected, got %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidCreateRecorder.Body.String(), "uri") {
|
||
t.Fatalf("expected uri validation issue, body=%s", invalidCreateRecorder.Body.String())
|
||
}
|
||
|
||
createPayload := []byte(`{
|
||
"name":"业务 MongoDB",
|
||
"description":"测试业务库",
|
||
"uri":"mongodb://business-user:business-pass@business.example:27017",
|
||
"database":"business_data"
|
||
}`)
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/mongo/connections", bytes.NewReader(createPayload)),
|
||
)
|
||
if createRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create mongo connection 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 mongo connection: %v", err)
|
||
}
|
||
if _, ok := created["uri"]; ok {
|
||
t.Fatalf("expected masked uri to be omitted: %#v", created)
|
||
}
|
||
if created["hasUri"] != true || created["uriPreview"] == "" {
|
||
t.Fatalf("expected uri presence metadata: %#v", created)
|
||
}
|
||
|
||
stored, err := dataStore.GetMongoConnection(ctx, created["id"].(string))
|
||
if err != nil {
|
||
t.Fatalf("get stored mongo connection: %v", err)
|
||
}
|
||
if stored == nil {
|
||
t.Fatalf("expected stored mongo connection")
|
||
}
|
||
if stored.URI != "mongodb://business-user:business-pass@business.example:27017" {
|
||
t.Fatalf("expected real uri preserved in store: %#v", stored)
|
||
}
|
||
|
||
updatePayload := []byte(`{
|
||
"description":"更新后的测试业务库",
|
||
"uri":"",
|
||
"database":"business_data_v2"
|
||
}`)
|
||
updateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
updateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/mongo/connections/"+created["id"].(string), bytes.NewReader(updatePayload)),
|
||
)
|
||
if updateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("update mongo connection status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||
}
|
||
|
||
updated, err := dataStore.GetMongoConnection(ctx, created["id"].(string))
|
||
if err != nil {
|
||
t.Fatalf("get updated mongo connection: %v", err)
|
||
}
|
||
if updated == nil {
|
||
t.Fatalf("expected updated mongo connection")
|
||
}
|
||
if updated.Description != "更新后的测试业务库" || updated.Database != "business_data_v2" {
|
||
t.Fatalf("unexpected updated mongo connection: %#v", updated)
|
||
}
|
||
if updated.URI != "mongodb://business-user:business-pass@business.example:27017" {
|
||
t.Fatalf("expected blank update payload to preserve uri: %#v", updated)
|
||
}
|
||
|
||
invalidUpdateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
invalidUpdateRecorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/mongo/connections/"+created["id"].(string), bytes.NewReader([]byte(`{"uri":"http://business.example:27017"}`))),
|
||
)
|
||
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected invalid mongo URI update rejected, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(invalidUpdateRecorder.Body.String(), "uri") {
|
||
t.Fatalf("expected uri validation issue, body=%s", invalidUpdateRecorder.Body.String())
|
||
}
|
||
|
||
persisted, err := dataStore.GetMongoConnection(ctx, created["id"].(string))
|
||
if err != nil {
|
||
t.Fatalf("get mongo connection after invalid update: %v", err)
|
||
}
|
||
if persisted == nil || persisted.URI != "mongodb://business-user:business-pass@business.example:27017" || persisted.Database != "business_data_v2" {
|
||
t.Fatalf("expected invalid update not to persist mongo changes: %#v", persisted)
|
||
}
|
||
|
||
listRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/mongo/connections", nil))
|
||
if listRecorder.Code != http.StatusOK {
|
||
t.Fatalf("list mongo connections status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
||
}
|
||
var listBody struct {
|
||
Items []map[string]any `json:"items"`
|
||
}
|
||
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listBody); err != nil {
|
||
t.Fatalf("decode mongo connection list: %v", err)
|
||
}
|
||
if len(listBody.Items) != 1 {
|
||
t.Fatalf("expected 1 mongo connection, got %d", len(listBody.Items))
|
||
}
|
||
}
|
||
|
||
func TestStorageBrowserSupportsConnectionID(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 主连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
AccessKeyID: "ak-browser-1234",
|
||
SecretAccessKey: "sk-browser-5678",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
originalBucketsResolver := storageConnectionBucketsResolver
|
||
originalObjectsResolver := storageConnectionObjectsResolver
|
||
storageConnectionBucketsResolver = func(ctx context.Context, item *domain.StorageConnectionRecord) ([]string, error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection for buckets: %#v", item)
|
||
}
|
||
return []string{"datastorage", "incoming"}, nil
|
||
}
|
||
storageConnectionObjectsResolver = func(ctx context.Context, item *domain.StorageConnectionRecord, bucket string, prefix string) ([]string, error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection for objects: %#v", item)
|
||
}
|
||
if bucket != "datastorage" || prefix != "algo/X-platform-test" {
|
||
t.Fatalf("unexpected bucket/prefix: %s %s", bucket, prefix)
|
||
}
|
||
return []string{"algo/X-platform-test/BJ_001/", "algo/X-platform-test/meta.json"}, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
storageConnectionBucketsResolver = originalBucketsResolver
|
||
storageConnectionObjectsResolver = originalObjectsResolver
|
||
})
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
bucketsRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
bucketsRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/storage/buckets?connectionId="+connection.ID, nil),
|
||
)
|
||
if bucketsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("connection buckets status: %d body=%s", bucketsRecorder.Code, bucketsRecorder.Body.String())
|
||
}
|
||
|
||
objectsRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
objectsRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/storage/objects?connectionId="+connection.ID+"&bucket=datastorage&prefix=algo/X-platform-test", nil),
|
||
)
|
||
if objectsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("connection objects status: %d body=%s", objectsRecorder.Code, objectsRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
type fakeObjectStorageBrowser struct {
|
||
buckets map[string]struct{}
|
||
objects map[string]domain.ObjectStorageObjectSummary
|
||
content map[string][]byte
|
||
uploads map[string][]domain.ObjectStorageMultipartUploadPart
|
||
abortedUploads map[string]bool
|
||
readErr map[string]error
|
||
readCalls map[string]int
|
||
readRangeCalls map[string]int
|
||
lastReadRange map[string][2]int64
|
||
readPreviewCalls map[string]int
|
||
lastPreviewMaxLen map[string]int64
|
||
}
|
||
|
||
func newFakeObjectStorageBrowser() *fakeObjectStorageBrowser {
|
||
return &fakeObjectStorageBrowser{
|
||
buckets: map[string]struct{}{
|
||
"datastorage": {},
|
||
},
|
||
objects: map[string]domain.ObjectStorageObjectSummary{
|
||
"datastorage|algo/demo.json": {
|
||
Bucket: "datastorage",
|
||
Key: "algo/demo.json",
|
||
Name: "demo.json",
|
||
Kind: "json",
|
||
SizeBytes: 13,
|
||
ContentType: "application/json",
|
||
},
|
||
},
|
||
content: map[string][]byte{
|
||
"datastorage|algo/demo.json": []byte(`{"demo":true}`),
|
||
},
|
||
uploads: map[string][]domain.ObjectStorageMultipartUploadPart{},
|
||
abortedUploads: map[string]bool{},
|
||
readErr: map[string]error{},
|
||
readCalls: map[string]int{},
|
||
readRangeCalls: map[string]int{},
|
||
lastReadRange: map[string][2]int64{},
|
||
readPreviewCalls: map[string]int{},
|
||
lastPreviewMaxLen: map[string]int64{},
|
||
}
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) ListBuckets(context.Context) ([]domain.ObjectStorageBucketSummary, error) {
|
||
items := make([]domain.ObjectStorageBucketSummary, 0, len(browser.buckets))
|
||
for bucket := range browser.buckets {
|
||
items = append(items, domain.ObjectStorageBucketSummary{Name: bucket})
|
||
}
|
||
sort.Slice(items, func(left int, right int) bool { return items[left].Name < items[right].Name })
|
||
return items, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) ListObjects(_ context.Context, bucket string, prefix string) ([]domain.ObjectStorageObjectSummary, error) {
|
||
items := make([]domain.ObjectStorageObjectSummary, 0)
|
||
normalizedPrefix := strings.Trim(strings.TrimSpace(prefix), "/")
|
||
for _, item := range browser.objects {
|
||
if item.Bucket != bucket {
|
||
continue
|
||
}
|
||
if normalizedPrefix != "" && !strings.HasPrefix(item.Key, normalizedPrefix) {
|
||
continue
|
||
}
|
||
items = append(items, item)
|
||
}
|
||
sort.Slice(items, func(left int, right int) bool { return items[left].Key < items[right].Key })
|
||
return items, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) CreateBucket(_ context.Context, bucket string) (domain.ObjectStorageBucketSummary, error) {
|
||
browser.buckets[bucket] = struct{}{}
|
||
return domain.ObjectStorageBucketSummary{Name: bucket}, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) DeleteBucket(_ context.Context, bucket string) error {
|
||
delete(browser.buckets, bucket)
|
||
return nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) PutObject(_ context.Context, bucket string, key string, payload []byte, contentType string) (domain.ObjectStorageObjectSummary, error) {
|
||
browser.buckets[bucket] = struct{}{}
|
||
item := domain.ObjectStorageObjectSummary{
|
||
Bucket: bucket,
|
||
Key: key,
|
||
Name: filepath.Base(key),
|
||
Kind: "file",
|
||
SizeBytes: int64(len(payload)),
|
||
ContentType: contentType,
|
||
}
|
||
if strings.HasSuffix(strings.ToLower(key), ".json") {
|
||
item.Kind = "json"
|
||
}
|
||
browser.objects[bucket+"|"+key] = item
|
||
browser.content[bucket+"|"+key] = append([]byte(nil), payload...)
|
||
return item, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) PutObjectStream(ctx context.Context, bucket string, key string, reader io.Reader, contentType string) (domain.ObjectStorageObjectSummary, error) {
|
||
payload, err := io.ReadAll(reader)
|
||
if err != nil {
|
||
return domain.ObjectStorageObjectSummary{}, err
|
||
}
|
||
return browser.PutObject(ctx, bucket, key, payload, contentType)
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) PresignPutObject(_ context.Context, bucket string, key string, contentType string, _ int) (domain.ObjectStoragePresignedUploadResult, error) {
|
||
headers := map[string]string{}
|
||
if strings.TrimSpace(contentType) != "" {
|
||
headers["Content-Type"] = strings.TrimSpace(contentType)
|
||
}
|
||
return domain.ObjectStoragePresignedUploadResult{
|
||
Bucket: bucket,
|
||
Key: key,
|
||
Method: http.MethodPut,
|
||
URL: "https://example.com/upload/" + url.PathEscape(bucket) + "/" + url.PathEscape(key),
|
||
Headers: headers,
|
||
ExpiresAt: time.Now().UTC().Add(time.Hour).Format(time.RFC3339),
|
||
}, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) InitiateMultipartUpload(_ context.Context, bucket string, key string, _ string, _ int) (domain.ObjectStorageMultipartUploadCreateResult, error) {
|
||
uploadID := "upload-" + strings.ReplaceAll(key, "/", "-")
|
||
browser.uploads[bucket+"|"+key+"|"+uploadID] = nil
|
||
return domain.ObjectStorageMultipartUploadCreateResult{
|
||
Bucket: bucket,
|
||
Key: key,
|
||
UploadID: uploadID,
|
||
ExpiresAt: time.Now().UTC().Add(time.Hour).Format(time.RFC3339),
|
||
}, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) PresignMultipartUploadPart(_ context.Context, bucket string, key string, uploadID string, partNumber int, _ int) (domain.ObjectStorageMultipartUploadPartURLResult, error) {
|
||
return domain.ObjectStorageMultipartUploadPartURLResult{
|
||
Bucket: bucket,
|
||
Key: key,
|
||
UploadID: uploadID,
|
||
PartNumber: partNumber,
|
||
Method: http.MethodPut,
|
||
URL: fmt.Sprintf("https://example.com/upload/%s/%s?uploadId=%s&partNumber=%d", url.PathEscape(bucket), url.PathEscape(key), url.QueryEscape(uploadID), partNumber),
|
||
Headers: map[string]string{},
|
||
ExpiresAt: time.Now().UTC().Add(time.Hour).Format(time.RFC3339),
|
||
}, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) CompleteMultipartUpload(_ context.Context, bucket string, key string, uploadID string, parts []domain.ObjectStorageMultipartUploadPart) (domain.ObjectStorageObjectSummary, error) {
|
||
browser.uploads[bucket+"|"+key+"|"+uploadID] = append([]domain.ObjectStorageMultipartUploadPart(nil), parts...)
|
||
payload := []byte(fmt.Sprintf("completed:%s:%d", uploadID, len(parts)))
|
||
return browser.PutObject(context.Background(), bucket, key, payload, "application/octet-stream")
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) AbortMultipartUpload(_ context.Context, bucket string, key string, uploadID string) error {
|
||
browser.abortedUploads[bucket+"|"+key+"|"+uploadID] = true
|
||
return nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) RenameObject(_ context.Context, bucket string, key string, newKey string) (domain.ObjectStorageObjectSummary, error) {
|
||
item, ok := browser.objects[bucket+"|"+key]
|
||
if !ok {
|
||
return domain.ObjectStorageObjectSummary{}, os.ErrNotExist
|
||
}
|
||
delete(browser.objects, bucket+"|"+key)
|
||
item.Key = newKey
|
||
item.Name = filepath.Base(newKey)
|
||
browser.objects[bucket+"|"+newKey] = item
|
||
browser.content[bucket+"|"+newKey] = browser.content[bucket+"|"+key]
|
||
delete(browser.content, bucket+"|"+key)
|
||
return item, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) MovePrefix(_ context.Context, bucket string, prefix string, newPrefix string) (domain.ObjectStorageObjectSummary, error) {
|
||
normalized := strings.Trim(strings.TrimSpace(prefix), "/")
|
||
destination := strings.Trim(strings.TrimSpace(newPrefix), "/")
|
||
if normalized == "" || destination == "" {
|
||
return domain.ObjectStorageObjectSummary{}, os.ErrInvalid
|
||
}
|
||
keysToMove := []string{}
|
||
for objectKey, item := range browser.objects {
|
||
if item.Bucket != bucket {
|
||
continue
|
||
}
|
||
key := strings.TrimPrefix(objectKey, bucket+"|")
|
||
if key == normalized || strings.HasPrefix(key, normalized+"/") {
|
||
keysToMove = append(keysToMove, objectKey)
|
||
}
|
||
}
|
||
moved := false
|
||
for _, objectKey := range keysToMove {
|
||
item := browser.objects[objectKey]
|
||
key := strings.TrimPrefix(objectKey, bucket+"|")
|
||
newKey := destination
|
||
if strings.HasPrefix(key, normalized+"/") {
|
||
newKey = destination + "/" + strings.TrimPrefix(key, normalized+"/")
|
||
}
|
||
payload := browser.content[objectKey]
|
||
delete(browser.objects, objectKey)
|
||
delete(browser.content, objectKey)
|
||
item.Key = newKey
|
||
item.Name = filepath.Base(newKey)
|
||
browser.objects[bucket+"|"+newKey] = item
|
||
browser.content[bucket+"|"+newKey] = payload
|
||
moved = true
|
||
}
|
||
if !moved {
|
||
return domain.ObjectStorageObjectSummary{}, os.ErrNotExist
|
||
}
|
||
return domain.ObjectStorageObjectSummary{
|
||
Bucket: bucket,
|
||
Key: destination,
|
||
Name: filepath.Base(destination),
|
||
Kind: "directory",
|
||
}, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) DeleteObject(_ context.Context, bucket string, key string) error {
|
||
delete(browser.objects, bucket+"|"+key)
|
||
delete(browser.content, bucket+"|"+key)
|
||
return nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) DeletePrefix(_ context.Context, bucket string, prefix string) error {
|
||
normalized := strings.Trim(strings.TrimSpace(prefix), "/")
|
||
for objectKey := range browser.objects {
|
||
if !strings.HasPrefix(objectKey, bucket+"|") {
|
||
continue
|
||
}
|
||
key := strings.TrimPrefix(objectKey, bucket+"|")
|
||
if key == normalized || strings.HasPrefix(key, normalized+"/") {
|
||
delete(browser.objects, objectKey)
|
||
delete(browser.content, objectKey)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) GetObjectSummary(_ context.Context, bucket string, key string) (domain.ObjectStorageObjectSummary, error) {
|
||
item, ok := browser.objects[bucket+"|"+key]
|
||
if !ok {
|
||
return domain.ObjectStorageObjectSummary{}, os.ErrNotExist
|
||
}
|
||
return item, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) ReadObject(_ context.Context, bucket string, key string) ([]byte, string, error) {
|
||
browser.readCalls[bucket+"|"+key]++
|
||
if err, ok := browser.readErr[bucket+"|"+key]; ok && err != nil {
|
||
return nil, "", err
|
||
}
|
||
item, ok := browser.objects[bucket+"|"+key]
|
||
if !ok {
|
||
return nil, "", os.ErrNotExist
|
||
}
|
||
payload, ok := browser.content[bucket+"|"+key]
|
||
if !ok {
|
||
return nil, "", os.ErrNotExist
|
||
}
|
||
return append([]byte(nil), payload...), item.ContentType, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) ReadObjectRange(_ context.Context, bucket string, key string, start int64, end int64) (io.ReadCloser, string, error) {
|
||
objectID := bucket + "|" + key
|
||
browser.readRangeCalls[objectID]++
|
||
browser.lastReadRange[objectID] = [2]int64{start, end}
|
||
if err, ok := browser.readErr[objectID]; ok && err != nil {
|
||
return nil, "", err
|
||
}
|
||
item, ok := browser.objects[objectID]
|
||
if !ok {
|
||
return nil, "", os.ErrNotExist
|
||
}
|
||
payload, ok := browser.content[objectID]
|
||
if !ok {
|
||
return nil, "", os.ErrNotExist
|
||
}
|
||
if start < 0 || end < start || start >= int64(len(payload)) {
|
||
return nil, "", os.ErrInvalid
|
||
}
|
||
if end >= int64(len(payload)) {
|
||
end = int64(len(payload)) - 1
|
||
}
|
||
return io.NopCloser(bytes.NewReader(payload[start : end+1])), item.ContentType, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) ReadObjectPreview(_ context.Context, bucket string, key string, maxBytes int64) ([]byte, string, error) {
|
||
browser.readPreviewCalls[bucket+"|"+key]++
|
||
browser.lastPreviewMaxLen[bucket+"|"+key] = maxBytes
|
||
if err, ok := browser.readErr[bucket+"|"+key]; ok && err != nil {
|
||
return nil, "", err
|
||
}
|
||
item, ok := browser.objects[bucket+"|"+key]
|
||
if !ok {
|
||
return nil, "", os.ErrNotExist
|
||
}
|
||
payload, ok := browser.content[bucket+"|"+key]
|
||
if !ok {
|
||
return nil, "", os.ErrNotExist
|
||
}
|
||
if maxBytes > 0 && int64(len(payload)) > maxBytes {
|
||
payload = payload[:maxBytes]
|
||
}
|
||
return append([]byte(nil), payload...), item.ContentType, nil
|
||
}
|
||
|
||
func (browser *fakeObjectStorageBrowser) BuildPreviewURL(_ context.Context, bucket string, key string) (string, error) {
|
||
if _, ok := browser.objects[bucket+"|"+key]; !ok {
|
||
return "", os.ErrNotExist
|
||
}
|
||
return "https://example.com/preview/" + url.PathEscape(bucket) + "/" + url.PathEscape(key), nil
|
||
}
|
||
|
||
func TestObjectStorageBrowserSupportsConnectionCRUDAndPreview(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 管理连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage",
|
||
AccessKeyID: "ak-storage-browser",
|
||
SecretAccessKey: "sk-storage-browser",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(_ context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection: %#v", item)
|
||
}
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
storageConnectionBrowserResolver = originalResolver
|
||
})
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
overviewRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
overviewRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/object-storage?connectionId="+connection.ID, nil),
|
||
)
|
||
if overviewRecorder.Code != http.StatusOK {
|
||
t.Fatalf("object storage overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
||
}
|
||
|
||
createObjectRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createObjectRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"key":"algo/created.json","content":"{\"ok\":true}","contentType":"application/json"}`)),
|
||
)
|
||
if createObjectRecorder.Code != http.StatusOK {
|
||
t.Fatalf("create object status: %d body=%s", createObjectRecorder.Code, createObjectRecorder.Body.String())
|
||
}
|
||
|
||
renameRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
renameRecorder,
|
||
httptest.NewRequest(http.MethodPatch, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"key":"algo/created.json","newKey":"algo/renamed.json"}`)),
|
||
)
|
||
if renameRecorder.Code != http.StatusOK {
|
||
t.Fatalf("rename object status: %d body=%s", renameRecorder.Code, renameRecorder.Body.String())
|
||
}
|
||
|
||
objectsRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
objectsRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/object-storage/objects?connectionId="+connection.ID+"&prefix=algo", nil),
|
||
)
|
||
if objectsRecorder.Code != http.StatusOK {
|
||
t.Fatalf("list objects status: %d body=%s", objectsRecorder.Code, objectsRecorder.Body.String())
|
||
}
|
||
|
||
contentRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
contentRecorder,
|
||
httptest.NewRequest(http.MethodGet, "/api/object-storage/content?connectionId="+connection.ID+"&key=algo/renamed.json", nil),
|
||
)
|
||
if contentRecorder.Code != http.StatusOK {
|
||
t.Fatalf("object content status: %d body=%s", contentRecorder.Code, contentRecorder.Body.String())
|
||
}
|
||
if got := strings.TrimSpace(contentRecorder.Header().Get("Content-Type")); !strings.HasPrefix(got, "application/json") {
|
||
t.Fatalf("unexpected content type: %s", got)
|
||
}
|
||
|
||
var multipartBody bytes.Buffer
|
||
multipartWriter := multipart.NewWriter(&multipartBody)
|
||
fileWriter, err := multipartWriter.CreateFormFile("file", "hello.txt")
|
||
if err != nil {
|
||
t.Fatalf("create multipart file: %v", err)
|
||
}
|
||
if _, err := io.WriteString(fileWriter, "hello object storage"); err != nil {
|
||
t.Fatalf("write multipart file: %v", err)
|
||
}
|
||
if err := multipartWriter.Close(); err != nil {
|
||
t.Fatalf("close multipart writer: %v", err)
|
||
}
|
||
uploadRequest := httptest.NewRequest(http.MethodPost, "/api/object-storage/objects/upload?connectionId="+connection.ID+"&key=algo/nested/uploaded.txt", &multipartBody)
|
||
uploadRequest.Header.Set("Content-Type", multipartWriter.FormDataContentType())
|
||
uploadRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(uploadRecorder, uploadRequest)
|
||
if uploadRecorder.Code != http.StatusOK {
|
||
t.Fatalf("upload object status: %d body=%s", uploadRecorder.Code, uploadRecorder.Body.String())
|
||
}
|
||
|
||
moveDirectoryRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
moveDirectoryRecorder,
|
||
httptest.NewRequest(http.MethodPatch, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"destinationPrefix":"algo/moved","items":[{"key":"algo/nested","kind":"directory"}]}`)),
|
||
)
|
||
if moveDirectoryRecorder.Code != http.StatusOK {
|
||
t.Fatalf("move directory status: %d body=%s", moveDirectoryRecorder.Code, moveDirectoryRecorder.Body.String())
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/nested/uploaded.txt"]; ok {
|
||
t.Fatalf("expected recursive directory move to remove nested source object")
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/moved/nested/uploaded.txt"]; !ok {
|
||
t.Fatalf("expected recursive directory move to create destination directory object")
|
||
}
|
||
|
||
createBatchFileRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createBatchFileRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"key":"algo/batch-file.txt","content":"batch move file","contentType":"text/plain"}`)),
|
||
)
|
||
if createBatchFileRecorder.Code != http.StatusOK {
|
||
t.Fatalf("create batch file status: %d body=%s", createBatchFileRecorder.Code, createBatchFileRecorder.Body.String())
|
||
}
|
||
batchMoveRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
batchMoveRecorder,
|
||
httptest.NewRequest(http.MethodPatch, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"destinationPrefix":"algo/batch-destination","items":[{"key":"algo/moved/nested","kind":"directory"},{"key":"algo/batch-file.txt","kind":"file"}]}`)),
|
||
)
|
||
if batchMoveRecorder.Code != http.StatusOK {
|
||
t.Fatalf("batch move status: %d body=%s", batchMoveRecorder.Code, batchMoveRecorder.Body.String())
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/moved/nested/uploaded.txt"]; ok {
|
||
t.Fatalf("expected batch move to remove source directory object")
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/batch-file.txt"]; ok {
|
||
t.Fatalf("expected batch move to remove source file")
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/batch-destination/nested/uploaded.txt"]; !ok {
|
||
t.Fatalf("expected batch move to move directory itself under destination")
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/batch-destination/batch-file.txt"]; !ok {
|
||
t.Fatalf("expected batch move to move file under destination")
|
||
}
|
||
rootBatchMoveRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
rootBatchMoveRecorder,
|
||
httptest.NewRequest(http.MethodPatch, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"destinationPrefix":"","items":[{"key":"algo/batch-destination/nested","kind":"directory"},{"key":"algo/batch-destination/batch-file.txt","kind":"file"}]}`)),
|
||
)
|
||
if rootBatchMoveRecorder.Code != http.StatusOK {
|
||
t.Fatalf("root batch move status: %d body=%s", rootBatchMoveRecorder.Code, rootBatchMoveRecorder.Body.String())
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/batch-destination/nested/uploaded.txt"]; ok {
|
||
t.Fatalf("expected root batch move to remove source directory object")
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/batch-destination/batch-file.txt"]; ok {
|
||
t.Fatalf("expected root batch move to remove source file")
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|nested/uploaded.txt"]; !ok {
|
||
t.Fatalf("expected root batch move to move directory itself to bucket root")
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|batch-file.txt"]; !ok {
|
||
t.Fatalf("expected root batch move to move file to bucket root")
|
||
}
|
||
|
||
deleteObjectRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteObjectRecorder,
|
||
httptest.NewRequest(http.MethodDelete, "/api/object-storage/objects?connectionId="+connection.ID+"&key=algo/renamed.json", nil),
|
||
)
|
||
if deleteObjectRecorder.Code != http.StatusOK {
|
||
t.Fatalf("delete object status: %d body=%s", deleteObjectRecorder.Code, deleteObjectRecorder.Body.String())
|
||
}
|
||
|
||
deleteDirectoryRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
deleteDirectoryRecorder,
|
||
httptest.NewRequest(http.MethodDelete, "/api/object-storage/objects?connectionId="+connection.ID+"&key=algo/batch-destination&recursive=true", nil),
|
||
)
|
||
if deleteDirectoryRecorder.Code != http.StatusOK {
|
||
t.Fatalf("delete directory status: %d body=%s", deleteDirectoryRecorder.Code, deleteDirectoryRecorder.Body.String())
|
||
}
|
||
if _, ok := fakeBrowser.objects["datastorage|algo/batch-destination/nested/uploaded.txt"]; ok {
|
||
t.Fatalf("expected recursive directory delete to remove nested object")
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageUploadControlAPISupportsPresignAndMultipart(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 管理连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage",
|
||
AccessKeyID: "ak-storage-browser",
|
||
SecretAccessKey: "sk-storage-browser",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(_ context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection: %#v", item)
|
||
}
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
storageConnectionBrowserResolver = originalResolver
|
||
})
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
presignRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
presignRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/presign-put?connectionId="+connection.ID, strings.NewReader(`{"key":"large/direct.bin","contentType":"application/octet-stream"}`)),
|
||
)
|
||
if presignRecorder.Code != http.StatusOK {
|
||
t.Fatalf("presign put status: %d body=%s", presignRecorder.Code, presignRecorder.Body.String())
|
||
}
|
||
var presign domain.ObjectStoragePresignedUploadResult
|
||
if err := json.Unmarshal(presignRecorder.Body.Bytes(), &presign); err != nil {
|
||
t.Fatalf("decode presign: %v", err)
|
||
}
|
||
if presign.Method != http.MethodPut || presign.Key != "large/direct.bin" || presign.Headers["Content-Type"] != "application/octet-stream" {
|
||
t.Fatalf("unexpected presign result: %#v", presign)
|
||
}
|
||
|
||
initiateRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
initiateRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/multipart/initiate?connectionId="+connection.ID, strings.NewReader(`{"key":"large/multipart.bin","contentType":"application/octet-stream"}`)),
|
||
)
|
||
if initiateRecorder.Code != http.StatusOK {
|
||
t.Fatalf("multipart initiate status: %d body=%s", initiateRecorder.Code, initiateRecorder.Body.String())
|
||
}
|
||
var initiated domain.ObjectStorageMultipartUploadCreateResult
|
||
if err := json.Unmarshal(initiateRecorder.Body.Bytes(), &initiated); err != nil {
|
||
t.Fatalf("decode initiate: %v", err)
|
||
}
|
||
if initiated.UploadID == "" {
|
||
t.Fatalf("expected upload id: %#v", initiated)
|
||
}
|
||
|
||
partURLRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
partURLRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/multipart/part-url?connectionId="+connection.ID, strings.NewReader(fmt.Sprintf(`{"key":"large/multipart.bin","uploadId":%q,"partNumber":2}`, initiated.UploadID))),
|
||
)
|
||
if partURLRecorder.Code != http.StatusOK {
|
||
t.Fatalf("multipart part-url status: %d body=%s", partURLRecorder.Code, partURLRecorder.Body.String())
|
||
}
|
||
var partURL domain.ObjectStorageMultipartUploadPartURLResult
|
||
if err := json.Unmarshal(partURLRecorder.Body.Bytes(), &partURL); err != nil {
|
||
t.Fatalf("decode part url: %v", err)
|
||
}
|
||
if partURL.Method != http.MethodPut || partURL.PartNumber != 2 || !strings.Contains(partURL.URL, "partNumber=2") {
|
||
t.Fatalf("unexpected part url result: %#v", partURL)
|
||
}
|
||
|
||
completeRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
completeRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/multipart/complete?connectionId="+connection.ID, strings.NewReader(fmt.Sprintf(`{"key":"large/multipart.bin","uploadId":%q,"parts":[{"partNumber":2,"eTag":"\"etag-2\""},{"partNumber":1,"eTag":"etag-1"}]}`, initiated.UploadID))),
|
||
)
|
||
if completeRecorder.Code != http.StatusOK {
|
||
t.Fatalf("multipart complete status: %d body=%s", completeRecorder.Code, completeRecorder.Body.String())
|
||
}
|
||
parts := fakeBrowser.uploads["datastorage|large/multipart.bin|"+initiated.UploadID]
|
||
if len(parts) != 2 || parts[0].PartNumber != 1 || parts[0].ETag != "etag-1" || parts[1].PartNumber != 2 || parts[1].ETag != "etag-2" {
|
||
t.Fatalf("complete parts not normalized: %#v", parts)
|
||
}
|
||
|
||
abortRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
abortRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/multipart/abort?connectionId="+connection.ID, strings.NewReader(fmt.Sprintf(`{"key":"large/multipart.bin","uploadId":%q}`, initiated.UploadID))),
|
||
)
|
||
if abortRecorder.Code != http.StatusOK {
|
||
t.Fatalf("multipart abort status: %d body=%s", abortRecorder.Code, abortRecorder.Body.String())
|
||
}
|
||
if !fakeBrowser.abortedUploads["datastorage|large/multipart.bin|"+initiated.UploadID] {
|
||
t.Fatalf("expected aborted upload to be recorded")
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageBatchUploadPreparePlansNestedFilesConflictsAndPlaceholders(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS batch upload",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage",
|
||
AccessKeyID: "ak-storage-browser",
|
||
SecretAccessKey: "sk-storage-browser",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
existingKey := "datasets/root/nested/existing.bin"
|
||
fakeBrowser.objects["datastorage|"+existingKey] = domain.ObjectStorageObjectSummary{
|
||
Bucket: "datastorage", Key: existingKey, Name: "existing.bin", Kind: "file",
|
||
}
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(_ context.Context, _ *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
t.Cleanup(func() { storageConnectionBrowserResolver = originalResolver })
|
||
handler := NewHandler(dataStore, nil)
|
||
|
||
body := `{"bucket":"datastorage","prefix":"datasets","conflictPolicy":"skip","items":[` +
|
||
`{"relativePath":"root/nested/existing.bin","kind":"file","sizeBytes":10},` +
|
||
`{"relativePath":"root/new.bin","kind":"file","sizeBytes":20},` +
|
||
`{"relativePath":"root/empty","kind":"directory"}]}`
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/batch/prepare?connectionId="+connection.ID, strings.NewReader(body)),
|
||
)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("batch prepare status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var result domain.ObjectStorageBatchUploadPrepareResult
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
|
||
t.Fatalf("decode batch prepare: %v", err)
|
||
}
|
||
if result.UploadCount != 1 || result.PlaceholderCount != 1 || result.SkipCount != 1 || result.ConflictCount != 0 {
|
||
t.Fatalf("unexpected batch counts: %#v", result)
|
||
}
|
||
if result.Items[0].Action != "skip" || result.Items[1].Key != "datasets/root/new.bin" || result.Items[1].Action != "upload" {
|
||
t.Fatalf("unexpected file plan: %#v", result.Items)
|
||
}
|
||
if result.Items[2].Key != "datasets/root/empty/.keep" || result.Items[2].Action != "create_placeholder" {
|
||
t.Fatalf("unexpected directory plan: %#v", result.Items[2])
|
||
}
|
||
|
||
errorBody := strings.Replace(body, `"conflictPolicy":"skip"`, `"conflictPolicy":"error"`, 1)
|
||
errorRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
errorRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/batch/prepare?connectionId="+connection.ID, strings.NewReader(errorBody)),
|
||
)
|
||
if errorRecorder.Code != http.StatusOK {
|
||
t.Fatalf("batch error policy status: %d body=%s", errorRecorder.Code, errorRecorder.Body.String())
|
||
}
|
||
if err := json.Unmarshal(errorRecorder.Body.Bytes(), &result); err != nil {
|
||
t.Fatalf("decode error plan: %v", err)
|
||
}
|
||
if result.ConflictCount != 1 || result.Items[0].Action != "conflict" {
|
||
t.Fatalf("expected one explicit conflict: %#v", result)
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageBatchUploadPrepareRejectsUnsafeAndDuplicatePaths(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS batch validation", Provider: domain.StorageProviderBOS, Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage", AccessKeyID: "ak", SecretAccessKey: "sk",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(_ context.Context, _ *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
t.Cleanup(func() { storageConnectionBrowserResolver = originalResolver })
|
||
handler := NewHandler(dataStore, nil)
|
||
tests := []string{
|
||
`{"items":[{"relativePath":"../escape.bin","kind":"file"}]}`,
|
||
`{"items":[{"relativePath":"same.bin","kind":"file"},{"relativePath":"same.bin","kind":"file"}]}`,
|
||
`{"conflictPolicy":"rename","items":[{"relativePath":"valid.bin","kind":"file"}]}`,
|
||
}
|
||
for _, body := range tests {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/object-storage/uploads/batch/prepare?connectionId="+connection.ID, strings.NewReader(body)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status = %d body=%s for input %s", recorder.Code, recorder.Body.String(), body)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageBatchUploadPrepareValidatesFinalKeyUTF8Bytes(t *testing.T) {
|
||
exactLimitPath := strings.Repeat("d", objectStorageKeyMaxUTF8Bytes)
|
||
items, _, _, issue := prepareObjectStorageBatchItems(domain.ObjectStorageBatchUploadPrepareInput{
|
||
Items: []domain.ObjectStorageBatchUploadItemInput{{RelativePath: exactLimitPath, Kind: "file"}},
|
||
})
|
||
if issue != nil || len(items) != 1 || len(items[0].Key) != objectStorageKeyMaxUTF8Bytes {
|
||
t.Fatalf("expected exact key byte limit to pass, items=%#v issue=%#v", items, issue)
|
||
}
|
||
|
||
safePath := strings.Repeat("d/", 256) + "payload.txt"
|
||
items, prefix, _, issue := prepareObjectStorageBatchItems(domain.ObjectStorageBatchUploadPrepareInput{
|
||
Prefix: "deep-test",
|
||
Items: []domain.ObjectStorageBatchUploadItemInput{{RelativePath: safePath, Kind: "file"}},
|
||
})
|
||
if issue != nil || len(items) != 1 || prefix != "deep-test" {
|
||
t.Fatalf("expected 256-level path to pass, items=%#v prefix=%q issue=%#v", items, prefix, issue)
|
||
}
|
||
|
||
tests := []struct {
|
||
name string
|
||
prefix string
|
||
item domain.ObjectStorageBatchUploadItemInput
|
||
}{
|
||
{
|
||
name: "512 levels",
|
||
item: domain.ObjectStorageBatchUploadItemInput{RelativePath: strings.Repeat("d/", 512) + "payload.txt", Kind: "file"},
|
||
},
|
||
{
|
||
name: "multibyte path with prefix",
|
||
prefix: "目标路径",
|
||
item: domain.ObjectStorageBatchUploadItemInput{RelativePath: strings.Repeat("层/", 254) + "文件.txt", Kind: "file"},
|
||
},
|
||
{
|
||
name: "empty directory placeholder",
|
||
item: domain.ObjectStorageBatchUploadItemInput{RelativePath: strings.Repeat("d", 1018), Kind: "directory"},
|
||
},
|
||
}
|
||
for _, test := range tests {
|
||
t.Run(test.name, func(t *testing.T) {
|
||
_, _, _, issue := prepareObjectStorageBatchItems(domain.ObjectStorageBatchUploadPrepareInput{
|
||
Prefix: test.prefix,
|
||
Items: []domain.ObjectStorageBatchUploadItemInput{test.item},
|
||
})
|
||
if issue == nil || issue.Field != "items[0].relativePath" || !strings.Contains(issue.Message, "1023 UTF-8 bytes") {
|
||
t.Fatalf("expected explicit key byte limit error, got %#v", issue)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageUploadControlAPIRejectsInvalidMultipartInput(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 管理连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage",
|
||
AccessKeyID: "ak-storage-browser",
|
||
SecretAccessKey: "sk-storage-browser",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(_ context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
t.Cleanup(func() {
|
||
storageConnectionBrowserResolver = originalResolver
|
||
})
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
tests := []struct {
|
||
name string
|
||
path string
|
||
body string
|
||
}{
|
||
{name: "missing connection", path: "/api/object-storage/uploads/presign-put", body: `{"key":"x.bin"}`},
|
||
{name: "missing key", path: "/api/object-storage/uploads/presign-put?connectionId=" + connection.ID, body: `{}`},
|
||
{name: "invalid part number", path: "/api/object-storage/uploads/multipart/part-url?connectionId=" + connection.ID, body: `{"key":"x.bin","uploadId":"u1","partNumber":0}`},
|
||
{name: "empty complete parts", path: "/api/object-storage/uploads/multipart/complete?connectionId=" + connection.ID, body: `{"key":"x.bin","uploadId":"u1","parts":[]}`},
|
||
}
|
||
for _, test := range tests {
|
||
t.Run(test.name, func(t *testing.T) {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, test.path, strings.NewReader(test.body)))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("status = %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageContentStreamsVideoPreviewWithoutReadingWholeObject(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 管理连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage",
|
||
AccessKeyID: "ak-storage-browser",
|
||
SecretAccessKey: "sk-storage-browser",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
videoKey := "algo/demo.mp4"
|
||
fakeBrowser.objects["datastorage|"+videoKey] = domain.ObjectStorageObjectSummary{
|
||
Bucket: "datastorage",
|
||
Key: videoKey,
|
||
Name: "demo.mp4",
|
||
Kind: "video",
|
||
SizeBytes: 7 << 30,
|
||
ContentType: "video/mp4",
|
||
}
|
||
fakeBrowser.readErr["datastorage|"+videoKey] = fmt.Errorf("read should not be called for redirected video preview")
|
||
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(ctx context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection: %#v", item)
|
||
}
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
defer func() { storageConnectionBrowserResolver = originalResolver }()
|
||
|
||
previewFile, err := os.CreateTemp(t.TempDir(), "object-storage-preview-*.mp4")
|
||
if err != nil {
|
||
t.Fatalf("create preview file: %v", err)
|
||
}
|
||
if _, err := previewFile.Write([]byte("preview-video")); err != nil {
|
||
t.Fatalf("write preview file: %v", err)
|
||
}
|
||
if err := previewFile.Close(); err != nil {
|
||
t.Fatalf("close preview file: %v", err)
|
||
}
|
||
|
||
originalPreviewResolver := sourceBrowserVideoPreviewPath
|
||
sourceBrowserVideoPreviewPath = func(ctx context.Context, request sourceBrowserPreviewRequest) (string, error) {
|
||
if request.RedirectURL == "" {
|
||
t.Fatalf("expected redirect url for video preview request")
|
||
}
|
||
if len(request.Payload) != 0 {
|
||
t.Fatalf("expected video preview to avoid in-memory payload reads")
|
||
}
|
||
return previewFile.Name(), nil
|
||
}
|
||
defer func() { sourceBrowserVideoPreviewPath = originalPreviewResolver }()
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(
|
||
http.MethodGet,
|
||
"/api/object-storage/content?connectionId="+url.QueryEscape(connection.ID)+"&bucket=datastorage&key="+url.QueryEscape(videoKey)+"&transcode=browser",
|
||
nil,
|
||
)
|
||
handler.ServeHTTP(recorder, request)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("video preview status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if contentType := recorder.Header().Get("Content-Type"); contentType != "video/mp4" {
|
||
t.Fatalf("unexpected content type: %s", contentType)
|
||
}
|
||
if body := recorder.Body.String(); body != "preview-video" {
|
||
t.Fatalf("unexpected preview body: %q", body)
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageContentSupportsRangeAndHeadWithoutReadingWholeObject(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 管理连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage",
|
||
AccessKeyID: "ak-storage-browser",
|
||
SecretAccessKey: "sk-storage-browser",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
videoKey := "algo/range-demo.mp4"
|
||
videoPayload := []byte("0123456789")
|
||
fakeBrowser.objects["datastorage|"+videoKey] = domain.ObjectStorageObjectSummary{
|
||
Bucket: "datastorage",
|
||
Key: videoKey,
|
||
Name: "range-demo.mp4",
|
||
Kind: "video",
|
||
SizeBytes: int64(len(videoPayload)),
|
||
ContentType: "video/mp4",
|
||
ETag: "etag-range-demo",
|
||
}
|
||
fakeBrowser.content["datastorage|"+videoKey] = videoPayload
|
||
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(ctx context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection: %#v", item)
|
||
}
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
defer func() { storageConnectionBrowserResolver = originalResolver }()
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
rangeRecorder := httptest.NewRecorder()
|
||
rangeRequest := httptest.NewRequest(
|
||
http.MethodGet,
|
||
"/api/object-storage/content?connectionId="+url.QueryEscape(connection.ID)+"&bucket=datastorage&key="+url.QueryEscape(videoKey),
|
||
nil,
|
||
)
|
||
rangeRequest.Header.Set("Range", "bytes=2-5")
|
||
handler.ServeHTTP(rangeRecorder, rangeRequest)
|
||
if rangeRecorder.Code != http.StatusPartialContent {
|
||
t.Fatalf("range status: %d body=%s", rangeRecorder.Code, rangeRecorder.Body.String())
|
||
}
|
||
if body := rangeRecorder.Body.String(); body != "2345" {
|
||
t.Fatalf("range body = %q, want 2345", body)
|
||
}
|
||
if got := rangeRecorder.Header().Get("Content-Range"); got != "bytes 2-5/10" {
|
||
t.Fatalf("content range = %q", got)
|
||
}
|
||
if got := rangeRecorder.Header().Get("Accept-Ranges"); got != "bytes" {
|
||
t.Fatalf("accept ranges = %q", got)
|
||
}
|
||
if got := rangeRecorder.Header().Get("Content-Length"); got != "4" {
|
||
t.Fatalf("content length = %q", got)
|
||
}
|
||
if fakeBrowser.readCalls["datastorage|"+videoKey] != 0 {
|
||
t.Fatalf("expected no full reads, got %d", fakeBrowser.readCalls["datastorage|"+videoKey])
|
||
}
|
||
if fakeBrowser.readRangeCalls["datastorage|"+videoKey] != 1 {
|
||
t.Fatalf("expected one range read, got %d", fakeBrowser.readRangeCalls["datastorage|"+videoKey])
|
||
}
|
||
if got := fakeBrowser.lastReadRange["datastorage|"+videoKey]; got != [2]int64{2, 5} {
|
||
t.Fatalf("range read = %#v", got)
|
||
}
|
||
|
||
headRecorder := httptest.NewRecorder()
|
||
headRequest := httptest.NewRequest(
|
||
http.MethodHead,
|
||
"/api/object-storage/content?connectionId="+url.QueryEscape(connection.ID)+"&bucket=datastorage&key="+url.QueryEscape(videoKey),
|
||
nil,
|
||
)
|
||
handler.ServeHTTP(headRecorder, headRequest)
|
||
if headRecorder.Code != http.StatusOK {
|
||
t.Fatalf("head status: %d body=%s", headRecorder.Code, headRecorder.Body.String())
|
||
}
|
||
if headRecorder.Body.Len() != 0 {
|
||
t.Fatalf("HEAD should not return a body, got %q", headRecorder.Body.String())
|
||
}
|
||
if got := headRecorder.Header().Get("Content-Length"); got != "10" {
|
||
t.Fatalf("HEAD content length = %q", got)
|
||
}
|
||
if fakeBrowser.readCalls["datastorage|"+videoKey] != 0 {
|
||
t.Fatalf("HEAD should not read full object, got %d", fakeBrowser.readCalls["datastorage|"+videoKey])
|
||
}
|
||
if fakeBrowser.readRangeCalls["datastorage|"+videoKey] != 1 {
|
||
t.Fatalf("HEAD should not open object range, got %d", fakeBrowser.readRangeCalls["datastorage|"+videoKey])
|
||
}
|
||
|
||
invalidRecorder := httptest.NewRecorder()
|
||
invalidRequest := httptest.NewRequest(
|
||
http.MethodGet,
|
||
"/api/object-storage/content?connectionId="+url.QueryEscape(connection.ID)+"&bucket=datastorage&key="+url.QueryEscape(videoKey),
|
||
nil,
|
||
)
|
||
invalidRequest.Header.Set("Range", "bytes=99-100")
|
||
handler.ServeHTTP(invalidRecorder, invalidRequest)
|
||
if invalidRecorder.Code != http.StatusRequestedRangeNotSatisfiable {
|
||
t.Fatalf("invalid range status: %d body=%s", invalidRecorder.Code, invalidRecorder.Body.String())
|
||
}
|
||
if got := invalidRecorder.Header().Get("Content-Range"); got != "bytes */10" {
|
||
t.Fatalf("invalid content range = %q", got)
|
||
}
|
||
}
|
||
|
||
func TestObjectStorageContentPreviewTruncatesLargeJSONObjects(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 管理连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
Bucket: "datastorage",
|
||
AccessKeyID: "ak-storage-browser",
|
||
SecretAccessKey: "sk-storage-browser",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
fakeBrowser := newFakeObjectStorageBrowser()
|
||
largeKey := "algo/large.json"
|
||
largePayload := []byte(strings.Repeat(`{"imu":1}`+"\n", 40000))
|
||
fakeBrowser.objects["datastorage|"+largeKey] = domain.ObjectStorageObjectSummary{
|
||
Bucket: "datastorage",
|
||
Key: largeKey,
|
||
Name: "large.json",
|
||
Kind: "json",
|
||
SizeBytes: int64(len(largePayload)),
|
||
ContentType: "application/json",
|
||
}
|
||
fakeBrowser.content["datastorage|"+largeKey] = largePayload
|
||
|
||
originalResolver := storageConnectionBrowserResolver
|
||
storageConnectionBrowserResolver = func(_ context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
||
if item == nil || item.ID != connection.ID {
|
||
t.Fatalf("unexpected storage connection: %#v", item)
|
||
}
|
||
return fakeBrowser, func() {}, nil
|
||
}
|
||
defer func() { storageConnectionBrowserResolver = originalResolver }()
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
recorder := httptest.NewRecorder()
|
||
request := httptest.NewRequest(
|
||
http.MethodGet,
|
||
"/api/object-storage/content?connectionId="+url.QueryEscape(connection.ID)+"&bucket=datastorage&key="+url.QueryEscape(largeKey)+"&preview=true",
|
||
nil,
|
||
)
|
||
handler.ServeHTTP(recorder, request)
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("content preview status: %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if fakeBrowser.readCalls["datastorage|"+largeKey] != 0 {
|
||
t.Fatalf("expected no full-object reads, got %d", fakeBrowser.readCalls["datastorage|"+largeKey])
|
||
}
|
||
if fakeBrowser.readPreviewCalls["datastorage|"+largeKey] != 1 {
|
||
t.Fatalf("expected one preview read, got %d", fakeBrowser.readPreviewCalls["datastorage|"+largeKey])
|
||
}
|
||
if got := fakeBrowser.lastPreviewMaxLen["datastorage|"+largeKey]; got != objectStoragePartialPreviewBytes {
|
||
t.Fatalf("preview max bytes = %d, want %d", got, objectStoragePartialPreviewBytes)
|
||
}
|
||
if recorder.Header().Get(objectStoragePreviewTruncatedHeader) != "true" {
|
||
t.Fatalf("expected truncated header true, got %q", recorder.Header().Get(objectStoragePreviewTruncatedHeader))
|
||
}
|
||
if got := recorder.Header().Get(objectStoragePreviewOriginalSizeHeader); got != fmt.Sprintf("%d", len(largePayload)) {
|
||
t.Fatalf("unexpected original size header: %q", got)
|
||
}
|
||
if recorder.Body.Len() != int(objectStoragePartialPreviewBytes) {
|
||
t.Fatalf("preview body length = %d, want %d", recorder.Body.Len(), objectStoragePartialPreviewBytes)
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowAPICreatesConnectionBackedScanWorkflow(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
for _, name := range []string{"BOS 增量扫描", "X 平台数据集索引", "采集目录校验"} {
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: name,
|
||
Type: "builtin",
|
||
Category: "IO",
|
||
Description: name,
|
||
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "output", Type: domain.PortTypeJSON}},
|
||
}); err != nil {
|
||
t.Fatalf("create operator %s: %v", name, err)
|
||
}
|
||
}
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "连接型扫描项目",
|
||
Description: "scan with storage connection",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "BOS 主连接",
|
||
Provider: domain.StorageProviderBOS,
|
||
Endpoint: "https://bos.example.test",
|
||
AccessKeyID: "ak-conn-1234",
|
||
SecretAccessKey: "sk-conn-5678",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
createPayload := []byte(`{
|
||
"name":"连接型 BOS 扫描",
|
||
"description":"使用 connectionId 的扫描 workflow",
|
||
"type":"batch",
|
||
"kind":"scan",
|
||
"schedule":{"interval":600},
|
||
"scanConfig":{
|
||
"dataSource":"BOS",
|
||
"connectionId":"` + connection.ID + `",
|
||
"bucket":"datastorage",
|
||
"prefix":"algo/X-platform-test/",
|
||
"scanMode":"INCREMENTAL",
|
||
"incrementalUnit":"DIRECTORY",
|
||
"directoryDepth":3,
|
||
"enabled":true
|
||
},
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`)
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
||
)
|
||
if createRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create connection-backed scan workflow 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.ScanConfig == nil || created.ScanConfig.ConnectionID != connection.ID {
|
||
t.Fatalf("expected connection-backed scan config, got %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.IncrementalUnit != "DIRECTORY" || created.ScanConfig.DirectoryDepth != 3 {
|
||
t.Fatalf("expected connection-backed directory incremental config, got %+v", created.ScanConfig)
|
||
}
|
||
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
||
t.Fatalf("expected workflow response not to expose secrets: %+v", created.ScanConfig)
|
||
}
|
||
if created.Type != domain.WorkflowTypeStreaming {
|
||
t.Fatalf("expected enabled connection-backed scan workflow to be streaming, got %s", created.Type)
|
||
}
|
||
|
||
stored, err := dataStore.GetWorkflow(ctx, created.ID)
|
||
if err != nil {
|
||
t.Fatalf("get stored workflow: %v", err)
|
||
}
|
||
if stored == nil || stored.ScanConfig == nil {
|
||
t.Fatalf("expected stored scan config")
|
||
}
|
||
if stored.ScanConfig.ConnectionID != connection.ID {
|
||
t.Fatalf("expected stored connection id, got %+v", stored.ScanConfig)
|
||
}
|
||
if stored.ScanConfig.IncrementalUnit != "DIRECTORY" || stored.ScanConfig.DirectoryDepth != 3 {
|
||
t.Fatalf("expected stored connection-backed directory incremental config, got %+v", stored.ScanConfig)
|
||
}
|
||
if stored.ScanConfig.AccessKeyID != "" || stored.ScanConfig.SecretAccessKey != "" {
|
||
t.Fatalf("expected workflow store to keep connection ref instead of duplicating secrets: %+v", stored.ScanConfig)
|
||
}
|
||
|
||
runRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
runRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/workflows/"+created.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
||
)
|
||
if runRecorder.Code != http.StatusAccepted {
|
||
t.Fatalf("run connection-backed scan workflow status: %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowAPIRejectsUnknownNodeStorageConnection(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "连接校验项目",
|
||
Description: "reject invalid node connection",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
createPayload := []byte(`{
|
||
"name":"无效连接工作流",
|
||
"description":"节点 connectionId 应该校验",
|
||
"type":"batch",
|
||
"nodes":[
|
||
{
|
||
"id":"node-oss",
|
||
"type":"operator",
|
||
"data":{
|
||
"label":"OSS 数据源",
|
||
"category":"IO",
|
||
"description":"source",
|
||
"connectionId":"connection-missing",
|
||
"inputs":[],
|
||
"outputs":[],
|
||
"hyperparams":[]
|
||
},
|
||
"position":{"x":0,"y":0}
|
||
}
|
||
],
|
||
"edges":[]
|
||
}`)
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
||
)
|
||
if createRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected validation failure, got %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(createRecorder.Body.String(), "connectionId") {
|
||
t.Fatalf("expected connectionId issue, body=%s", createRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func duplicateInputEdgeWorkflowNodes() []domain.WorkflowNode {
|
||
return []domain.WorkflowNode{
|
||
{
|
||
ID: "source-a",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "上游 A",
|
||
Category: "IO",
|
||
Description: "source a",
|
||
Inputs: []domain.PortDefinition{},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
||
},
|
||
Position: domain.Position{X: 0, Y: 0},
|
||
},
|
||
{
|
||
ID: "source-b",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "上游 B",
|
||
Category: "IO",
|
||
Description: "source b",
|
||
Inputs: []domain.PortDefinition{},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
||
},
|
||
Position: domain.Position{X: 120, Y: 0},
|
||
},
|
||
{
|
||
ID: "upload",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "结果上传",
|
||
Category: "IO",
|
||
Description: "upload",
|
||
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "result", Type: domain.PortTypeJSON}},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "uploaded", Type: domain.PortTypeJSON}},
|
||
},
|
||
Position: domain.Position{X: 240, Y: 0},
|
||
},
|
||
}
|
||
}
|
||
|
||
func duplicateInputEdgeWorkflowEdges() []domain.WorkflowEdge {
|
||
return []domain.WorkflowEdge{
|
||
{ID: "edge-a", Source: "source-a", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
||
{ID: "edge-b", Source: "source-b", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowAPIRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "重复输入端口项目",
|
||
Description: "reject duplicate input edges",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
|
||
payload, err := json.Marshal(domain.CreateWorkflowInput{
|
||
Name: "非法上传工作流",
|
||
Description: "duplicate input edges",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: duplicateInputEdgeWorkflowNodes(),
|
||
Edges: duplicateInputEdgeWorkflowEdges(),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
||
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "上传算子") {
|
||
t.Fatalf("expected upload node guidance, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func aiDraftOperatorWorkflowNodes(operatorID string) []domain.WorkflowNode {
|
||
return []domain.WorkflowNode{
|
||
{
|
||
ID: "source",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "真实数据源",
|
||
Category: "IO",
|
||
Description: "source",
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "file", Type: domain.PortTypeFile}},
|
||
},
|
||
Position: domain.Position{X: 0, Y: 0},
|
||
},
|
||
{
|
||
ID: "ai-draft",
|
||
Type: "operator",
|
||
Data: domain.WorkflowNodeData{
|
||
Label: "AI 草稿算子",
|
||
Category: "AI",
|
||
Description: "draft",
|
||
OperatorID: operatorID,
|
||
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "file", Type: domain.PortTypeFile}},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
||
},
|
||
Position: domain.Position{X: 200, Y: 0},
|
||
},
|
||
}
|
||
}
|
||
|
||
func aiDraftOperatorWorkflowEdges() []domain.WorkflowEdge {
|
||
return []domain.WorkflowEdge{{ID: "edge-source-ai", Source: "source", SourceHandle: "out-1", Target: "ai-draft", TargetHandle: "in-1"}}
|
||
}
|
||
|
||
func createAIDraftOperatorForWorkflowTest(t *testing.T, dataStore store.Store) *domain.OperatorRecord {
|
||
t.Helper()
|
||
operator, err := dataStore.CreateOperator(context.Background(), domain.CreateOperatorInput{
|
||
Name: "AI 草稿算子",
|
||
Type: "custom",
|
||
Category: "AI",
|
||
Description: "draft",
|
||
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "file", Type: domain.PortTypeFile}},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
||
RuntimeContract: map[string]any{"generatedRepository": map[string]any{"repositoryName": "operator-ai-draft"}},
|
||
GenerationSource: domain.OperatorGenerationSourceAI,
|
||
PublicationStatus: domain.OperatorPublicationStatusDraft,
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create ai draft operator: %v", err)
|
||
}
|
||
return operator
|
||
}
|
||
|
||
func TestProductionWorkflowRejectsAIDraftOperator(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
operator := createAIDraftOperatorForWorkflowTest(t, dataStore)
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "AI 草稿生产阻断", Description: "production block"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
payload, err := json.Marshal(domain.CreateWorkflowInput{
|
||
Name: "生产工作流",
|
||
Description: "must reject draft",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: aiDraftOperatorWorkflowNodes(operator.ID),
|
||
Edges: aiDraftOperatorWorkflowEdges(),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected production workflow validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "ai_operator_draft_production_blocked") {
|
||
t.Fatalf("expected AI draft production block code, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestSandboxWorkflowAllowsAIDraftOperator(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
operator := createAIDraftOperatorForWorkflowTest(t, dataStore)
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "AI 草稿沙盒", Description: "sandbox allowed"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
payload, err := json.Marshal(domain.CreateWorkflowInput{
|
||
Name: "沙盒工作流",
|
||
Description: "allow draft",
|
||
Type: domain.WorkflowTypeBatch,
|
||
ExecutionScope: domain.WorkflowExecutionScopeSandbox,
|
||
Nodes: aiDraftOperatorWorkflowNodes(operator.ID),
|
||
Edges: aiDraftOperatorWorkflowEdges(),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusCreated {
|
||
t.Fatalf("expected sandbox workflow creation to succeed, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), `"executionScope":"sandbox"`) {
|
||
t.Fatalf("expected sandbox scope in response, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowRunRejectsAIDraftOperatorInProduction(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
operator := createAIDraftOperatorForWorkflowTest(t, dataStore)
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "AI 草稿运行阻断", Description: "run block"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "旧生产工作流",
|
||
Description: "legacy direct insert",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: aiDraftOperatorWorkflowNodes(operator.ID),
|
||
Edges: aiDraftOperatorWorkflowEdges(),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected run validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "ai_operator_draft_production_blocked") {
|
||
t.Fatalf("expected AI draft production block code, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowUpdateRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "更新重复输入端口项目",
|
||
Description: "reject duplicate input edges on update",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "合法工作流",
|
||
Description: "valid before update",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: duplicateInputEdgeWorkflowNodes(),
|
||
Edges: []domain.WorkflowEdge{
|
||
{ID: "edge-a", Source: "source-a", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
||
},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
|
||
payload, err := json.Marshal(map[string]any{
|
||
"nodes": duplicateInputEdgeWorkflowNodes(),
|
||
"edges": duplicateInputEdgeWorkflowEdges(),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID, bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected update validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
||
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowRunRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "运行重复输入端口项目",
|
||
Description: "reject duplicate input edges on run",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID,
|
||
Name: "非法运行工作流",
|
||
Description: "invalid graph",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: duplicateInputEdgeWorkflowNodes(),
|
||
Edges: duplicateInputEdgeWorkflowEdges(),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected run validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
||
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestTemplateCreateRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
|
||
payload, err := json.Marshal(domain.CreateTemplateInput{
|
||
Name: "非法模板",
|
||
Description: "duplicate input edges",
|
||
Type: domain.WorkflowTypeBatch,
|
||
Nodes: duplicateInputEdgeWorkflowNodes(),
|
||
Edges: duplicateInputEdgeWorkflowEdges(),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("marshal payload: %v", err)
|
||
}
|
||
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(
|
||
recorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(payload)),
|
||
)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected template validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
||
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestProjectWorkflowAPICreatesScanDraftAndBlocksRunUntilConfigured(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
for _, name := range []string{"BOS 增量扫描", "X 平台数据集索引", "采集目录校验"} {
|
||
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||
Name: name,
|
||
Type: "builtin",
|
||
Category: "IO",
|
||
Description: name,
|
||
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
||
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "output", Type: domain.PortTypeJSON}},
|
||
}); err != nil {
|
||
t.Fatalf("create operator %s: %v", name, err)
|
||
}
|
||
}
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||
Name: "扫描草稿项目",
|
||
Description: "scan draft",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
|
||
handler := NewHandler(dataStore, nil)
|
||
createPayload := []byte(`{
|
||
"name":"扫描草稿",
|
||
"description":"先创建后配置",
|
||
"type":"batch",
|
||
"kind":"scan",
|
||
"schedule":{"interval":0},
|
||
"scanConfig":{
|
||
"dataSource":"BOS",
|
||
"endpoint":"https://bos.example.test",
|
||
"scanMode":"INCREMENTAL",
|
||
"enabled":false
|
||
},
|
||
"nodes":[],
|
||
"edges":[]
|
||
}`)
|
||
createRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
createRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
||
)
|
||
if createRecorder.Code != http.StatusCreated {
|
||
t.Fatalf("create scan draft 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 scan draft: %v", err)
|
||
}
|
||
if created.Kind != domain.WorkflowKindScan {
|
||
t.Fatalf("expected scan kind, got %s", created.Kind)
|
||
}
|
||
if created.ScanConfig == nil {
|
||
t.Fatalf("expected scan config")
|
||
}
|
||
if created.ScanConfig.Bucket != "" || created.ScanConfig.Prefix != "" {
|
||
t.Fatalf("expected incomplete draft scan target, got %+v", created.ScanConfig)
|
||
}
|
||
if created.Type != domain.WorkflowTypeBatch {
|
||
t.Fatalf("expected draft scan workflow to stay batch, got %s", created.Type)
|
||
}
|
||
if len(created.Nodes) != 3 {
|
||
t.Fatalf("expected scaffolded scan nodes, got %d", len(created.Nodes))
|
||
}
|
||
|
||
runRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(
|
||
runRecorder,
|
||
httptest.NewRequest(http.MethodPost, "/api/workflows/"+created.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
||
)
|
||
if runRecorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected scan draft run to be rejected, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||
}
|
||
if !strings.Contains(runRecorder.Body.String(), "扫描源配置未完成") {
|
||
t.Fatalf("expected missing scan config error, got %s", runRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
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())
|
||
}
|
||
}
|
||
|
||
func TestRunsListCursorPaginationFiltersAndKeepsLegacyResponse(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
ctx := context.Background()
|
||
for _, run := range []domain.RunRecord{
|
||
{ID: "run-page-3", WorkflowID: "workflow-a", WorkflowName: "Vision Flow", ProjectID: "project-a", ProjectName: "Project A", Status: domain.RunStatusFailed, CreatedAt: "2026-07-10T03:00:00Z", UpdatedAt: "2026-07-10T03:00:00Z"},
|
||
{ID: "run-page-2", WorkflowID: "workflow-a", WorkflowName: "Vision Flow", ProjectID: "project-a", ProjectName: "Project A", Status: domain.RunStatusRunning, CreatedAt: "2026-07-10T02:00:00Z", UpdatedAt: "2026-07-10T02:00:00Z"},
|
||
{ID: "run-page-1", WorkflowID: "workflow-a", WorkflowName: "Vision Flow", ProjectID: "project-a", ProjectName: "Project A", Status: domain.RunStatusCompleted, CreatedAt: "2026-07-10T01:00:00Z", UpdatedAt: "2026-07-10T01:00:00Z"},
|
||
} {
|
||
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
||
t.Fatalf("upsert run %s: %v", run.ID, err)
|
||
}
|
||
}
|
||
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{})
|
||
|
||
type pageResponse struct {
|
||
Items []domain.RunSummary `json:"items"`
|
||
Total int64 `json:"total"`
|
||
StatusCounts map[domain.RunStatus]int64 `json:"statusCounts"`
|
||
NextCursor string `json:"nextCursor"`
|
||
HasMore bool `json:"hasMore"`
|
||
}
|
||
firstRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(firstRecorder, httptest.NewRequest(http.MethodGet, "/api/runs?limit=2&q=vision&projectId=project-a", nil))
|
||
if firstRecorder.Code != http.StatusOK {
|
||
t.Fatalf("first page status = %d body=%s", firstRecorder.Code, firstRecorder.Body.String())
|
||
}
|
||
var first pageResponse
|
||
if err := json.Unmarshal(firstRecorder.Body.Bytes(), &first); err != nil {
|
||
t.Fatalf("decode first page: %v", err)
|
||
}
|
||
if first.Total != 3 || len(first.Items) != 2 || first.Items[0].ID != "run-page-3" || first.Items[1].ID != "run-page-2" || !first.HasMore || first.NextCursor == "" {
|
||
t.Fatalf("unexpected first page: %#v", first)
|
||
}
|
||
if first.StatusCounts[domain.RunStatusFailed] != 1 || first.StatusCounts[domain.RunStatusRunning] != 1 || first.StatusCounts[domain.RunStatusCompleted] != 1 {
|
||
t.Fatalf("unexpected status counts: %#v", first.StatusCounts)
|
||
}
|
||
|
||
secondRecorder := httptest.NewRecorder()
|
||
secondURL := "/api/runs?limit=2&q=vision&projectId=project-a&cursor=" + url.QueryEscape(first.NextCursor)
|
||
handler.ServeHTTP(secondRecorder, httptest.NewRequest(http.MethodGet, secondURL, nil))
|
||
var second pageResponse
|
||
if err := json.Unmarshal(secondRecorder.Body.Bytes(), &second); err != nil {
|
||
t.Fatalf("decode second page: %v", err)
|
||
}
|
||
if secondRecorder.Code != http.StatusOK || second.Total != 3 || len(second.Items) != 1 || second.Items[0].ID != "run-page-1" || second.HasMore || second.NextCursor != "" {
|
||
t.Fatalf("unexpected second page status=%d page=%#v", secondRecorder.Code, second)
|
||
}
|
||
|
||
filteredRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(filteredRecorder, httptest.NewRequest(http.MethodGet, "/api/runs?limit=50&status=failed&q=vision", nil))
|
||
var filtered pageResponse
|
||
if err := json.Unmarshal(filteredRecorder.Body.Bytes(), &filtered); err != nil {
|
||
t.Fatalf("decode filtered page: %v", err)
|
||
}
|
||
if filtered.Total != 3 || len(filtered.Items) != 1 || filtered.Items[0].ID != "run-page-3" || filtered.StatusCounts[domain.RunStatusRunning] != 1 {
|
||
t.Fatalf("unexpected filtered page: %#v", filtered)
|
||
}
|
||
|
||
legacyRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(legacyRecorder, httptest.NewRequest(http.MethodGet, "/api/runs?status=failed", nil))
|
||
var legacy map[string]json.RawMessage
|
||
if err := json.Unmarshal(legacyRecorder.Body.Bytes(), &legacy); err != nil {
|
||
t.Fatalf("decode legacy response: %v", err)
|
||
}
|
||
if _, ok := legacy["items"]; !ok {
|
||
t.Fatalf("legacy response missing items: %s", legacyRecorder.Body.String())
|
||
}
|
||
if _, ok := legacy["total"]; ok {
|
||
t.Fatalf("legacy status-only response unexpectedly changed shape: %s", legacyRecorder.Body.String())
|
||
}
|
||
|
||
for _, path := range []string{
|
||
"/api/runs?limit=0",
|
||
"/api/runs?limit=50&status=unknown",
|
||
"/api/runs?limit=50&cursor=not-a-cursor",
|
||
} {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("expected validation error for %s, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
}
|
||
|
||
type readinessErrorStore struct {
|
||
store.Store
|
||
}
|
||
|
||
func (readinessErrorStore) Ready(context.Context) error {
|
||
return errors.New("mongo unavailable")
|
||
}
|
||
|
||
func TestHealthAndReadinessExposeBuildMetadataAndRemainPublic(t *testing.T) {
|
||
dataStore := store.NewMemoryStore()
|
||
appConfig := config.AppConfig{
|
||
APIToken: "required-token",
|
||
RequireSessionAuth: true,
|
||
BuildCommit: "3cda38c",
|
||
BuildTime: "2026-07-10T12:00:00Z",
|
||
Image: "ccr.example/droboticflow-api:3cda38c",
|
||
}
|
||
handler := NewHandlerWithConfig(dataStore, nil, appConfig)
|
||
|
||
for _, path := range []string{"/api/health", "/api/ready"} {
|
||
recorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
|
||
if recorder.Code != http.StatusOK {
|
||
t.Fatalf("%s status = %d body=%s", path, recorder.Code, recorder.Body.String())
|
||
}
|
||
var body map[string]any
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||
t.Fatalf("decode %s: %v", path, err)
|
||
}
|
||
if body["buildCommit"] != "3cda38c" || body["buildTime"] != "2026-07-10T12:00:00Z" || body["image"] != "ccr.example/droboticflow-api:3cda38c" {
|
||
t.Fatalf("%s build metadata = %#v", path, body)
|
||
}
|
||
}
|
||
|
||
notReadyHandler := NewHandlerWithConfig(readinessErrorStore{Store: dataStore}, nil, appConfig)
|
||
recorder := httptest.NewRecorder()
|
||
notReadyHandler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/ready", nil))
|
||
if recorder.Code != http.StatusServiceUnavailable || !strings.Contains(recorder.Body.String(), "mongo unavailable") {
|
||
t.Fatalf("not-ready response = %d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowUpdateRejectsActiveRunButAllowsCompletedRun(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{Name: "Editable workflow"})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
run := domain.RunRecord{ID: "run-edit-guard", WorkflowID: workflow.ID, Status: domain.RunStatusRunning, CreatedAt: "2026-07-10T01:00:00Z", UpdatedAt: "2026-07-10T01:00:00Z"}
|
||
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
||
t.Fatalf("upsert run: %v", err)
|
||
}
|
||
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{})
|
||
|
||
activeRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(activeRecorder, httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID, bytes.NewReader([]byte(`{"description":"blocked"}`))))
|
||
if activeRecorder.Code != http.StatusConflict || !strings.Contains(activeRecorder.Body.String(), "workflow_run_active") {
|
||
t.Fatalf("active update response = %d body=%s", activeRecorder.Code, activeRecorder.Body.String())
|
||
}
|
||
|
||
run.Status = domain.RunStatusCompleted
|
||
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
||
t.Fatalf("complete run: %v", err)
|
||
}
|
||
completedRecorder := httptest.NewRecorder()
|
||
handler.ServeHTTP(completedRecorder, httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID, bytes.NewReader([]byte(`{"description":"allowed"}`))))
|
||
if completedRecorder.Code != http.StatusOK {
|
||
t.Fatalf("completed update response = %d body=%s", completedRecorder.Code, completedRecorder.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestBuiltinOperatorCatalogFingerprintUsesAuthoritativeCatalog(t *testing.T) {
|
||
hash, count, err := builtinOperatorCatalogFingerprint()
|
||
if err != nil {
|
||
t.Fatalf("catalog fingerprint: %v", err)
|
||
}
|
||
records, err := builtinOperatorRecords("", "")
|
||
if err != nil {
|
||
t.Fatalf("catalog records: %v", err)
|
||
}
|
||
if count != len(records) || count == 0 {
|
||
t.Fatalf("catalog count = %d, records = %d", count, len(records))
|
||
}
|
||
if len(hash) != 64 {
|
||
t.Fatalf("catalog hash = %q, want sha256 hex", hash)
|
||
}
|
||
again, againCount, err := builtinOperatorCatalogFingerprint()
|
||
if err != nil || again != hash || againCount != count {
|
||
t.Fatalf("catalog fingerprint is not stable: hash=%q/%q count=%d/%d err=%v", hash, again, count, againCount, err)
|
||
}
|
||
}
|
||
|
||
func TestAcceptanceArtifactDataExtractsFreshnessSourceFields(t *testing.T) {
|
||
payload := map[string]any{
|
||
"operatorCount": float64(102),
|
||
"static": map[string]any{
|
||
"operatorFileCount": float64(102),
|
||
},
|
||
}
|
||
request := pocAcceptanceImportRequest{}
|
||
request.Source.SourceCommit = " 3cda38c "
|
||
request.Source.CatalogHash = " abcdef "
|
||
request.Source.CatalogCount = 102
|
||
request.Source.CompletedAt = " 2026-07-10T12:00:00Z "
|
||
data := acceptanceResultArtifactData(payload, request, "2026-07-10T13:00:00Z")
|
||
if data["sourceCommit"] != "3cda38c" || data["catalogHash"] != "abcdef" || data["catalogCount"] != 102 || data["completedAt"] != "2026-07-10T12:00:00Z" {
|
||
t.Fatalf("freshness fields not normalized: %#v", data)
|
||
}
|
||
importSource := dashboardMap(data["importSource"])
|
||
if importSource["sourceCommit"] != "3cda38c" || importSource["catalogCount"] != 102 {
|
||
t.Fatalf("import source metadata missing: %#v", importSource)
|
||
}
|
||
}
|
||
|
||
func TestEvaluateDashboardAcceptanceFreshness(t *testing.T) {
|
||
now := time.Date(2026, time.July, 10, 12, 0, 0, 0, time.UTC)
|
||
base := dashboardAcceptancePayload{
|
||
"sourceCommit": "3cda38c",
|
||
"catalogHash": "sha256:abcdef",
|
||
"catalogCount": float64(102),
|
||
"completedAt": "2026-07-10T11:00:00Z",
|
||
}
|
||
run := domain.RunRecord{UpdatedAt: "2026-07-10T11:00:00Z"}
|
||
|
||
fresh := evaluateDashboardAcceptanceFreshness(base, run, "3cda38c1b0bea3df30e7f6f292cd3216ac14185d", "abcdef", 102, now)
|
||
if fresh.Status != "fresh" || fresh.StaleReason != "" || fresh.CompletedAt == "" {
|
||
t.Fatalf("expected fresh acceptance evidence, got %#v", fresh)
|
||
}
|
||
|
||
cases := []struct {
|
||
name string
|
||
mutate func(dashboardAcceptancePayload)
|
||
reasonPart string
|
||
}{
|
||
{name: "age", mutate: func(payload dashboardAcceptancePayload) { payload["completedAt"] = "2026-07-09T11:59:59Z" }, reasonPart: "acceptance_older_than_24h"},
|
||
{name: "commit", mutate: func(payload dashboardAcceptancePayload) { payload["sourceCommit"] = "different" }, reasonPart: "source_commit_mismatch"},
|
||
{name: "hash", mutate: func(payload dashboardAcceptancePayload) { payload["catalogHash"] = "different" }, reasonPart: "catalog_hash_mismatch"},
|
||
{name: "count", mutate: func(payload dashboardAcceptancePayload) { payload["catalogCount"] = float64(101) }, reasonPart: "catalog_count_mismatch"},
|
||
}
|
||
for _, testCase := range cases {
|
||
t.Run(testCase.name, func(t *testing.T) {
|
||
payload := dashboardAcceptancePayload{}
|
||
for key, value := range base {
|
||
payload[key] = value
|
||
}
|
||
testCase.mutate(payload)
|
||
freshness := evaluateDashboardAcceptanceFreshness(payload, run, "3cda38c1b0bea3df30e7f6f292cd3216ac14185d", "abcdef", 102, now)
|
||
if freshness.Status != "stale" || !strings.Contains(freshness.StaleReason, testCase.reasonPart) {
|
||
t.Fatalf("expected stale reason %q, got %#v", testCase.reasonPart, freshness)
|
||
}
|
||
gate := dashboardStrictAcceptanceGate(payload, true, freshness)
|
||
if gate.Status != "unknown" {
|
||
t.Fatalf("stale strict acceptance must be unknown, got %#v", gate)
|
||
}
|
||
})
|
||
}
|
||
|
||
unknown := evaluateDashboardAcceptanceFreshness(dashboardAcceptancePayload{"completedAt": "2026-07-10T11:00:00Z"}, run, "", "abcdef", 102, now)
|
||
if unknown.Status != "unknown" || unknown.UnknownReason == "" {
|
||
t.Fatalf("expected incomplete metadata to be unknown, got %#v", unknown)
|
||
}
|
||
}
|
||
|
||
func TestFreshAcceptanceSplitsBusinessAndOperatorCoverageGates(t *testing.T) {
|
||
payload := dashboardAcceptancePayload(pocAcceptanceFullResult("fresh-split-gates"))
|
||
freshness := dashboardAcceptanceFreshness{
|
||
Status: "fresh",
|
||
SourceCommit: "3cda38c",
|
||
CatalogHash: "abcdef",
|
||
CatalogCount: 28,
|
||
CompletedAt: "2026-07-10T11:00:00Z",
|
||
CurrentSourceCommit: "3cda38c",
|
||
CurrentCatalogHash: "abcdef",
|
||
CurrentCatalogCount: 28,
|
||
}
|
||
strict := dashboardStrictAcceptanceGate(payload, true, freshness)
|
||
business := dashboardBusinessAcceptanceGate(payload, true, freshness)
|
||
coverage := dashboardOperatorCoverageGate(payload, true, freshness)
|
||
if strict.Status != "passed" || business.Status != "passed" || coverage.Status != "passed" {
|
||
t.Fatalf("expected fresh gates to pass: strict=%#v business=%#v coverage=%#v", strict, business, coverage)
|
||
}
|
||
|
||
freshness.Status = "stale"
|
||
freshness.StaleReason = "catalog_hash_mismatch"
|
||
strict = dashboardStrictAcceptanceGate(payload, true, freshness)
|
||
business = dashboardBusinessAcceptanceGate(payload, true, freshness)
|
||
coverage = dashboardOperatorCoverageGate(payload, true, freshness)
|
||
if strict.Status != "unknown" || business.Status != "unknown" || coverage.Status != "unknown" {
|
||
t.Fatalf("stale artifact must not remain passed or blocked: strict=%#v business=%#v coverage=%#v", strict, business, coverage)
|
||
}
|
||
}
|
||
|
||
func TestWorkflowDefinitionReleaseLocksUpdatesAndCreatesNewVersion(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "输入契约项目", Description: "workflow inputs"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
||
Name: "Artifact BOS", Provider: domain.StorageProviderBOS, Endpoint: "https://bos.example.test", AccessKeyID: "ak", SecretAccessKey: "sk",
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create storage connection: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID, Name: "输入契约", Description: "release", Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{{ID: "source", Type: "operator", Data: domain.WorkflowNodeData{
|
||
Label: "Source", Inputs: []domain.PortDefinition{{ID: "config", Name: "Config", Type: domain.PortTypeJSON, Required: true}},
|
||
}}},
|
||
WorkflowInputs: []domain.WorkflowInputDefinition{{
|
||
ID: "config", Name: "Config", Type: domain.PortTypeJSON, Required: true,
|
||
Bindings: []domain.WorkflowInputTarget{{NodeID: "source", TargetPortID: "config"}},
|
||
}},
|
||
ArtifactStorage: &domain.ArtifactStoragePolicy{ConnectionID: connection.ID, Bucket: "robot-data"},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
handler := NewHandler(dataStore, nil)
|
||
release := httptest.NewRecorder()
|
||
handler.ServeHTTP(release, httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/release", nil))
|
||
if release.Code != http.StatusOK || !strings.Contains(release.Body.String(), `"definitionState":"released"`) {
|
||
t.Fatalf("release workflow: status=%d body=%s", release.Code, release.Body.String())
|
||
}
|
||
update := httptest.NewRecorder()
|
||
handler.ServeHTTP(update, httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID, strings.NewReader(`{"name":"mutated"}`)))
|
||
if update.Code != http.StatusConflict || !strings.Contains(update.Body.String(), "workflow_definition_locked") {
|
||
t.Fatalf("released update should be locked: status=%d body=%s", update.Code, update.Body.String())
|
||
}
|
||
version := httptest.NewRecorder()
|
||
handler.ServeHTTP(version, httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/versions", nil))
|
||
if version.Code != http.StatusCreated || !strings.Contains(version.Body.String(), `"definitionVersion":2`) || !strings.Contains(version.Body.String(), `"definitionState":"draft"`) {
|
||
t.Fatalf("create workflow version: status=%d body=%s", version.Code, version.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestWorkflowRunAcceptsAndPersistsInlineRootInput(t *testing.T) {
|
||
ctx := context.Background()
|
||
dataStore := store.NewMemoryStore()
|
||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Run Input", Description: "inline"})
|
||
if err != nil {
|
||
t.Fatalf("create project: %v", err)
|
||
}
|
||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||
ProjectID: project.ID, Name: "Inline Run", Description: "inline", Type: domain.WorkflowTypeBatch,
|
||
Nodes: []domain.WorkflowNode{{ID: "source", Type: "operator", Data: domain.WorkflowNodeData{
|
||
Label: "Source", Inputs: []domain.PortDefinition{{ID: "config", Name: "Config", Type: domain.PortTypeJSON, Required: true}},
|
||
}}},
|
||
WorkflowInputs: []domain.WorkflowInputDefinition{{
|
||
ID: "config", Name: "Config", Type: domain.PortTypeJSON, Required: true,
|
||
Bindings: []domain.WorkflowInputTarget{{NodeID: "source", TargetPortID: "config"}},
|
||
}},
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("create workflow: %v", err)
|
||
}
|
||
payload := `{"trigger":"manual","inputs":{"config":{"mode":"inline","type":"json","inline":{"threshold":0.5}}}}`
|
||
recorder := httptest.NewRecorder()
|
||
NewHandler(dataStore, nil).ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", strings.NewReader(payload)))
|
||
if recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("create run: status=%d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
var run domain.RunRecord
|
||
if err := json.Unmarshal(recorder.Body.Bytes(), &run); err != nil {
|
||
t.Fatalf("decode run: %v", err)
|
||
}
|
||
if run.Inputs["config"].Mode != domain.ArtifactTransferModeInline || len(run.ResolvedInputBindings) != 1 || run.ResolvedInputBindings[0].TargetPortID != "config" {
|
||
t.Fatalf("run input snapshot missing: %#v", run)
|
||
}
|
||
}
|