Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

28.0 KB
Raw Blame
use serde_json::json;
use serial_test::serial;

use super::*;

fn credential(uid: &str, domain: &str, access: &str, session: &str) -> String {
    serde_json::to_string_pretty(&json!({
        "auth": {
            "accessToken": access,
            "refreshToken": format!("refresh-{uid}-{session}"),
            "tokenType": "Bearer",
            "expiresIn": 3600,
            "expiresAt": 4_102_444_800_000_i64,
            "refreshExpiresIn": 2_592_000,
            "refreshExpiresAt": 4_102_444_800_000_i64,
            "lastRefreshTime": 1_760_000_000_000_i64,
            "scope": "openid",
            "domain": domain,
            "sessionState": session,
            "notBeforePolicy": 0,
        },
        "account": {"uid": uid, "nickname": format!("nick-{uid}"), "type": "personal"},
        "accounts": [],
        "allAccounts": [],
    }))
    .unwrap()
}

struct Fixture {
    _dir: tempfile::TempDir,
    auth_dir: PathBuf,
    data_dir: PathBuf,
    manager: CodebuddyAccountManager,
}

impl Fixture {
    fn new() -> Self {
        let dir = tempfile::tempdir().unwrap();
        let auth_dir = dir.path().join("auth");
        let data_dir = dir.path().join("data");
        std::fs::create_dir_all(&auth_dir).unwrap();
        std::fs::create_dir_all(&data_dir).unwrap();
        let manager = CodebuddyAccountManager::with_auth_dir(data_dir.clone(), auth_dir.clone());
        Self {
            _dir: dir,
            auth_dir,
            data_dir,
            manager,
        }
    }

    fn set_live(&self, content: &str) {
        std::fs::write(self.auth_dir.join(CREDENTIAL_FILENAME), content).unwrap();
    }

    fn live(&self) -> String {
        std::fs::read_to_string(self.auth_dir.join(CREDENTIAL_FILENAME)).unwrap()
    }

    fn marker(&self) -> PathBuf {
        self.auth_dir.join(LOGGED_OUT_FILENAME)
    }
}

const CN: &str = "www.codebuddy.cn";
const INTL: &str = "www.codebuddy.ai";

#[tokio::test]
async fn status_does_not_import_an_uncollected_login() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    let status = fx.manager.status().await.unwrap();
    assert!(status.accounts.is_empty());
    assert_eq!(status.current_account_id, None);
    let live = fx.manager.live_summary().await.unwrap();
    assert_eq!(live.state, LiveState::Uncollected);
    assert_eq!(live.account_id.as_deref(), Some("[email protected]"));
    assert_eq!(live.label.as_deref(), Some("nick-100"));
}

#[tokio::test]
async fn import_current_collects_and_status_tracks_refreshes() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    let account = fx.manager.import_current().await.unwrap();
    assert_eq!(account.id, "[email protected]");
    assert_eq!(account.label(), "nick-100");
    assert!(!account.expired);

    // CodeBuddy 续期后写回文件:查看状态时更新副本
    let refreshed = credential("100", CN, "a2", "s1");
    fx.set_live(&refreshed);
    let status = fx.manager.status().await.unwrap();
    assert_eq!(
        status.current_account_id.as_deref(),
        Some(account.id.as_str())
    );
    let store = fx.manager.store.lock().await;
    assert_eq!(store.accounts[&account.id].raw, refreshed);
    drop(store);

    // 账号文件 0600,重新打开还在
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(fx.data_dir.join(STORE_FILENAME))
            .unwrap()
            .permissions()
            .mode();
        assert_eq!(mode & 0o777, 0o600);
    }
    let reopened = CodebuddyAccountManager::with_auth_dir(fx.data_dir.clone(), fx.auth_dir.clone());
    assert_eq!(reopened.status().await.unwrap().accounts.len(), 1);
}

#[tokio::test]
async fn same_uid_on_both_sites_are_two_accounts() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    fx.manager.import_current().await.unwrap();
    fx.set_live(&credential("100", INTL, "b1", "s2"));
    fx.manager.import_current().await.unwrap();
    let status = fx.manager.status().await.unwrap();
    assert_eq!(status.accounts.len(), 2);
    assert_eq!(
        status.current_account_id.as_deref(),
        Some("[email protected]")
    );
}

