package config import ( "os" "path/filepath" "strings" "testing" ) // validCfg возвращает минимально валидный конфиг поверх Default() с медиа-путями // во временном каталоге (существуют как директории). func validCfg(t *testing.T) *Config { t.Helper() dir := t.TempDir() c := Default() c.QBittorrent.Password = "secret" c.Paths.Downloads = filepath.Join(dir, "downloads") c.Paths.Movies = filepath.Join(dir, "movies") c.Paths.Series = filepath.Join(dir, "series") for _, p := range []string{c.Paths.Downloads, c.Paths.Movies, c.Paths.Series} { if err := os.MkdirAll(p, 0o755); err != nil { t.Fatalf("mkdir %s: %v", p, err) } } // LLM по умолчанию без base_url — секция выключена, api_key не требуется. c.LLM.BaseURL = "" return c } func TestValidate_OK(t *testing.T) { if err := validCfg(t).validate(); err != nil { t.Fatalf("ожидался валидный конфиг, got %v", err) } } // TestValidate_KeylessLocalLLM — keyless-local LLM (задан base_url, пустой // api_key, напр. LM Studio) — валиден: ключ не обязателен. func TestValidate_KeylessLocalLLM(t *testing.T) { c := validCfg(t) c.LLM.BaseURL = "http://host.docker.internal:1234/v1" c.LLM.APIKey = "" if err := c.validate(); err != nil { t.Fatalf("keyless-local LLM должен быть валиден, got %v", err) } } func TestValidate_Errors(t *testing.T) { cases := []struct { name string mutate func(*Config) want string }{ {"empty qbittorrent.url", func(c *Config) { c.QBittorrent.URL = "" }, "qbittorrent.url"}, {"empty qbittorrent.password", func(c *Config) { c.QBittorrent.Password = "" }, "qbittorrent.password"}, {"empty db_path", func(c *Config) { c.Storage.DBPath = "" }, "storage.db_path"}, {"bad llm.type", func(c *Config) { c.LLM.Type = "anthropic" }, "llm.type"}, {"relative movies", func(c *Config) { c.Paths.Movies = "movies" }, "absolute"}, {"traversal series", func(c *Config) { c.Paths.Series = c.Paths.Series + "/../x" }, "clean"}, {"missing downloads", func(c *Config) { c.Paths.Downloads = "/no/such/dir/jellybit" }, "not accessible"}, {"threshold high", func(c *Config) { c.Recognition.AutoConfidenceThreshold = 1.5 }, "auto_confidence_threshold"}, {"negative retries", func(c *Config) { c.LLM.MaxRetries = -1 }, "max_retries"}, {"tmdb enabled no key", func(c *Config) { c.Metadata.TMDB.Enabled = true }, "metadata.tmdb"}, {"tvdb enabled no key", func(c *Config) { c.Metadata.TVDB.Enabled = true }, "metadata.tvdb"}, {"jellyfin enabled no url", func(c *Config) { c.Jellyfin.Enabled = true; c.Jellyfin.URL = "" }, "jellyfin.url"}, {"jellyfin enabled no key", func(c *Config) { c.Jellyfin.Enabled = true; c.Jellyfin.URL = "http://j"; c.Jellyfin.APIKey = "" }, "jellyfin.api_key"}, {"telegram enabled no token", func(c *Config) { c.Telegram.Enabled = true }, "telegram.token"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { c := validCfg(t) tc.mutate(c) err := c.validate() if err == nil || !strings.Contains(err.Error(), tc.want) { t.Fatalf("ожидалась ошибка про %q, got %v", tc.want, err) } }) } }