tuitbot-cli 0.1.52

CLI for Tuitbot autonomous X growth assistant
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
//! Implementation of the `tuitbot test` command.
//!
//! Validates configuration, credentials, and connectivity before
//! running the agent. Each check runs independently -- a failure
//! in one does not skip others.

#[cfg(test)]
mod tests;

use serde::Serialize;
use tuitbot_core::config::Config;
use tuitbot_core::error::LlmError;
use tuitbot_core::llm::factory::create_provider;
use tuitbot_core::startup::{expand_tilde, load_tokens_from_file, StartupError, StoredTokens};

use crate::output::CliOutput;

/// A single diagnostic check result.
#[derive(Clone, Serialize)]
struct CheckResult {
    label: &'static str,
    passed: bool,
    message: String,
}

impl CheckResult {
    fn ok(label: &'static str, message: impl Into<String>) -> Self {
        Self {
            label,
            passed: true,
            message: message.into(),
        }
    }

    fn fail(label: &'static str, message: impl Into<String>) -> Self {
        Self {
            label,
            passed: false,
            message: message.into(),
        }
    }
}

impl std::fmt::Display for CheckResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let status = if self.passed { "OK" } else { "FAIL" };
        write!(
            f,
            "{:<18}{status} ({})",
            format!("{}:", self.label),
            self.message
        )
    }
}

#[derive(Clone, Serialize)]
struct AuthDetails {
    token_valid: bool,
    expires_at: Option<String>,
    expires_in_seconds: Option<i64>,
    has_refresh_token: bool,
    scopes_tracked: bool,
    granted_scopes: Vec<String>,
    missing_scopes: Vec<String>,
    degraded_features: Vec<String>,
    fix_action: Option<String>,
}

#[derive(Serialize)]
struct TestOutput {
    passed: bool,
    checks: Vec<CheckResult>,
    #[serde(skip_serializing_if = "Option::is_none")]
    auth_details: Option<AuthDetails>,
}

struct AuthEvaluation {
    checks: Vec<CheckResult>,
    details: AuthDetails,
}

/// Run all diagnostic checks and return results.
fn collect_checks(config: &Config, config_path: &str) -> Vec<CheckResult> {
    let mut checks = vec![
        check_config(config, config_path),
        check_business_profile(config),
    ];
    checks.extend(check_auth(config));
    checks.push(check_llm_config(config));
    checks.push(check_database(config));
    checks
}

fn collect_checks_with_auth(
    config: &Config,
    config_path: &str,
    auth_checks: Vec<CheckResult>,
) -> Vec<CheckResult> {
    let mut checks = vec![
        check_config(config, config_path),
        check_business_profile(config),
    ];
    checks.extend(auth_checks);
    checks.push(check_llm_config(config));
    checks.push(check_database(config));
    checks
}

fn build_test_output(checks: Vec<CheckResult>, auth_details: Option<AuthDetails>) -> TestOutput {
    let passed = checks.iter().all(|r| r.passed);
    TestOutput {
        passed,
        checks,
        auth_details,
    }
}

/// Run all diagnostic checks and print results.
///
/// Returns `true` if all checks pass, `false` if any fail.
/// Does **not** call `process::exit` — callers decide what to do on failure.
pub async fn run_checks(config: &Config, config_path: &str) -> bool {
    let mut results = collect_checks(config, config_path);
    results.push(check_llm_connectivity(config).await);

    // Print results.
    eprintln!();
    for result in &results {
        eprintln!("{result}");
    }
    eprintln!();

    let all_passed = results.iter().all(|r| r.passed);
    if all_passed {
        eprintln!("All checks passed.");
        print_enrichment_hint(config);
    } else {
        let failed = results.iter().filter(|r| !r.passed).count();
        eprintln!("{failed} check(s) failed.");
    }

    if let Some(hint) = next_step_guidance(&results) {
        eprintln!("{hint}");
    }

    all_passed
}