#[tokio::test]
async fn logged_out_marker_wins_over_the_file() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    fx.manager.import_current().await.unwrap();
    std::fs::write(fx.marker(), "").unwrap();
    let status = fx.manager.status().await.unwrap();
    assert_eq!(status.current_account_id, None);
    assert_eq!(
        fx.manager.live_summary().await.unwrap().state,
        LiveState::LoggedOut
    );
    assert!(fx.manager.import_current().await.is_err());
}

#[tokio::test]
async fn encrypted_envelope_is_stashed_and_never_overwritten() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    fx.manager.import_current().await.unwrap();

    let envelope = r#"{"$wbEncrypted":1,"envelope":"opaque"}"#;
    fx.set_live(envelope);
    assert_eq!(
        fx.manager.live_summary().await.unwrap().state,
        LiveState::Unrecognized
    );
    assert!(fx.manager.import_current().await.is_err());
    assert!(fx.manager.switch_to("[email protected]").await.is_err());
    assert_eq!(fx.live(), envelope);

    // 字段级信封也认不出
    let nested = r#"{"auth":{"$wbEncrypted":1,"envelope":"x"},"account":{"uid":"1"}}"#;
    fx.set_live(nested);
    assert!(fx.manager.import_current().await.is_err());

    let stash: Value =
        serde_json::from_slice(&std::fs::read(fx.data_dir.join(STASH_FILENAME)).unwrap()).unwrap();
    let raws: Vec<&str> = stash["entries"]
        .as_array()
        .unwrap()
        .iter()
        .filter_map(|entry| entry["raw"].as_str())
        .collect();
    assert_eq!(raws, vec![envelope, nested]);
    assert!(!fx.auth_dir.join(LOCK_DIRNAME).exists());
}

#[tokio::test]
async fn switch_absorbs_the_current_copy_then_writes_the_target() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    fx.manager.import_current().await.unwrap();
    fx.set_live(&credential("200", INTL, "b1", "s2"));
    fx.manager.import_current().await.unwrap();

    // CLI 续期了 bob 的令牌、而且用户点过登出:切走前要收回最新那份
    let bob_refreshed = credential("200", INTL, "b2", "s2");
    fx.set_live(&bob_refreshed);
    fx.manager.switch_to("[email protected]").await.unwrap();
    assert_eq!(fx.live(), alice);
    assert!(!fx.auth_dir.join(LOCK_DIRNAME).exists());
    {
        let store = fx.manager.store.lock().await;
        assert_eq!(store.accounts["[email protected]"].raw, bob_refreshed);
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(fx.auth_dir.join(CREDENTIAL_FILENAME))
            .unwrap()
            .permissions()
            .mode();
        assert_eq!(mode & 0o777, 0o600);
    }

    std::fs::write(fx.marker(), "").unwrap();
    fx.manager.switch_to("[email protected]").await.unwrap();
    assert_eq!(fx.live(), bob_refreshed);
    assert!(!fx.marker().exists());
    let status = fx.manager.status().await.unwrap();
    assert_eq!(
        status.current_account_id.as_deref(),
        Some("[email protected]")
    );
}

#[tokio::test]
async fn switching_away_from_an_uncollected_login_collects_it() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    fx.manager.import_current().await.unwrap();
    let stranger = credential("300", CN, "c1", "s3");
    fx.set_live(&stranger);
    fx.manager.switch_to("[email protected]").await.unwrap();
    let store = fx.manager.store.lock().await;
    assert_eq!(store.accounts["[email protected]"].raw, stranger);
}

#[tokio::test]
async fn expired_accounts_are_flagged_and_not_switched_to() {
    let fx = Fixture::new();
    let mut value: Value = serde_json::from_str(&credential("100", CN, "a1", "s1")).unwrap();
    // 秒级时间戳也认
    value["auth"]["refreshExpiresAt"] = json!(1_000_000_000);
    fx.set_live(&value.to_string());
    let account = fx.manager.import_current().await.unwrap();
    assert!(account.expired);
    assert_eq!(account.refresh_expires_at, Some(1_000_000_000_000));
    fx.set_live(&credential("200", CN, "b1", "s2"));
    assert!(fx.manager.switch_to(&account.id).await.is_err());
}

