byteclaw/internal/config/config_test.go

56 lines
1.5 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoadYAMLAndEnv(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "c.yaml")
os.WriteFile(p, []byte("auth_token: aaaaaaaaaaaaaaaaaaaa\nllm:\n model: llama3\n"), 0o600)
t.Setenv("BYTECLAW_LLM_BASE_URL", "http://localhost:11434/v1")
t.Setenv("BYTECLAW_ALLOWED_ORIGINS", "https://a.example, https://b.example")
c, err := Load(p)
if err != nil {
t.Fatal(err)
}
if c.LLM.Model != "llama3" || c.LLM.BaseURL != "http://localhost:11434/v1" {
t.Fatalf("unexpected llm cfg %+v", c.LLM)
}
if len(c.AllowedOrigins) != 2 || c.AllowedOrigins[1] != "https://b.example" {
t.Fatalf("origins %v", c.AllowedOrigins)
}
if c.Listen != "127.0.0.1:8787" {
t.Fatalf("default listen changed: %s", c.Listen)
}
}
func TestLoadJSON(t *testing.T) {
p := filepath.Join(t.TempDir(), "c.json")
os.WriteFile(p, []byte(`{"auth_token":"bbbbbbbbbbbbbbbbbbbb","listen":":9000"}`), 0o600)
c, err := Load(p)
if err != nil || c.Listen != ":9000" {
t.Fatalf("%v %v", c.Listen, err)
}
}
func TestValidateShortToken(t *testing.T) {
t.Setenv("BYTECLAW_AUTH_TOKEN", "short")
if _, err := Load(""); err == nil {
t.Fatal("expected error for short token")
}
t.Setenv("BYTECLAW_AUTH_TOKEN", "")
c, err := Load("")
if err != nil {
t.Fatalf("token is optional now: %v", err)
}
if !c.RegistrationAllowed() {
t.Fatal("registration must default to true")
}
t.Setenv("BYTECLAW_ALLOW_REGISTRATION", "false")
if c, _ = Load(""); c.RegistrationAllowed() {
t.Fatal("env must disable registration")
}
}