forked from longtao.wu/droboticflow
Same-project saves queued behind one project lock. Add default-off record stripes while deletion, authorization and claim operations retain exclusive protection across all stripes. Require a complete barrier rollout before striped activation. Verify local Mongo deletion, revocation, crash recovery and two 100-user HTTP runs with save p95 below 400ms.
373 lines
16 KiB
Go
373 lines
16 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
var configEnvKeys = []string{
|
|
"DROBOTICFLOW_PROJECT_WRITE_LOCK_MODE",
|
|
"PORT",
|
|
"WORKER_PORT",
|
|
"MONGO_URI",
|
|
"MONGO_DB",
|
|
"MONGO_CONNECT_RETRIES",
|
|
"MONGO_CONNECT_RETRY_DELAY_MS",
|
|
"WORKER_POLL_INTERVAL_MS",
|
|
"WORKER_API_BASE_URL",
|
|
"K8S_NAMESPACE",
|
|
"K8S_IMAGE_PULL_SECRET_NAME",
|
|
"WORKSPACE_PVC_NAME",
|
|
"WORKSPACE_MOUNT_PATH",
|
|
"OPERATOR_RUNNER_IMAGE",
|
|
"OPERATOR_RUNNER_BUILD_COMMIT",
|
|
"OPERATOR_RUNNER_BUILD_TIME",
|
|
"AI_API_BASE_URL",
|
|
"AI_API_KEY",
|
|
"AI_MODEL",
|
|
"BOS_ENDPOINT",
|
|
"DIST_DIR",
|
|
"DROBOTICFLOW_API_TOKEN",
|
|
"OPERATOR_JOB_TTL_SECONDS",
|
|
"DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED",
|
|
"DROBOTICFLOW_QWEN_NODE_CACHE_PVC",
|
|
"DROBOTICFLOW_QWEN_NODE_CACHE_MOUNT_PATH",
|
|
"DROBOTICFLOW_QWEN_NODE_CACHE_NODE_SELECTOR",
|
|
"DROBOTICFLOW_QWEN_NODE_CACHE_STALE_STAGING_SECONDS",
|
|
"DROBOTICFLOW_REQUIRE_SESSION_AUTH",
|
|
"DROBOTICFLOW_SESSION_COOKIE_SECURE",
|
|
"DROBOTICFLOW_TRUSTED_PROXY_CIDRS",
|
|
"OPERATOR_GPU_NODE_SELECTOR",
|
|
"OPERATOR_GPU_TOLERATIONS_JSON",
|
|
"OPERATOR_GPU_RUNTIME_CLASS",
|
|
"CUSTOM_OPERATOR_IMAGE_REGISTRY",
|
|
"CUSTOM_OPERATOR_IMAGE_IMPORTER_IMAGE",
|
|
"CUSTOM_OPERATOR_IMAGE_IMPORT_TIMEOUT_SECONDS",
|
|
"CUSTOM_OPERATOR_IMAGE_ARCHIVE_MAX_MIB",
|
|
"DROBOTICFLOW_BUILD_COMMIT",
|
|
"DROBOTICFLOW_BUILD_TIME",
|
|
"DROBOTICFLOW_IMAGE_BUILD_COMMIT",
|
|
"DROBOTICFLOW_IMAGE_BUILD_TIME",
|
|
"DROBOTICFLOW_IMAGE",
|
|
}
|
|
|
|
func TestProjectWriteLockModeValidation(t *testing.T) {
|
|
for _, value := range []string{"", "legacy", "barrier", "striped"} {
|
|
t.Run("valid-"+value, func(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
setRequiredConfigEnv(t)
|
|
t.Setenv("DROBOTICFLOW_PROJECT_WRITE_LOCK_MODE", value)
|
|
if _, e := LoadValidated(); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
})
|
|
}
|
|
t.Run("typo-blocks-startup", func(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
setRequiredConfigEnv(t)
|
|
t.Setenv("DROBOTICFLOW_PROJECT_WRITE_LOCK_MODE", "stripe")
|
|
if _, e := LoadValidated(); e == nil {
|
|
t.Fatal("invalid write lock protocol accepted")
|
|
}
|
|
})
|
|
}
|
|
|
|
func clearConfigEnv(t *testing.T) {
|
|
t.Helper()
|
|
for _, key := range configEnvKeys {
|
|
t.Setenv(key, "")
|
|
}
|
|
}
|
|
|
|
func setRequiredConfigEnv(t *testing.T) {
|
|
t.Helper()
|
|
t.Setenv("MONGO_URI", "mongodb://dev-mongo.example:27017")
|
|
}
|
|
|
|
func TestLoadValidatedRequiresMongoURIWhenUnset(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
|
|
_, err := LoadValidated()
|
|
if err == nil {
|
|
t.Fatalf("expected missing MONGO_URI to fail")
|
|
}
|
|
if !strings.Contains(err.Error(), "MONGO_URI is required and must be set explicitly") {
|
|
t.Fatalf("expected required mongo uri error, got %q", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatedUsesDefaultsForUnsetEnv(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
setRequiredConfigEnv(t)
|
|
|
|
cfg, err := LoadValidated()
|
|
if err != nil {
|
|
t.Fatalf("LoadValidated defaults: %v", err)
|
|
}
|
|
if cfg.Port != 3001 {
|
|
t.Fatalf("default api port = %d", cfg.Port)
|
|
}
|
|
if cfg.WorkerPort != 3002 {
|
|
t.Fatalf("default worker port = %d", cfg.WorkerPort)
|
|
}
|
|
if cfg.MongoConnectRetries != 30 {
|
|
t.Fatalf("default mongo retries = %d", cfg.MongoConnectRetries)
|
|
}
|
|
if cfg.MongoConnectRetryDelayMS != 2000 {
|
|
t.Fatalf("default mongo retry delay = %d", cfg.MongoConnectRetryDelayMS)
|
|
}
|
|
if cfg.PollIntervalMS != 2000 {
|
|
t.Fatalf("default worker poll interval = %d", cfg.PollIntervalMS)
|
|
}
|
|
if cfg.OperatorJobTTLSeconds != 86400 {
|
|
t.Fatalf("default operator job ttl = %d", cfg.OperatorJobTTLSeconds)
|
|
}
|
|
if cfg.QwenNodeCacheEnabled || cfg.QwenNodeCachePVCName != "" || cfg.QwenNodeCacheMountPath != "/qwen-node-cache" || cfg.QwenNodeCacheNodeSelector != "" || cfg.QwenNodeCacheStaleStagingSeconds != 86400 {
|
|
t.Fatalf("default qwen node cache config should be disabled, got %#v", cfg)
|
|
}
|
|
if cfg.CustomOperatorImageRegistry != "" {
|
|
t.Fatalf("default custom operator image registry should be empty, got %q", cfg.CustomOperatorImageRegistry)
|
|
}
|
|
if cfg.CustomOperatorImageImporterImage != "quay.io/skopeo/stable:v1.16.1" {
|
|
t.Fatalf("default custom operator importer image = %q", cfg.CustomOperatorImageImporterImage)
|
|
}
|
|
if cfg.CustomOperatorImageImportTimeoutSeconds != 1800 || cfg.CustomOperatorImageArchiveMaxMiB != 8192 {
|
|
t.Fatalf("default custom operator import config = timeout %d maxMiB %d", cfg.CustomOperatorImageImportTimeoutSeconds, cfg.CustomOperatorImageArchiveMaxMiB)
|
|
}
|
|
if cfg.RequireSessionAuth {
|
|
t.Fatalf("default session auth should be false")
|
|
}
|
|
if cfg.SessionCookieSecure {
|
|
t.Fatalf("default secure cookie should be false")
|
|
}
|
|
if cfg.GlobalBOSEndpoint != "" {
|
|
t.Fatalf("global BOS endpoint should be empty when unset, got %q", cfg.GlobalBOSEndpoint)
|
|
}
|
|
if cfg.OperatorRunnerBuildCommit != "" || cfg.OperatorRunnerBuildTime != "" {
|
|
t.Fatalf("runner build metadata should be empty when unset, got commit %q time %q", cfg.OperatorRunnerBuildCommit, cfg.OperatorRunnerBuildTime)
|
|
}
|
|
if cfg.AIAPIBaseURL != "" || cfg.AIModel != "" || cfg.AIAPIKey != "" {
|
|
t.Fatalf("AI config should be empty when unset, got base %q model %q key %q", cfg.AIAPIBaseURL, cfg.AIModel, cfg.AIAPIKey)
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatedAcceptsExplicitStartupEnv(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
t.Setenv("PORT", "3101")
|
|
t.Setenv("WORKER_PORT", "3102")
|
|
t.Setenv("MONGO_URI", " mongodb://mongo.example:27017 ")
|
|
t.Setenv("MONGO_DB", " custom-db ")
|
|
t.Setenv("MONGO_CONNECT_RETRIES", "5")
|
|
t.Setenv("MONGO_CONNECT_RETRY_DELAY_MS", "250")
|
|
t.Setenv("WORKER_POLL_INTERVAL_MS", "750")
|
|
t.Setenv("WORKER_API_BASE_URL", " http://worker.example:3002 ")
|
|
t.Setenv("K8S_NAMESPACE", " custom-namespace ")
|
|
t.Setenv("K8S_IMAGE_PULL_SECRET_NAME", " registry-secret ")
|
|
t.Setenv("WORKSPACE_PVC_NAME", " workspace-pvc ")
|
|
t.Setenv("WORKSPACE_MOUNT_PATH", " /mnt/workspace ")
|
|
t.Setenv("OPERATOR_RUNNER_IMAGE", " ccr.example/runner:20260617 ")
|
|
t.Setenv("OPERATOR_RUNNER_BUILD_COMMIT", " runner-commit ")
|
|
t.Setenv("OPERATOR_RUNNER_BUILD_TIME", " 2026-07-10T11:58:00Z ")
|
|
t.Setenv("AI_API_BASE_URL", " https://ai.example/v1 ")
|
|
t.Setenv("AI_API_KEY", " ai-secret ")
|
|
t.Setenv("AI_MODEL", " model-x ")
|
|
t.Setenv("BOS_ENDPOINT", " https://gz.bcebos.com ")
|
|
t.Setenv("DIST_DIR", " /srv/droboticflow/dist ")
|
|
t.Setenv("DROBOTICFLOW_API_TOKEN", " api-secret ")
|
|
t.Setenv("OPERATOR_JOB_TTL_SECONDS", "0")
|
|
t.Setenv("DROBOTICFLOW_QWEN_NODE_CACHE_ENABLED", "true")
|
|
t.Setenv("DROBOTICFLOW_QWEN_NODE_CACHE_PVC", "qwen-node-cache")
|
|
t.Setenv("DROBOTICFLOW_QWEN_NODE_CACHE_MOUNT_PATH", " /qwen-node-cache ")
|
|
t.Setenv("DROBOTICFLOW_QWEN_NODE_CACHE_NODE_SELECTOR", " kubernetes.io/hostname=192.168.80.136 ")
|
|
t.Setenv("DROBOTICFLOW_QWEN_NODE_CACHE_STALE_STAGING_SECONDS", "3600")
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "yes")
|
|
t.Setenv("DROBOTICFLOW_SESSION_COOKIE_SECURE", "on")
|
|
t.Setenv("DROBOTICFLOW_TRUSTED_PROXY_CIDRS", " 10.0.0.0/8 ")
|
|
t.Setenv("OPERATOR_GPU_NODE_SELECTOR", " accelerator=nvidia ")
|
|
t.Setenv("OPERATOR_GPU_TOLERATIONS_JSON", " [{\"key\":\"nvidia.com/gpu\"}] ")
|
|
t.Setenv("OPERATOR_GPU_RUNTIME_CLASS", " nvidia ")
|
|
t.Setenv("CUSTOM_OPERATOR_IMAGE_REGISTRY", " ccr.example/dataflow/custom-operators/ ")
|
|
t.Setenv("CUSTOM_OPERATOR_IMAGE_IMPORTER_IMAGE", " ccr.example/tools/skopeo:1 ")
|
|
t.Setenv("CUSTOM_OPERATOR_IMAGE_IMPORT_TIMEOUT_SECONDS", "900")
|
|
t.Setenv("CUSTOM_OPERATOR_IMAGE_ARCHIVE_MAX_MIB", "1024")
|
|
t.Setenv("DROBOTICFLOW_BUILD_COMMIT", " 3cda38c ")
|
|
t.Setenv("DROBOTICFLOW_BUILD_TIME", " 2026-07-10T12:00:00Z ")
|
|
t.Setenv("DROBOTICFLOW_IMAGE_BUILD_COMMIT", " image-commit ")
|
|
t.Setenv("DROBOTICFLOW_IMAGE_BUILD_TIME", " 2026-07-10T11:59:00Z ")
|
|
t.Setenv("DROBOTICFLOW_IMAGE", " ccr.example/droboticflow-api:3cda38c ")
|
|
|
|
cfg, err := LoadValidated()
|
|
if err != nil {
|
|
t.Fatalf("LoadValidated explicit env: %v", err)
|
|
}
|
|
if cfg.Port != 3101 || cfg.WorkerPort != 3102 {
|
|
t.Fatalf("ports = api %d worker %d", cfg.Port, cfg.WorkerPort)
|
|
}
|
|
if cfg.MongoConnectRetries != 5 || cfg.MongoConnectRetryDelayMS != 250 || cfg.PollIntervalMS != 750 {
|
|
t.Fatalf("startup timings = retries %d retryDelay %d poll %d", cfg.MongoConnectRetries, cfg.MongoConnectRetryDelayMS, cfg.PollIntervalMS)
|
|
}
|
|
if cfg.MongoURI != "mongodb://mongo.example:27017" || cfg.MongoDB != "custom-db" {
|
|
t.Fatalf("mongo config = uri %q db %q", cfg.MongoURI, cfg.MongoDB)
|
|
}
|
|
if cfg.WorkerAPIBaseURL != "http://worker.example:3002" || cfg.K8sNamespace != "custom-namespace" || cfg.K8sImagePullSecretName != "registry-secret" {
|
|
t.Fatalf("cluster config = worker %q namespace %q pullSecret %q", cfg.WorkerAPIBaseURL, cfg.K8sNamespace, cfg.K8sImagePullSecretName)
|
|
}
|
|
if cfg.WorkspacePVCName != "workspace-pvc" || cfg.WorkspaceMountPath != "/mnt/workspace" || cfg.OperatorRunnerImage != "ccr.example/runner:20260617" {
|
|
t.Fatalf("workspace/operator config = pvc %q mount %q image %q", cfg.WorkspacePVCName, cfg.WorkspaceMountPath, cfg.OperatorRunnerImage)
|
|
}
|
|
if cfg.OperatorRunnerBuildCommit != "runner-commit" || cfg.OperatorRunnerBuildTime != "2026-07-10T11:58:00Z" {
|
|
t.Fatalf("runner build metadata = commit %q time %q", cfg.OperatorRunnerBuildCommit, cfg.OperatorRunnerBuildTime)
|
|
}
|
|
if cfg.AIAPIBaseURL != "https://ai.example/v1" || cfg.AIAPIKey != "ai-secret" || cfg.AIModel != "model-x" || cfg.DistDir != "/srv/droboticflow/dist" {
|
|
t.Fatalf("ai/dist config = base %q key %q model %q dist %q", cfg.AIAPIBaseURL, cfg.AIAPIKey, cfg.AIModel, cfg.DistDir)
|
|
}
|
|
if cfg.GlobalBOSEndpoint != "https://gz.bcebos.com" {
|
|
t.Fatalf("global bos endpoint = %q", cfg.GlobalBOSEndpoint)
|
|
}
|
|
if cfg.APIToken != "api-secret" || cfg.TrustedProxyCIDRs != "10.0.0.0/8" {
|
|
t.Fatalf("security config = api token %q proxies %q", cfg.APIToken, cfg.TrustedProxyCIDRs)
|
|
}
|
|
if cfg.OperatorGPUNodeSelector != "accelerator=nvidia" || cfg.OperatorGPUTolerations != "[{\"key\":\"nvidia.com/gpu\"}]" || cfg.OperatorGPURuntimeClass != "nvidia" {
|
|
t.Fatalf("gpu config = selector %q tolerations %q runtimeClass %q", cfg.OperatorGPUNodeSelector, cfg.OperatorGPUTolerations, cfg.OperatorGPURuntimeClass)
|
|
}
|
|
if cfg.OperatorJobTTLSeconds != 0 {
|
|
t.Fatalf("operator job ttl = %d", cfg.OperatorJobTTLSeconds)
|
|
}
|
|
if !cfg.QwenNodeCacheEnabled || cfg.QwenNodeCachePVCName != "qwen-node-cache" || cfg.QwenNodeCacheMountPath != "/qwen-node-cache" || cfg.QwenNodeCacheNodeSelector != "kubernetes.io/hostname=192.168.80.136" || cfg.QwenNodeCacheStaleStagingSeconds != 3600 {
|
|
t.Fatalf("qwen node cache config = %#v", cfg)
|
|
}
|
|
if cfg.CustomOperatorImageRegistry != "ccr.example/dataflow/custom-operators" || cfg.CustomOperatorImageImporterImage != "ccr.example/tools/skopeo:1" {
|
|
t.Fatalf("custom operator image import refs = registry %q importer %q", cfg.CustomOperatorImageRegistry, cfg.CustomOperatorImageImporterImage)
|
|
}
|
|
if cfg.CustomOperatorImageImportTimeoutSeconds != 900 || cfg.CustomOperatorImageArchiveMaxMiB != 1024 {
|
|
t.Fatalf("custom operator image import runtime = timeout %d maxMiB %d", cfg.CustomOperatorImageImportTimeoutSeconds, cfg.CustomOperatorImageArchiveMaxMiB)
|
|
}
|
|
if !cfg.RequireSessionAuth || !cfg.SessionCookieSecure {
|
|
t.Fatalf("expected explicit boolean envs to enable auth and secure cookie")
|
|
}
|
|
if cfg.BuildCommit != "image-commit" || cfg.BuildTime != "2026-07-10T11:59:00Z" || cfg.Image != "ccr.example/droboticflow-api:3cda38c" {
|
|
t.Fatalf("build metadata = commit %q time %q image %q", cfg.BuildCommit, cfg.BuildTime, cfg.Image)
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatedTreatsWhitespaceStringEnvAsUnset(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
setRequiredConfigEnv(t)
|
|
for _, key := range []string{
|
|
"MONGO_DB",
|
|
"WORKER_API_BASE_URL",
|
|
"K8S_NAMESPACE",
|
|
"K8S_IMAGE_PULL_SECRET_NAME",
|
|
"WORKSPACE_MOUNT_PATH",
|
|
"AI_API_BASE_URL",
|
|
"AI_MODEL",
|
|
"BOS_ENDPOINT",
|
|
"DIST_DIR",
|
|
} {
|
|
t.Setenv(key, " \t ")
|
|
}
|
|
|
|
cfg, err := LoadValidated()
|
|
if err != nil {
|
|
t.Fatalf("LoadValidated whitespace string env: %v", err)
|
|
}
|
|
if cfg.MongoURI != "mongodb://dev-mongo.example:27017" || cfg.MongoDB != "droboticflow" {
|
|
t.Fatalf("mongo defaults = uri %q db %q", cfg.MongoURI, cfg.MongoDB)
|
|
}
|
|
if cfg.WorkerAPIBaseURL != "http://droboticflow-worker:3002" || cfg.K8sNamespace != "droboticflow" || cfg.K8sImagePullSecretName != "droboticflow-registry" {
|
|
t.Fatalf("cluster defaults = worker %q namespace %q pullSecret %q", cfg.WorkerAPIBaseURL, cfg.K8sNamespace, cfg.K8sImagePullSecretName)
|
|
}
|
|
if cfg.WorkspaceMountPath != "/workspace" || cfg.AIAPIBaseURL != "" || cfg.AIModel != "" || cfg.GlobalBOSEndpoint != "" {
|
|
t.Fatalf("runtime defaults = mount %q aiBase %q aiModel %q bos %q", cfg.WorkspaceMountPath, cfg.AIAPIBaseURL, cfg.AIModel, cfg.GlobalBOSEndpoint)
|
|
}
|
|
if !strings.HasSuffix(cfg.DistDir, "dist") {
|
|
t.Fatalf("dist default = %q", cfg.DistDir)
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatedRejectsInvalidStartupEnv(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
key string
|
|
value string
|
|
message string
|
|
}{
|
|
{name: "non integer port", key: "PORT", value: "abc", message: "PORT must be an integer"},
|
|
{name: "port below range", key: "PORT", value: "0", message: "PORT must be greater than or equal to 1"},
|
|
{name: "port above range", key: "WORKER_PORT", value: "65536", message: "WORKER_PORT must be less than or equal to 65535"},
|
|
{name: "zero mongo retries", key: "MONGO_CONNECT_RETRIES", value: "0", message: "MONGO_CONNECT_RETRIES must be greater than or equal to 1"},
|
|
{name: "zero mongo retry delay", key: "MONGO_CONNECT_RETRY_DELAY_MS", value: "0", message: "MONGO_CONNECT_RETRY_DELAY_MS must be greater than or equal to 1"},
|
|
{name: "zero worker poll", key: "WORKER_POLL_INTERVAL_MS", value: "0", message: "WORKER_POLL_INTERVAL_MS must be greater than or equal to 1"},
|
|
{name: "negative ttl", key: "OPERATOR_JOB_TTL_SECONDS", value: "-1", message: "OPERATOR_JOB_TTL_SECONDS must be greater than or equal to 0"},
|
|
{name: "short custom operator import timeout", key: "CUSTOM_OPERATOR_IMAGE_IMPORT_TIMEOUT_SECONDS", value: "1", message: "CUSTOM_OPERATOR_IMAGE_IMPORT_TIMEOUT_SECONDS must be greater than or equal to 60"},
|
|
{name: "zero custom operator archive max", key: "CUSTOM_OPERATOR_IMAGE_ARCHIVE_MAX_MIB", value: "0", message: "CUSTOM_OPERATOR_IMAGE_ARCHIVE_MAX_MIB must be greater than or equal to 1"},
|
|
{name: "invalid session auth bool", key: "DROBOTICFLOW_REQUIRE_SESSION_AUTH", value: "maybe", message: "DROBOTICFLOW_REQUIRE_SESSION_AUTH must be a boolean"},
|
|
{name: "invalid secure cookie bool", key: "DROBOTICFLOW_SESSION_COOKIE_SECURE", value: "maybe", message: "DROBOTICFLOW_SESSION_COOKIE_SECURE must be a boolean"},
|
|
{name: "invalid worker api base url", key: "WORKER_API_BASE_URL", value: "worker-service:3002", message: "WORKER_API_BASE_URL must be an absolute http(s) URL"},
|
|
{name: "invalid ai api base url", key: "AI_API_BASE_URL", value: "https://ai.example/with space", message: "AI_API_BASE_URL must be an absolute http(s) URL"},
|
|
{name: "invalid ai model", key: "AI_MODEL", value: "model x", message: "AI_MODEL must not contain whitespace"},
|
|
{name: "invalid bos endpoint", key: "BOS_ENDPOINT", value: "bj.bcebos.com", message: "BOS_ENDPOINT must be an absolute http(s) URL"},
|
|
{name: "blank mongo uri", key: "MONGO_URI", value: " \t ", message: "MONGO_URI is required and must be set explicitly"},
|
|
{name: "mongo uri placeholder", key: "MONGO_URI", value: "mongodb://<user>:<password>@<host>:27017/", message: "MONGO_URI must not contain placeholder markers"},
|
|
{name: "mongo uri wrong scheme", key: "MONGO_URI", value: "http://mongo.example:27017", message: "MONGO_URI must be a mongodb:// or mongodb+srv:// URI"},
|
|
{name: "mongo uri missing host", key: "MONGO_URI", value: "mongodb:///droboticflow", message: "MONGO_URI must be a mongodb:// or mongodb+srv:// URI"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
setRequiredConfigEnv(t)
|
|
t.Setenv(tc.key, tc.value)
|
|
|
|
_, err := LoadValidated()
|
|
if err == nil {
|
|
t.Fatalf("expected %s=%q to fail", tc.key, tc.value)
|
|
}
|
|
if !strings.Contains(err.Error(), tc.message) {
|
|
t.Fatalf("expected error %q, got %q", tc.message, err.Error())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadValidatedRequiresExplicitAIBaseAndModelWithAPIKey(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
env map[string]string
|
|
message string
|
|
}{
|
|
{
|
|
name: "missing base url",
|
|
env: map[string]string{
|
|
"AI_API_KEY": "secret",
|
|
"AI_MODEL": "model-x",
|
|
},
|
|
message: "AI_API_BASE_URL is required when AI_API_KEY is configured",
|
|
},
|
|
{
|
|
name: "missing model",
|
|
env: map[string]string{
|
|
"AI_API_KEY": "secret",
|
|
"AI_API_BASE_URL": "https://ai.example/v1",
|
|
},
|
|
message: "AI_MODEL is required when AI_API_KEY is configured",
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
clearConfigEnv(t)
|
|
setRequiredConfigEnv(t)
|
|
for key, value := range tc.env {
|
|
t.Setenv(key, value)
|
|
}
|
|
|
|
_, err := LoadValidated()
|
|
if err == nil {
|
|
t.Fatalf("expected AI config validation to fail")
|
|
}
|
|
if !strings.Contains(err.Error(), tc.message) {
|
|
t.Fatalf("expected error %q, got %q", tc.message, err.Error())
|
|
}
|
|
})
|
|
}
|
|
}
|