clauth 0.5.6

Simple Claude Code account switcher and usage monitor
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
#![allow(unsafe_code)]
//! Showcase — a fake-data TUI for taking README screenshots. Compiled ONLY
//! under `#[cfg(test)]` (included via `#[path]` into `crate::tui`), so none of
//! this ships in the `clauth` binary and it lives outside `src/`.
//!
//! Launch it in a real terminal (it takes over the screen; press q twice to quit):
//!
//! ```text
//! cargo test showcase -- --ignored --nocapture
//! ```
//!
//! All tests redirect `~/.clauth` and `~/.claude` into a tempdir via
//! [`crate::profile::set_home_override`] so real files are never touched.

use std::collections::BTreeMap;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use anyhow::Result;
use ratatui::crossterm::event::{
    self, Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers,
};
use tempfile::TempDir;

use super::{TICK, Term, app, render, restore_terminal, setup_terminal};
use crate::profile::{AppConfig, AppState, Profile, ProfileName};
use crate::usage::{
    ExtraUsage, FetchStatus, PlanInfo, ProfileActivity, UsageInfo, UsageWindow, now_ms,
};

// ── Launch ──────────────────────────────────────────────────────────────────

#[test]
#[ignore = "interactive TUI; run with `cargo test showcase -- --ignored --nocapture` in a real terminal"]
fn showcase() {
    run(demo_config()).expect("showcase loop");
}

/// Redirect home into a tempdir so all disk ops land on scratch space.
struct ShowcaseHome {
    _home_lock: std::sync::MutexGuard<'static, ()>,
    _tmp: TempDir,
}

