Skip to main content

fetchium_core/
config.rs

1//! Configuration system for Fetchium (PRD §11).
2//!
3//! Layered: defaults → config file → env vars → CLI args.
4//! Config file: `~/.fetchium/config.toml`
5
6use crate::ai::providers::ProvidersConfig;
7use crate::types::{OutputFormat, PdsTier, ResourceTier};
8use serde::{Deserialize, Serialize};
9use std::path::PathBuf;
10
11/// Top-level configuration.
12#[derive(Debug, Clone, Serialize, Deserialize, Default)]
13#[serde(default)]
14pub struct FetchiumConfig {
15    pub general: GeneralConfig,
16    pub search: SearchConfig,
17    pub fetch: FetchConfig,
18    pub cache: CacheConfig,
19    pub ai: AiConfig,
20    pub output: OutputConfig,
21    pub ranking: RankingConfig,
22    pub youtube: YouTubeConfig,
23    pub social: SocialConfig,
24    pub headless: HeadlessConfig,
25    pub proxy: ProxyConfig,
26    pub dataimpulse: DataImpulseConfig,
27}
28
29/// DataImpulse residential proxy configuration.
30///
31/// Country targeting: `{username}__cr.{cc}:{password}@{host}:{port}`
32/// Only activates for scraper backends blocked by datacenter IPs (Google, DDG, Bing, Brave).
33/// API backends (Serper, Exa, Tavily) are never routed through DataImpulse.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(default)]
36pub struct DataImpulseConfig {
37    /// Enable DataImpulse residential proxy routing.
38    pub enabled: bool,
39    /// DataImpulse account username.
40    pub username: String,
41    /// DataImpulse account password.
42    pub password: String,
43    /// Gateway host (default: gw.dataimpulse.com).
44    pub host: String,
45    /// Gateway port (default: 823).
46    pub port: u16,
47    /// Domains to route through DataImpulse (empty = use built-in blocked-domain list).
48    #[serde(default)]
49    pub proxy_domains: Vec<String>,
50}
51
52impl Default for DataImpulseConfig {
53    fn default() -> Self {
54        Self {
55            enabled: false,
56            username: String::new(),
57            password: String::new(),
58            host: "gw.dataimpulse.com".into(),
59            port: 823,
60            proxy_domains: Vec::new(),
61        }
62    }
63}
64
65/// Proxy rotation configuration.
66#[derive(Debug, Clone, Serialize, Deserialize)]
67#[serde(default)]
68pub struct ProxyConfig {
69    /// Enable proxy rotation for search backends.
70    pub enabled: bool,
71    /// Path to proxy list file (default: ~/.fetchium/proxies.txt).
72    pub proxy_file: Option<PathBuf>,
73    /// Proxy protocol (http, https, socks5).
74    pub protocol: String,
75    /// Domains that should use proxies (empty = all search backends).
76    #[serde(default)]
77    pub proxy_domains: Vec<String>,
78    /// Domains that should never use proxies.
79    #[serde(default)]
80    pub bypass_domains: Vec<String>,
81}
82
83impl Default for ProxyConfig {
84    fn default() -> Self {
85        Self {
86            enabled: false,
87            proxy_file: None,
88            protocol: "http".into(),
89            proxy_domains: Vec::new(),
90            bypass_domains: vec!["localhost".into(), "127.0.0.1".into()],
91        }
92    }
93}
94
95/// Headless browser configuration.
96///
97/// Stored as `[headless]` in config.toml. Controls Chrome binary resolution.
98#[derive(Debug, Clone, Serialize, Deserialize, Default)]
99#[serde(default)]
100pub struct HeadlessConfig {
101    /// Override path to Chrome/Chromium binary.
102    ///
103    /// Priority: `FETCHIUM_CHROME_PATH` env var > this value >
104    /// fetchium-managed (`~/.fetchium/chromium/`) > system Chrome/Chromium.
105    pub chrome_path: Option<PathBuf>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[serde(default)]
110pub struct GeneralConfig {
111    /// Default number of results to return.
112    pub max_results: u32,
113    /// Default output format.
114    pub format: OutputFormat,
115    /// Verbose logging.
116    pub verbose: bool,
117    /// Data directory (default: ~/.fetchium).
118    pub data_dir: Option<PathBuf>,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
122#[serde(default)]
123pub struct SearchConfig {
124    /// Enabled backends.
125    pub backends: Vec<String>,
126    /// Tavily API key.
127    pub tavily_api_key: Option<String>,
128    /// Tavily API keys for rotation.
129    pub tavily_api_keys: Vec<String>,
130    /// Serper API key.
131    pub serper_api_key: Option<String>,
132    /// Serper API keys for rotation.
133    pub serper_api_keys: Vec<String>,
134    /// Exa API key.
135    pub exa_api_key: Option<String>,
136    /// Exa API keys for rotation.
137    pub exa_api_keys: Vec<String>,
138    /// Firecrawl API key.
139    pub firecrawl_api_key: Option<String>,
140    /// Firecrawl API keys for rotation.
141    pub firecrawl_api_keys: Vec<String>,
142    /// Default token budget for agent commands.
143    pub default_budget: u32,
144    /// Default PDS tier.
145    pub default_tier: PdsTier,
146    /// Maximum concurrent requests.
147    pub max_concurrent: u32,
148    /// Timeout per request in seconds.
149    pub timeout_secs: u64,
150    /// SearXNG instance URL (if configured).
151    pub searxng_url: Option<String>,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize)]
155#[serde(default)]
156pub struct FetchConfig {
157    /// User-Agent string.
158    pub user_agent: String,
159    /// Respect robots.txt.
160    pub respect_robots: bool,
161    /// Maximum page size in bytes.
162    pub max_page_size: u64,
163    /// Request timeout in seconds.
164    pub timeout_secs: u64,
165    /// Maximum redirects to follow.
166    pub max_redirects: u32,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
170#[serde(default)]
171pub struct CacheConfig {
172    /// Enable caching.
173    pub enabled: bool,
174    /// Memory cache max entries.
175    pub memory_max_entries: u64,
176    /// Disk cache max size in MB.
177    pub disk_max_mb: u64,
178    /// Cache TTL in seconds.
179    pub ttl_secs: u64,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
183#[serde(default)]
184pub struct AiConfig {
185    /// Ollama host.
186    pub ollama_host: String,
187    /// Default model (Ollama).
188    pub default_model: String,
189    /// Max tokens for AI responses.
190    pub max_tokens: u32,
191    /// Optional fast model for latency-sensitive tasks (HyDE, intent classification).
192    /// Falls back to `default_model` when unset.
193    pub fast_model: Option<String>,
194    /// Multi-provider configuration block.
195    ///
196    /// Stored as `[ai.providers]` in config.toml. Defines the fallback chain
197    /// and per-provider API keys / model overrides.
198    pub providers: ProvidersConfig,
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize)]
202#[serde(default)]
203pub struct OutputConfig {
204    /// Default output format.
205    pub format: OutputFormat,
206    /// Include source URLs in output.
207    pub include_sources: bool,
208    /// Include confidence scores.
209    pub include_confidence: bool,
210}
211
212/// YouTube Intelligence System configuration.
213#[derive(Debug, Clone, Serialize, Deserialize)]
214#[serde(default)]
215pub struct YouTubeConfig {
216    /// Preferred Invidious instance URLs (tried in order).
217    pub invidious_instances: Vec<String>,
218    /// Preferred Piped API instance URLs (tried in order).
219    pub piped_instances: Vec<String>,
220    /// Maximum videos to analyze in a pipeline run.
221    pub max_videos: usize,
222    /// Per-instance request timeout in seconds.
223    pub timeout_secs: u64,
224    /// Whether to fetch comments by default.
225    pub fetch_comments: bool,
226}
227
228impl Default for YouTubeConfig {
229    fn default() -> Self {
230        Self {
231            invidious_instances: vec![
232                "https://invidious.nerdvpn.de".into(), // verified Feb 2026
233                "https://yewtu.be".into(),             // verified Feb 2026
234            ],
235            // Limit to 2 most reliable Piped instances. Fewer instances =
236            // fewer orphaned retry tasks per video when they're unreachable.
237            // Innertube + oEmbed already guarantee metadata; Piped is bonus.
238            piped_instances: vec![
239                "https://pipedapi.kavin.rocks".into(), // official, most reliable
240                "https://api.piped.yt".into(),         // Germany fallback
241            ],
242            max_videos: 5,
243            timeout_secs: 4, // tight per-source timeout; sources are raced in parallel
244            fetch_comments: false, // comments disabled by default (expensive)
245        }
246    }
247}
248
249/// Social Media Intelligence configuration.
250#[derive(Debug, Clone, Serialize, Deserialize, Default)]
251#[serde(default)]
252pub struct SocialConfig {
253    /// Meta Graph API token for Facebook (`APP_ID|APP_SECRET`).
254    ///
255    /// Get one for free at <https://developers.facebook.com>:
256    /// 1. Create an app (type: "Consumer" or "Business")
257    /// 2. Go to Settings → Basic → copy App ID + App Secret
258    /// 3. Set token as `APP_ID|APP_SECRET`
259    ///
260    /// Without a token, Facebook uses DDG + Open Graph (free, limited data).
261    pub facebook_graph_token: Option<String>,
262}
263
264impl FetchiumConfig {
265    /// Returns the Facebook Graph API token from config or env var.
266    ///
267    /// Checks `FETCHIUM_FACEBOOK_TOKEN`, then the config file value.
268    pub fn facebook_graph_token(&self) -> Option<String> {
269        self.social
270            .facebook_graph_token
271            .clone()
272            .or_else(|| std::env::var("FETCHIUM_FACEBOOK_TOKEN").ok())
273    }
274}
275
276/// Ranking configuration for HyperFusion signal weights and thresholds.
277#[derive(Debug, Clone, Serialize, Deserialize)]
278#[serde(default)]
279pub struct RankingConfig {
280    /// Per-signal weight overrides (signal name → weight in [0.0, 1.0]).
281    /// Example: `{"temporal": 0.3, "authority": 0.2}`
282    #[serde(default)]
283    pub weight_overrides: std::collections::HashMap<String, f64>,
284    /// How much recency matters for temporal scoring (0.0 = ignore, 1.0 = max).
285    #[serde(default = "RankingConfig::default_freshness")]
286    pub freshness_need: f64,
287    /// SimHash Hamming distance threshold for near-duplicate detection (0–64).
288    #[serde(default = "RankingConfig::default_simhash_threshold")]
289    pub simhash_threshold: u32,
290}
291
292impl RankingConfig {
293    fn default_freshness() -> f64 {
294        0.5
295    }
296    fn default_simhash_threshold() -> u32 {
297        6
298    }
299}
300
301impl Default for RankingConfig {
302    fn default() -> Self {
303        Self {
304            weight_overrides: std::collections::HashMap::new(),
305            freshness_need: Self::default_freshness(),
306            simhash_threshold: Self::default_simhash_threshold(),
307        }
308    }
309}
310
311// ─── Defaults ────────────────────────────────────────────────────
312
313// Manual default removed, used #[derive(Default)] on the struct instead.
314
315impl Default for GeneralConfig {
316    fn default() -> Self {
317        Self {
318            max_results: 10,
319            format: OutputFormat::Markdown,
320            verbose: false,
321            data_dir: None,
322        }
323    }
324}
325
326impl Default for SearchConfig {
327    fn default() -> Self {
328        Self {
329            // Default backends: stable API/meta-search sources only.
330            // SearXNG covers Google, Bing, DDG internally — no per-server CAPTCHAs.
331            // Wikipedia/HN/Reddit/SO/Arxiv/GitHub use public APIs (no rate limiting).
332            // Direct Bing/DDG/Google scrapers excluded — they CAPTCHA-block server IPs.
333            backends: vec![
334                "searxng".into(),
335                "wikipedia".into(),
336                "hackernews".into(),
337                "reddit".into(),
338                "stackoverflow".into(),
339                "arxiv".into(),
340                "github".into(),
341            ],
342            tavily_api_key: None,
343            tavily_api_keys: Vec::new(),
344            serper_api_key: None,
345            serper_api_keys: Vec::new(),
346            exa_api_key: None,
347            exa_api_keys: Vec::new(),
348            firecrawl_api_key: None,
349            firecrawl_api_keys: Vec::new(),
350            default_budget: 4000,
351            default_tier: PdsTier::Summary,
352            max_concurrent: 100,
353            timeout_secs: 60,
354            searxng_url: Some("http://localhost:4040".into()),
355        }
356    }
357}
358
359impl Default for FetchConfig {
360    fn default() -> Self {
361        Self {
362            user_agent: format!(
363                "Fetchium/{} (https://github.com/fetchium/fetchium)",
364                env!("CARGO_PKG_VERSION")
365            ),
366            respect_robots: true,
367            max_page_size: 10 * 1024 * 1024, // 10 MB
368            timeout_secs: 30,
369            max_redirects: 10,
370        }
371    }
372}
373
374impl Default for CacheConfig {
375    fn default() -> Self {
376        Self {
377            enabled: true,
378            memory_max_entries: 1000,
379            disk_max_mb: 500,
380            ttl_secs: 3600,
381        }
382    }
383}
384
385impl Default for AiConfig {
386    fn default() -> Self {
387        Self {
388            ollama_host: "http://localhost:11434".into(),
389            default_model: "llama3.2".into(),
390            max_tokens: 8192,
391            fast_model: None,
392            providers: ProvidersConfig::default(),
393        }
394    }
395}
396
397impl Default for OutputConfig {
398    fn default() -> Self {
399        Self {
400            format: OutputFormat::Markdown,
401            include_sources: true,
402            include_confidence: true,
403        }
404    }
405}
406
407// ─── Validation ──────────────────────────────────────────────────
408
409impl FetchiumConfig {
410    /// Validate all configuration values are within acceptable bounds.
411    /// Returns a list of warnings for non-fatal issues or an error for fatal ones.
412    pub fn validate(&self) -> Result<Vec<String>, String> {
413        let mut warnings = Vec::new();
414
415        // Search config bounds
416        if self.search.default_budget == 0 {
417            return Err("search.default_budget must be > 0".into());
418        }
419        if self.search.default_budget > 1_000_000 {
420            warnings.push(format!(
421                "search.default_budget={} is very high, may cause slow responses",
422                self.search.default_budget
423            ));
424        }
425        if self.search.max_concurrent == 0 {
426            return Err("search.max_concurrent must be > 0".into());
427        }
428        if self.search.max_concurrent > 100 {
429            warnings.push(format!(
430                "search.max_concurrent={} exceeds recommended maximum of 100",
431                self.search.max_concurrent
432            ));
433        }
434        if self.search.timeout_secs == 0 {
435            return Err("search.timeout_secs must be > 0".into());
436        }
437        if self.search.timeout_secs > 300 {
438            warnings.push(format!(
439                "search.timeout_secs={} is very high (>5min)",
440                self.search.timeout_secs
441            ));
442        }
443
444        // Fetch config bounds
445        if self.fetch.timeout_secs == 0 {
446            return Err("fetch.timeout_secs must be > 0".into());
447        }
448        if self.fetch.max_page_size == 0 {
449            return Err("fetch.max_page_size must be > 0".into());
450        }
451        if self.fetch.max_page_size > 500 * 1024 * 1024 {
452            warnings.push("fetch.max_page_size > 500MB is excessive".into());
453        }
454        if self.fetch.max_redirects > 50 {
455            warnings.push("fetch.max_redirects > 50 may indicate a redirect loop issue".into());
456        }
457
458        // Cache config bounds
459        if self.cache.enabled && self.cache.ttl_secs == 0 {
460            warnings.push("cache.ttl_secs=0 means entries expire immediately".into());
461        }
462
463        // Ranking config bounds
464        if !(0.0..=1.0).contains(&self.ranking.freshness_need) {
465            return Err(format!(
466                "ranking.freshness_need={} must be in [0.0, 1.0]",
467                self.ranking.freshness_need
468            ));
469        }
470        if self.ranking.simhash_threshold > 64 {
471            return Err(format!(
472                "ranking.simhash_threshold={} must be in [0, 64]",
473                self.ranking.simhash_threshold
474            ));
475        }
476
477        // AI config bounds
478        if self.ai.max_tokens == 0 {
479            return Err("ai.max_tokens must be > 0".into());
480        }
481
482        // General config bounds
483        if self.general.max_results == 0 {
484            return Err("general.max_results must be > 0".into());
485        }
486        if self.general.max_results > 10_000 {
487            warnings.push(format!(
488                "general.max_results={} is very high",
489                self.general.max_results
490            ));
491        }
492
493        Ok(warnings)
494    }
495}
496
497// ─── Loading ─────────────────────────────────────────────────────
498
499impl FetchiumConfig {
500    /// Get the data directory, creating it if it doesn't exist.
501    ///
502    /// Resolution order:
503    /// 1. Explicit override in config (`general.data_dir`)
504    /// 2. `~/.fetchium/` (new canonical location)
505    /// 2. `~/.fetchium/`
506    pub fn data_dir(&self) -> PathBuf {
507        if let Some(ref dir) = self.general.data_dir {
508            return dir.clone();
509        }
510
511        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
512        home.join(".fetchium")
513    }
514
515    /// Load config from the default location.
516    pub fn load() -> Self {
517        Self::load_from(None)
518    }
519
520    /// Load config with an optional path override.
521    ///
522    /// Config file resolution (when no explicit path is given):
523    /// 1. `~/.fetchium/config.toml` (new canonical location)
524    /// 2. Default values if file does not exist
525    pub fn load_from(path: Option<&std::path::Path>) -> Self {
526        let config_path = path.map(|p| p.to_path_buf()).unwrap_or_else(|| {
527            let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
528            home.join(".fetchium").join("config.toml")
529        });
530
531        let mut config = if config_path.exists() {
532            match std::fs::read_to_string(&config_path) {
533                Ok(contents) => match toml::from_str(&contents) {
534                    Ok(config) => config,
535                    Err(e) => {
536                        tracing::warn!("Failed to parse config at {}: {e}", config_path.display());
537                        Self::default()
538                    }
539                },
540                Err(e) => {
541                    tracing::warn!("Failed to read config at {}: {e}", config_path.display());
542                    Self::default()
543                }
544            }
545        } else {
546            Self::default()
547        };
548
549        // Layer 3: environment variable overrides
550        config.apply_env_overrides();
551
552        // Layer 4: validation — warn on suspicious values, fail on invalid ones
553        match config.validate() {
554            Ok(warnings) => {
555                for w in warnings {
556                    tracing::warn!("Config warning: {w}");
557                }
558            }
559            Err(e) => {
560                tracing::error!("Config validation failed: {e} — using defaults");
561                return Self::default();
562            }
563        }
564
565        config
566    }
567
568    /// Detect the resource tier based on system capabilities.
569    pub fn detect_resource_tier() -> ResourceTier {
570        let sys = sysinfo::System::new_all();
571        let total_ram_gb = sys.total_memory() / (1024 * 1024 * 1024);
572        let cpu_count = sys.cpus().len();
573
574        match (total_ram_gb, cpu_count) {
575            (ram, cpus) if ram >= 32 && cpus >= 8 => ResourceTier::Server,
576            (ram, cpus) if ram >= 16 && cpus >= 4 => ResourceTier::Performance,
577            (ram, _) if ram >= 4 => ResourceTier::Standard,
578            _ => ResourceTier::Minimal,
579        }
580    }
581
582    /// Apply environment variable overrides.
583    ///
584    /// Reads `FETCHIUM_*` environment variables.
585    ///
586    /// Examples:
587    ///   `FETCHIUM_SEARCH_DEFAULT_BUDGET=8000`
588    ///   `FETCHIUM_CACHE_ENABLED=false`
589    pub fn apply_env_overrides(&mut self) {
590        // Helper: read env var string.
591        macro_rules! env_str {
592            ($name:expr) => {
593                std::env::var($name).ok()
594            };
595        }
596
597        if let Some(val) = env_str!("FETCHIUM_SEARCH_DEFAULT_BUDGET") {
598            if let Ok(budget) = val.parse::<u32>() {
599                self.search.default_budget = budget;
600            }
601        }
602        if let Some(val) = env_str!("FETCHIUM_SEARCH_MAX_CONCURRENT") {
603            if let Ok(n) = val.parse::<u32>() {
604                self.search.max_concurrent = n;
605            }
606        }
607        if let Some(val) = env_str!("FETCHIUM_SEARCH_TIMEOUT_SECS") {
608            if let Ok(n) = val.parse::<u64>() {
609                self.search.timeout_secs = n;
610            }
611        }
612        if let Some(val) = env_str!("FETCHIUM_CACHE_ENABLED") {
613            if let Ok(b) = val.parse::<bool>() {
614                self.cache.enabled = b;
615            }
616        }
617        if let Some(val) = env_str!("FETCHIUM_CACHE_TTL_SECS") {
618            if let Ok(n) = val.parse::<u64>() {
619                self.cache.ttl_secs = n;
620            }
621        }
622        if let Some(val) = env_str!("FETCHIUM_AI_OLLAMA_HOST") {
623            self.ai.ollama_host = val;
624        }
625        if let Some(val) = env_str!("FETCHIUM_AI_DEFAULT_MODEL") {
626            self.ai.default_model = val;
627        }
628        if let Some(val) = env_str!("FETCHIUM_GENERAL_VERBOSE") {
629            if let Ok(b) = val.parse::<bool>() {
630                self.general.verbose = b;
631            }
632        }
633        if let Some(val) = env_str!("FETCHIUM_FETCH_USER_AGENT") {
634            self.fetch.user_agent = val;
635        }
636        if let Some(val) = env_str!("FETCHIUM_FETCH_RESPECT_ROBOTS") {
637            if let Ok(b) = val.parse::<bool>() {
638                self.fetch.respect_robots = b;
639            }
640        }
641        // Provider API key overrides (also read directly by ProviderEntry::resolve_api_key)
642        if let Ok(val) = std::env::var("OPENAI_API_KEY") {
643            if !val.is_empty() {
644                self.ai.providers.openai.api_key = Some(val);
645            }
646        }
647        if let Ok(val) = std::env::var("ANTHROPIC_API_KEY") {
648            if !val.is_empty() {
649                self.ai.providers.anthropic.api_key = Some(val);
650            }
651        }
652        if let Ok(val) = std::env::var("GEMINI_API_KEY") {
653            if !val.is_empty() {
654                self.ai.providers.gemini.api_key = Some(val);
655            }
656        }
657        if let Ok(val) = std::env::var("OPENROUTER_API_KEY") {
658            if !val.is_empty() {
659                self.ai.providers.openrouter.api_key = Some(val);
660            }
661        }
662        // Primary provider override (prepends to chain)
663        if let Some(val) = env_str!("FETCHIUM_AI_PROVIDER") {
664            if !val.is_empty() {
665                self.ai.providers.fallback_chain.insert(0, val);
666            }
667        }
668        // Chrome binary override
669        if let Some(val) = env_str!("FETCHIUM_CHROME_PATH") {
670            if !val.is_empty() {
671                self.headless.chrome_path = Some(PathBuf::from(val));
672            }
673        }
674    }
675
676    /// Ensure the data directory exists, creating it if necessary.
677    pub fn ensure_data_dir(&self) -> std::io::Result<PathBuf> {
678        let dir = self.data_dir();
679        if !dir.exists() {
680            std::fs::create_dir_all(&dir)?;
681        }
682        Ok(dir)
683    }
684
685    /// Get the path to the config file.
686    ///
687    /// Returns the canonical Fetchium config path.
688    pub fn config_file_path() -> PathBuf {
689        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
690        home.join(".fetchium").join("config.toml")
691    }
692
693    /// Write the current config to the config file (for `fetchium config set`).
694    ///
695    /// Always writes to `~/.fetchium/config.toml` — the new canonical location.
696    pub fn save(&self) -> std::io::Result<()> {
697        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
698        let path = home.join(".fetchium").join("config.toml");
699        if let Some(parent) = path.parent() {
700            std::fs::create_dir_all(parent)?;
701        }
702        let toml_str = toml::to_string_pretty(self).map_err(std::io::Error::other)?;
703        std::fs::write(&path, toml_str)
704    }
705}
706
707#[cfg(test)]
708mod tests {
709    use super::*;
710
711    #[test]
712    fn default_config_is_valid() {
713        let config = FetchiumConfig::default();
714        assert_eq!(config.search.default_budget, 4000);
715        assert!(config.fetch.respect_robots);
716        assert!(config.cache.enabled);
717    }
718
719    #[test]
720    fn config_roundtrip_toml() {
721        let config = FetchiumConfig::default();
722        let toml_str = toml::to_string_pretty(&config).unwrap();
723        let back: FetchiumConfig = toml::from_str(&toml_str).unwrap();
724        assert_eq!(back.search.default_budget, config.search.default_budget);
725    }
726
727    #[test]
728    fn data_dir_default() {
729        let config = FetchiumConfig::default();
730        let dir = config.data_dir();
731        // The canonical default is ~/.fetchium.
732        let s = dir.to_string_lossy();
733        assert!(s.contains(".fetchium"));
734    }
735
736    #[test]
737    fn env_override_budget_new_prefix() {
738        std::env::set_var("FETCHIUM_SEARCH_DEFAULT_BUDGET", "8000");
739        let mut config = FetchiumConfig::default();
740        config.apply_env_overrides();
741        assert_eq!(config.search.default_budget, 8000);
742        std::env::remove_var("FETCHIUM_SEARCH_DEFAULT_BUDGET");
743    }
744
745    #[test]
746    fn env_override_cache_disabled() {
747        std::env::set_var("FETCHIUM_CACHE_ENABLED", "false");
748        let mut config = FetchiumConfig::default();
749        config.apply_env_overrides();
750        assert!(!config.cache.enabled);
751        std::env::remove_var("FETCHIUM_CACHE_ENABLED");
752    }
753
754    #[test]
755    fn save_and_reload() {
756        let dir = tempfile::tempdir().unwrap();
757        let config_path = dir.path().join("config.toml");
758
759        let mut config = FetchiumConfig::default();
760        config.search.default_budget = 9999;
761
762        // Save
763        let toml_str = toml::to_string_pretty(&config).unwrap();
764        std::fs::write(&config_path, &toml_str).unwrap();
765
766        // Reload
767        let loaded = FetchiumConfig::load_from(Some(&config_path));
768        assert_eq!(loaded.search.default_budget, 9999);
769    }
770
771    #[test]
772    fn config_file_path_is_fetchium() {
773        let path = FetchiumConfig::config_file_path();
774        let s = path.to_string_lossy();
775        assert!(s.contains(".fetchium"));
776        assert!(s.ends_with("config.toml"));
777    }
778}