clauth 0.5.2

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
//! Behaviour tests for `rotation_candidates` — the filter that decides which
//! profiles `refresh_all` will attempt to rotate.
//!
//! These tests never touch the network. They assert on the candidate list
//! returned by `rotation_candidates`, which is the only part of `refresh_all`
//! that `force` affects.

use super::*;
use crate::lockorder::RankedMutex;
use crate::profile::{AppState, ClaudeCredentials, OAuthToken, Profile, profile_dir};
use crate::runtime::open_pid_file;
use crate::usage::is_idle;

fn single_profile_config(name: &str, refresh_token: &str) -> AppConfig {
    use std::collections::BTreeMap;
    let profile = Profile {
        name: name.into(),
        base_url: None,
        api_key: None,
        auto_start: false,
        env: BTreeMap::new(),
        fallback_threshold: None,
        credentials: Some(ClaudeCredentials {
            claude_ai_oauth: Some(OAuthToken {
                access_token: "at".to_string(),
                refresh_token: Some(refresh_token.to_string()),
                expires_at: None,
                scopes: None,
                subscription_type: None,
            }),
        }),
        usage: None,
        fetch_status: None,
    };
    let mut config = AppConfig {
        state: AppState::default(),
        profiles: vec![profile],
    };
    config.state.profiles.push(name.into());
    config
}

/// RAII home sandbox: holds `HOME_TEST_LOCK` and redirects `home_dir()` into a
/// tempdir for the test's duration, clearing on drop (even on panic). Required
/// for any test that creates session dirs, pid files, or rotation locks —
/// including indirectly via `RotationGuard::acquire` — or those paths land in
/// the real `~/.clauth`.
struct HomeSandbox {
    // Drop order: tempdir first, then shared lock.
    _tmp: tempfile::TempDir,
    _guard: std::sync::MutexGuard<'static, ()>,
}

impl HomeSandbox {
    fn new() -> Self {
        let guard = crate::profile::HOME_TEST_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = tempfile::tempdir().expect("create home sandbox");
        crate::profile::set_home_override(tmp.path().to_path_buf());
        Self {
            _tmp: tmp,
            _guard: guard,
        }
    }
}

impl Drop for HomeSandbox {
    fn drop(&mut self) {
        crate::profile::clear_home_override();
    }
}

#[test]
fn no_live_session_included_with_force_false() {
    let config = single_profile_config("test-oauth-no-session-force-false", "rt-abc");
    let candidates = rotation_candidates(&config, false);
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].0, "test-oauth-no-session-force-false");
    assert_eq!(candidates[0].1, "rt-abc");
}

#[test]
fn no_live_session_included_with_force_true() {
    let config = single_profile_config("test-oauth-no-session-force-true", "rt-def");
    let candidates = rotation_candidates(&config, true);
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].0, "test-oauth-no-session-force-true");
}

#[test]
fn live_session_excluded_when_force_false() {
    let _home = HomeSandbox::new();
    let name = "test-oauth-live-session-guard";
    let sessions = profile_dir(name).expect("profile_dir").join("sessions");
    std::fs::create_dir_all(&sessions).expect("create sessions dir");
    let pid_file = sessions.join("test-pid");
    let file = open_pid_file(&pid_file).expect("open pid file");
    file.lock().expect("lock pid file");

    let config = single_profile_config(name, "rt-ghi");
    let candidates = rotation_candidates(&config, false);
    assert!(
        candidates.is_empty(),
        "force=false should exclude a profile with a live session"
    );

    drop(file);
}

#[test]
fn live_session_included_when_force_true() {
    let _home = HomeSandbox::new();
    let name = "test-oauth-live-session-force";
    let sessions = profile_dir(name).expect("profile_dir").join("sessions");
    std::fs::create_dir_all(&sessions).expect("create sessions dir");
    let pid_file = sessions.join("test-pid");
    let file = open_pid_file(&pid_file).expect("open pid file");
    file.lock().expect("lock pid file");

    let config = single_profile_config(name, "rt-jkl");
    let candidates = rotation_candidates(&config, true);
    assert_eq!(
        candidates.len(),
        1,
        "force=true should include a profile with a live session"
    );
    assert_eq!(candidates[0].0, name);

    drop(file);
}