/// Execute the `tuitbot test` command.
///
/// Runs all diagnostic checks and reports results. Exits with code 1
/// if any check fails.
pub async fn execute(config: &Config, config_path: &str, out: CliOutput) -> anyhow::Result<()> {
    if out.is_json() {
        let auth = if config.x_api.provider_backend == "scraper" {
            AuthEvaluation {
                checks: vec![CheckResult::ok(
                    "X API auth",
                    "skipped (Local No-Key Mode — scraper backend)",
                )],
                details: AuthDetails {
                    token_valid: false,
                    expires_at: None,
                    expires_in_seconds: None,
                    has_refresh_token: false,
                    scopes_tracked: false,
                    granted_scopes: vec![],
                    missing_scopes: vec![],
                    degraded_features: vec![],
                    fix_action: None,
                },
            }
        } else {
            evaluate_auth(load_tokens_from_file())
        };
        let mut checks = collect_checks_with_auth(config, config_path, auth.checks);
        checks.push(check_llm_connectivity(config).await);
        let output = build_test_output(checks, Some(auth.details));
        out.json(&output)?;
        if !output.passed {
            std::process::exit(1);
        }
    } else if !run_checks(config, config_path).await {
        std::process::exit(1);
    }
    Ok(())
}

/// Check that configuration loaded successfully.
fn check_config(config: &Config, config_path: &str) -> CheckResult {
    let path = expand_tilde(config_path);
    match config.validate() {
        Ok(()) => CheckResult::ok("Configuration", format!("loaded from {}", path.display())),
        Err(errors) => {
            let msgs: Vec<String> = errors.iter().map(|e| e.to_string()).collect();
            CheckResult::fail("Configuration", msgs.join("; "))
        }
    }
}

/// Check that the business profile has required fields.
fn check_business_profile(config: &Config) -> CheckResult {
    let name = &config.business.product_name;
    if name.is_empty() {
        return CheckResult::fail("Business profile", "product_name is empty");
    }

    let keyword_count =
        config.business.product_keywords.len() + config.business.competitor_keywords.len();
    let topic_count = config.business.effective_industry_topics().len();

    if keyword_count == 0 {
        return CheckResult::fail(
            "Business profile",
            "no product_keywords or competitor_keywords configured",
        );
    }

    if config.business.product_description.trim().is_empty() {
        return CheckResult::fail("Business profile", "product_description is empty");
    }

    if config.business.industry_topics.is_empty() {
        return CheckResult::fail("Business profile", "no industry_topics configured");
    }

    CheckResult::ok(
        "Business profile",
        format!("product_name: \"{name}\", {keyword_count} keywords, {topic_count} topics"),
    )
}

/// Check that OAuth tokens exist and are valid.
fn check_auth(config: &Config) -> Vec<CheckResult> {
    if config.x_api.provider_backend == "scraper" {
        return vec![CheckResult::ok(
            "X API auth",
            "skipped (Local No-Key Mode — scraper backend)",
        )];
    }
    evaluate_auth(load_tokens_from_file()).checks
}

fn evaluate_auth(tokens_result: Result<StoredTokens, StartupError>) -> AuthEvaluation {
    match tokens_result {
        Ok(tokens) => evaluate_auth_from_tokens(tokens),
        Err(StartupError::AuthRequired) => AuthEvaluation {
            checks: vec![CheckResult::fail(
                "X API auth",
                "no tokens found, run `tuitbot auth` first",
            )],
            details: AuthDetails {
                token_valid: false,
                expires_at: None,
                expires_in_seconds: None,
                has_refresh_token: false,
                scopes_tracked: false,
                granted_scopes: vec![],
                missing_scopes: vec![],
                degraded_features: vec![],
                fix_action: Some("Run `tuitbot auth` to authenticate.".to_string()),
            },
        },
        Err(err) => AuthEvaluation {
            checks: vec![CheckResult::fail(
                "X API auth",
                format!("failed to load tokens ({err}), run `tuitbot auth` to re-authenticate"),
            )],
            details: AuthDetails {
                token_valid: false,
                expires_at: None,
                expires_in_seconds: None,
                has_refresh_token: false,
                scopes_tracked: false,
                granted_scopes: vec![],
                missing_scopes: vec![],
                degraded_features: vec![],
                fix_action: Some("Run `tuitbot auth` to re-authenticate.".to_string()),
            },
        },
    }
}

