ai-usagebar 0.21.0

Waybar widget + TUI for tracking AI plan usage across multiple providers
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
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
//! Which Claude account the `claude` **CLI** is signed into, and moving that
//! login between managed accounts.
//!
//! This is a separate identity from the Claude Desktop app's (see
//! [`crate::claude_desktop`]) — the two drift apart constantly, which is most
//! of why they are both worth reporting.
//!
//! The CLI keeps exactly **one** default login: `~/.claude/.credentials.json`
//! on Linux, the login-Keychain item `Claude Code-credentials` on macOS. A
//! named account instead lives under its own `CLAUDE_CONFIG_DIR`, in the
//! per-directory Keychain item [`crate::anthropic::keychain`] resolves. Making
//! a named account "the one plain `claude` uses" therefore means moving its
//! credential into that single default slot.
//!
//! Copying would leave both slots holding the same *rotating* refresh token,
//! and whichever client refreshed first would invalidate the other — the
//! failure [`crate::anthropic::creds`] documents. The switch therefore moves
//! the credential: it captures the outgoing default credential back into its
//! named slot first, installs the target in the default slot, and removes the
//! target's named copy. In addition,
//! [`crate::config::AnthropicConfig::account_target_with`] routes reads for
//! whichever label is *currently* active to the default slot, so only one copy
//! is ever live.
//!
//! Everything except [`KeychainStore`] is platform-agnostic and unit-tested
//! against a fake store, so the logic stays under CI's linter on Linux.

use std::path::Path;
use std::time::Duration;

use serde_json::{Map, Value};

use crate::config::AnthropicAccount;
use crate::error::{AppError, Result};

/// The `claude` CLI's per-config-dir state file. Holds a plaintext
/// `oauthAccount` identity marker (uuid, email, org) — never a token.
const CLAUDE_JSON: &str = ".claude.json";

/// Read/write access to the credential slots. Abstracted so the switch logic
/// can be tested without a macOS Keychain.
pub trait CredentialStore {
    fn read_default(&self) -> Result<Option<String>>;
    fn write_default(&self, blob: &str) -> Result<()>;
    fn delete_default(&self) -> Result<()>;
    fn read_named(&self, config_dir: &Path) -> Result<Option<String>>;
    fn write_named(&self, config_dir: &Path, blob: &str) -> Result<()>;
    fn delete_named(&self, config_dir: &Path) -> Result<()>;
}

/// The real store. The `#[cfg]` pairs are confined here so no other logic in
/// this module is invisible to a Linux build — the same shape
/// [`crate::anthropic::creds::resolve`] uses.
pub struct KeychainStore;

/// Path to the home `.claude.json` that records the live CLI identity.
pub fn home_claude_json() -> Result<std::path::PathBuf> {
    Ok(crate::cache::home_dir()?.join(CLAUDE_JSON))
}

/// The identity marker inside an account's own `CLAUDE_CONFIG_DIR`.
pub fn marker_path(config_dir: &Path) -> std::path::PathBuf {
    config_dir.join(CLAUDE_JSON)
}

/// The account UUID recorded in a `.claude.json`, if any.
pub fn account_uuid_in(claude_json: &Path) -> Option<String> {
    oauth_account_in(claude_json)?
        .get("accountUuid")?
        .as_str()
        .filter(|uuid| !uuid.is_empty())
        .map(str::to_string)
}

/// The e-mail recorded alongside it, for display only.
pub fn account_email_in(claude_json: &Path) -> Option<String> {
    oauth_account_in(claude_json)?
        .get("emailAddress")?
        .as_str()
        .filter(|email| !email.is_empty())
        .map(str::to_string)
}

/// Which managed label the live CLI login belongs to.
///
/// `None` when the CLI is signed into something we do not manage — a plain
/// `claude` login done by hand, say. Callers surface that as "unknown" rather
/// than hiding it: it is exactly the case where the named copies may have gone
/// stale behind our back.
pub fn resolve_active_label(
    home_claude_json: &Path,
    accounts: &[AnthropicAccount],
) -> Option<String> {
    let live = account_uuid_in(home_claude_json)?;
    accounts
        .iter()
        .find(|account| {
            account_uuid_in(&account.config_dir().join(CLAUDE_JSON)) == Some(live.clone())
        })
        .map(|account| account.label.clone())
}

