mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Load analytics reports with server plans and account identity checks (#45762)
## Why Token plan claims can be stale, and the active account or user can change while a request is in flight. Analytics report selection and account-bound response data need to reflect the verified identity and current server plan. ## What changed - Fetch the active account's plan once per analytics session and use it to select report endpoints and supported credit breakdowns. - Add report loading with a fixed end date, account-scoped response caching, and token model filtering. Reuse payloads across grouping changes and evict invalid cached responses so requests can retry. - Prefer complete attribution for usage breakdowns within the requested range; retain legacy surface/model data when attribution is incomplete and include all features in turn-start breakdowns. - Add cancellable report-loading state with timeout and interruption errors, and preserve actionable sign-in and retry messages. - Recheck the active identity after rate-limit reads before exposing account-bound fields. ## Testing Add regression tests for server plan discovery, report routing and caching, model filtering, attribution fallback, failed-request retries, load cancellation, authentication recovery, and account or user changes during requests. GitOrigin-RevId: 45c4c09f108c9703893f3ed15613437ebd7c74a8
This commit is contained in:
1 parent
aaa2cabfbc
commit
1fc46a532b
17 files changed
+1477
-63
No files matched your search
@@ -13,7 +13,7 @@ use wiremock::matchers::header;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
fn sign_in(home: &std::path::Path, account: &str, user: &str, plan: &str) {
|
||||
pub(super) fn sign_in(home: &std::path::Path, account: &str, user: &str, plan: &str) {
|
||||
let claims = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(
|
||||
json!({
|
||||
"exp": 4102444800_i64, "email": "analytics@example.test",
|
||||
@@ -53,7 +53,29 @@ pub(in crate::analytics) async fn live(
|
||||
config.chatgpt_base_url = format!("{}/backend-api", server.uri());
|
||||
config.cli_auth_credentials_store_mode = codex_login::AuthCredentialsStoreMode::File;
|
||||
sign_in(home.path(), "account-a", "user-a", plan);
|
||||
(home, Live::new(Arc::new(config)))
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/backend-api/wham/accounts/check"))
|
||||
.respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(json!({
|
||||
"accounts": [{"id": "account-a", "plan_type": plan}],
|
||||
"account_ordering": ["account-a"]
|
||||
})))
|
||||
.with_priority(/*p*/ 2)
|
||||
.mount(server)
|
||||
.await;
|
||||
(
|
||||
home,
|
||||
Live::new(Arc::new(config), "2026-01-15".parse().unwrap()),
|
||||
)
|
||||
}
|
||||
|
||||
async fn report_requests(server: &MockServer) -> Vec<wiremock::Request> {
|
||||
server
|
||||
.received_requests()
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|request| request.url.path() != "/backend-api/wham/accounts/check")
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -72,7 +94,7 @@ async fn analytics_rejects_identity_changes_during_requests() {
|
||||
result.unwrap_err().to_string(),
|
||||
"Account changed. Press R to refresh Analytics."
|
||||
);
|
||||
assert!(server.received_requests().await.unwrap().is_empty());
|
||||
assert!(report_requests(&server).await.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -112,7 +134,7 @@ async fn analytics_requires_local_chatgpt_authentication() {
|
||||
live.session().await.err(),
|
||||
Some("Sign in locally with ChatGPT to view Analytics.".to_string())
|
||||
);
|
||||
assert!(server.received_requests().await.unwrap().is_empty());
|
||||
assert!(report_requests(&server).await.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,8 +176,8 @@ async fn analytics_requests_use_reloaded_credentials_for_the_same_identity() {
|
||||
assert_eq!(
|
||||
result,
|
||||
codex_backend_client::AnalyticsResponse::Usage(
|
||||
codex_backend_client::analytics_models::DailyProductSurfaceUsageResponse::default(),
|
||||
),
|
||||
codex_backend_client::analytics_models::DailyProductSurfaceUsageResponse::default()
|
||||
)
|
||||
);
|
||||
server.verify().await;
|
||||
}
|
||||
@@ -203,8 +225,8 @@ async fn analytics_retries_unauthorized_requests_after_credentials_reload() {
|
||||
assert_eq!(
|
||||
result,
|
||||
codex_backend_client::AnalyticsResponse::Usage(
|
||||
codex_backend_client::analytics_models::DailyProductSurfaceUsageResponse::default(),
|
||||
),
|
||||
codex_backend_client::analytics_models::DailyProductSurfaceUsageResponse::default()
|
||||
)
|
||||
);
|
||||
server.verify().await;
|
||||
}
|
||||
@@ -221,3 +243,523 @@ async fn analytics_retries_session_initialization_after_sign_in() {
|
||||
"account-a"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn analytics_matches_app_account_routes_and_parameters() {
|
||||
let server = MockServer::start().await;
|
||||
for (plan, report, grouping, endpoint, extra) in [
|
||||
(
|
||||
"plus",
|
||||
Report::Usage,
|
||||
Grouping::Model,
|
||||
"usage/daily-token-usage-breakdown",
|
||||
vec![("group_by", "day")],
|
||||
),
|
||||
(
|
||||
"business",
|
||||
Report::Usage,
|
||||
Grouping::TokenType,
|
||||
"usage/daily-workspace-user-token-usage-breakdown",
|
||||
vec![
|
||||
("group_by", "day"),
|
||||
("breakdown_by", "model"),
|
||||
("modes", "codex"),
|
||||
("modes", "work"),
|
||||
],
|
||||
),
|
||||
(
|
||||
"business",
|
||||
Report::Messages,
|
||||
Grouping::Model,
|
||||
"analytics/daily-workspace-usage-counts",
|
||||
vec![("group_by", "day"), ("workspace_user", "true")],
|
||||
),
|
||||
(
|
||||
"unknown",
|
||||
Report::Usage,
|
||||
Grouping::Surface,
|
||||
"usage/daily-token-usage-breakdown",
|
||||
vec![("group_by", "day")],
|
||||
),
|
||||
(
|
||||
"unknown",
|
||||
Report::Messages,
|
||||
Grouping::Model,
|
||||
"analytics/daily-workspace-usage-counts",
|
||||
vec![("group_by", "day"), ("workspace_user", "true")],
|
||||
),
|
||||
(
|
||||
"unknown",
|
||||
Report::Plugins,
|
||||
Grouping::Feature,
|
||||
"analytics/daily-plugin-usage-metrics",
|
||||
vec![
|
||||
("group_by", "day"),
|
||||
("workspace_user", "true"),
|
||||
("top_plugin_limit", "10"),
|
||||
],
|
||||
),
|
||||
(
|
||||
"unknown",
|
||||
Report::Skills,
|
||||
Grouping::Feature,
|
||||
"analytics/daily-skill-usage-metrics",
|
||||
vec![
|
||||
("group_by", "day"),
|
||||
("workspace_user", "true"),
|
||||
("top_skill_limit", "10"),
|
||||
],
|
||||
),
|
||||
(
|
||||
"edu",
|
||||
Report::Credits,
|
||||
Grouping::Model,
|
||||
"usage/daily-workspace-user-credit-usage",
|
||||
vec![("breakdown", "model")],
|
||||
),
|
||||
(
|
||||
"plus",
|
||||
Report::Credits,
|
||||
Grouping::Surface,
|
||||
"usage/credit-usage-events",
|
||||
vec![],
|
||||
),
|
||||
(
|
||||
"team",
|
||||
Report::Usage,
|
||||
Grouping::Model,
|
||||
"usage/daily-workspace-user-token-usage-breakdown",
|
||||
vec![("group_by", "day")],
|
||||
),
|
||||
(
|
||||
"team",
|
||||
Report::Credits,
|
||||
Grouping::Speed,
|
||||
"usage/daily-workspace-user-token-usage-breakdown",
|
||||
vec![("group_by", "day")],
|
||||
),
|
||||
(
|
||||
"business",
|
||||
Report::Credits,
|
||||
Grouping::Reasoning,
|
||||
"usage/daily-workspace-user-credit-usage",
|
||||
vec![("breakdown", "reasoning_effort")],
|
||||
),
|
||||
(
|
||||
"plus",
|
||||
Report::Messages,
|
||||
Grouping::Model,
|
||||
"analytics/daily-workspace-usage-counts",
|
||||
vec![("group_by", "day"), ("workspace_user", "true")],
|
||||
),
|
||||
(
|
||||
"plus",
|
||||
Report::Plugins,
|
||||
Grouping::Feature,
|
||||
"analytics/daily-plugin-usage-metrics",
|
||||
vec![
|
||||
("group_by", "day"),
|
||||
("workspace_user", "true"),
|
||||
("top_plugin_limit", "10"),
|
||||
],
|
||||
),
|
||||
(
|
||||
"business",
|
||||
Report::Plugins,
|
||||
Grouping::Feature,
|
||||
"analytics/daily-plugin-usage-metrics",
|
||||
vec![
|
||||
("group_by", "day"),
|
||||
("workspace_user", "true"),
|
||||
("top_plugin_limit", "8"),
|
||||
],
|
||||
),
|
||||
(
|
||||
"business",
|
||||
Report::Skills,
|
||||
Grouping::Feature,
|
||||
"analytics/daily-skill-usage-metrics",
|
||||
vec![
|
||||
("group_by", "day"),
|
||||
("workspace_user", "true"),
|
||||
("top_skill_limit", "6"),
|
||||
],
|
||||
),
|
||||
] {
|
||||
server.reset().await;
|
||||
let (_home, live) = live(&server, plan).await;
|
||||
let today = live.end_date;
|
||||
let start = (today - chrono::Days::new(/*num*/ 6)).to_string();
|
||||
let response = if endpoint == "usage/daily-workspace-user-credit-usage" {
|
||||
let breakdown = extra.iter().find(|(key, _)| *key == "breakdown").unwrap().1;
|
||||
json!({"data": [], "series": [], "breakdown": breakdown})
|
||||
} else {
|
||||
json!({"data": []})
|
||||
};
|
||||
Mock::given(method("GET"))
|
||||
.and(path(format!("/backend-api/wham/{endpoint}")))
|
||||
.and(header("chatgpt-account-id", "account-a"))
|
||||
.respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(response))
|
||||
.expect(/*r*/ 1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
assert!(
|
||||
live.history(report, /*days*/ 7, grouping)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_some()
|
||||
);
|
||||
let requests = report_requests(&server).await;
|
||||
let mut actual = requests[0]
|
||||
.url
|
||||
.query_pairs()
|
||||
.map(|(key, value)| (key.into_owned(), value.into_owned()))
|
||||
.collect::<Vec<_>>();
|
||||
let mut expected = extra
|
||||
.into_iter()
|
||||
.map(|(key, value)| (key.to_string(), value.to_string()))
|
||||
.collect::<Vec<_>>();
|
||||
if endpoint != "usage/credit-usage-events" {
|
||||
expected.push(("start_date".into(), start.clone()));
|
||||
expected.push(("end_date".into(), today.to_string()));
|
||||
}
|
||||
actual.sort();
|
||||
expected.sort();
|
||||
assert_eq!(actual, expected);
|
||||
assert!(requests[0].headers.get("authorization").is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn analytics_uses_api_response_for_usage_and_turns_availability() {
|
||||
let server = MockServer::start().await;
|
||||
for report in [Report::Usage, Report::Messages] {
|
||||
for status in [403, 404] {
|
||||
server.reset().await;
|
||||
let (_home, live) = live(&server, "business").await;
|
||||
Mock::given(method("GET"))
|
||||
.respond_with(ResponseTemplate::new(status))
|
||||
.expect(/*r*/ 1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
assert_eq!(
|
||||
live.history(report, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
if status == 403 {
|
||||
"Access denied for this report."
|
||||
} else {
|
||||
"This report endpoint is unavailable. Press R to retry."
|
||||
}
|
||||
);
|
||||
server.verify().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn analytics_reuses_payload_for_grouping_and_rejects_account_changes() {
|
||||
let server = MockServer::start().await;
|
||||
let (home, live) = live(&server, "team").await;
|
||||
let today = live.end_date.to_string();
|
||||
Mock::given(method("GET"))
|
||||
.and(path(
|
||||
"/backend-api/wham/usage/daily-workspace-user-token-usage-breakdown",
|
||||
))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(/*s*/ 200).set_body_json(json!({"data": [{
|
||||
"date": today, "product_surface_usage_values": {}, "premium_usage_values": {
|
||||
"credit_usage_credits": {"cli": 12.5}, "total_usage_credits": {},
|
||||
"uncached_text_input_tokens_by_surface": {}, "cached_text_input_tokens_by_surface": {},
|
||||
"text_output_tokens_by_surface": {}, "text_total_tokens_by_surface": {}
|
||||
},
|
||||
"models": [{"model": "model-a", "credits": 12.5, "speed": "fast", "on_demand_credits": 12.5}]
|
||||
}]})),
|
||||
)
|
||||
.expect(/*r*/ 1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let surface = live
|
||||
.history(Report::Credits, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let model = live
|
||||
.history(Report::Credits, /*days*/ 7, Grouping::Model)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
(
|
||||
surface.data.last().unwrap().total,
|
||||
model.data.last().unwrap().total
|
||||
),
|
||||
(12.5, 12.5)
|
||||
);
|
||||
sign_in(home.path(), "account-a", "user-b", "team");
|
||||
assert_eq!(
|
||||
live.history(Report::Credits, /*days*/ 7, Grouping::Model)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
"Account changed. Press R to refresh Analytics."
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn token_model_filter_reuses_the_account_scoped_response() {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, "business").await;
|
||||
let today = live.end_date.to_string();
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/backend-api/wham/usage/daily-workspace-user-token-usage-breakdown"))
|
||||
.respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(json!({"data":[{
|
||||
"date":today, "product_surface_usage_values": {}, "groups":[
|
||||
{"dimensions":{"model":"alpha"}, "credits": 0, "uncached_text_input_tokens":10,"cached_text_input_tokens":20,"text_output_tokens":30},
|
||||
{"dimensions":{"model":"beta"}, "credits": 0, "uncached_text_input_tokens":100,"cached_text_input_tokens":200,"text_output_tokens":300}
|
||||
]
|
||||
}]})))
|
||||
.expect(/*r*/ 1).mount(&server).await;
|
||||
let all = live
|
||||
.history(Report::Usage, /*days*/ 7, Grouping::TokenType)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let alpha = live
|
||||
.filtered_history(
|
||||
Report::Usage,
|
||||
/*days*/ 7,
|
||||
Grouping::TokenType,
|
||||
Some("alpha"),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let beta = live
|
||||
.filtered_history(
|
||||
Report::Usage,
|
||||
/*days*/ 7,
|
||||
Grouping::TokenType,
|
||||
Some("beta"),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
(all.data[0].total, alpha.data[0].total, beta.data[0].total),
|
||||
(660.0, 60.0, 600.0)
|
||||
);
|
||||
assert_eq!(
|
||||
live.token_models(),
|
||||
vec!["beta".to_string(), "alpha".to_string()]
|
||||
);
|
||||
assert_eq!(report_requests(&server).await.len(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn legacy_usage_falls_back_to_surface_for_attribution_only_groupings() {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, "plus").await;
|
||||
let today = live.end_date.to_string();
|
||||
Mock::given(method("GET"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(/*s*/ 200).set_body_json(json!({"data": [{
|
||||
"date": today, "product_surface_usage_values": {"cli": 12.5}
|
||||
}]})),
|
||||
)
|
||||
.expect(/*r*/ 1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let surface = live
|
||||
.history(Report::Usage, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.unwrap();
|
||||
for grouping in [Grouping::Feature, Grouping::TaskStart] {
|
||||
let history = live
|
||||
.history(Report::Usage, /*days*/ 7, grouping)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!((live.attributed_usage(), history), (false, surface.clone()));
|
||||
}
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_ranges_fail_without_requesting_reports() {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, "plus").await;
|
||||
for days in [0, u32::MAX] {
|
||||
assert_eq!(
|
||||
live.history(Report::Usage, days, Grouping::Surface)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
"Invalid analytics date range."
|
||||
);
|
||||
}
|
||||
assert!(report_requests(&server).await.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unknown_plan_exposes_no_credit_breakdowns() {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, "unknown").await;
|
||||
live.session().await.unwrap();
|
||||
assert!(live.credit_groups().is_empty());
|
||||
assert!(
|
||||
live.history(Report::Credits, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(report_requests(&server).await.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn in_flight_account_switch_retains_refresh_guidance() {
|
||||
let server = MockServer::start().await;
|
||||
let (home, live) = live(&server, "plus").await;
|
||||
let home_path = home.path().to_path_buf();
|
||||
Mock::given(method("GET"))
|
||||
.respond_with(move |_: &wiremock::Request| {
|
||||
sign_in(&home_path, "account-b", "user-a", "plus");
|
||||
ResponseTemplate::new(/*s*/ 200).set_body_json(json!({"data": []}))
|
||||
})
|
||||
.expect(/*r*/ 1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
assert_eq!(
|
||||
live.history(Report::Usage, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
"Account changed. Press R to refresh Analytics."
|
||||
);
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn missing_breakdowns_remain_unavailable() {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, "plus").await;
|
||||
let today = live.end_date.to_string();
|
||||
Mock::given(method("GET"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(/*s*/ 200).set_body_json(
|
||||
json!({"data": [{"date": today, "product_surface_usage_values": {}}]}),
|
||||
),
|
||||
)
|
||||
.expect(/*r*/ 1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
assert_eq!(
|
||||
live.history(Report::Usage, /*days*/ 7, Grouping::Model)
|
||||
.await,
|
||||
Ok(None)
|
||||
);
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn out_of_range_legacy_rows_do_not_disable_attribution() {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, "plus").await;
|
||||
let today = live.end_date.to_string();
|
||||
Mock::given(method("GET"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(/*s*/ 200).set_body_json(json!({"data": [
|
||||
{"date": "2000-01-01", "product_surface_usage_values": {}},
|
||||
{"date": today, "product_surface_usage_values": {}, "attribution": [{"thread_source": "user", "turn_trigger": "composer", "model": "example", "surface": "cli", "value": 12.0}]}
|
||||
]})),
|
||||
)
|
||||
.expect(/*r*/ 1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let history = live
|
||||
.history(Report::Usage, /*days*/ 7, Grouping::Feature)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
history.data,
|
||||
vec![super::super::models::AccountAnalyticsDay {
|
||||
date: live.end_date,
|
||||
total: 12.0,
|
||||
values: vec![super::super::models::AccountAnalyticsValue {
|
||||
key: "user".into(),
|
||||
label: "Tasks".into(),
|
||||
value: 12.0,
|
||||
}],
|
||||
}]
|
||||
);
|
||||
assert!(live.attributed_usage());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_reports_can_retry_without_recreating_the_session() {
|
||||
for plan in ["plus", "business"] {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, plan).await;
|
||||
let requests = Arc::new(std::sync::atomic::AtomicUsize::new(/*v*/ 0));
|
||||
let count = Arc::clone(&requests);
|
||||
Mock::given(method("GET"))
|
||||
.respond_with(move |_: &wiremock::Request| {
|
||||
let body = if count.fetch_add(/*val*/ 1, std::sync::atomic::Ordering::SeqCst) == 0 {
|
||||
json!({"data": [{"date": "invalid"}]})
|
||||
} else {
|
||||
json!({"data": []})
|
||||
};
|
||||
ResponseTemplate::new(/*s*/ 200).set_body_json(body)
|
||||
})
|
||||
.expect(/*r*/ 2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
assert!(
|
||||
live.history(Report::Usage, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
live.history(Report::Usage, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
assert_eq!(requests.load(std::sync::atomic::Ordering::SeqCst), 2);
|
||||
server.verify().await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_cached_breakdowns_are_evicted_before_retry() {
|
||||
let server = MockServer::start().await;
|
||||
let (_home, live) = live(&server, "plus").await;
|
||||
let today = live.end_date.to_string();
|
||||
let attempts = Arc::new(std::sync::atomic::AtomicUsize::new(/*v*/ 0));
|
||||
let count = Arc::clone(&attempts);
|
||||
Mock::given(method("GET"))
|
||||
.respond_with(move |_: &wiremock::Request| {
|
||||
let model = if count.fetch_add(/*val*/ 1, std::sync::atomic::Ordering::SeqCst) == 0 {
|
||||
json!({"model": "alpha", "credits": -1})
|
||||
} else {
|
||||
json!({"model": "alpha", "credits": 12})
|
||||
};
|
||||
ResponseTemplate::new(/*s*/ 200).set_body_json(json!({"data": [{
|
||||
"date": today, "product_surface_usage_values": {"cli": 12}, "models": [model]
|
||||
}]}))
|
||||
})
|
||||
.expect(/*r*/ 2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
live.history(Report::Usage, /*days*/ 7, Grouping::Surface)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
live.history(Report::Usage, /*days*/ 7, Grouping::Model)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
live.history(Report::Usage, /*days*/ 7, Grouping::Model)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_some()
|
||||
);
|
||||
assert_eq!(attempts.load(std::sync::atomic::Ordering::SeqCst), 2);
|
||||
server.verify().await;
|
||||
}
|
||||
Reference in new issue
Block a user