chore: align forest AI development setup

This commit is contained in:
2026-07-21 20:07:51 +08:00
parent b7de3c28b6
commit d2ef10763c
7 changed files with 150 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -45,5 +46,18 @@ func LoadFromDir(configDir string) (Config, error) {
if cfg.MaxUploadBytes <= 0 {
cfg.MaxUploadBytes = 32 << 20
}
if strings.TrimSpace(cfg.StorageDir) == "" {
return Config{}, fmt.Errorf("storage_dir must not be empty")
}
hasAllowedOrigin := false
for _, origin := range cfg.AllowedOrigins {
if strings.TrimSpace(origin) != "" {
hasAllowedOrigin = true
break
}
}
if !hasAllowedOrigin {
return Config{}, fmt.Errorf("allowed_origins must include at least one origin")
}
return cfg, nil
}

View File

@@ -3,11 +3,38 @@ package config
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestDevelopmentConfigMatchesLocalWorkspaceContract(t *testing.T) {
t.Setenv("SENLIN_APP_MODE", "dev")
cfg, err := LoadFromDir(filepath.Join("..", "..", "etc"))
require.NoError(t, err)
require.Equal(t, "development", cfg.Env)
require.Equal(t, "9150", cfg.Port)
require.Equal(t, "postgres://agent:agent@localhost:5432/agent?sslmode=disable", cfg.DSN)
require.NotEmpty(t, strings.TrimSpace(cfg.StorageDir))
require.Equal(t, int64(32<<20), cfg.MaxUploadBytes)
require.ElementsMatch(t, []string{
"http://localhost:5173",
"http://127.0.0.1:5173",
"http://localhost:4173",
"http://127.0.0.1:4173",
"http://localhost:4174",
"http://127.0.0.1:4174",
"http://localhost:4175",
"http://127.0.0.1:4175",
"http://localhost:4176",
"http://127.0.0.1:4176",
"http://tauri.localhost",
}, cfg.AllowedOrigins)
}
func TestLoadFromDirDefaultsToDevYAML(t *testing.T) {
configDir := t.TempDir()
writeConfig(t, configDir, "agent.dev.yaml", "development", "18080", "postgres://dev", "./dev-files", "dev-auth", "dev-system", "dev-ai")
@@ -47,6 +74,32 @@ func TestLoadFromDirUsesSENLINAppMode(t *testing.T) {
require.Equal(t, []string{"https://workbench.example.com"}, cfg.AllowedOrigins)
}
func TestLoadFromDirRejectsMissingStorageDir(t *testing.T) {
configDir := t.TempDir()
writeConfig(t, configDir, "agent.dev.yaml", "development", "9150", "postgres://agent", "", "dev-auth", "", "dev-ai")
t.Setenv("SENLIN_APP_MODE", "dev")
_, err := LoadFromDir(configDir)
require.ErrorContains(t, err, "storage_dir")
}
func TestLoadFromDirRejectsMissingAllowedOrigins(t *testing.T) {
configDir := t.TempDir()
content := []byte("env: development\n" +
"port: \"9150\"\n" +
"dsn: \"postgres://agent\"\n" +
"storage_dir: \"./data/files\"\n" +
"auth_secret: \"dev-auth\"\n" +
"ai_key_encryption_secret: \"dev-ai\"\n")
require.NoError(t, os.WriteFile(filepath.Join(configDir, "agent.dev.yaml"), content, 0o600))
t.Setenv("SENLIN_APP_MODE", "dev")
_, err := LoadFromDir(configDir)
require.ErrorContains(t, err, "allowed_origins")
}
func writeConfig(t *testing.T, dir string, name string, env string, port string, databaseURL string, storageDir string, authSecret string, systemAIKey string, aiKeySecret string) {
t.Helper()
allowedOrigins := " - http://localhost:5173\n - http://tauri.localhost\n"