tuitbot-core 0.1.47

Core library for Tuitbot autonomous X growth assistant
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! Configuration validation logic.

use super::Config;
use crate::error::ConfigError;

impl Config {
    /// Validate the minimum configuration required for progressive activation.
    ///
    /// Only checks business profile fields and structural requirements.
    /// Skips LLM API key, X API client_id, and other advanced fields that
    /// can be configured later via Settings.
    pub fn validate_minimum(&self) -> Result<(), Vec<ConfigError>> {
        let mut errors = Vec::new();

        // Business profile — required for tier 1
        if self.business.product_name.is_empty() {
            errors.push(ConfigError::MissingField {
                field: "business.product_name".to_string(),
            });
        }

        if self.business.product_description.trim().is_empty() {
            errors.push(ConfigError::MissingField {
                field: "business.product_description".to_string(),
            });
        }

        if self.business.product_keywords.is_empty() && self.business.competitor_keywords.is_empty()
        {
            errors.push(ConfigError::MissingField {
                field: "business.product_keywords or business.competitor_keywords".to_string(),
            });
        }

        // Validate LLM provider value if present (but don't require it)
        if !self.llm.provider.is_empty() {
            match self.llm.provider.as_str() {
                "openai" | "anthropic" | "ollama" | "groq" => {}
                _ => {
                    errors.push(ConfigError::InvalidValue {
                        field: "llm.provider".to_string(),
                        message: "must be openai, anthropic, ollama, or groq".to_string(),
                    });
                }
            }
        }

        // Validate provider_backend value if present
        let backend = self.x_api.provider_backend.as_str();
        if !backend.is_empty() && backend != "x_api" && backend != "scraper" {
            errors.push(ConfigError::InvalidValue {
                field: "x_api.provider_backend".to_string(),
                message: format!(
                    "must be 'x_api' or 'scraper', got '{}'",
                    self.x_api.provider_backend
                ),
            });
        }

        // Structural: db_path
        let db_path_trimmed = self.storage.db_path.trim();
        if db_path_trimmed.is_empty() {
            errors.push(ConfigError::InvalidValue {
                field: "storage.db_path".to_string(),
                message: "must not be empty or whitespace-only".to_string(),
            });
        } else {
            let expanded = crate::startup::expand_tilde(db_path_trimmed);
            if expanded.is_dir() {
                errors.push(ConfigError::InvalidValue {
                    field: "storage.db_path".to_string(),
                    message: format!("'{}' is a directory, must point to a file", db_path_trimmed),
                });
            }
        }

        // Validate content sources against deployment capabilities (if any)
        for (i, source) in self.content_sources.sources.iter().enumerate() {
            if !self.deployment_mode.allows_source_type(&source.source_type) {
                errors.push(ConfigError::InvalidValue {
                    field: format!("content_sources.sources[{}].source_type", i),
                    message: format!(
                        "source type '{}' is not available in {} deployment mode",
                        source.source_type, self.deployment_mode
                    ),
                });
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Validate the configuration, returning all errors found (not just the first).
    pub fn validate(&self) -> Result<(), Vec<ConfigError>> {
        let mut errors = Vec::new();

        // Validate business profile
        if self.business.product_name.is_empty() {
            errors.push(ConfigError::MissingField {
                field: "business.product_name".to_string(),
            });
        }

        if self.business.product_keywords.is_empty() && self.business.competitor_keywords.is_empty()
        {
            errors.push(ConfigError::MissingField {
                field: "business.product_keywords or business.competitor_keywords".to_string(),
            });
        }

        if self.business.product_description.trim().is_empty() {
            errors.push(ConfigError::MissingField {
                field: "business.product_description".to_string(),
            });
        }

        if self.business.industry_topics.is_empty() {
            errors.push(ConfigError::MissingField {
                field: "business.industry_topics".to_string(),
            });
        }

        // Validate LLM provider
        if !self.llm.provider.is_empty() {
            match self.llm.provider.as_str() {
                "openai" | "anthropic" | "ollama" | "groq" => {}
                _ => {
                    errors.push(ConfigError::InvalidValue {
                        field: "llm.provider".to_string(),
                        message: "must be openai, anthropic, ollama, or groq".to_string(),
                    });
                }
            }

            if matches!(self.llm.provider.as_str(), "openai" | "anthropic" | "groq") {
                match &self.llm.api_key {
                    Some(key) if !key.is_empty() => {}
                    _ => {
                        errors.push(ConfigError::MissingField {
                            field: format!(
                                "llm.api_key (required for {} provider)",
                                self.llm.provider
                            ),
                        });
                    }
                }
            }
        }

        // Validate auth mode
        if !self.auth.mode.is_empty() {
            match self.auth.mode.as_str() {
                "manual" | "local_callback" => {}
                _ => {
                    errors.push(ConfigError::InvalidValue {
                        field: "auth.mode".to_string(),
                        message: "must be manual or local_callback".to_string(),
                    });
                }
            }
        }

        // Validate scoring threshold
        if self.scoring.threshold > 100 {
            errors.push(ConfigError::InvalidValue {
                field: "scoring.threshold".to_string(),
                message: "must be between 0 and 100".to_string(),
            });
        }

        // Validate limits
        if self.limits.max_replies_per_day == 0 {
            errors.push(ConfigError::InvalidValue {
                field: "limits.max_replies_per_day".to_string(),
                message: "must be greater than 0".to_string(),
            });
        }

        if self.limits.max_tweets_per_day == 0 {
            errors.push(ConfigError::InvalidValue {
                field: "limits.max_tweets_per_day".to_string(),
                message: "must be greater than 0".to_string(),
            });
        }

        if self.limits.max_threads_per_week == 0 {
            errors.push(ConfigError::InvalidValue {
                field: "limits.max_threads_per_week".to_string(),
                message: "must be greater than 0".to_string(),
            });
        }

        if self.limits.min_action_delay_seconds > self.limits.max_action_delay_seconds {
            errors.push(ConfigError::InvalidValue {
                field: "limits.min_action_delay_seconds".to_string(),
                message: "must be less than or equal to max_action_delay_seconds".to_string(),
            });
        }

        // Validate schedule
        if self.schedule.active_hours_start > 23 {
            errors.push(ConfigError::InvalidValue {
                field: "schedule.active_hours_start".to_string(),
                message: "must be between 0 and 23".to_string(),
            });
        }
        if self.schedule.active_hours_end > 23 {
            errors.push(ConfigError::InvalidValue {
                field: "schedule.active_hours_end".to_string(),
                message: "must be between 0 and 23".to_string(),
            });
        }
        if !self.schedule.timezone.is_empty()
            && self.schedule.timezone.parse::<chrono_tz::Tz>().is_err()
        {
            errors.push(ConfigError::InvalidValue {
                field: "schedule.timezone".to_string(),
                message: format!(
                    "'{}' is not a valid IANA timezone name",
                    self.schedule.timezone
                ),
            });
        }
        let valid_days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
        for day in &self.schedule.active_days {
            if !valid_days.contains(&day.as_str()) {
                errors.push(ConfigError::InvalidValue {
                    field: "schedule.active_days".to_string(),
                    message: format!(
                        "'{}' is not a valid day abbreviation (use Mon, Tue, Wed, Thu, Fri, Sat, Sun)",
                        day
                    ),
                });
                break;
            }
        }

        // Validate preferred_times
        for time_str in &self.schedule.preferred_times {
            if time_str != "auto" && !is_valid_hhmm(time_str) {
                errors.push(ConfigError::InvalidValue {
                    field: "schedule.preferred_times".to_string(),
                    message: format!(
                        "'{}' is not a valid time (use HH:MM 24h format or \"auto\")",
                        time_str
                    ),
                });
                break;
            }
        }

        // Validate preferred_times_override keys and values
        for (day, times) in &self.schedule.preferred_times_override {
            if !valid_days.contains(&day.as_str()) {
                errors.push(ConfigError::InvalidValue {
                    field: "schedule.preferred_times_override".to_string(),
                    message: format!(
                        "'{}' is not a valid day abbreviation (use Mon, Tue, Wed, Thu, Fri, Sat, Sun)",
                        day
                    ),
                });
                break;
            }
            for time_str in times {
                if !is_valid_hhmm(time_str) {
                    errors.push(ConfigError::InvalidValue {
                        field: "schedule.preferred_times_override".to_string(),
                        message: format!(
                            "'{}' is not a valid time for {} (use HH:MM 24h format)",
                            time_str, day
                        ),
                    });
                    break;
                }
            }
        }

        // Validate MCP policy: tools can't be in both blocked_tools and require_approval_for
        for tool in &self.mcp_policy.blocked_tools {
            if self.mcp_policy.require_approval_for.contains(tool) {
                errors.push(ConfigError::InvalidValue {
                    field: "mcp_policy.blocked_tools".to_string(),
                    message: format!(
                        "tool '{tool}' cannot be in both blocked_tools and require_approval_for"
                    ),
                });
                break;
            }
        }

        // Count effective slots per day vs max_tweets_per_day
        let effective_slots = if self.schedule.preferred_times.is_empty() {
            0
        } else {
            // "auto" expands to 3 slots
            let base_count: usize = self
                .schedule
                .preferred_times
                .iter()
                .map(|t| if t == "auto" { 3 } else { 1 })
                .sum();
            // Check max across all override days too
            let max_override = self
                .schedule
                .preferred_times_override
                .values()
                .map(|v| v.len())
                .max()
                .unwrap_or(0);
            base_count.max(max_override)
        };
        if effective_slots > self.limits.max_tweets_per_day as usize {
            errors.push(ConfigError::InvalidValue {
                field: "schedule.preferred_times".to_string(),
                message: format!(
                    "preferred_times has {} slots but limits.max_tweets_per_day is {}\
                     increase the limit or reduce the number of time slots",
                    effective_slots, self.limits.max_tweets_per_day
                ),
            });
        }

        // Validate thread_preferred_day
        if let Some(day) = &self.schedule.thread_preferred_day {
            if !valid_days.contains(&day.as_str()) {
                errors.push(ConfigError::InvalidValue {
                    field: "schedule.thread_preferred_day".to_string(),
                    message: format!(
                        "'{}' is not a valid day abbreviation (use Mon, Tue, Wed, Thu, Fri, Sat, Sun)",
                        day
                    ),
                });
            }
        }

        // Validate thread_preferred_time
        if !is_valid_hhmm(&self.schedule.thread_preferred_time) {
            errors.push(ConfigError::InvalidValue {
                field: "schedule.thread_preferred_time".to_string(),
                message: format!(
                    "'{}' is not a valid time (use HH:MM 24h format)",
                    self.schedule.thread_preferred_time
                ),
            });
        }

        // Validate provider_backend value
        let backend = self.x_api.provider_backend.as_str();
        if !backend.is_empty() && backend != "x_api" && backend != "scraper" {
            errors.push(ConfigError::InvalidValue {
                field: "x_api.provider_backend".to_string(),
                message: format!(
                    "must be 'x_api' or 'scraper', got '{}'",
                    self.x_api.provider_backend
                ),
            });
        }

        // Reject scraper mode in cloud deployment
        if self.deployment_mode == super::DeploymentMode::Cloud
            && self.x_api.provider_backend == "scraper"
        {
            errors.push(ConfigError::InvalidValue {
                field: "x_api.provider_backend".to_string(),
                message: "Local No-Key Mode is not available in cloud deployment. \
                          Use the Official X API (provider_backend = \"x_api\")."
                    .to_string(),
            });
        }

        // Require client_id when using official X API backend
        let is_x_api_backend = backend.is_empty() || backend == "x_api";
        if is_x_api_backend && self.x_api.client_id.trim().is_empty() {
            errors.push(ConfigError::MissingField {
                field: "x_api.client_id".to_string(),
            });
        }

        // Validate storage.db_path is not empty/whitespace and not a directory
        let db_path_trimmed = self.storage.db_path.trim();
        if db_path_trimmed.is_empty() {
            errors.push(ConfigError::InvalidValue {
                field: "storage.db_path".to_string(),
                message: "must not be empty or whitespace-only".to_string(),
            });
        } else {
            let expanded = crate::startup::expand_tilde(db_path_trimmed);
            if expanded.is_dir() {
                errors.push(ConfigError::InvalidValue {
                    field: "storage.db_path".to_string(),
                    message: format!("'{}' is a directory, must point to a file", db_path_trimmed),
                });
            }
        }

        // Validate content sources against deployment capabilities
        for (i, source) in self.content_sources.sources.iter().enumerate() {
            if !self.deployment_mode.allows_source_type(&source.source_type) {
                errors.push(ConfigError::InvalidValue {
                    field: format!("content_sources.sources[{}].source_type", i),
                    message: format!(
                        "source type '{}' is not available in {} deployment mode",
                        source.source_type, self.deployment_mode
                    ),
                });
            }

            // Validate change_detection value.
            let valid_cd = [
                super::types::CHANGE_DETECTION_AUTO,
                super::types::CHANGE_DETECTION_POLL,
                super::types::CHANGE_DETECTION_NONE,
            ];
            if !valid_cd.contains(&source.change_detection.as_str()) {
                errors.push(ConfigError::InvalidValue {
                    field: format!("content_sources.sources[{}].change_detection", i),
                    message: format!(
                        "must be one of: auto, poll, none — got '{}'",
                        source.change_detection
                    ),
                });
            }

            // Validate poll_interval_seconds minimum.
            if let Some(interval) = source.poll_interval_seconds {
                if interval < super::types::MIN_POLL_INTERVAL_SECONDS {
                    errors.push(ConfigError::InvalidValue {
                        field: format!("content_sources.sources[{}].poll_interval_seconds", i),
                        message: format!(
                            "must be at least {} seconds, got {}",
                            super::types::MIN_POLL_INTERVAL_SECONDS,
                            interval
                        ),
                    });
                }
            }

            // Validate enabled sources have required fields.
            if source.is_enabled() {
                if source.source_type == "local_fs"
                    && source.path.as_ref().map_or(true, |p| p.is_empty())
                {
                    errors.push(ConfigError::MissingField {
                        field: format!(
                            "content_sources.sources[{}].path (required for enabled local_fs source)",
                            i
                        ),
                    });
                }
                if source.source_type == "google_drive"
                    && source.folder_id.as_ref().map_or(true, |f| f.is_empty())
                {
                    errors.push(ConfigError::MissingField {
                        field: format!(
                            "content_sources.sources[{}].folder_id (required for enabled google_drive source)",
                            i
                        ),
                    });
                }
            }

            // Warn if both connection_id and service_account_key are set.
            // Not a blocking error -- session 04 handles precedence.
            if source.source_type == "google_drive"
                && source.connection_id.is_some()
                && source.service_account_key.is_some()
            {
                tracing::warn!(
                    source_index = i,
                    "content_sources.sources[{}] has both connection_id and \
                     service_account_key; connection_id takes precedence",
                    i
                );
            }

            // Warn if a google_drive source has neither auth method configured.
            // The Watchtower will skip this source at runtime, but surface it
            // during validation so the user knows to connect via the dashboard.
            if source.source_type == "google_drive"
                && source.is_enabled()
                && source.connection_id.is_none()
                && source.service_account_key.is_none()
            {
                tracing::warn!(
                    source_index = i,
                    "content_sources.sources[{}] has no authentication configured \
                     (neither connection_id nor service_account_key); this source \
                     will be skipped at runtime -- connect via Settings > Content Sources",
                    i
                );
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }
}

/// Check if a string is a valid HH:MM time (24h format).
fn is_valid_hhmm(s: &str) -> bool {
    let parts: Vec<&str> = s.split(':').collect();
    if parts.len() != 2 {
        return false;
    }
    let Ok(hour) = parts[0].parse::<u8>() else {
        return false;
    };
    let Ok(minute) = parts[1].parse::<u8>() else {
        return false;
    };
    hour <= 23 && minute <= 59
}

#[cfg(test)]
mod tests {
    use super::*;

    fn minimal_valid_config() -> Config {
        let mut c = Config::default();
        c.business.product_name = "TestBot".to_string();
        c.business.product_description = "A test product for unit testing".to_string();
        c.business.product_keywords = vec!["test".to_string()];
        c
    }

    // ── validate_minimum ──────────────────────────────────────────────────

    #[test]
    fn validate_minimum_default_config_fails() {
        let c = Config::default();
        assert!(c.validate_minimum().is_err());
    }

    #[test]
    fn validate_minimum_populated_config_passes() {
        let c = minimal_valid_config();
        assert!(c.validate_minimum().is_ok(), "{:?}", c.validate_minimum());
    }

    #[test]
    fn validate_minimum_missing_product_name_fails() {
        let mut c = minimal_valid_config();
        c.business.product_name = String::new();
        let errs = c.validate_minimum().unwrap_err();
        assert!(errs
            .iter()
            .any(|e| format!("{e:?}").contains("product_name")));
    }

    #[test]
    fn validate_minimum_missing_description_fails() {
        let mut c = minimal_valid_config();
        c.business.product_description = "   ".to_string(); // whitespace only
        let errs = c.validate_minimum().unwrap_err();
        assert!(errs
            .iter()
            .any(|e| format!("{e:?}").contains("product_description")));
    }

    #[test]
    fn validate_minimum_missing_both_keyword_fields_fails() {
        let mut c = minimal_valid_config();
        c.business.product_keywords = vec![];
        c.business.competitor_keywords = vec![];
        let errs = c.validate_minimum().unwrap_err();
        assert!(errs.iter().any(|e| format!("{e:?}").contains("keywords")));
    }

    #[test]
    fn validate_minimum_competitor_keywords_satisfies_keyword_requirement() {
        let mut c = minimal_valid_config();
        c.business.product_keywords = vec![];
        c.business.competitor_keywords = vec!["competitor".to_string()];
        assert!(c.validate_minimum().is_ok(), "{:?}", c.validate_minimum());
    }

    #[test]
    fn validate_minimum_invalid_llm_provider_fails() {
        let mut c = minimal_valid_config();
        c.llm.provider = "invalid_provider".to_string();
        let errs = c.validate_minimum().unwrap_err();
        assert!(errs
            .iter()
            .any(|e| format!("{e:?}").contains("llm.provider")));
    }

    #[test]
    fn validate_minimum_valid_llm_providers_pass() {
        for provider in &["openai", "anthropic", "ollama", "groq"] {
            let mut c = minimal_valid_config();
            c.llm.provider = provider.to_string();
            assert!(
                c.validate_minimum().is_ok(),
                "provider {provider} should pass"
            );
        }
    }

    #[test]
    fn validate_minimum_invalid_provider_backend_fails() {
        let mut c = minimal_valid_config();
        c.x_api.provider_backend = "invalid_backend".to_string();
        let errs = c.validate_minimum().unwrap_err();
        assert!(errs
            .iter()
            .any(|e| format!("{e:?}").contains("provider_backend")));
    }

    #[test]
    fn validate_minimum_valid_provider_backends_pass() {
        for backend in &["x_api", "scraper"] {
            let mut c = minimal_valid_config();
            c.x_api.provider_backend = backend.to_string();
            assert!(
                c.validate_minimum().is_ok(),
                "backend {backend} should pass"
            );
        }
    }

    // ── validate (full) ───────────────────────────────────────────────────

    #[test]
    fn validate_default_config_fails() {
        let c = Config::default();
        assert!(c.validate().is_err());
    }

    #[test]
    fn validate_collects_multiple_errors() {
        let c = Config::default();
        let errs = c.validate().unwrap_err();
        assert!(errs.len() >= 2, "expected ≥2 errors, got {}", errs.len());
    }

    // ── is_valid_hhmm ────────────────────────────────────────────────────

    #[test]
    fn is_valid_hhmm_valid_times() {
        assert!(is_valid_hhmm("00:00"));
        assert!(is_valid_hhmm("09:30"));
        assert!(is_valid_hhmm("23:59"));
        assert!(is_valid_hhmm("12:00"));
    }

    #[test]
    fn is_valid_hhmm_invalid_times() {
        assert!(!is_valid_hhmm("24:00")); // hour out of range
        assert!(!is_valid_hhmm("12:60")); // minute out of range
        assert!(!is_valid_hhmm("noon")); // non-numeric
        assert!(!is_valid_hhmm("")); // empty
        assert!(!is_valid_hhmm("12:30:00")); // too many parts
        assert!(!is_valid_hhmm("1230")); // no colon
    }
}