package llm
|
|
import (
|
"os"
|
"path/filepath"
|
"strings"
|
"testing"
|
)
|
|
// TestLoadConfigDefaultExpandsTilde 验证无配置文件时(走 getDefaultConfig 分支)
|
// 返回的路径已经过 expandPath 展开,不含未展开的 ~
|
func TestLoadConfigDefaultExpandsTilde(t *testing.T) {
|
home := t.TempDir()
|
t.Setenv("HOME", home)
|
|
// 确保该 HOME 下没有 .kb-cli/config.yaml
|
if _, err := os.Stat(filepath.Join(home, ".kb-cli", "config.yaml")); !os.IsNotExist(err) {
|
t.Fatalf("测试前置条件不满足: %s 下不应存在 config.yaml", home)
|
}
|
|
cfg := LoadConfig()
|
|
homedir, _ := os.UserHomeDir()
|
for name, p := range map[string]string{
|
"VaultPath": cfg.KnowledgeBase.VaultPath,
|
"DBPath": cfg.KnowledgeBase.DBPath,
|
} {
|
if strings.HasPrefix(p, "~/") {
|
t.Errorf("%s 含未展开的 ~: %q", name, p)
|
}
|
if !filepath.IsAbs(p) {
|
t.Errorf("%s 不是绝对路径: %q", name, p)
|
}
|
}
|
// APIBase 是 URL,仅要求不含未展开的 ~
|
if strings.Contains(cfg.LLM.Primary.APIBase, "~/") {
|
t.Errorf("APIBase 含未展开的 ~: %q", cfg.LLM.Primary.APIBase)
|
}
|
|
if want := filepath.Join(homedir, ".cache", "kb-cli", "kb.db"); cfg.KnowledgeBase.DBPath != want {
|
t.Errorf("DBPath = %q, want %q", cfg.KnowledgeBase.DBPath, want)
|
}
|
}
|