#[tokio::test]
async fn a_held_lock_blocks_the_switch_and_a_stale_one_is_cleared() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    fx.manager.import_current().await.unwrap();
    let bob = credential("200", CN, "b1", "s2");
    fx.set_live(&bob);
    let lock = fx.auth_dir.join(LOCK_DIRNAME);
    std::fs::create_dir(&lock).unwrap();
    assert!(fx.manager.switch_to("[email protected]").await.is_err());
    assert_eq!(fx.live(), bob);
    assert!(lock.exists(), "别人的锁不能被删");

    #[cfg(unix)]
    {
        let old = std::time::SystemTime::now() - Duration::from_secs(60);
        std::fs::File::open(&lock)
            .unwrap()
            .set_modified(old)
            .unwrap();
        fx.manager.switch_to("[email protected]").await.unwrap();
        assert!(!lock.exists());
        assert_ne!(fx.live(), bob);
    }
}

#[tokio::test]
async fn terminal_login_completes_on_a_new_account_or_session() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    let start = fx.manager.start_login().await.unwrap();
    // 开始登录时把现在登着的号收进来,再写登出标记让 CLI 进登录界面;凭据文件不动
    assert_eq!(fx.manager.status().await.unwrap().accounts.len(), 1);
    assert!(fx.marker().exists());
    assert_eq!(fx.live(), alice);
    assert!(fx
        .manager
        .poll_login(&start.flow_id)
        .await
        .unwrap()
        .is_none());

    // CLI 登录成功:写新凭据、自己删掉标记
    fx.set_live(&credential("200", INTL, "b1", "s2"));
    assert!(
        fx.manager
            .poll_login(&start.flow_id)
            .await
            .unwrap()
            .is_none(),
        "标记还在就还没登录完"
    );
    std::fs::remove_file(fx.marker()).unwrap();
    let account = fx
        .manager
        .poll_login(&start.flow_id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(account.id, "[email protected]");
    assert!(fx.manager.poll_login(&start.flow_id).await.is_err());
    // 原来的号还在
    assert_eq!(fx.manager.status().await.unwrap().accounts.len(), 2);

    // 同一个号重新登录(新会话)
    let again = fx.manager.start_login().await.unwrap();
    assert!(fx.marker().exists());
    std::fs::remove_file(fx.marker()).unwrap();
    // 只是续期(同一会话)不算登录完成
    fx.set_live(&credential("200", INTL, "b1b", "s2"));
    assert!(fx
        .manager
        .poll_login(&again.flow_id)
        .await
        .unwrap()
        .is_none());
    fx.set_live(&credential("200", INTL, "b2", "s9"));
    let account = fx
        .manager
        .poll_login(&again.flow_id)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(account.id, "[email protected]");
    assert!(!fx.manager.cancel_login(&again.flow_id).await.unwrap());
}

#[tokio::test]
async fn login_without_a_current_sign_in_writes_no_marker() {
    let fx = Fixture::new();
    // 凭据目录还不存在:不拿锁、不建目录、不写标记
    std::fs::remove_dir(&fx.auth_dir).unwrap();
    let start = fx.manager.start_login().await.unwrap();
    assert!(!fx.auth_dir.exists());
    assert!(fx.manager.cancel_login(&start.flow_id).await.unwrap());

    // 已经登出(标记是用户自己 /logout 写的):不重写,取消时也不删别人的标记
    std::fs::create_dir_all(&fx.auth_dir).unwrap();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    std::fs::write(fx.marker(), "user-logout").unwrap();
    let start = fx.manager.start_login().await.unwrap();
    assert_eq!(std::fs::read_to_string(fx.marker()).unwrap(), "user-logout");
    assert!(fx.manager.cancel_login(&start.flow_id).await.unwrap());
    assert_eq!(std::fs::read_to_string(fx.marker()).unwrap(), "user-logout");
}

#[tokio::test]
async fn logout_marker_is_an_iso_timestamp_written_atomically() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    fx.manager.start_login().await.unwrap();
    let content = std::fs::read_to_string(fx.marker()).unwrap();
    assert!(chrono::DateTime::parse_from_rfc3339(&content).is_ok());
    assert!(content.ends_with('Z'));
    // 凭据文件原样、没有别的文件(临时文件已 rename,锁已释放)
    assert_eq!(fx.live(), alice);
    let mut names: Vec<String> = std::fs::read_dir(&fx.auth_dir)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
        .collect();
    names.sort();
    assert_eq!(names, vec![CREDENTIAL_FILENAME, LOGGED_OUT_FILENAME]);
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mode = std::fs::metadata(fx.marker()).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o600);
    }
}