fn evaluate_auth_from_tokens(tokens: StoredTokens) -> AuthEvaluation {
    let token_valid = !tokens.is_expired();
    let expires_at = tokens.expires_at.map(|dt| dt.to_rfc3339());
    let expires_in_seconds = tokens.time_until_expiry().map(|d| d.num_seconds());
    let has_refresh_token = tokens
        .refresh_token
        .as_ref()
        .is_some_and(|token| !token.trim().is_empty());

    let mut checks = Vec::new();

    if token_valid {
        checks.push(CheckResult::ok(
            "X API token",
            format!("token valid, expires in {}", tokens.format_expiry()),
        ));
    } else {
        checks.push(CheckResult::fail(
            "X API token",
            "token expired, run `tuitbot auth` to re-authenticate",
        ));
    }

    if has_refresh_token {
        checks.push(CheckResult::ok("X API refresh", "refresh token present"));
    } else {
        checks.push(CheckResult::fail(
            "X API refresh",
            "refresh token missing; re-run `tuitbot auth` and grant `offline.access`",
        ));
    }

    let (scopes_tracked, granted_scopes, missing_scopes, degraded_features) =
        if tokens.has_scope_info() {
            let analysis = tokens.analyze_scopes();
            let degraded_names: Vec<String> = analysis
                .degraded_features
                .iter()
                .map(|feature| feature.feature.clone())
                .collect();

            if analysis.all_required_present {
                checks.push(CheckResult::ok(
                    "X API scopes",
                    format!(
                        "all required scopes granted ({})",
                        analysis.required.join(", ")
                    ),
                ));
            } else {
                let degraded_display = if degraded_names.is_empty() {
                    "(none)".to_string()
                } else {
                    degraded_names.join(", ")
                };
                checks.push(CheckResult::fail(
                    "X API scopes",
                    format!(
                        "missing scopes: {}; degraded features: {}",
                        analysis.missing.join(", "),
                        degraded_display
                    ),
                ));
            }

            (true, analysis.granted, analysis.missing, degraded_names)
        } else {
            checks.push(CheckResult::ok(
            "X API scopes",
            "scope metadata not tracked (legacy token file); run `tuitbot auth` to capture scopes",
        ));
            (false, vec![], vec![], vec![])
        };

    let fix_action = if !token_valid {
        Some("Run `tuitbot auth` to re-authenticate.".to_string())
    } else if !has_refresh_token {
        Some(
            "Re-run `tuitbot auth` and grant `offline.access` so refresh tokens are issued."
                .to_string(),
        )
    } else if scopes_tracked && !missing_scopes.is_empty() {
        Some(format!(
            "Run `tuitbot auth` to grant missing scopes: {}",
            missing_scopes.join(", ")
        ))
    } else if !scopes_tracked {
        Some("Re-run `tuitbot auth` to store granted scope metadata for diagnostics.".to_string())
    } else {
        None
    };

    AuthEvaluation {
        checks,
        details: AuthDetails {
            token_valid,
            expires_at,
            expires_in_seconds,
            has_refresh_token,
            scopes_tracked,
            granted_scopes,
            missing_scopes,
            degraded_features,
            fix_action,
        },
    }
}