#[test]
fn force_true_bypasses_diverged_active_when_no_active_profile() {
    // active_profile is None → active_link_diverged returns false → both force values include the
    // profile. The meaningful contract is `!force && active_link_diverged(config)` (was
    // `active_link_diverged(config)`, ignoring force); tested here without FS side effects.
    let config = single_profile_config("test-oauth-force-diverged", "rt-xyz");
    let force_false = rotation_candidates(&config, false);
    let force_true = rotation_candidates(&config, true);
    assert_eq!(force_false.len(), 1);
    assert_eq!(force_true.len(), 1);
    assert_eq!(force_true[0].0, "test-oauth-force-diverged");
}

/// `rotate_one_inner` must not stamp `Refreshing` when no refresh token —
/// the short-circuit runs before any HTTP, leaving the activity slot Idle.
#[test]
fn rotate_one_no_stamp_when_no_refresh_token() {
    use std::collections::BTreeMap;
    use std::sync::mpsc;

    let _home = HomeSandbox::new();
    let profile = Profile {
        name: "test-rotate-one-no-rt".into(),
        base_url: None,
        api_key: None,
        auto_start: false,
        env: BTreeMap::new(),
        fallback_threshold: None,
        credentials: Some(ClaudeCredentials {
            claude_ai_oauth: Some(OAuthToken {
                access_token: "at".to_string(),
                refresh_token: None,
                expires_at: None,
                scopes: None,
                subscription_type: None,
            }),
        }),
        usage: None,
        fetch_status: None,
    };
    let mut config = AppConfig {
        state: AppState::default(),
        profiles: vec![profile],
    };
    config.state.profiles.push("test-rotate-one-no-rt".into());

    let config = Arc::new(RankedMutex::new(config));
    let activity: ActivityStore = Arc::new(RankedMutex::new(std::collections::HashMap::new()));
    let (tx, _rx) = mpsc::channel();

    let result = rotate_one_inner(
        &config,
        "test-rotate-one-no-rt",
        Some(&activity),
        &tx,
        false,
    );

    assert!(
        matches!(result, RotateOutcome::Persisted(false)),
        "rotate_one_inner should return Persisted(false) when no refresh token"
    );
    assert!(
        is_idle(&activity, "test-rotate-one-no-rt"),
        "activity slot must remain Idle when rotation short-circuits at no-token"
    );
}

#[test]
fn profile_without_refresh_token_excluded() {
    use std::collections::BTreeMap;
    let profile = Profile {
        name: "test-oauth-no-rt".into(),
        base_url: None,
        api_key: None,
        auto_start: false,
        env: BTreeMap::new(),
        fallback_threshold: None,
        credentials: Some(ClaudeCredentials {
            claude_ai_oauth: Some(OAuthToken {
                access_token: "at".to_string(),
                refresh_token: None,
                expires_at: None,
                scopes: None,
                subscription_type: None,
            }),
        }),
        usage: None,
        fetch_status: None,
    };
    let mut config = AppConfig {
        state: AppState::default(),
        profiles: vec![profile],
    };
    config.state.profiles.push("test-oauth-no-rt".into());
    assert!(rotation_candidates(&config, false).is_empty()); // no refresh token → excluded regardless of force
    assert!(rotation_candidates(&config, true).is_empty());
}

/// Per-profile rotation lock: acquiring for `b` must not block while `a` is held.
/// Without this, `refresh_all` workers would serialize behind the slowest profile.
/// `b` is acquired on a separate thread because the ROTATION rank forbids a single
/// thread from re-entering it — exactly the cross-thread guarantee needed.
#[test]
fn rotation_guard_is_independent_across_profiles() {
    use crate::runtime::RotationGuard;
    use std::sync::mpsc;
    use std::time::Duration;

    // HOME_OVERRIDE is process-global, so the worker thread's acquire also resolves into the sandbox.
    let _home = HomeSandbox::new();
    let a = "test-rotation-guard-indep-a";
    let b = "test-rotation-guard-indep-b";
    let held_a = RotationGuard::acquire(a).expect("acquire a");

    let (tx, rx) = mpsc::channel();
    let worker = std::thread::spawn(move || {
        let held_b = RotationGuard::acquire(b).expect("acquire b while a is held"); // distinct lock file → must not block
        tx.send(()).expect("signal acquired");
        drop(held_b);
    });
    rx.recv_timeout(Duration::from_secs(5))
        .expect("acquiring b must not block on a (per-profile locks are independent)");
    worker.join().expect("join b worker");
    drop(held_a);
}