#[tokio::test]
async fn cancelling_a_login_restores_the_original_sign_in() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    let start = fx.manager.start_login().await.unwrap();
    assert_eq!(
        fx.manager.live_summary().await.unwrap().state,
        LiveState::LoggedOut
    );
    assert_eq!(fx.manager.status().await.unwrap().current_account_id, None);

    assert!(fx.manager.cancel_login(&start.flow_id).await.unwrap());
    assert!(!fx.marker().exists());
    assert_eq!(fx.live(), alice);
    assert_eq!(
        fx.manager
            .status()
            .await
            .unwrap()
            .current_account_id
            .as_deref(),
        Some("[email protected]")
    );
    // 再取消一次什么都没有
    assert!(!fx.manager.cancel_login(&start.flow_id).await.unwrap());
}

#[tokio::test]
async fn a_restart_mid_login_restores_the_original_sign_in() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    fx.manager.start_login().await.unwrap();
    assert!(fx.marker().exists());
    // CC Switch 退出:进行中的登录只在内存里,重开后靠落盘的记录认出自己的标记
    let reopened = CodebuddyAccountManager::with_auth_dir(fx.data_dir.clone(), fx.auth_dir.clone());
    assert!(reopened.recover_stale_marker().await.unwrap());
    assert!(!fx.marker().exists());
    assert!(!fx.data_dir.join(MARKER_RECORD_FILENAME).exists());
    assert_eq!(fx.live(), alice);
    // 只恢复一次
    assert!(!reopened.recover_stale_marker().await.unwrap());
}

#[tokio::test]
async fn a_restart_leaves_a_marker_that_is_no_longer_ours() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    fx.manager.start_login().await.unwrap();
    // 退出期间用户在 CLI 里自己 /logout 了:标记换成了别人的
    std::fs::write(fx.marker(), "2026-01-01T00:00:00.000Z").unwrap();
    let reopened = CodebuddyAccountManager::with_auth_dir(fx.data_dir.clone(), fx.auth_dir.clone());
    assert!(!reopened.recover_stale_marker().await.unwrap());
    assert!(fx.marker().exists());
    assert!(!fx.data_dir.join(MARKER_RECORD_FILENAME).exists());
}

#[tokio::test]
async fn finishing_or_cancelling_a_login_drops_the_marker_record() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    let start = fx.manager.start_login().await.unwrap();
    assert!(fx.data_dir.join(MARKER_RECORD_FILENAME).exists());
    fx.manager.cancel_login(&start.flow_id).await.unwrap();
    assert!(!fx.data_dir.join(MARKER_RECORD_FILENAME).exists());

    let start = fx.manager.start_login().await.unwrap();
    // CLI 登上新号:写新凭据、删标记
    fx.set_live(&credential("200", CN, "b1", "s2"));
    std::fs::remove_file(fx.marker()).unwrap();
    assert!(fx
        .manager
        .poll_login(&start.flow_id)
        .await
        .unwrap()
        .is_some());
    assert!(!fx.data_dir.join(MARKER_RECORD_FILENAME).exists());
}

#[tokio::test]
async fn cancel_leaves_a_marker_that_is_not_ours() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    let start = fx.manager.start_login().await.unwrap();
    // 等待期间用户在 CLI 里又 /logout 了一次:标记换成了别人的
    std::fs::write(fx.marker(), "2026-01-01T00:00:00.000Z").unwrap();
    assert!(fx.manager.cancel_login(&start.flow_id).await.unwrap());
    assert!(fx.marker().exists());
}

#[tokio::test]
async fn starting_again_restores_the_previous_marker_first() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    let first = fx.manager.start_login().await.unwrap();
    let second = fx.manager.start_login().await.unwrap();
    // 第二次开始前先恢复了原登录,所以又收录、又写了一份自己的标记
    assert!(fx.marker().exists());
    assert!(!fx.manager.cancel_login(&first.flow_id).await.unwrap());
    assert!(fx.manager.cancel_login(&second.flow_id).await.unwrap());
    assert!(!fx.marker().exists());
    assert_eq!(fx.live(), alice);
}