/// Check LLM provider configuration.
fn check_llm_config(config: &Config) -> CheckResult {
    if config.llm.provider.is_empty() {
        return CheckResult::fail("LLM provider", "no provider configured");
    }

    match config.llm.provider.as_str() {
        "openai" | "anthropic" | "ollama" => {}
        other => {
            return CheckResult::fail("LLM provider", format!("unknown provider: {other}"));
        }
    }

    if matches!(config.llm.provider.as_str(), "openai" | "anthropic") {
        match &config.llm.api_key {
            Some(key) if !key.is_empty() => {}
            _ => {
                return CheckResult::fail(
                    "LLM provider",
                    format!("api_key required for {} provider", config.llm.provider),
                );
            }
        }
    }

    let model = if config.llm.model.is_empty() {
        "default"
    } else {
        &config.llm.model
    };

    CheckResult::ok(
        "LLM provider",
        format!("{}, model: {model}", config.llm.provider),
    )
}

/// Check database file status.
fn check_database(config: &Config) -> CheckResult {
    let trimmed = config.storage.db_path.trim();
    if trimmed.is_empty() {
        return CheckResult::fail(
            "Database",
            "storage.db_path must not be empty or whitespace-only",
        );
    }

    let db_path = expand_tilde(trimmed);

    if db_path.is_dir() {
        return CheckResult::fail(
            "Database",
            format!(
                "storage.db_path '{}' is a directory, must point to a file",
                trimmed
            ),
        );
    }

    if db_path.exists() {
        match std::fs::metadata(&db_path) {
            Ok(meta) => {
                let size_mb = meta.len() as f64 / (1024.0 * 1024.0);
                let name = db_path
                    .file_name()
                    .map(|n| n.to_string_lossy().to_string())
                    .unwrap_or_else(|| db_path.display().to_string());
                CheckResult::ok("Database", format!("{name}, {size_mb:.1} MB"))
            }
            Err(e) => CheckResult::fail(
                "Database",
                format!("cannot read {}: {e}", db_path.display()),
            ),
        }
    } else {
        CheckResult::ok(
            "Database",
            format!("will be created at {}", db_path.display()),
        )
    }
}

/// Check LLM connectivity by creating the provider and calling health_check.
async fn check_llm_connectivity(config: &Config) -> CheckResult {
    let provider = match create_provider(&config.llm) {
        Ok(p) => p,
        Err(LlmError::NotConfigured) => {
            return CheckResult::fail("LLM connectivity", "provider not configured");
        }
        Err(e) => {
            return CheckResult::fail("LLM connectivity", format!("{}: {e}", config.llm.provider));
        }
    };

    match provider.health_check().await {
        Ok(()) => CheckResult::ok(
            "LLM connectivity",
            format!("{}: reachable", provider.name()),
        ),
        Err(e) => CheckResult::fail("LLM connectivity", format!("{}: {e}", provider.name())),
    }
}

/// Synchronous provider-creation check for unit tests (no network call).
#[cfg(test)]
fn check_llm_connectivity_sync(config: &tuitbot_core::config::LlmConfig) -> CheckResult {
    match create_provider(config) {
        Ok(p) => CheckResult::ok(
            "LLM connectivity",
            format!("{}: created (not tested)", p.name()),
        ),
        Err(LlmError::NotConfigured) => {
            CheckResult::fail("LLM connectivity", "provider not configured")
        }
        Err(e) => CheckResult::fail("LLM connectivity", format!("{e}")),
    }
}

/// Return a next-step hint if all checks passed, or `None` if any failed.
fn next_step_guidance(checks: &[CheckResult]) -> Option<&'static str> {
    if checks.iter().all(|r| r.passed) {
        Some("Ready! Try: tuitbot tick --dry-run")
    } else {
        None
    }
}

/// Print an enrichment hint when all checks pass but profile isn't fully enriched.
fn print_enrichment_hint(config: &Config) {
    let completeness = config.profile_completeness();
    if completeness.is_fully_enriched() {
        return;
    }

    eprintln!();
    eprintln!("Profile: {}", completeness.one_line_summary());

    if let Some(stage) = completeness.next_incomplete() {
        eprintln!(
            "Tip: Run `tuitbot settings enrich` to configure {} ({})",
            stage.label().to_lowercase(),
            stage.description()
        );
    }
}