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
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
use anyhow::Result;
use console::Style;
use tuitbot_core::config::Config;
use tuitbot_core::safety::redact::mask_optional_secret;

use crate::output::write_stdout;

pub(super) fn show_config(config: &Config) {
    let bold = Style::new().bold();
    let dim = Style::new().dim();

    eprintln!();
    eprintln!("{}", bold.apply_to("Tuitbot Configuration"));
    eprintln!("{}", dim.apply_to("══════════════════════"));

    // Product
    eprintln!();
    eprintln!("{}", bold.apply_to("Your Product"));
    eprintln!("  Product name:        {}", config.business.product_name);
    eprintln!(
        "  Description:         {}",
        config.business.product_description
    );
    eprintln!(
        "  Product URL:         {}",
        config
            .business
            .product_url
            .as_deref()
            .unwrap_or("(not set)")
    );
    eprintln!("  Target audience:     {}", config.business.target_audience);
    eprintln!(
        "  Discovery keywords:  {}",
        format_list(&config.business.product_keywords)
    );
    eprintln!(
        "  Competitor keywords: {}",
        format_list(&config.business.competitor_keywords)
    );
    eprintln!(
        "  Content topics:      {}",
        format_list(&config.business.industry_topics)
    );

    // Brand voice
    eprintln!();
    eprintln!("{}", bold.apply_to("Brand Voice"));
    eprintln!(
        "  Personality:         {}",
        config
            .business
            .brand_voice
            .as_deref()
            .unwrap_or("(default)")
    );
    eprintln!(
        "  Reply style:         {}",
        config
            .business
            .reply_style
            .as_deref()
            .unwrap_or("(default)")
    );
    eprintln!(
        "  Content style:       {}",
        config
            .business
            .content_style
            .as_deref()
            .unwrap_or("(default)")
    );

    // Persona
    eprintln!();
    eprintln!("{}", bold.apply_to("Persona"));
    eprintln!(
        "  Opinions:            {}",
        format_list(&config.business.persona_opinions)
    );
    eprintln!(
        "  Experiences:         {}",
        format_list(&config.business.persona_experiences)
    );
    eprintln!(
        "  Content pillars:     {}",
        format_list(&config.business.content_pillars)
    );

    // AI Provider
    eprintln!();
    eprintln!("{}", bold.apply_to("AI Provider"));
    eprintln!("  Provider:            {}", config.llm.provider);
    eprintln!(
        "  API key:             {}",
        mask_optional_secret(&config.llm.api_key)
    );
    eprintln!("  Model:               {}", config.llm.model);
    eprintln!(
        "  Base URL:            {}",
        config.llm.base_url.as_deref().unwrap_or("(default)")
    );

    // X API
    eprintln!();
    eprintln!("{}", bold.apply_to("X API Credentials"));
    eprintln!("  Client ID:           {}", config.x_api.client_id);
    eprintln!(
        "  Client secret:       {}",
        mask_optional_secret(&config.x_api.client_secret)
    );

    // Targets
    eprintln!();
    eprintln!("{}", bold.apply_to("Target Accounts"));
    eprintln!(
        "  Accounts:            {}",
        format_list(&config.targets.accounts)
    );
    eprintln!(
        "  Max replies/day:     {}",
        config.targets.max_target_replies_per_day
    );

    // Limits
    eprintln!();
    eprintln!("{}", bold.apply_to("Posting Limits"));
    eprintln!(
        "  Replies/day:         {}",
        config.limits.max_replies_per_day
    );
    eprintln!(
        "  Tweets/day:          {}",
        config.limits.max_tweets_per_day
    );
    eprintln!(
        "  Threads/week:        {}",
        config.limits.max_threads_per_week
    );
    eprintln!(
        "  Action delay:        {}-{} seconds",
        config.limits.min_action_delay_seconds, config.limits.max_action_delay_seconds
    );
    eprintln!(
        "  Replies/author/day:  {}",
        config.limits.max_replies_per_author_per_day
    );
    eprintln!(
        "  Product mention %:   {:.0}%",
        config.limits.product_mention_ratio * 100.0
    );
    eprintln!(
        "  Banned phrases:      {}",
        format_list(&config.limits.banned_phrases)
    );

    // Scoring
    eprintln!();
    eprintln!("{}", bold.apply_to("Scoring"));
    eprintln!(
        "  Threshold:           {} (0-100, higher = pickier)",
        config.scoring.threshold
    );
    eprintln!(
        "  Keyword relevance:   {:.0} pts max",
        config.scoring.keyword_relevance_max
    );
    eprintln!(
        "  Follower count:      {:.0} pts max",
        config.scoring.follower_count_max
    );
    eprintln!(
        "  Recency:             {:.0} pts max",
        config.scoring.recency_max
    );
    eprintln!(
        "  Engagement rate:     {:.0} pts max",
        config.scoring.engagement_rate_max
    );
    eprintln!(
        "  Reply count:         {:.0} pts max",
        config.scoring.reply_count_max
    );
    eprintln!(
        "  Content type:        {:.0} pts max",
        config.scoring.content_type_max
    );

    // Timing
    eprintln!();
    eprintln!("{}", bold.apply_to("Timing"));
    eprintln!(
        "  Mention checks:      {}",
        format_duration(config.intervals.mentions_check_seconds)
    );
    eprintln!(
        "  Discovery searches:  {}",
        format_duration(config.intervals.discovery_search_seconds)
    );
    eprintln!(
        "  Content post window: {}",
        format_duration(config.intervals.content_post_window_seconds)
    );
    eprintln!(
        "  Thread interval:     {}",
        format_duration(config.intervals.thread_interval_seconds)
    );

    // Approval
    eprintln!();
    eprintln!("{}", bold.apply_to("Approval Mode"));
    eprintln!(
        "  Enabled:             {}",
        if config.approval_mode { "yes" } else { "no" }
    );

    // Schedule
    eprintln!();
    eprintln!("{}", bold.apply_to("Schedule"));
    eprintln!("  Timezone:            {}", config.schedule.timezone);
    eprintln!(
        "  Active hours:        {}:00 – {}:00",
        config.schedule.active_hours_start, config.schedule.active_hours_end
    );
    eprintln!(
        "  Active days:         {}",
        format_list(&config.schedule.active_days)
    );
    eprintln!(
        "  Tweet times:         {}",
        if config.schedule.preferred_times.is_empty() {
            "(interval mode)".to_string()
        } else {
            format_list(&config.schedule.preferred_times)
        }
    );
    if !config.schedule.preferred_times_override.is_empty() {
        let mut overrides: Vec<_> = config.schedule.preferred_times_override.iter().collect();
        overrides.sort_by_key(|(k, _)| (*k).clone());
        for (day, times) in overrides {
            eprintln!(
                "    {} override:      {}",
                day,
                if times.is_empty() {
                    "(no posts)".to_string()
                } else {
                    times.join(", ")
                }
            );
        }
    }
    eprintln!(
        "  Thread day:          {}",
        config
            .schedule
            .thread_preferred_day
            .as_deref()
            .unwrap_or("(interval mode)")
    );
    eprintln!(
        "  Thread time:         {}",
        config.schedule.thread_preferred_time
    );

    // Storage & Logging
    eprintln!();
    eprintln!("{}", bold.apply_to("Storage & Logging"));
    eprintln!("  Database path:       {}", config.storage.db_path);
    eprintln!(
        "  Data retention:      {} days",
        config.storage.retention_days
    );
    eprintln!(
        "  Status interval:     {}",
        if config.logging.status_interval_seconds == 0 {
            "disabled".to_string()
        } else {
            format_duration(config.logging.status_interval_seconds)
        }
    );
    eprintln!();
}