impl ShowcaseHome {
    fn new() -> Self {
        let _home_lock = crate::profile::HOME_TEST_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = TempDir::new().expect("tempdir for showcase");
        let path = tmp.path().to_path_buf();
        std::fs::create_dir_all(&path).expect("create temp home");
        crate::profile::set_home_override(path);

        // Pre-seed usage history files on disk so on_tick → apply_usage doesn't
        // overwrite the in-memory seed with an empty/partial reload.
        for (name, entries) in &build_synthetic_history() {
            let history_path = crate::profile::profile_history_path(name).expect("history path");
            std::fs::create_dir_all(history_path.parent().unwrap()).expect("history dir");
            let content: String = entries
                .iter()
                .map(|(ts, usage)| {
                    let usage_json = serde_json::to_string(usage).unwrap_or_default();
                    let name_json =
                        serde_json::to_string(name).unwrap_or_else(|_| format!(r#""{}""#, name));
                    format!(
                        r#"{{"ts":{},"name":{},"usage":{}}}"#,
                        ts, name_json, usage_json
                    )
                })
                .collect::<Vec<_>>()
                .join("\n")
                + "\n";
            std::fs::write(&history_path, content).expect("write seeded history");
        }

        Self {
            _home_lock,
            _tmp: tmp,
        }
    }
}

/// Build the same synthetic history used by [`seed_history`], returned as a map
/// so callers can write it to disk or populate the in-memory cache.
fn build_synthetic_history() -> std::collections::HashMap<String, Vec<(u64, UsageInfo)>> {
    use std::collections::HashMap;
    let now = now_ms();

    let mut personal: Vec<(u64, UsageInfo)> = Vec::with_capacity(40);
    for i in 0..=30 {
        let ts = now - (30 - i) as u64 * 480_000;
        let pct = 45.0 + (i as f64 / 30.0) * (64.3 - 45.0);
        personal.push((
            ts,
            UsageInfo {
                five_hour: Some(UsageWindow {
                    utilization: pct,
                    resets_at: None,
                }),
                seven_day_sonnet: Some(UsageWindow {
                    utilization: 22.1,
                    resets_at: None,
                }),
                seven_day_opus: Some(UsageWindow {
                    utilization: 8.4,
                    resets_at: None,
                }),
                ..UsageInfo::default()
            },
        ));
    }
    for i in 0..=10 {
        let ts = now - (10 - i) as u64 * 43_200_000;
        personal.push((
            ts,
            UsageInfo {
                five_hour: None,
                seven_day_sonnet: Some(UsageWindow {
                    utilization: 22.1,
                    resets_at: None,
                }),
                seven_day_opus: Some(UsageWindow {
                    utilization: 8.4,
                    resets_at: None,
                }),
                ..UsageInfo::default()
            },
        ));
    }

    let mut work: Vec<(u64, UsageInfo)> = Vec::with_capacity(40);
    for i in 0..=30 {
        let ts = now - (30 - i) as u64 * 480_000;
        let pct = 55.0 + (i as f64 / 30.0) * (88.7 - 55.0);
        work.push((
            ts,
            UsageInfo {
                five_hour: Some(UsageWindow {
                    utilization: pct,
                    resets_at: None,
                }),
                seven_day_sonnet: Some(UsageWindow {
                    utilization: 61.2,
                    resets_at: None,
                }),
                seven_day_opus: Some(UsageWindow {
                    utilization: 33.9,
                    resets_at: None,
                }),
                ..UsageInfo::default()
            },
        ));
    }
    for i in 0..=10 {
        let ts = now - (10 - i) as u64 * 43_200_000;
        work.push((
            ts,
            UsageInfo {
                five_hour: None,
                seven_day_sonnet: Some(UsageWindow {
                    utilization: 61.2,
                    resets_at: None,
                }),
                seven_day_opus: Some(UsageWindow {
                    utilization: 33.9,
                    resets_at: None,
                }),
                ..UsageInfo::default()
            },
        ));
    }

    let mut side: Vec<(u64, UsageInfo)> = Vec::with_capacity(40);
    for i in 0..=30 {
        let ts = now - (30 - i) as u64 * 480_000;
        let pct = 5.0 + (i as f64 / 30.0) * (12.0 - 5.0);
        side.push((
            ts,
            UsageInfo {
                five_hour: Some(UsageWindow {
                    utilization: pct,
                    resets_at: None,
                }),
                ..UsageInfo::default()
            },
        ));
    }

    let mut cache = HashMap::new();
    cache.insert("personal".to_string(), personal);
    cache.insert("work".to_string(), work);
    cache.insert("side-project".to_string(), side);
    cache
}

/// Same as [`super::run`] but with home redirected into a tempdir.
fn run(config: AppConfig) -> Result<()> {
    let _home = ShowcaseHome::new();
    let mut terminal = setup_terminal()?;
    let outcome = showcase_loop(&mut terminal, config);
    let restore = restore_terminal(&mut terminal);
    outcome.and(restore)
}

/// Real event loop without startup reconciliation — no bootstrap/scheduler spawns.
fn showcase_loop(terminal: &mut Term, config: AppConfig) -> Result<()> {
    let mut application = app::App::new(config);
    seed_usage(&application);
    seed_timers(&application);
    seed_history(&application);
    let mut last_tick = Instant::now();

    while !application.quit {
        terminal.draw(|frame| render::draw(frame, &application))?;

        let timeout = TICK.saturating_sub(last_tick.elapsed());
        if event::poll(timeout)? {
            match event::read()? {
                Event::Key(key) if key.kind == KeyEventKind::Press => {
                    app::handle_key(&mut application, key);
                }
                Event::Resize(_, _) => {}
                _ => {}
            }
        }

        if last_tick.elapsed() >= TICK {
            app::on_tick(&mut application);
            last_tick = Instant::now();
        }
    }

    Ok(())
}

/// RFC3339-ish string for `now + offset` (matches `iso_to_epoch_secs` format).
fn future_iso(offset: Duration) -> String {
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
        + offset.as_secs();
    // no chrono dep; YYYY-MM-DDTHH:MM:SS+00:00 shape iso_to_epoch_secs parses
    let (y, mo, d, h, mi, sec) = epoch_to_parts(secs);
    format!("{y:04}-{mo:02}-{d:02}T{h:02}:{mi:02}:{sec:02}+00:00")
}

fn epoch_to_parts(secs: u64) -> (u64, u64, u64, u64, u64, u64) {
    let s = secs % 60;
    let m = (secs / 60) % 60;
    let h = (secs / 3600) % 24;
    let days = secs / 86400;
    // Gregorian civil calendar — Howard Hinnant's algorithm, unsigned edition.
    let z = days + 719468;
    let era = z / 146097;
    let doe = z - era * 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let mo = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if mo <= 2 { y + 1 } else { y };
    (y, mo, d, h, m, s)
}

#[allow(clippy::too_many_arguments)]
fn oauth_profile(
    name: &str,
    plan_type: &str,
    tier: &str,
    has_max: bool,
    has_pro: bool,
    auto_start: bool,
    fallback_threshold: Option<f64>,
    five_util: f64,
    five_resets_in: Option<Duration>,
    seven_sonnet: Option<(f64, Duration)>,
    seven_opus: Option<(f64, Duration)>,
    extra: Option<ExtraUsage>,
    fetch_status: Option<FetchStatus>,
) -> Profile {
    let five_hour = Some(UsageWindow {
        utilization: five_util,
        resets_at: five_resets_in.map(future_iso),
    });
    let seven_day_sonnet = seven_sonnet.map(|(u, reset)| UsageWindow {
        utilization: u,
        resets_at: Some(future_iso(reset)),
    });
    let seven_day_opus = seven_opus.map(|(u, reset)| UsageWindow {
        utilization: u,
        resets_at: Some(future_iso(reset)),
    });
    Profile {
        name: name.into(),
        base_url: None,
        api_key: None,
        auto_start,
        env: BTreeMap::new(),
        fallback_threshold,
        bell_threshold: None,
        credentials: None,
        usage: Some(UsageInfo {
            plan: Some(PlanInfo {
                organization_type: Some(plan_type.to_string()),
                rate_limit_tier: Some(tier.to_string()),
                has_max,
                has_pro,
            }),
            five_hour,
            seven_day: None,
            seven_day_sonnet,
            seven_day_opus,
            extra_usage: extra,
        }),
        fetch_status,
        provider: None,
        third_party_usage: None,
    }
}

fn api_profile(name: &str) -> Profile {
    Profile {
        name: name.into(),
        base_url: Some("https://api.example.com".to_string()),
        api_key: Some(
            "sk-ant-api03-demo0000000000000000000000000000000000000000000000".to_string(),
        ),
        auto_start: false,
        env: BTreeMap::new(),
        fallback_threshold: None,
        bell_threshold: None,
        credentials: None,
        usage: None,
        fetch_status: None,
        provider: None,
        third_party_usage: None,
    }
}

fn failed_profile(name: &str) -> Profile {
    Profile {
        name: name.into(),
        base_url: None,
        api_key: None,
        auto_start: false,
        env: BTreeMap::new(),
        fallback_threshold: Some(90.0),
        bell_threshold: None,
        credentials: None,
        usage: None,
        fetch_status: Some(FetchStatus::Failed),
        provider: None,
        third_party_usage: None,
    }
}

fn demo_config() -> AppConfig {
    let max20 = oauth_profile(
        "personal",
        "claude_max",
        "default_claude_max_20x",
        true,
        false,
        true,
        Some(80.0),
        64.3,
        Some(Duration::from_secs(2 * 3600 + 17 * 60)), // ~2h17m
        Some((22.1, Duration::from_secs(5 * 86400 + 6 * 3600))), // 7d sonnet ~5d
        Some((8.4, Duration::from_secs(6 * 86400 + 2 * 3600))), // 7d opus ~6d
        None,
        None,
    );

    let extra = ExtraUsage {
        is_enabled: true,
        monthly_limit: Some(100.00),
        used_credits: Some(42.50),
        utilization: Some(42.5),
        currency: Some("USD".to_string()),
    };
    let max5 = oauth_profile(
        "work",
        "claude_max",
        "default_claude_max_5x",
        true,
        false,
        true,
        Some(90.0),
        88.7,
        Some(Duration::from_secs(45 * 60)), // ~45m
        Some((61.2, Duration::from_secs(3 * 86400 + 9 * 3600))), // 7d sonnet ~3d
        Some((33.9, Duration::from_secs(6 * 86400 + 3600))), // 7d opus ~6d
        Some(extra),
        Some(FetchStatus::Cached), // cached → warning underline
    );

    let pro = oauth_profile(
        "side-project",
        "claude_pro",
        "default_claude_pro",
        false,
        true,
        false,
        Some(100.0),
        12.0,
        Some(Duration::from_secs(4 * 3600 + 5 * 60)),
        None,
        None,
        None,
        None,
    );

    let api = api_profile("bedrock-dev");

    let stale = failed_profile("research");

    let names: Vec<ProfileName> = [
        "personal",
        "work",
        "side-project",
        "bedrock-dev",
        "research",
    ]
    .iter()
    .map(|s| (*s).into())
    .collect();

    AppConfig {
        state: AppState {
            active_profile: Some("personal".into()),
            profiles: names,
            fallback_chain: vec!["personal".into(), "work".into(), "side-project".into()],
            ..AppState::default()
        },
        profiles: vec![max20, max5, pro, api, stale],
    }
}

// ── Seeding ─────────────────────────────────────────────────────────────────

/// Seed the live usage stores from demo profile data, as a real fetch worker would.
/// Without this, the first `on_tick` → `apply_usage` blanks all windows to `-`.
fn seed_usage(application: &app::App) {
    let snapshot: Vec<(String, Option<UsageInfo>, Option<FetchStatus>)> = {
        let cfg = application.config();
        cfg.profiles
            .iter()
            .map(|p| (p.name.to_string(), p.usage.clone(), p.fetch_status))
            .collect()
    };
    if let Ok(mut store) = application.usage_store.lock() {
        for (name, usage, _) in &snapshot {
            if let Some(u) = usage {
                store.insert(name.clone(), u.clone());
            }
        }
    }
    if let Ok(mut status) = application.usage_status.lock() {
        for (name, _, fetch_status) in &snapshot {
            if let Some(s) = fetch_status {
                status.insert(name.clone(), *s);
            }
        }
    }
}

/// Seed timer sources so the overview shows a spinner and countdowns (no
/// scheduler runs in the demo). Both stores are leaf-rank, in-memory only.
fn seed_timers(application: &app::App) {
    let now = now_ms();
    if let Ok(mut next) = application.next_refresh_per_profile.lock() {
        next.insert("work".to_string(), now + 43_000); // ~43s
        next.insert("side-project".to_string(), now + 78_000); // ~78s
    }
    if let Ok(mut activity) = application.activity.lock() {
        activity.insert("personal".to_string(), ProfileActivity::Fetching);
    }
}

/// Seed `history_cache` with mock usage data so `compute_burn_rates_from_history`
/// has entries to derive burn-rate predictions for the usage-tab detail pane.
///
/// Each profile gets a synthetic timeline: ~30 entries over the last ~4h for 5h
/// windows, and ~12 entries over the last ~5d for 7d windows, with plausible
/// utilization ramps so the burn-rate math yields non-zero results.
///
/// The same data is pre-seeded as disk files by [`ShowcaseHome::new`] so
/// `apply_usage`'s history reload preserves the full timeline.
fn seed_history(application: &app::App) {
    let cache = build_synthetic_history();
    unsafe {
        let app_ptr = application as *const app::App as *mut app::App;
        (*app_ptr).history_cache = cache;
    }
}

// ── Unit tests on demo data ──────────────────────────────────────────────────

#[test]
fn demo_config_has_expected_profiles() {
    let cfg = demo_config();
    assert_eq!(cfg.profiles.len(), 5);
    assert_eq!(cfg.state.active_profile.as_deref(), Some("personal"));
    assert_eq!(cfg.state.fallback_chain.len(), 3);

    let personal = cfg.profiles.iter().find(|p| p.name == "personal");
    assert!(personal.is_some_and(|p| p.auto_start && p.base_url.is_none()));

    let work = cfg.profiles.iter().find(|p| p.name == "work");
    assert!(work.is_some_and(|p| {
        p.fetch_status == Some(FetchStatus::Cached)
            && p.usage
                .as_ref()
                .and_then(|u| u.extra_usage.as_ref())
                .is_some_and(|e| e.is_enabled)
    }));

    let api = cfg.profiles.iter().find(|p| p.name == "bedrock-dev");
    assert!(api.is_some_and(|p| !p.is_oauth()));

    let failed = cfg.profiles.iter().find(|p| p.name == "research");
    assert!(
        failed.is_some_and(|p| p.fetch_status == Some(FetchStatus::Failed) && p.usage.is_none())
    );
}

#[test]
fn future_iso_parses() {
    use crate::usage::iso_to_epoch_secs;
    let s = future_iso(Duration::from_secs(3600));
    assert!(iso_to_epoch_secs(&s).is_some());
}

/// Synthetic history entries must be parseable by the burn-rate engine.
#[test]
fn seed_history_yields_burn_rates() {
    let _home = ShowcaseHome::new();
    let app = app::App::new(demo_config());
    seed_usage(&app);
    seed_history(&app);

    let personal = app.history_cache.get("personal").unwrap();
    assert!(
        personal.len() >= 30,
        "personal must have enough history entries for burn-rate window"
    );

    let work = app.history_cache.get("work").unwrap();
    assert!(
        work.len() >= 30,
        "work must have enough history entries for burn-rate window"
    );
}

/// Headless render gate: draws every tab and a post-tick frame through
/// `TestBackend` on the shared demo data. The `term.draw(...).unwrap()` is the
/// no-panic invariant for each surface; the content asserts prove the populated
/// (non-empty-state) layout actually rendered. Runs under plain `cargo test` —
/// no TTY, no `--ignored` — so CI gates that the surfaces render.
#[test]
fn headless_showcase_renders() {
    // Home redirected into a tempdir + disk history seeded; held for the whole
    // fn so on_tick writes land on scratch. Acquired BEFORE App::new so no
    // RankedMutex is ever held while taking the (untracked) HOME_TEST_LOCK.
    let _home = ShowcaseHome::new();
    let mut app = app::App::new(demo_config());
    seed_usage(&app);
    seed_timers(&app);
    seed_history(&app);

    // 120 wide so the overview name column keeps "personal"/"work" un-truncated.
    let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 30)).unwrap();