/// Live session must NOT exclude a windowless profile: CC holds the lock but only
/// opens a window on first message. Kick is access-token-only (safe).
/// Regression: "CC open on background account, usage never started".
#[test]
fn windowless_candidate_even_with_live_session() {
    use std::collections::HashMap;

    let _home = HomeSandbox::new();
    let name = "test-windowless-live-session";

    let sessions = profile_dir(name).expect("profile_dir").join("sessions");
    std::fs::create_dir_all(&sessions).expect("create sessions dir");
    let pid_file = sessions.join("test-pid-live");
    let file = open_pid_file(&pid_file).expect("open pid file");
    file.lock().expect("lock pid file");

    let mut config = single_profile_config(name, "rt-live-session");
    config.profiles[0].auto_start = true;
    let config = Arc::new(RankedMutex::new(config));

    // empty store → no 5h window → windowless; live session must not change this
    let store: crate::usage::UsageStore = Arc::new(RankedMutex::new(HashMap::new()));

    let candidates = windowless_auto_start_candidates(&config, &store);
    assert_eq!(
        candidates,
        vec![name.to_string()],
        "a windowless profile must be re-armed even while a live session holds its lock"
    );

    drop(file);
}

/// A past `resets_at` must not be treated as a live window — profile must be re-armed.
/// Regression: `.is_some()` check treated stale timestamp as live, so the profile was
/// never re-kicked ("auto-start only works for the active account").
#[test]
fn windowless_candidate_when_resets_at_is_in_the_past() {
    use std::collections::HashMap;

    let _home = HomeSandbox::new();
    let name = "test-windowless-expired-window";

    let mut config = single_profile_config(name, "rt-expired");
    config.profiles[0].auto_start = true;
    let config = Arc::new(RankedMutex::new(config));

    let store: crate::usage::UsageStore = Arc::new(RankedMutex::new(HashMap::new()));

    // resets_at in past → still windowless → candidate
    let expired = crate::usage::UsageInfo {
        five_hour: Some(crate::usage::UsageWindow {
            utilization: 0.0,
            resets_at: Some("2020-01-01T00:00:00Z".to_string()),
        }),
        ..Default::default()
    };
    store.lock().unwrap().insert(name.to_string(), expired);

    let candidates = windowless_auto_start_candidates(&config, &store);
    assert_eq!(
        candidates,
        vec![name.to_string()],
        "a profile with an expired (past) resets_at must be re-armed"
    );

    // resets_at in future → live window → excluded
    let live = crate::usage::UsageInfo {
        five_hour: Some(crate::usage::UsageWindow {
            utilization: 0.0,
            resets_at: Some("2999-01-01T00:00:00Z".to_string()),
        }),
        ..Default::default()
    };
    store.lock().unwrap().insert(name.to_string(), live);

    let candidates = windowless_auto_start_candidates(&config, &store);
    assert!(
        candidates.is_empty(),
        "a profile with a future resets_at still has a live window — no re-arm"
    );
}