#[tokio::test]
async fn a_held_lock_blocks_writing_the_marker_and_restoring_it() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    let lock = fx.auth_dir.join(LOCK_DIRNAME);
    std::fs::create_dir(&lock).unwrap();
    assert!(fx.manager.start_login().await.is_err());
    assert!(!fx.marker().exists());
    assert!(lock.exists(), "别人的锁不能被删");
    std::fs::remove_dir(&lock).unwrap();

    let start = fx.manager.start_login().await.unwrap();
    assert!(!lock.exists(), "写完标记就放锁");
    std::fs::create_dir(&lock).unwrap();
    assert!(fx.manager.cancel_login(&start.flow_id).await.is_err());
    assert!(fx.marker().exists(), "拿不到锁就不删标记");
    std::fs::remove_dir(&lock).unwrap();
    // 锁放了再点一次就能恢复
    assert!(fx.manager.cancel_login(&start.flow_id).await.unwrap());
    assert!(!fx.marker().exists());
    assert_eq!(fx.live(), alice);
}

#[tokio::test]
async fn removing_copies_never_touches_codebuddy() {
    let fx = Fixture::new();
    let alice = credential("100", CN, "a1", "s1");
    fx.set_live(&alice);
    let account = fx.manager.import_current().await.unwrap();
    fx.manager.remove(&account.id).await.unwrap();
    assert_eq!(fx.live(), alice);
    assert!(!fx.marker().exists());
}

#[test]
fn parse_identity_requires_tokens_and_uid() {
    assert!(parse_identity("not json").is_none());
    assert!(parse_identity(r#"{"auth":{"accessToken":"a"},"account":{"uid":"1"}}"#).is_none());
    let numeric = r#"{"auth":{"accessToken":"a","refreshToken":"r"},"account":{"uid":42}}"#;
    let identity = parse_identity(numeric).unwrap();
    assert_eq!(identity.key(), "42@");
    assert_eq!(identity.refresh_expires_at, None);
    let derived = r#"{"auth":{"accessToken":"a","refreshToken":"r","lastRefreshTime":1000,"refreshExpiresIn":60},"account":{"uid":"1"}}"#;
    assert_eq!(
        parse_identity(derived).unwrap().refresh_expires_at,
        Some(1_060_000)
    );
}

#[test]
#[serial]
fn auth_dir_can_be_overridden() {
    let dir = tempfile::tempdir().unwrap();
    std::env::set_var(AUTH_DIR_ENV, dir.path());
    assert_eq!(default_auth_dir(), dir.path());
    std::env::remove_var(AUTH_DIR_ENV);
    assert!(default_auth_dir().ends_with(
        Path::new("CodeBuddyExtension")
            .join("Data")
            .join("Public")
            .join("auth")
    ));
}

// ---------- 额度 ----------

#[test]
fn quota_base_url_only_for_known_sites() {
    assert_eq!(
        quota::base_url(Some("www.codebuddy.cn")).as_deref(),
        Some("https://www.codebuddy.cn")
    );
    assert_eq!(
        quota::base_url(Some("WWW.CodeBuddy.ai")).as_deref(),
        Some("https://www.codebuddy.ai")
    );
    assert_eq!(
        quota::base_url(Some("acme.sso.copilot.qq.com")).as_deref(),
        Some("https://acme.sso.copilot.qq.com")
    );
    assert_eq!(quota::base_url(None), None);
    assert_eq!(quota::base_url(Some("evil.example.com")), None);
    assert_eq!(quota::base_url(Some("codebuddy.cn.evil.com")), None);
    assert_eq!(quota::base_url(Some("evilcodebuddy.cn")), None);
    assert_eq!(quota::base_url(Some("www.codebuddy.cn/x")), None);
    assert_eq!(quota::base_url(Some("www.codebuddy.cn:8443")), None);
}

#[test]
fn quota_parses_personal_summary() {
    let body = br#"{"code":0,"msg":"OK","data":{
        "Packages":[
            {"PackageCode":"a","CycleTotalCapacity":"100","CycleRemainCapacity":"60.5","CycleUsedCapacity":"39.5"},
            {"PackageCode":"b","CycleTotalCapacity":50,"CycleRemainCapacity":"50","CycleUsedCapacity":"0"}
        ],
        "SubscriptionPackageName":" Plan ","IsPaidUser":true}}"#;
    let quota::ParseOutcome::Ok(credits) = quota::parse_personal(body) else {
        panic!("should parse");
    };
    assert_eq!(credits.plan_name.as_deref(), Some("Plan"));
    assert!(credits.is_paid);
    assert!(!credits.enterprise);
    assert_eq!(credits.total, 150.0);
    assert_eq!(credits.remaining, 110.5);
    assert_eq!(credits.used, 39.5);

    let empty = br#"{"code":0,"data":{"SubscriptionPackageName":""}}"#;
    let quota::ParseOutcome::Ok(credits) = quota::parse_personal(empty) else {
        panic!("should parse");
    };
    assert_eq!(credits.plan_name, None);
    assert_eq!(credits.total, 0.0);
}