    // Flatten the backend buffer to a newline-joined String of cell symbols.
    fn flatten(term: &ratatui::Terminal<ratatui::backend::TestBackend>) -> String {
        let buf = term.backend().buffer().clone();
        let (w, h) = (buf.area.width as usize, buf.area.height as usize);
        let mut out = String::with_capacity((w + 1) * h);
        for y in 0..h {
            for x in 0..w {
                out.push_str(buf.content[y * w + x].symbol());
            }
            out.push('\n');
        }
        out
    }

    // Every tab renders without panicking and keeps the header brand.
    for tab in app::Tab::ALL {
        app.tab = tab;
        term.draw(|f| render::draw(f, &app)).unwrap();
        let frame = flatten(&term);
        assert!(frame.contains("clauth"), "header brand renders on {tab:?}");
    }

    // One tick exercises the drain/apply_usage/reload paths against the tempdir.
    app.tab = app::Tab::Overview;
    app::on_tick(&mut app);
    term.draw(|f| render::draw(f, &app)).unwrap();
    let overview = flatten(&term);

    assert!(overview.contains("clauth"), "header brand survives a tick");
    assert!(
        overview.contains("personal"),
        "active demo profile populates the overview list"
    );
    assert!(
        overview.contains("work"),
        "second profile row renders in the overview list"
    );
    // Tab-bar labels render (lowercase, un-truncated at 120w); `setup`/`config`
    // are tab-only words on the overview surface, so finding them proves the bar.
    assert!(
        overview.contains("setup") && overview.contains("config"),
        "tab bar renders its labels"
    );
}