/// A live 5h window does NOT exclude a profile whose weekly window is present and
/// sitting at zero usage: the kick is what seeds the weekly clock. Any nonzero
/// weekly usage flips it back to excluded.
#[test]
fn weekly_idle_candidate_even_with_live_window() {
    use std::collections::HashMap;

    let _home = HomeSandbox::new();
    let name = "test-weekly-idle";

    let mut config = single_profile_config(name, "rt-weekly-idle");
    config.profiles[0].auto_start = true;
    let config = Arc::new(RankedMutex::new(config));

    let store: crate::usage::UsageStore = Arc::new(RankedMutex::new(HashMap::new()));

    // live 5h window + weekly window present at 0% → cold weekly clock → candidate
    let cold_weekly = crate::usage::UsageInfo {
        five_hour: Some(crate::usage::UsageWindow {
            utilization: 0.0,
            resets_at: Some("2999-01-01T00:00:00Z".to_string()),
        }),
        seven_day: Some(crate::usage::UsageWindow {
            utilization: 0.0,
            resets_at: Some("2999-01-08T00:00:00Z".to_string()),
        }),
        ..Default::default()
    };
    store.lock().unwrap().insert(name.to_string(), cold_weekly);

    let candidates = windowless_auto_start_candidates(&config, &store);
    assert_eq!(
        candidates,
        vec![name.to_string()],
        "a live window with zero weekly usage must still be re-armed to seed the weekly clock"
    );

    // weekly usage now nonzero → both windows live → excluded
    let warm_weekly = crate::usage::UsageInfo {
        five_hour: Some(crate::usage::UsageWindow {
            utilization: 0.0,
            resets_at: Some("2999-01-01T00:00:00Z".to_string()),
        }),
        seven_day: Some(crate::usage::UsageWindow {
            utilization: 1.0,
            resets_at: Some("2999-01-08T00:00:00Z".to_string()),
        }),
        ..Default::default()
    };
    store.lock().unwrap().insert(name.to_string(), warm_weekly);

    let candidates = windowless_auto_start_candidates(&config, &store);
    assert!(
        candidates.is_empty(),
        "once weekly usage is nonzero, a profile with a live window is no longer a candidate"
    );
}

/// The cooldown filter governs the weekly-idle leg too: a profile that *would*
/// be a weekly-idle candidate (live 5h window, zero weekly usage) is excluded
/// while it sits inside `AUTO_START_COOLDOWN_MS`. This is what keeps `on_tick`
/// from spawning an auto-start worker every tick for a 0%-weekly profile —
/// `start_window` stamps `last_auto_start_at` on the kick, and this filter then
/// drops the profile until the cooldown elapses.
#[test]
fn weekly_idle_candidate_suppressed_inside_cooldown() {
    use std::collections::HashMap;

    let _home = HomeSandbox::new();
    let name = "test-weekly-idle-cooldown";

    let mut config = single_profile_config(name, "rt-weekly-idle-cd");
    config.profiles[0].auto_start = true;
    // Stamp the cooldown slot as if a kick just landed.
    config
        .state
        .last_auto_start_at
        .insert(name.to_string(), now_ms());
    let config = Arc::new(RankedMutex::new(config));

    let store: crate::usage::UsageStore = Arc::new(RankedMutex::new(HashMap::new()));

    // live 5h window + cold (0%) weekly window: weekly-idle leg would arm it,
    // but the fresh cooldown stamp must suppress it.
    let cold_weekly = crate::usage::UsageInfo {
        five_hour: Some(crate::usage::UsageWindow {
            utilization: 0.0,
            resets_at: Some("2999-01-01T00:00:00Z".to_string()),
        }),
        seven_day: Some(crate::usage::UsageWindow {
            utilization: 0.0,
            resets_at: Some("2999-01-08T00:00:00Z".to_string()),
        }),
        ..Default::default()
    };
    store.lock().unwrap().insert(name.to_string(), cold_weekly);

    let candidates = windowless_auto_start_candidates(&config, &store);
    assert!(
        candidates.is_empty(),
        "a weekly-idle profile inside the auto-start cooldown must NOT be a candidate \
         (else on_tick would respawn a worker every tick)"
    );

    // Move the stamp past the cooldown → the weekly-idle leg arms it again.
    {
        let mut cfg = config.lock().unwrap();
        let stale = now_ms().saturating_sub(AUTO_START_COOLDOWN_MS + 1);
        cfg.state.last_auto_start_at.insert(name.to_string(), stale);
    }
    let candidates = windowless_auto_start_candidates(&config, &store);
    assert_eq!(
        candidates,
        vec![name.to_string()],
        "past the cooldown, a weekly-idle profile is a candidate again"
    );
}