use pretty_assertions::assert_eq;
use super::parse;
use crate::usage_limits::UsageLimitWindow;
/// `agy usage --json` from agy 1.5.22, descriptions trimmed.
const AGY_1_5_22_USAGE: &str = r#"{"conversation_id":"","status":"SUCCESS","response":"","duration_seconds":0,"num_turns":0,"usage":{"input_tokens":0,"output_tokens":0,"thinking_tokens":0,"cache_read_tokens":0,"total_tokens":0},"command":{"name":"usage","data":{"description":"Within each group, models share a weekly limit and a 5-hour limit.","groups":[{"name":"Gemini Models","description":"Models within this group: Gemini 4 Neon","buckets":[{"id":"gemini-weekly","name":"Weekly Limit Remaining","description":"You have used some of your weekly limit.","window":"weekly","remaining_fraction":0.5335460901260376,"reset_time":"2026-12-08T09:38:33Z"},{"id":"gemini-5h","name":"Five Hour Limit Remaining","window":"5h","remaining_fraction":0.9072046875953674,"reset_time":"2026-12-03T01:22:24Z"}]},{"name":"Claude and GPT models","description":"Models within this group: Claude Opus 5.5","buckets":[{"id":"3p-weekly","name":"Weekly Limit Remaining","window":"weekly","remaining_fraction":1,"reset_time":"1970-01-01T00:00:00Z"},{"id":"3p-5h","name":"Five Hour Limit Remaining","window":"5h","remaining_fraction":1,"reset_time":"1970-01-01T00:00:00Z"}]}]}}}"#;
fn window(label: &str, remaining_percent: f64, resets_at_unix: Option<i64>) -> UsageLimitWindow {
UsageLimitWindow {
label: label.into(),
remaining_percent: Some(remaining_percent),
resets_at_unix,
note: None,
}
}
// Covers: agy's real report becomes one row per bucket, 5-hour first, with
// untouched (epoch) buckets showing no reset.
// Owner: pure unit
#[test]
fn parses_agy_usage_report() {
let windows = parse(AGY_1_5_22_USAGE.as_bytes()).unwrap();
assert_eq!(
windows,
vec![
window(
"Gemini 5-hour",
0.9072046875953674 * 100.0,
Some(1_796_260_944)
),
window(
"Gemini Weekly",
0.5335460901260376 * 100.0,
Some(1_796_722_713)
),
window("Claude and GPT 5-hour", 100.0, None),
window("Claude and GPT Weekly", 100.0, None),
]
);
}
// Covers: a report agy marks as failed is an error, not an empty section.
// Owner: pure unit
#[test]
fn rejects_unsuccessful_report() {
let failed = AGY_1_5_22_USAGE.replace(r#""status":"SUCCESS""#, r#""status":"ERROR""#);
assert!(parse(failed.as_bytes()).is_err());
}
// Covers: `fetch` runs the binary it is given and reads its stdout, and a
// failing agy (older than `agy usage`) is an error, not empty quota.
// Owner: CLI boundary (fake `agy` script)
#[cfg(unix)]
#[tokio::test]
async fn fetch_reads_agy_stdout_and_rejects_failed_exit() {
let dir = tempfile::tempdir().unwrap();
let report = dir.path().join("report.json");
std::fs::write(&report, AGY_1_5_22_USAGE).unwrap();
let fake = |name: &str, body: String| {
let path = dir.path().join(name);
std::fs::write(&path, format!("{body}\n")).unwrap();
// Run through /bin/sh rather than exec the fresh file, which can fail
// with ETXTBSY under parallel load (see `cli_runtime::probe`).
crate::cli_runtime::CliExecutable::interpreted("/bin/sh".into(), vec![path.into()])
};
let working = fake(
"agy-working",
format!(
"[ \"$1 $2\" = \"usage --json\" ] || exit 2\ncat '{}'",
report.display()
),
);
let old = fake(
"agy-old",
"echo 'unknown command \"usage\"' >&2; exit 1".into(),
);
assert_eq!(
super::fetch(&working).await.unwrap(),
parse(AGY_1_5_22_USAGE.as_bytes()).unwrap()
);
assert!(matches!(
super::fetch(&old).await,
Err(crate::usage_limits::UsageLimitsError::Command { .. })
));
}