#[test]
fn quota_reports_business_errors() {
    assert_eq!(
        quota::parse_personal(br#"{"code":10085,"msg":"bad request"}"#),
        quota::ParseOutcome::Rejected("bad request".into())
    );
    assert_eq!(
        quota::parse_personal(br#"{"code":0}"#),
        quota::ParseOutcome::Rejected("CodeBuddy 没有返回额度".into())
    );
    assert!(matches!(
        quota::parse_personal(b"<html>"),
        quota::ParseOutcome::Rejected(_)
    ));
}

#[test]
fn quota_parses_enterprise_usage() {
    let quota::ParseOutcome::Ok(credits) =
        quota::parse_enterprise(br#"{"code":0,"data":{"limit_num":1000,"used_num":250}}"#, 0)
    else {
        panic!("should parse");
    };
    assert!(credits.enterprise && !credits.unlimited);
    assert_eq!(
        (credits.total, credits.remaining, credits.used),
        (1000.0, 750.0, 250.0)
    );

    let quota::ParseOutcome::Ok(credits) =
        quota::parse_enterprise(br#"{"code":0,"data":{"limitNum":-1,"credit":12}}"#, 0)
    else {
        panic!("should parse");
    };
    assert!(credits.unlimited);
    assert_eq!((credits.total, credits.used), (0.0, 12.0));
}

#[test]
fn quota_enterprise_remaining_expires_at_cycle_end() {
    // 2026-12-31 23:59:59 北京时间
    let expires_at = 1_798_732_799_000;
    let now = expires_at - 86_400_000;
    let body = br#"{"code":0,"data":{"limit_num":1000,"used_num":250,"cycleEndTime":"2026-12-31 23:59:59"}}"#;
    let quota::ParseOutcome::Ok(credits) = quota::parse_enterprise(body, now) else {
        panic!("should parse");
    };
    let expiries = credits.expiries.expect("enterprise expiry");
    assert_eq!(expiries.len(), 1);
    assert_eq!(
        (expiries[0].credits, expiries[0].expires_at),
        (750.0, Some(expires_at))
    );

    // 不限额:没有会作废的剩余
    let body =
        br#"{"code":0,"data":{"limitNum":-1,"credit":12,"cycleEndTime":"2026-12-31 23:59:59"}}"#;
    let quota::ParseOutcome::Ok(credits) = quota::parse_enterprise(body, now) else {
        panic!("should parse");
    };
    assert_eq!(credits.expiries, Some(vec![]));
}

#[test]
fn quota_package_details_follow_summary_plans() {
    use crate::services::workbuddy_account::quota::package_detail_requests;
    let day = chrono::NaiveDate::from_ymd_opt(2026, 11, 24).unwrap();
    // 只有体验版、拉新包(都是免费套餐):只查 free 明细,带当天的切片区间
    let summary = br#"{"code":0,"data":{"Packages":[{"PackageCode":"TCACA_code_008_cfWoLwvjU4"},{"PackageCode":"TCACA_code_030_BjSt89qTvr"}]}}"#;
    let requests = package_detail_requests(summary, day);
    assert_eq!(requests.len(), 1);
    assert_eq!(
        requests[0].0,
        "/billing/meter/get-user-resource-free-packages"
    );
    assert_eq!(requests[0].1["SlicePeriodStartTime"], "2026-11-24 00:00:00");
    // 汇总里没有套餐:不发明细请求
    assert!(package_detail_requests(br#"{"code":0,"data":{}}"#, day).is_empty());
}

#[test]
fn quota_credentials_and_expiry() {
    let raw = r#"{"auth":{"accessToken":"t","domain":"www.codebuddy.cn","expiresAt":2000,"refreshExpiresAt":5000},
        "account":{"uid":"u1","enterpriseId":" "}}"#;
    let creds = quota::credentials_of(raw).unwrap();
    assert_eq!(creds.uid, "u1");
    assert_eq!(creds.enterprise_id, None);
    assert!(!format!("{creds:?}").contains("\"t\""));
    // 小于 1e12 的时间戳按秒算:access 2_000_000 ms,refresh 5_000_000 ms
    assert!(quota::expired_status(&creds, 1_000_000).is_none());
    assert!(matches!(
        quota::expired_status(&creds, 3_000_000),
        Some(crate::services::subscription::CredentialStatus::RefreshPending)
    ));
    let creds = quota::QuotaCredentials {
        access_expires_at: Some(1_000_000),
        refresh_expires_at: Some(5_000_000),
        ..creds
    };
    assert!(quota::expired_status(&creds, 500_000).is_none());
    assert!(matches!(
        quota::expired_status(&creds, 2_000_000),
        Some(crate::services::subscription::CredentialStatus::RefreshPending)
    ));
    assert!(matches!(
        quota::expired_status(&creds, 6_000_000),
        Some(crate::services::subscription::CredentialStatus::Expired)
    ));
    assert!(quota::credentials_of(r#"{"auth":{},"account":{"uid":"1"}}"#).is_none());
}

#[tokio::test]
async fn quota_does_not_query_with_expired_tokens() {
    let fx = Fixture::new();
    let mut value: Value = serde_json::from_str(&credential("100", CN, "a1", "s1")).unwrap();
    value["auth"]["expiresAt"] = json!(1_000);
    let raw = serde_json::to_string(&value).unwrap();
    fx.set_live(&raw);
    let account = fx.manager.import_current().await.unwrap();
    let quota = fx.manager.quota(&account.id, "zh-CN", false).await.unwrap();
    assert!(!quota.success);
    assert!(matches!(
        quota.credential_status,
        crate::services::subscription::CredentialStatus::RefreshPending
    ));
    // 没查到的不进缓存,文件没被动过
    assert!(fx.manager.quota_cache.lock().await.is_empty());
    assert_eq!(fx.live(), raw);
    assert!(fx.manager.quota("nope@x", "en", false).await.is_err());
}

#[tokio::test]
async fn an_unreadable_account_file_is_kept_instead_of_overwritten() {
    let fx = Fixture::new();
    fx.set_live(&credential("100", CN, "a1", "s1"));
    fx.manager.import_current().await.unwrap();
    fx.set_live(&credential("200", INTL, "b1", "s2"));
    fx.manager.import_current().await.unwrap();
    let store_path = fx.data_dir.join(STORE_FILENAME);
    let mut unreadable = std::fs::read(&store_path).unwrap();
    unreadable.extend_from_slice(b"\n}");
    std::fs::write(&store_path, &unreadable).unwrap();

    let reopened = CodebuddyAccountManager::with_auth_dir(fx.data_dir.clone(), fx.auth_dir.clone());
    fx.set_live(&credential("300", CN, "c1", "s3"));
    let account = reopened.import_current().await.unwrap();

    assert_eq!(account.id, "[email protected]");
    assert!(
        crate::test_support::dir_holds_file_with(&fx.data_dir, &unreadable),
        "the unreadable account file was overwritten"
    );
}

/// The rescue stash keeps logins CC Switch couldn't parse; a stash it can't read
/// itself must not be replaced by a fresh one.
#[tokio::test]
async fn an_unreadable_rescue_stash_is_kept_instead_of_overwritten() {
    let fx = Fixture::new();
    let unreadable = br#"{"entries":[{"raw":"an earlier rescued login","saved_at":1}]"#.to_vec();
    std::fs::write(fx.data_dir.join(STASH_FILENAME), &unreadable).unwrap();
    fx.set_live(r#"{"$wbEncrypted":1,"envelope":"opaque"}"#);

    assert!(fx.manager.import_current().await.is_err());

    assert!(
        crate::test_support::dir_holds_file_with(&fx.data_dir, &unreadable),
        "the unreadable rescue stash was overwritten"
    );
}