/// Set (or clear) the `oauthAccount` marker in a `.claude.json`, preserving
/// every other key. That document is tens of kilobytes of unrelated CLI state,
/// so it is merged into, never replaced.
pub fn merge_oauth_account(existing: &[u8], oauth_account: Option<&Value>) -> Result<Vec<u8>> {
    let mut document: Value = if existing.iter().all(u8::is_ascii_whitespace) {
        Value::Object(Map::new())
    } else {
        serde_json::from_slice(existing)?
    };
    let object = document
        .as_object_mut()
        .ok_or_else(|| AppError::Other(format!("{CLAUDE_JSON} is not a JSON object")))?;
    match oauth_account {
        Some(account) => {
            object.insert("oauthAccount".into(), account.clone());
        }
        // Better an absent marker than one that names the previous account.
        None => {
            object.remove("oauthAccount");
        }
    }
    Ok(serde_json::to_vec(&document)?)
}

#[derive(Debug, Clone, Copy, Default)]
pub struct CliSwitchOpts {
    /// Overwrite the default slot even though the live login belongs to no
    /// managed account. That login cannot be captured anywhere first, so this
    /// genuinely destroys it.
    pub force: bool,
    /// Validate everything and report, without writing.
    pub dry_run: bool,
}

#[derive(Debug, PartialEq, Eq)]
pub enum CliSwitchOutcome {
    AlreadyActive,
    /// The account was already active, and a redundant named Keychain copy was
    /// removed so the rotating token again has one live lineage.
    RemovedDuplicate,
    /// Dry-run counterpart of [`Self::RemovedDuplicate`].
    WouldRemoveDuplicate,
    /// The identity marker named this account but the default credential slot
    /// was empty; its named credential was moved into the default slot.
    RepairedActive,
    /// `outgoing` is the label whose credential was captured back first, when
    /// there was one.
    Switched {
        outgoing: Option<String>,
    },
    /// `dry_run` was set; nothing was written.
    WouldSwitch {
        outgoing: Option<String>,
    },
}

/// Make `label` the account plain `claude` uses.
///
/// Ordering is deliberate. The outgoing credential is captured back **before**
/// anything is overwritten — that is the only irreversible step, and a failure
/// there aborts. The default slot is then written **before** the identity
/// marker, because the credential is the source of truth: a marker claiming an
/// account whose token is not there is worse than the reverse.
pub fn switch_cli_account(
    home_claude_json: &Path,
    accounts: &[AnthropicAccount],
    label: &str,
    opts: CliSwitchOpts,
    store: &dyn CredentialStore,
) -> Result<CliSwitchOutcome> {
    let lock_path = home_claude_json
        .parent()
        .unwrap_or_else(|| Path::new("."))
        .join(".ai-usagebar-account-switch.lock");
    let _lock = crate::cache::acquire_lock(&lock_path, Duration::from_secs(2))?;

    let target = accounts
        .iter()
        .find(|account| account.label == label)
        .ok_or_else(|| {
            let known: Vec<&str> = accounts.iter().map(|a| a.label.as_str()).collect();
            AppError::Credentials(format!(
                "no Claude CLI account {label:?} in [[anthropic.accounts]] or accounts_dir; \
                 known: {known:?}"
            ))
        })?;

    let active = resolve_active_label(home_claude_json, accounts);

    // Read every piece needed for both the move and its rollback before the
    // first write. An empty default slot is safe to populate without --force;
    // an unrecognised *existing* login is the destructive case.
    let original_default = store.read_default()?;
    if active.as_deref() == Some(label) && original_default.is_some() {
        if store.read_named(&target.config_dir())?.is_none() {
            return Ok(CliSwitchOutcome::AlreadyActive);
        }
        if opts.dry_run {
            return Ok(CliSwitchOutcome::WouldRemoveDuplicate);
        }
        store.delete_named(&target.config_dir())?;
        return Ok(CliSwitchOutcome::RemovedDuplicate);
    }
    if active.is_none() && original_default.is_some() && !opts.force {
        return Err(AppError::Credentials(format!(
            "the `claude` CLI is signed into an account that is not managed here, so \
             switching to {label:?} would overwrite a login that cannot be saved first. \
             Register it with `ai-usagebar account add <label>`, or pass --force to \
             discard it."
        )));
    }

    // Fail before touching anything if the target has never been signed in.
    let target_blob = store.read_named(&target.config_dir())?.ok_or_else(|| {
        AppError::Credentials(format!(
            "no stored credential for {label:?}; sign it in once with \
             `ai-usagebar account add {label}`"
        ))
    })?;
    let oauth_account =
        oauth_account_in(&target.config_dir().join(CLAUDE_JSON)).ok_or_else(|| {
            AppError::Credentials(format!(
                "the identity marker for {label:?} is missing or invalid; sign it in again with \
             `ai-usagebar account add {label}` before switching"
            ))
        })?;
    let original_marker = read_optional(home_claude_json)?;
    let merged_marker = merge_oauth_account(
        original_marker.as_deref().unwrap_or_default(),
        Some(&oauth_account),
    )?;

    let outgoing_account = active
        .as_deref()
        .and_then(|outgoing| accounts.iter().find(|account| account.label == outgoing));
    let original_outgoing_named = match outgoing_account {
        Some(account) => store.read_named(&account.config_dir())?,
        None => None,
    };

    if opts.dry_run {
        return Ok(CliSwitchOutcome::WouldSwitch { outgoing: active });
    }

    let rollback = RollbackState {
        target,
        target_blob: &target_blob,
        outgoing: outgoing_account,
        original_outgoing_named: original_outgoing_named.as_deref(),
        original_default: original_default.as_deref(),
        marker_path: home_claude_json,
        original_marker: original_marker.as_deref(),
    };

    if let (Some(account), Some(blob)) = (outgoing_account, original_default.as_deref())
        && let Err(error) = store.write_named(&account.config_dir(), blob)
    {
        return Err(with_rollback(error, rollback_switch(store, &rollback)));
    }
    if let Err(error) = store.write_default(&target_blob) {
        return Err(with_rollback(error, rollback_switch(store, &rollback)));
    }

    if let Err(error) = crate::cache::atomic_write(home_claude_json, &merged_marker) {
        return Err(with_rollback(error, rollback_switch(store, &rollback)));
    }
    if let Err(error) = store.delete_named(&target.config_dir()) {
        return Err(with_rollback(error, rollback_switch(store, &rollback)));
    }

    if active.as_deref() == Some(label) {
        Ok(CliSwitchOutcome::RepairedActive)
    } else {
        Ok(CliSwitchOutcome::Switched { outgoing: active })
    }
}