// ── Non-interactive driver ──────────────────────────────────────────────────
//
// Feeds synthetic key events through `handle_key` / `on_tick` so CI can prove
// every action — switch, edit, toggle, reorder, threshold, delete, create —
// without a TTY.  HOME_OVERRIDE points at a tempdir so all disk operations
// (save_profile, save_app_state, flock, symlinks, history) land on scratch
// space and never touch the real filesystem.

use crate::testutil::key;

fn key_shift(code: KeyCode) -> KeyEvent {
    KeyEvent {
        code,
        modifiers: KeyModifiers::SHIFT,
        kind: KeyEventKind::Press,
        state: KeyEventState::NONE,
    }
}

fn key_ctrl(code: KeyCode) -> KeyEvent {
    KeyEvent {
        code,
        modifiers: KeyModifiers::CONTROL,
        kind: KeyEventKind::Press,
        state: KeyEventState::NONE,
    }
}

fn press(app: &mut app::App, code: KeyCode) {
    app::handle_key(app, key(code));
}

/// Type a string one `Char` event at a time.
fn type_str(app: &mut app::App, s: &str) {
    for c in s.chars() {
        app::handle_key(app, key(KeyCode::Char(c)));
    }
}

/// Drain `on_tick` until `pred` holds or budget exhausted (async worker results).
fn settle(app: &mut app::App, what: &str, mut pred: impl FnMut(&app::App) -> bool) {
    for _ in 0..400 {
        app::on_tick(app);
        if pred(app) {
            return;
        }
        std::thread::sleep(Duration::from_millis(5));
    }
    panic!("'{what}' never settled after draining ticks");
}