/// Output configuration as JSON with secrets redacted.
pub(super) fn show_config_json(config: &Config) -> Result<()> {
    let mut config = config.clone();
    config.llm.api_key = config
        .llm
        .api_key
        .as_ref()
        .map(|_| "***REDACTED***".to_string());
    config.x_api.client_secret = config
        .x_api
        .client_secret
        .as_ref()
        .map(|_| "***REDACTED***".to_string());
    write_stdout(&serde_json::to_string(&config)?)?;
    Ok(())
}

pub(super) fn format_list(items: &[String]) -> String {
    if items.is_empty() {
        "(none)".to_string()
    } else {
        items.join(", ")
    }
}

pub(super) fn format_duration(seconds: u64) -> String {
    if seconds == 0 {
        "0 seconds".to_string()
    } else if seconds < 60 {
        format!("{seconds} seconds")
    } else if seconds < 3600 {
        let mins = seconds / 60;
        let remaining = seconds % 60;
        if remaining == 0 {
            format!("{mins} min")
        } else {
            format!("{mins} min {remaining} sec")
        }
    } else if seconds < 86400 {
        let hours = seconds / 3600;
        let remaining_mins = (seconds % 3600) / 60;
        if remaining_mins == 0 {
            format!("{hours} hour{}", if hours == 1 { "" } else { "s" })
        } else {
            format!(
                "{hours} hour{} {remaining_mins} min",
                if hours == 1 { "" } else { "s" }
            )
        }
    } else {
        let days = seconds / 86400;
        let remaining_hours = (seconds % 86400) / 3600;
        if remaining_hours == 0 {
            format!("{days} day{}", if days == 1 { "" } else { "s" })
        } else {
            format!(
                "{days} day{} {remaining_hours} hour{}",
                if days == 1 { "" } else { "s" },
                if remaining_hours == 1 { "" } else { "s" }
            )
        }
    }
}

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

    // ── format_list ──────────────────────────────────────────────────

    #[test]
    fn format_list_empty_returns_none_label() {
        assert_eq!(format_list(&[]), "(none)");
    }

    #[test]
    fn format_list_single_item() {
        let items = vec!["hello".to_string()];
        assert_eq!(format_list(&items), "hello");
    }

    #[test]
    fn format_list_multiple_items() {
        let items = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        assert_eq!(format_list(&items), "a, b, c");
    }

    // ── format_duration ──────────────────────────────────────────────

    #[test]
    fn format_duration_zero() {
        assert_eq!(format_duration(0), "0 seconds");
    }

    #[test]
    fn format_duration_1_second() {
        assert_eq!(format_duration(1), "1 seconds");
    }

    #[test]
    fn format_duration_59_seconds() {
        assert_eq!(format_duration(59), "59 seconds");
    }

    #[test]
    fn format_duration_exact_minutes() {
        assert_eq!(format_duration(60), "1 min");
        assert_eq!(format_duration(120), "2 min");
        assert_eq!(format_duration(300), "5 min");
    }

    #[test]
    fn format_duration_minutes_with_seconds() {
        assert_eq!(format_duration(90), "1 min 30 sec");
        assert_eq!(format_duration(150), "2 min 30 sec");
    }

    #[test]
    fn format_duration_exact_hours() {
        assert_eq!(format_duration(3600), "1 hour");
        assert_eq!(format_duration(7200), "2 hours");
    }

    #[test]
    fn format_duration_hours_with_minutes() {
        assert_eq!(format_duration(5400), "1 hour 30 min");
        assert_eq!(format_duration(9000), "2 hours 30 min");
    }

    #[test]
    fn format_duration_exact_days() {
        assert_eq!(format_duration(86400), "1 day");
        assert_eq!(format_duration(172800), "2 days");
    }

    #[test]
    fn format_duration_days_with_hours() {
        assert_eq!(format_duration(90000), "1 day 1 hour");
        assert_eq!(format_duration(176400), "2 days 1 hour");
        assert_eq!(format_duration(180000), "2 days 2 hours");
    }

    #[test]
    fn format_duration_one_week() {
        assert_eq!(format_duration(604800), "7 days");
    }

    // ── format_duration additional edge cases ────────────────────────

    #[test]
    fn format_duration_30_seconds() {
        assert_eq!(format_duration(30), "30 seconds");
    }

    #[test]
    fn format_duration_61_seconds() {
        assert_eq!(format_duration(61), "1 min 1 sec");
    }

    #[test]
    fn format_duration_3661_seconds() {
        assert_eq!(format_duration(3661), "1 hour 1 min");
    }

    #[test]
    fn format_duration_7201_seconds() {
        // 7201 = 2*3600 + 1; remaining_mins = 1/60 = 0
        assert_eq!(format_duration(7201), "2 hours");
    }

    #[test]
    fn format_duration_two_days_three_hours() {
        assert_eq!(format_duration(2 * 86400 + 3 * 3600), "2 days 3 hours");
    }

    #[test]
    fn format_duration_one_day_one_hour() {
        assert_eq!(format_duration(86400 + 3600), "1 day 1 hour");
    }

    #[test]
    fn format_duration_three_days_exact() {
        assert_eq!(format_duration(3 * 86400), "3 days");
    }

    // ── show_config_json ────────────────────────────────────────────

    #[test]
    fn show_config_json_redacts_secrets() {
        let mut config = Config::default();
        config.llm.api_key = Some("sk-secret-key-12345".to_string());
        config.x_api.client_secret = Some("secret-value".to_string());

        // We can't easily capture stdout in a test, but we can verify the
        // redaction logic directly:
        let mut redacted = config.clone();
        redacted.llm.api_key = redacted
            .llm
            .api_key
            .as_ref()
            .map(|_| "***REDACTED***".to_string());
        redacted.x_api.client_secret = redacted
            .x_api
            .client_secret
            .as_ref()
            .map(|_| "***REDACTED***".to_string());

        let json = serde_json::to_string(&redacted).unwrap();
        assert!(json.contains("***REDACTED***"));
        assert!(!json.contains("sk-secret-key-12345"));
        assert!(!json.contains("secret-value"));
    }

    #[test]
    fn show_config_json_no_secrets_still_works() {
        let config = Config::default();
        let mut redacted = config.clone();
        redacted.llm.api_key = redacted
            .llm
            .api_key
            .as_ref()
            .map(|_| "***REDACTED***".to_string());
        let json = serde_json::to_string(&redacted).unwrap();
        assert!(!json.contains("***REDACTED***")); // None -> no redaction needed
    }

    // ── show_config display branches ────────────────────────────────

    #[test]
    fn show_config_does_not_panic() {
        let config = Config::default();
        show_config(&config);
    }

    #[test]
    fn show_config_with_all_fields_set_does_not_panic() {
        let mut config = Config::default();
        config.business.product_url = Some("https://example.com".to_string());
        config.business.brand_voice = Some("Friendly".to_string());
        config.business.reply_style = Some("Helpful".to_string());
        config.business.content_style = Some("Practical".to_string());
        config.llm.api_key = Some("sk-test".to_string());
        config.x_api.client_secret = Some("secret".to_string());
        config.schedule.preferred_times = vec!["09:00".to_string(), "12:00".to_string()];
        config
            .schedule
            .preferred_times_override
            .insert("Mon".to_string(), vec!["10:00".to_string()]);
        config.schedule.thread_preferred_day = Some("Tue".to_string());
        show_config(&config);
    }

    #[test]
    fn show_config_with_empty_preferred_times() {
        let config = Config::default();
        // exercises the "(interval mode)" branch
        show_config(&config);
    }

    #[test]
    fn show_config_with_logging_disabled() {
        let mut config = Config::default();
        config.logging.status_interval_seconds = 0;
        show_config(&config);
    }

    #[test]
    fn show_config_with_logging_enabled() {
        let mut config = Config::default();
        config.logging.status_interval_seconds = 300;
        show_config(&config);
    }

    #[test]
    fn show_config_approval_mode_on() {
        let mut config = Config::default();
        config.approval_mode = true;
        show_config(&config);
    }

    #[test]
    fn show_config_approval_mode_off() {
        let mut config = Config::default();
        config.approval_mode = false;
        show_config(&config);
    }

    // ── format_list additional cases ─────────────────────────────────

    #[test]
    fn format_list_many_items() {
        let items: Vec<String> = (0..10).map(|i| format!("item{i}")).collect();
        let result = format_list(&items);
        assert!(result.contains("item0"));
        assert!(result.contains("item9"));
        assert_eq!(result.matches(", ").count(), 9);
    }
}