struct RollbackState<'a> {
    target: &'a AnthropicAccount,
    target_blob: &'a str,
    outgoing: Option<&'a AnthropicAccount>,
    original_outgoing_named: Option<&'a str>,
    original_default: Option<&'a str>,
    marker_path: &'a Path,
    original_marker: Option<&'a [u8]>,
}

fn rollback_switch(store: &dyn CredentialStore, state: &RollbackState<'_>) -> Result<()> {
    let mut failures = Vec::new();
    if let Err(error) = restore_default(store, state.original_default) {
        failures.push(format!("default credential: {error}"));
    }
    // Recreate it unconditionally: a failed Keychain delete can still have
    // removed the item before returning its error.
    if let Err(error) = store.write_named(&state.target.config_dir(), state.target_blob) {
        failures.push(format!("target credential: {error}"));
    }
    if let Some(account) = state.outgoing
        && let Err(error) =
            restore_named(store, &account.config_dir(), state.original_outgoing_named)
    {
        failures.push(format!("outgoing credential: {error}"));
    }
    if let Err(error) = restore_marker(state.marker_path, state.original_marker) {
        failures.push(format!("identity marker: {error}"));
    }
    if failures.is_empty() {
        Ok(())
    } else {
        Err(AppError::Other(failures.join("; ")))
    }
}

fn restore_default(store: &dyn CredentialStore, original: Option<&str>) -> Result<()> {
    match original {
        Some(blob) => store.write_default(blob),
        None => store.delete_default(),
    }
}

fn restore_named(store: &dyn CredentialStore, path: &Path, original: Option<&str>) -> Result<()> {
    match original {
        Some(blob) => store.write_named(path, blob),
        None => store.delete_named(path),
    }
}

fn restore_marker(path: &Path, original: Option<&[u8]>) -> Result<()> {
    if let Some(bytes) = original {
        crate::cache::atomic_write(path, bytes)
    } else {
        match std::fs::remove_file(path) {
            Ok(()) => Ok(()),
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(error) => Err(AppError::io_at(path, error)),
        }
    }
}

fn read_optional(path: &Path) -> Result<Option<Vec<u8>>> {
    match std::fs::read(path) {
        Ok(bytes) => Ok(Some(bytes)),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(AppError::io_at(path, error)),
    }
}

fn with_rollback(error: AppError, rollback: Result<()>) -> AppError {
    match rollback {
        Ok(()) => error,
        Err(rollback) => AppError::Other(format!(
            "{error}; automatic rollback was incomplete: {rollback}"
        )),
    }
}

