use pretty_assertions::assert_eq;
use serde_json::{json, Value};
use super::{parse_print_output, UsageProbeError};
fn rate_limits() -> Value {
json!({
"limits": [
{"kind": "session", "group": "session", "percent": 12, "resets_at": "2026-12-02T20:00:00+00:00", "scope": null, "severity": "normal", "is_active": false},
{"kind": "weekly_all", "group": "weekly", "percent": 78, "resets_at": "2026-12-05T15:00:00.395273+00:00", "scope": null, "severity": "warning", "is_active": true},
{"kind": "weekly_scoped", "group": "weekly", "percent": 0, "resets_at": "2026-12-05T15:00:00+00:00", "scope": {"model": {"display_name": "Fable"}, "surface": null}, "severity": "normal", "is_active": false},
{"kind": "monthly_new", "group": "monthly", "percent": 5, "resets_at": null, "scope": null, "severity": "normal", "is_active": false}
],
"extra_usage": {"is_enabled": true, "monthly_limit": 50000, "used_credits": 7500, "utilization": 15, "currency": "USD"}
})
}
fn stream(rate_limits: Value, local_command: Option<&str>, is_error: bool) -> Vec<u8> {
let lines = [
json!({"type": "system", "subtype": "init"}),
json!({"type": "assistant", "message": {"content": []}, "usage_report": {"session": {}, "rate_limits": rate_limits}}),
json!({"type": "result", "subtype": "success", "is_error": is_error, "local_command": local_command, "result": "You are currently using your subscription"}),
];
lines
.iter()
.map(Value::to_string)
.collect::<Vec<_>>()
.join("\n")
.into_bytes()
}
type Window = (String, Option<f64>, Option<i64>, Option<String>);
type Outcome = Result<Vec<Window>, &'static str>;
fn outcome(stdout: &[u8]) -> Outcome {
match parse_print_output(stdout) {
Ok(state) => Ok(state
.sorted_windows()
.into_iter()
.map(|window| {
(
window.info.window_key().to_owned(),
window.info.utilization,
window.info.resets_at,
window.info.status.clone(),
)
})
.collect()),
Err(error) => Err(match error {
UsageProbeError::Run(_) => "Run",
UsageProbeError::NotLocal(_) => "NotLocal",
UsageProbeError::Unparseable(_) => "Unparseable",
UsageProbeError::NoReport => "NoReport",
UsageProbeError::Auth(_) => "Auth",
}),
}
}
#[test]
fn print_output_classification() {
let window = |key: &str, used: f64, resets_at: Option<i64>, status: Option<&str>| {
(
key.to_owned(),
Some(used),
resets_at,
status.map(str::to_owned),
)
};
let cases: Vec<(&str, Vec<u8>, Outcome)> = vec![
(
"live report",
stream(rate_limits(), Some("usage"), false),
Ok(vec![
window("five_hour", 0.12, Some(1_796_241_600), None),
window("seven_day", 0.78, Some(1_796_482_800), Some("warning")),
window("seven_day_fable", 0.0, Some(1_796_482_800), None),
window("extra_usage", 0.15, None, None),
window("monthly_new", 0.05, None, None),
]),
),
(
"usage fetch failing",
stream(Value::Null, Some("usage"), false),
Err("NoReport"),
),
(
"server sent no rows",
stream(
json!({"limits": null, "extra_usage": null}),
Some("usage"),
false,
),
Err("NoReport"),
),
(
"plan has no meters",
stream(
json!({"limits": [], "extra_usage": null}),
Some("usage"),
false,
),
Ok(Vec::new()),
),
(
"model answered instead",
stream(rate_limits(), None, false),
Err("NotLocal"),
),
(
"command errored",
stream(rate_limits(), Some("usage"), true),
Err("Run"),
),
(
"not json",
b"Error: unknown option".to_vec(),
Err("Unparseable"),
),
];
for (name, stdout, expected) in cases {
assert_eq!(outcome(&stdout), expected, "{name}");
}
}