#![allow(unused_imports)]
use crate::sdk::{self, types};
use crate::support;
#[tokio::test]
async fn reports_api_errors() {
let body = support::json(r#"{"code":"not_found","message":"No such resource.","details":{"field":"id"}}"#);
let reply = support::Reply::json(404, body).header("x-request-id", "req_test");
let api = support::MockApi::start(vec![reply]).await;
let error = api.client().usage().get().await.unwrap_err();
assert!(matches!(error, sdk::Error::Api { .. }), "{error:?}");
assert_eq!(error.status(), Some(404));
assert_eq!(error.code(), "not_found");
assert_eq!(error.request_id(), Some("req_test"));
assert_eq!(error.details().and_then(|d| d.get("field")), Some(&"id".into()));
assert_eq!(error.to_string(), "No such resource. (404 not_found, request req_test)");
assert_eq!(api.calls().await.len(), 1);
}
#[tokio::test]
async fn get() {
let response = support::json(r#"{
"object": "usage",
"period": {
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z"
},
"currency": "usd",
"billableAccountCount": 1,
"accountUnitPriceCents": 1,
"accountFeeCents": 1,
"proxyBytes": 1,
"includedProxyBytes": 1,
"billableProxyBytes": 1,
"proxyFeeCents": 1,
"totalCents": 1
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().usage().get().await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/usage");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn by_project() {
let response = support::json(r#"{
"object": "usage_report",
"period": {
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z"
},
"projects": [
{
"projectId": "projectId 1",
"externalId": "externalId 1",
"name": "name 1",
"billableAccountCount": 1,
"accountCount": 1,
"proxyBytes": 1,
"sentMessageCount": 1,
"receivedMessageCount": 1
}
],
"unassigned": {
"billableAccountCount": 1,
"accountCount": 1,
"proxyBytes": 1,
"sentMessageCount": 1,
"receivedMessageCount": 1
},
"totals": {
"billableAccountCount": 1,
"accountCount": 1,
"proxyBytes": 1,
"sentMessageCount": 1,
"receivedMessageCount": 1
}
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let params: types::UsageByProjectParams = support::from_json(r#"{"month":"2026-09"}"#);
let result = api.client().usage().by_project(params).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/usage/by-project");
assert_eq!(calls[0].query, support::pairs(&[("month", "2026-09")]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}
#[tokio::test]
async fn by_project_without_params() {
let response = support::json(r#"{
"object": "usage_report",
"period": {
"startsAt": "2026-09-24T09:00:00.000Z",
"endsAt": "2026-09-24T09:00:00.000Z"
},
"projects": [
{
"projectId": "projectId 1",
"externalId": "externalId 1",
"name": "name 1",
"billableAccountCount": 1,
"accountCount": 1,
"proxyBytes": 1,
"sentMessageCount": 1,
"receivedMessageCount": 1
}
],
"unassigned": {
"billableAccountCount": 1,
"accountCount": 1,
"proxyBytes": 1,
"sentMessageCount": 1,
"receivedMessageCount": 1
},
"totals": {
"billableAccountCount": 1,
"accountCount": 1,
"proxyBytes": 1,
"sentMessageCount": 1,
"receivedMessageCount": 1
}
}"#);
let api = support::MockApi::start(vec![support::Reply::json(200, response.clone())]).await;
let result = api.client().usage().by_project(Default::default()).await.unwrap();
support::assert_json(&result, &response);
let calls = api.calls().await;
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].method, "GET");
assert_eq!(calls[0].path, "/v1/usage/by-project");
assert_eq!(calls[0].query, support::pairs(&[]));
assert!(calls[0].body.is_none());
assert!(calls[0].header(sdk::IDEMPOTENCY_HEADER).is_none());
}