/// Helper: read profile fields without holding the config guard across handle_key.
fn base_url_of(app: &app::App, name: &str) -> Option<String> {
    app.config().find(name).and_then(|p| p.base_url.clone())
}

fn auto_start_of(app: &app::App, name: &str) -> bool {
    app.config()
        .find(name)
        .map(|p| p.auto_start)
        .unwrap_or(false)
}

fn threshold_of(app: &app::App, name: &str) -> Option<f64> {
    app.config().find(name).and_then(|p| p.fallback_threshold)
}

#[test]
fn demo_data_drives_all_actions() {
    let _home = ShowcaseHome::new();
    let mut app = app::App::new(demo_config());
    seed_usage(&app);
    seed_timers(&app);
    seed_history(&app);

    // reconcile_startup never called → no bootstrap, no scheduler.
    assert!(!app.reconcile_done && !app.bootstrap_started);

    // Pre-drive: timer slot has a spinner for the active profile and a countdown for an idle one.
    assert_eq!(
        app.activity.lock().unwrap().get("personal").copied(),
        Some(ProfileActivity::Fetching),
        "active profile must show a spinner in the timer slot"
    );
    assert!(
        app.next_refresh_per_profile
            .lock()
            .unwrap()
            .contains_key("work"),
        "idle profiles must show a refresh countdown in the timer slot"
    );

    // ── Tab navigation ──
    use app::Tab;
    assert_eq!(app.tab, Tab::Overview);
    press(&mut app, KeyCode::Right);
    assert_eq!(app.tab, Tab::Usage);
    press(&mut app, KeyCode::Right);
    assert_eq!(app.tab, Tab::Setup);
    press(&mut app, KeyCode::Right);
    assert_eq!(app.tab, Tab::Fallback);
    press(&mut app, KeyCode::Right);
    assert_eq!(app.tab, Tab::Config);
    press(&mut app, KeyCode::Right);
    assert_eq!(app.tab, Tab::Status);
    press(&mut app, KeyCode::Right);
    assert_eq!(app.tab, Tab::Overview, "→ wraps back to Overview");
    press(&mut app, KeyCode::Left);
    assert_eq!(app.tab, Tab::Status, "← wraps to the last tab");
    // Five ← from the last tab walk back to the first.
    for _ in 0..5 {
        press(&mut app, KeyCode::Left);
    }
    assert_eq!(app.tab, Tab::Overview);

    // ── Switch ──
    assert_eq!(
        app.config().state.active_profile.as_deref(),
        Some("personal")
    );
    press(&mut app, KeyCode::Down); // 0 (personal) → 1 (work)
    press(&mut app, KeyCode::Enter); // request switch → confirm modal
    assert_eq!(app.modals.len(), 1, "switch raises a confirm modal");
    press(&mut app, KeyCode::Enter); // accept (choice defaults to yes)
    assert!(app.modals.is_empty(), "confirming pops the modal");
    settle(&mut app, "switch to work", |a| {
        a.config().state.active_profile.as_deref() == Some("work")
    });

    // Seeded usage windows must survive on_tick → apply_usage during the switch.
    {
        let cfg = app.config();
        let util = cfg
            .find("personal")
            .and_then(|p| p.usage.as_ref())
            .and_then(|u| u.five_hour.as_ref())
            .map(|w| w.utilization);
        assert_eq!(
            util,
            Some(64.3),
            "seeded 5h utilization must survive on_tick → apply_usage"
        );
    }

    // State file written to the tempdir, not the real home.
    assert!(
        crate::profile::app_state_mtime().is_some(),
        "app state must be written (to the tempdir)"
    );

    // ── Edit ── (cursor at "work"/1 after switch; one ↓ → "side-project"/2)
    press(&mut app, KeyCode::Right); // Overview → Usage
    press(&mut app, KeyCode::Right); // Usage → Setup
    assert_eq!(app.tab, Tab::Setup);
    assert_eq!(app.profile_cursor, 1, "cursor carried over from the switch");
    press(&mut app, KeyCode::Down); // 1 → 2 (side-project)
    press(&mut app, KeyCode::Enter); // focus the detail pane
    assert_eq!(app.config_focus, app::ConfigFocus::Actions);
    assert!(app.config_draft.is_some());
    press(&mut app, KeyCode::Down); // Name → BaseUrl
    press(&mut app, KeyCode::Enter); // start capturing the field
    assert_eq!(
        app.config_draft.as_ref().and_then(|d| d.active),
        Some(app::ConfigRow::BaseUrl)
    );
    type_str(&mut app, "https://proxy.test");
    press(&mut app, KeyCode::Enter); // commit the field
    assert_eq!(
        base_url_of(&app, "side-project").as_deref(),
        Some("https://proxy.test"),
        "editing the BaseUrl field must persist it"
    );
    press(&mut app, KeyCode::Esc); // back out of the detail pane
    assert_eq!(app.config_focus, app::ConfigFocus::Profiles);

    // ── Toggle ──
    assert!(auto_start_of(&app, "personal"), "demo seeds personal ON");
    press(&mut app, KeyCode::Up); // 2 → 1 (work)
    press(&mut app, KeyCode::Up); // → 0 (personal)
    press(&mut app, KeyCode::Enter); // focus detail for personal
    press(&mut app, KeyCode::Down); // Name → BaseUrl
    press(&mut app, KeyCode::Down); // BaseUrl → ApiKey
    press(&mut app, KeyCode::Down); // ApiKey → AutoStart
    press(&mut app, KeyCode::Enter); // flip it
    assert!(
        !auto_start_of(&app, "personal"),
        "auto-start must toggle off"
    );
    press(&mut app, KeyCode::Esc);

    // ── Reorder ──
    press(&mut app, KeyCode::Right); // Config → Fallback
    assert_eq!(app.tab, Tab::Fallback);
    {
        let cfg = app.config();
        assert_eq!(
            cfg.state.fallback_chain,
            vec!["personal", "work", "side-project"]
        );
    }
    app::handle_key(&mut app, key_shift(KeyCode::Down)); // head → one step down
    {
        let cfg = app.config();
        assert_eq!(
            cfg.state.fallback_chain,
            vec!["work", "personal", "side-project"],
            "⇧↓ reorders the chain"
        );
    }
    assert_eq!(app.chain_cursor, 1, "cursor follows the moved member");

    // ── Set threshold (stepper) ── "personal" is chain index 1, 80% → 85%
    assert_eq!(threshold_of(&app, "personal"), Some(80.0));
    press(&mut app, KeyCode::Enter); // enter member detail pane
    assert_eq!(app.fallback_focus, app::FallbackFocus::Detail);
    press(&mut app, KeyCode::Char('+')); // +5 step
    assert_eq!(
        threshold_of(&app, "personal"),
        Some(85.0),
        "the + stepper bumps the threshold by 5"
    );

    // ── Set threshold (inline editor) ──
    press(&mut app, KeyCode::Enter); // open inline editor on Threshold row
    assert!(app.fallback_threshold_draft.is_some());
    press(&mut app, KeyCode::Backspace); // clear "85" (2 chars)
    press(&mut app, KeyCode::Backspace);

    // Out-of-range value: commit is blocked and the draft stays open so the
    // inline Invalid-input treatment shows.
    type_str(&mut app, "150");
    press(&mut app, KeyCode::Enter); // commit attempt — rejected
    assert!(
        app.fallback_threshold_draft.is_some(),
        "an out-of-range threshold keeps the editor open (inline invalid, no toast)"
    );
    assert_eq!(
        threshold_of(&app, "personal"),
        Some(85.0),
        "the rejected value never persists"
    );

    // ctrl+w wipes the bad input as one word, then a valid value commits.
    app::handle_key(&mut app, key_ctrl(KeyCode::Char('w')));
    assert_eq!(
        app.fallback_threshold_draft
            .as_ref()
            .map(|d| d.value.as_str()),
        Some(""),
        "ctrl+w clears the whole typed run"
    );
    type_str(&mut app, "50");
    press(&mut app, KeyCode::Enter); // commit
    assert!(app.fallback_threshold_draft.is_none());
    assert_eq!(
        threshold_of(&app, "personal"),
        Some(50.0),
        "the inline editor sets an absolute threshold"
    );
    press(&mut app, KeyCode::Esc); // leave the detail pane
    assert_eq!(app.fallback_focus, app::FallbackFocus::Chain);

    // ── Delete ──
    let before = app.profile_count();
    press(&mut app, KeyCode::Left); // Fallback → Setup
    assert_eq!(app.tab, Tab::Setup);
    for _ in 0..4 {
        press(&mut app, KeyCode::Down); // 0 → 4 ("research")
    }
    press(&mut app, KeyCode::Enter); // focus detail
    for _ in 0..4 {
        press(&mut app, KeyCode::Down); // Name → Delete (last row)
    }
    press(&mut app, KeyCode::Enter); // arm
    assert!(
        app.config_draft
            .as_ref()
            .map(|d| d.armed_delete)
            .unwrap_or(false),
        "first ⏎ arms the delete row"
    );
    press(&mut app, KeyCode::Enter); // confirm
    assert_eq!(app.profile_count(), before - 1, "delete drops one profile");
    assert!(
        app.config().find("research").is_none(),
        "the deleted profile is gone from the config"
    );

    // ── Create ──
    let before = app.profile_count();
    // Navigate to the + new row (last slot in Setup list)
    press(&mut app, KeyCode::Down); // 4 → end ("+ new")
    press(&mut app, KeyCode::Enter); // focus detail (auto-positions on Name)
    assert_eq!(app.config_focus, app::ConfigFocus::Actions);
    assert!(app.config_draft.is_some());

    // Fill the create form: Name, BaseUrl, ApiKey
    // Cursor starts on first row (Name)
    press(&mut app, KeyCode::Enter); // start capturing Name
    type_str(&mut app, "sandbox-test");
    press(&mut app, KeyCode::Enter); // commit Name

    // Navigate down to BaseUrl, fill it
    press(&mut app, KeyCode::Down); // Name → BaseUrl
    press(&mut app, KeyCode::Enter); // start capturing
    type_str(&mut app, "https://api.sandbox.test");
    press(&mut app, KeyCode::Enter); // commit BaseUrl

    // Move to ApiKey, fill it
    press(&mut app, KeyCode::Down); // BaseUrl → ApiKey
    press(&mut app, KeyCode::Enter); // start capturing
    type_str(&mut app, "sk-test-key-0000");
    press(&mut app, KeyCode::Enter); // commit ApiKey

    // Navigate to Create row and commit
    press(&mut app, KeyCode::Down); // ApiKey → Create

    assert!(
        app.config_draft
            .as_ref()
            .map(|d| d.editing_name.is_none())
            .unwrap_or(false),
        "Create row must only appear in new-account mode"
    );

    press(&mut app, KeyCode::Enter); // commit Create
    assert_eq!(
        app.profile_count(),
        before + 1,
        "create must add one profile"
    );
    let created = {
        let cfg = app.config();
        cfg.find("sandbox-test")
            .map(|p| (p.base_url.clone(), p.api_key.clone()))
    };
    assert!(
        created.is_some(),
        "the created profile must exist in config"
    );
    assert_eq!(
        created.as_ref().and_then(|(b, _)| b.as_deref()),
        Some("https://api.sandbox.test"),
        "created profile must preserve base_url"
    );
    assert_eq!(
        created.as_ref().and_then(|(_, k)| k.as_deref()),
        Some("sk-test-key-0000"),
        "created profile must preserve api_key"
    );

    // Clean up — delete what we just created
    // "sandbox-test" has base_url set → is_oauth = false → 4 rows (Name, BaseUrl, ApiKey, Delete)
    let before = app.profile_count();
    press(&mut app, KeyCode::Enter); // focus detail for sandbox-test
    assert_eq!(app.config_focus, app::ConfigFocus::Actions);
    for _ in 0..3 {
        press(&mut app, KeyCode::Down); // Name → BaseUrl → ApiKey → Delete
    }
    press(&mut app, KeyCode::Enter); // arm
    assert!(
        app.config_draft
            .as_ref()
            .map(|d| d.armed_delete)
            .unwrap_or(false)
    );
    press(&mut app, KeyCode::Enter); // confirm delete
    assert_eq!(
        app.profile_count(),
        before - 1,
        "delete the newly created profile"
    );
    assert!(
        app.config().find("sandbox-test").is_none(),
        "sandbox-test must be gone after delete"
    );

    // ── History cache seeded for usage predictions ──
    {
        let personal = app.history_cache.get("personal");
        assert!(
            personal.is_some(),
            "history_cache must be seeded for personal"
        );
        let personal = personal.unwrap();
        assert!(
            personal.len() >= 30,
            "personal must have enough history entries for burn rates"
        );

        // Burn rates should compute from the seeded history after apply_usage
        // (the first on_tick calls apply_usage which populates profile.usage
        // from usage_store, which was seeded in seed_usage).
    }

    // ── Usage tab shows prediction data ──
    // Navigate to usage tab and verify the profile detail renders with history.
    // The renderer uses history_cache for burn-rate computation.
    press(&mut app, KeyCode::Right); // Setup → Fallback
    press(&mut app, KeyCode::Right); // Fallback → Config
    press(&mut app, KeyCode::Right); // Config → Status
    // One more right wraps back to Overview
    press(&mut app, KeyCode::Right); // Status → Overview
    assert_eq!(app.tab, Tab::Overview);

    // ── Quit ──
    press(&mut app, KeyCode::Char('q'));
    assert!(app.armed_quit, "first q arms the quit");
    assert!(!app.quit, "first q does not quit yet");
    assert!(app.footer_alert.is_some(), "first q sets a footer alert");

    // any non-q key disarms the alert (Esc at top level is a no-op but still disarms)
    press(&mut app, KeyCode::Esc);
    assert!(!app.armed_quit, "unhandled key disarms quit");
    assert!(app.footer_alert.is_none(), "disarm clears footer alert");

    // x dismisses the alert directly when no toasts are queued
    app.toasts.clear(); // drain any toasts from earlier actions
    press(&mut app, KeyCode::Char('q'));
    assert!(app.footer_alert.is_some(), "re-arm sets alert");
    press(&mut app, KeyCode::Char('x'));
    assert!(app.footer_alert.is_none(), "x dismisses the footer alert");
    assert!(!app.armed_quit, "x also disarms quit");

    press(&mut app, KeyCode::Char('q'));
    press(&mut app, KeyCode::Char('q'));
    assert!(app.quit, "second q confirms quit");

    // All disk writes landed in the tempdir — clean-up on drop.
}