fn oauth_account_in(claude_json: &Path) -> Option<Value> {
    let bytes = std::fs::read(claude_json).ok()?;
    let document: Value = serde_json::from_slice(&bytes).ok()?;
    document
        .get("oauthAccount")
        .filter(|v| v.is_object())
        .cloned()
}

#[cfg(not(target_os = "macos"))]
fn unsupported() -> AppError {
    AppError::Credentials(
        "switching the `claude` CLI login is supported on macOS only (elsewhere, run \
         `CLAUDE_CONFIG_DIR=<account dir> claude` to use a specific account)"
            .into(),
    )
}

impl CredentialStore for KeychainStore {
    fn read_default(&self) -> Result<Option<String>> {
        #[cfg(target_os = "macos")]
        return super::keychain::read_raw();
        #[cfg(not(target_os = "macos"))]
        Err(unsupported())
    }

    fn write_default(&self, blob: &str) -> Result<()> {
        #[cfg(target_os = "macos")]
        return super::keychain::write_raw(blob);
        #[cfg(not(target_os = "macos"))]
        {
            let _ = blob;
            Err(unsupported())
        }
    }

    fn delete_default(&self) -> Result<()> {
        #[cfg(target_os = "macos")]
        return super::keychain::delete_raw();
        #[cfg(not(target_os = "macos"))]
        Err(unsupported())
    }

    fn read_named(&self, config_dir: &Path) -> Result<Option<String>> {
        #[cfg(target_os = "macos")]
        return super::keychain::read_raw_for(config_dir);
        #[cfg(not(target_os = "macos"))]
        {
            let _ = config_dir;
            Err(unsupported())
        }
    }

    fn write_named(&self, config_dir: &Path, blob: &str) -> Result<()> {
        #[cfg(target_os = "macos")]
        return super::keychain::write_raw_for(config_dir, blob);
        #[cfg(not(target_os = "macos"))]
        {
            let _ = (config_dir, blob);
            Err(unsupported())
        }
    }

    fn delete_named(&self, config_dir: &Path) -> Result<()> {
        #[cfg(target_os = "macos")]
        return super::keychain::delete_raw_for(config_dir);
        #[cfg(not(target_os = "macos"))]
        {
            let _ = config_dir;
            Err(unsupported())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::RefCell;
    use std::collections::BTreeMap;
    use std::path::PathBuf;

    #[derive(Default)]
    struct FakeStore {
        default: RefCell<Option<String>>,
        named: RefCell<BTreeMap<PathBuf, String>>,
        fail_delete_named: RefCell<Option<PathBuf>>,
    }

    impl CredentialStore for FakeStore {
        fn read_default(&self) -> Result<Option<String>> {
            Ok(self.default.borrow().clone())
        }
        fn write_default(&self, blob: &str) -> Result<()> {
            *self.default.borrow_mut() = Some(blob.to_string());
            Ok(())
        }
        fn delete_default(&self) -> Result<()> {
            *self.default.borrow_mut() = None;
            Ok(())
        }
        fn read_named(&self, config_dir: &Path) -> Result<Option<String>> {
            Ok(self.named.borrow().get(config_dir).cloned())
        }
        fn write_named(&self, config_dir: &Path, blob: &str) -> Result<()> {
            self.named
                .borrow_mut()
                .insert(config_dir.to_path_buf(), blob.to_string());
            Ok(())
        }
        fn delete_named(&self, config_dir: &Path) -> Result<()> {
            self.named.borrow_mut().remove(config_dir);
            if self.fail_delete_named.borrow().as_deref() == Some(config_dir) {
                Err(AppError::Other("injected delete failure".into()))
            } else {
                Ok(())
            }
        }
    }

    fn write(path: &Path, contents: &str) {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, contents).unwrap();
    }

    fn marker(uuid: &str, email: &str) -> String {
        format!(r#"{{"oauthAccount":{{"accountUuid":"{uuid}","emailAddress":"{email}"}}}}"#)
    }

    struct Fixture {
        _root: tempfile::TempDir,
        home: PathBuf,
        accounts: Vec<AnthropicAccount>,
        store: FakeStore,
    }

    /// Two accounts, `work` and `personal`; the CLI is signed into `personal`.
    fn fixture() -> Fixture {
        let root = tempfile::TempDir::new().unwrap();
        let home = root.path().join("home").join(CLAUDE_JSON);
        write(&home, &marker("uuid-personal", "me@personal.test"));

        let accounts: Vec<AnthropicAccount> = ["work", "personal"]
            .iter()
            .map(|label| AnthropicAccount {
                label: (*label).to_string(),
                credentials_path: root
                    .path()
                    .join("accounts")
                    .join(label)
                    .join(".credentials.json"),
            })
            .collect();
        write(
            &accounts[0].config_dir().join(CLAUDE_JSON),
            &marker("uuid-work", "me@work.test"),
        );
        write(
            &accounts[1].config_dir().join(CLAUDE_JSON),
            &marker("uuid-personal", "me@personal.test"),
        );

        let store = FakeStore::default();
        *store.default.borrow_mut() = Some("personal-live".into());
        store
            .named
            .borrow_mut()
            .insert(accounts[0].config_dir(), "work-saved".into());
        store
            .named
            .borrow_mut()
            .insert(accounts[1].config_dir(), "personal-stale".into());

        Fixture {
            _root: root,
            home,
            accounts,
            store,
        }
    }

    #[test]
    fn the_live_login_resolves_to_its_label() {
        let f = fixture();
        assert_eq!(
            resolve_active_label(&f.home, &f.accounts).as_deref(),
            Some("personal")
        );
        assert_eq!(
            account_email_in(&f.home).as_deref(),
            Some("me@personal.test")
        );
    }

    #[test]
    fn an_unmanaged_login_resolves_to_nothing() {
        let f = fixture();
        write(&f.home, &marker("uuid-stranger", "who@example.test"));
        assert_eq!(resolve_active_label(&f.home, &f.accounts), None);
    }

    #[test]
    fn a_missing_marker_file_is_not_an_error() {
        let f = fixture();
        std::fs::remove_file(&f.home).unwrap();
        assert_eq!(resolve_active_label(&f.home, &f.accounts), None);
        assert_eq!(account_uuid_in(&f.home), None);
    }

    #[test]
    fn switching_captures_the_outgoing_credential_first() {
        let f = fixture();

        let outcome = switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap();

        assert_eq!(
            outcome,
            CliSwitchOutcome::Switched {
                outgoing: Some("personal".into())
            }
        );
        // The freshest lineage was written back into personal's own slot before
        // the default slot was overwritten.
        assert_eq!(
            f.store.named.borrow()[&f.accounts[1].config_dir()],
            "personal-live"
        );
        assert_eq!(f.store.default.borrow().as_deref(), Some("work-saved"));
        assert!(
            !f.store
                .named
                .borrow()
                .contains_key(&f.accounts[0].config_dir()),
            "the active credential must be moved, not left as a rotating-token copy"
        );
        assert_eq!(
            resolve_active_label(&f.home, &f.accounts).as_deref(),
            Some("work")
        );
    }

    #[test]
    fn switching_is_idempotent() {
        let f = fixture();
        let outcome = switch_cli_account(
            &f.home,
            &f.accounts,
            "personal",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap();
        assert_eq!(outcome, CliSwitchOutcome::RemovedDuplicate);
        assert_eq!(f.store.default.borrow().as_deref(), Some("personal-live"));
        assert!(
            !f.store
                .named
                .borrow()
                .contains_key(&f.accounts[1].config_dir())
        );

        let second = switch_cli_account(
            &f.home,
            &f.accounts,
            "personal",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap();
        assert_eq!(second, CliSwitchOutcome::AlreadyActive);
    }

    #[test]
    fn an_active_marker_with_an_empty_default_slot_is_repaired() {
        let f = fixture();
        *f.store.default.borrow_mut() = None;

        let outcome = switch_cli_account(
            &f.home,
            &f.accounts,
            "personal",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap();

        assert_eq!(outcome, CliSwitchOutcome::RepairedActive);
        assert_eq!(f.store.default.borrow().as_deref(), Some("personal-stale"));
        assert!(
            !f.store
                .named
                .borrow()
                .contains_key(&f.accounts[1].config_dir())
        );
    }

    #[test]
    fn a_dry_run_validates_without_writing() {
        let f = fixture();
        let before = std::fs::read(&f.home).unwrap();

        let outcome = switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts {
                dry_run: true,
                ..CliSwitchOpts::default()
            },
            &f.store,
        )
        .unwrap();

        assert_eq!(
            outcome,
            CliSwitchOutcome::WouldSwitch {
                outgoing: Some("personal".into())
            }
        );
        assert_eq!(f.store.default.borrow().as_deref(), Some("personal-live"));
        assert_eq!(std::fs::read(&f.home).unwrap(), before);
    }

    #[test]
    fn an_unmanaged_live_login_is_refused_without_force() {
        let f = fixture();
        write(&f.home, &marker("uuid-stranger", "who@example.test"));

        let error = switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap_err();
        assert!(error.to_string().contains("--force"), "{error}");
        assert_eq!(f.store.default.borrow().as_deref(), Some("personal-live"));

        switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts {
                force: true,
                ..CliSwitchOpts::default()
            },
            &f.store,
        )
        .unwrap();
        assert_eq!(f.store.default.borrow().as_deref(), Some("work-saved"));
        assert!(
            !f.store
                .named
                .borrow()
                .contains_key(&f.accounts[0].config_dir())
        );
    }

    #[test]
    fn an_empty_default_slot_does_not_require_force() {
        let f = fixture();
        *f.store.default.borrow_mut() = None;
        std::fs::remove_file(&f.home).unwrap();

        switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap();

        assert_eq!(f.store.default.borrow().as_deref(), Some("work-saved"));
        assert_eq!(
            resolve_active_label(&f.home, &f.accounts).as_deref(),
            Some("work")
        );
    }

    #[test]
    fn a_target_without_an_identity_marker_changes_nothing() {
        let f = fixture();
        std::fs::remove_file(f.accounts[0].config_dir().join(CLAUDE_JSON)).unwrap();
        let before = std::fs::read(&f.home).unwrap();

        let error = switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap_err();

        assert!(error.to_string().contains("identity marker"), "{error}");
        assert_eq!(f.store.default.borrow().as_deref(), Some("personal-live"));
        assert_eq!(std::fs::read(&f.home).unwrap(), before);
    }

    #[test]
    fn a_late_failure_restores_every_credential_slot_and_marker() {
        let f = fixture();
        let before_marker = std::fs::read(&f.home).unwrap();
        *f.store.fail_delete_named.borrow_mut() = Some(f.accounts[0].config_dir());

        let error = switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap_err();

        assert!(
            error.to_string().contains("injected delete failure"),
            "{error}"
        );
        assert_eq!(f.store.default.borrow().as_deref(), Some("personal-live"));
        assert_eq!(
            f.store.named.borrow()[&f.accounts[0].config_dir()],
            "work-saved"
        );
        assert_eq!(
            f.store.named.borrow()[&f.accounts[1].config_dir()],
            "personal-stale"
        );
        assert_eq!(std::fs::read(&f.home).unwrap(), before_marker);
    }

    #[test]
    fn switching_to_an_account_that_never_signed_in_changes_nothing() {
        let f = fixture();
        f.store
            .named
            .borrow_mut()
            .remove(&f.accounts[0].config_dir());

        let error = switch_cli_account(
            &f.home,
            &f.accounts,
            "work",
            CliSwitchOpts::default(),
            &f.store,
        )
        .unwrap_err();
        assert!(error.to_string().contains("account add work"), "{error}");
        assert_eq!(f.store.default.borrow().as_deref(), Some("personal-live"));
    }

    #[test]
    fn merging_the_marker_preserves_unrelated_state() {
        let existing = br#"{"firstStartTime":"2026-01-01","oauthAccount":{"accountUuid":"old"},
            "projects":{"/tmp/x":{"allowedTools":[]}}}"#;
        let replacement = serde_json::json!({"accountUuid": "new", "emailAddress": "a@b.test"});

        let bytes = merge_oauth_account(existing, Some(&replacement)).unwrap();
        let value: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(value["oauthAccount"]["accountUuid"], "new");
        assert_eq!(value["firstStartTime"], "2026-01-01");
        assert!(value["projects"]["/tmp/x"].is_object());
    }

    #[test]
    fn merging_no_marker_clears_a_stale_one() {
        let existing = br#"{"oauthAccount":{"accountUuid":"old"},"autoUpdates":true}"#;
        let bytes = merge_oauth_account(existing, None).unwrap();
        let value: Value = serde_json::from_slice(&bytes).unwrap();
        assert!(value.get("oauthAccount").is_none());
        assert_eq!(value["autoUpdates"], true);
    }

    #[test]
    fn merging_into_an_absent_file_starts_a_fresh_document() {
        let replacement = serde_json::json!({"accountUuid": "new"});
        let bytes = merge_oauth_account(b"", Some(&replacement)).unwrap();
        let value: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(value["oauthAccount"]["accountUuid"], "new");
    }
}