mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
## Why Account reads can use a cached subscription plan, and rolling quota updates can carry an obsolete plan. This can leave thread usage unavailable or cause `/status` to display costs using the wrong billing model. ## What changed - Prefer a known plan from a full Codex quota read over the account plan, regardless of reply order. Ignore unknown quota plans and prevent rolling updates from changing the billing plan. - Preserve quota plan state across chat widget replacement and apply accepted quota reads even after the requesting status card is gone. - Refresh thread usage availability when the plan changes and pair asynchronous `/status` estimates with the corrected plan, including native dollar usage for `Ent26`. ## Testing Add regression tests for reply ordering, unknown plans, corrected pending status cards, obsolete rolling updates, and quota replies arriving after widget replacement. GitOrigin-RevId: 238fadb587fe98cda0eeb5111c1949928acc8153
1203 lines
46 KiB
Rust
1203 lines
46 KiB
Rust
use super::THREAD_USAGE_RETRY_DELAYS;
|
|
use super::THREAD_USAGE_SETTLEMENT_DELAYS;
|
|
use super::ThreadUsageOutcome;
|
|
use crate::app_event::AppEvent;
|
|
use crate::chatwidget::ChatWidget;
|
|
use crate::chatwidget::ReplayKind;
|
|
use crate::chatwidget::tests::make_chatwidget_manual_with_sender;
|
|
use crate::status::format_estimated_usd_micros;
|
|
use crate::tui::FrameRequester;
|
|
use codex_app_server_protocol::CodexErrorInfo;
|
|
use codex_app_server_protocol::ErrorNotification;
|
|
use codex_app_server_protocol::ServerNotification;
|
|
use codex_app_server_protocol::ThreadUsage;
|
|
use codex_app_server_protocol::ThreadUsageBreakdownGroup;
|
|
use codex_app_server_protocol::Turn;
|
|
use codex_app_server_protocol::TurnError;
|
|
use codex_app_server_protocol::TurnItemsView;
|
|
use codex_app_server_protocol::TurnStatus;
|
|
use codex_protocol::ThreadId;
|
|
use codex_protocol::account::PlanType;
|
|
use pretty_assertions::assert_eq;
|
|
use std::time::Duration;
|
|
use std::time::Instant;
|
|
use tokio::sync::mpsc::UnboundedReceiver;
|
|
use uuid::Uuid;
|
|
|
|
fn thread_usage_request_id(events: &mut UnboundedReceiver<AppEvent>) -> Uuid {
|
|
match events.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected a thread usage request, got {event:?}"),
|
|
}
|
|
}
|
|
|
|
fn thread_usage(
|
|
thread_id: ThreadId,
|
|
estimated_usage_credits_micros: i64,
|
|
estimated_usage_usd_micros: Option<i64>,
|
|
) -> ThreadUsage {
|
|
ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros,
|
|
estimated_usage_usd_micros,
|
|
groups: Vec::new(),
|
|
}
|
|
}
|
|
|
|
async fn widget_with_pending_billing_read(thread_id: ThreadId) -> (ChatWidget, Uuid) {
|
|
let (mut chat, _sender, mut events, _ops) = make_chatwidget_manual_with_sender().await;
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_chatgpt_account = true;
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.ensure_thread_usage_requested();
|
|
let request_id = thread_usage_request_id(&mut events);
|
|
(chat, request_id)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn replacement_widget_rejects_a_previous_widgets_read_for_the_same_thread() {
|
|
let thread = ThreadId::new();
|
|
let (_, old_request) = widget_with_pending_billing_read(thread).await;
|
|
let (mut replacement, current) = widget_with_pending_billing_read(thread).await;
|
|
assert!(!replacement.finish_thread_usage_refresh(
|
|
thread,
|
|
old_request,
|
|
Ok(ThreadUsageOutcome::Disabled)
|
|
));
|
|
assert!(replacement.is_pending_thread_usage(thread, current));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn billing_prefers_a_known_quota_plan_in_either_reply_order() {
|
|
for (account, quota_plan, expected) in [
|
|
(PlanType::Plus, PlanType::Business, PlanType::Business),
|
|
(PlanType::Unknown, PlanType::Business, PlanType::Business),
|
|
(PlanType::Business, PlanType::Unknown, PlanType::Business),
|
|
] {
|
|
for quota_first in [true, false] {
|
|
let (mut chat, ..) = make_chatwidget_manual_with_sender().await;
|
|
chat.status_account_display = Some(crate::status::StatusAccountDisplay::ChatGpt {
|
|
email: None,
|
|
plan: None,
|
|
workspace: None,
|
|
});
|
|
let quota =
|
|
serde_json::from_value(serde_json::json!({"planType": quota_plan})).unwrap();
|
|
let account_read = |chat: &mut ChatWidget| {
|
|
chat.on_account_email_loaded(
|
|
/*current_email*/ None, /*current_workspace*/ None, account,
|
|
);
|
|
};
|
|
if quota_first {
|
|
chat.on_rate_limit_snapshot(Some(quota));
|
|
account_read(&mut chat);
|
|
} else {
|
|
account_read(&mut chat);
|
|
assert_eq!(chat.current_plan_type(), Some(account));
|
|
chat.on_rate_limit_snapshot(Some(quota));
|
|
}
|
|
assert_eq!(chat.current_plan_type(), Some(expected));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn pending_status_card_uses_the_corrected_plan_for_its_usage_reply() {
|
|
let mut rows = Vec::new();
|
|
for original_plan in [PlanType::Business, PlanType::Plus] {
|
|
let (mut chat, _, mut events, _) = make_chatwidget_manual_with_sender().await;
|
|
let thread = ThreadId::new();
|
|
chat.thread_id = Some(thread);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(original_plan);
|
|
let status_request = 1;
|
|
chat.add_status_output(/*refreshing_rate_limits*/ true, Some(status_request));
|
|
let AppEvent::InsertHistoryCell(card) = events.try_recv().unwrap() else {
|
|
panic!("expected a /status card");
|
|
};
|
|
let corrected_ent26 =
|
|
serde_json::from_value(serde_json::json!({"planType": PlanType::Ent26})).unwrap();
|
|
chat.finish_status_rate_limit_refresh(status_request, vec![corrected_ent26]);
|
|
let usage = ThreadUsage {
|
|
native_usage_usd_micros: Some(1_800_000),
|
|
..thread_usage(
|
|
thread,
|
|
/*estimated_usage_credits_micros*/ 50_000_000,
|
|
Some(2_000_000),
|
|
)
|
|
};
|
|
let request = thread_usage_request_id(&mut events);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread,
|
|
request,
|
|
Ok(ThreadUsageOutcome::Available(usage))
|
|
));
|
|
let row = card
|
|
.display_lines(/*width*/ 90)
|
|
.into_iter()
|
|
.map(|line| line.to_string())
|
|
.find(|line| line.contains("Thread usage:"))
|
|
.unwrap();
|
|
rows.push(format!("{original_plan:?} → Ent26: {}", row.trim()));
|
|
}
|
|
insta::assert_snapshot!(rows.join("\n"), @r"
|
|
Business → Ent26: Thread usage: ~$1.80
|
|
Plus → Ent26: Thread usage: ~$1.80
|
|
");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rolling_quota_cannot_restore_an_obsolete_billing_plan() {
|
|
let (mut chat, _, mut events, _) = make_chatwidget_manual_with_sender().await;
|
|
let thread = ThreadId::new();
|
|
chat.thread_id = Some(thread);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".into()]);
|
|
chat.status_account_display = Some(crate::status::StatusAccountDisplay::ChatGpt {
|
|
email: None,
|
|
plan: None,
|
|
workspace: None,
|
|
});
|
|
chat.on_account_email_loaded(
|
|
/*current_email*/ None,
|
|
/*current_workspace*/ None,
|
|
PlanType::Business,
|
|
);
|
|
let pending = thread_usage_request_id(&mut events);
|
|
chat.on_account_email_loaded(
|
|
/*current_email*/ None,
|
|
/*current_workspace*/ None,
|
|
PlanType::Plus,
|
|
);
|
|
let rolling = serde_json::from_value(serde_json::json!({"planType": "business"})).unwrap();
|
|
chat.on_rolling_rate_limit_snapshot(rolling);
|
|
|
|
assert_eq!(chat.current_plan_type(), Some(PlanType::Plus));
|
|
assert!(!chat.finish_thread_usage_refresh(thread, pending, Ok(ThreadUsageOutcome::Disabled)));
|
|
}
|
|
|
|
#[test]
|
|
fn estimated_cost_formats_dollars_and_sub_cent_values() {
|
|
assert_eq!(
|
|
format_estimated_usd_micros(/*micros*/ 0),
|
|
Some("~$0.00".to_string())
|
|
);
|
|
assert_eq!(
|
|
format_estimated_usd_micros(/*micros*/ 1),
|
|
Some("~$0.000001".to_string())
|
|
);
|
|
assert_eq!(
|
|
format_estimated_usd_micros(/*micros*/ 400),
|
|
Some("~$0.0004".to_string())
|
|
);
|
|
assert_eq!(
|
|
format_estimated_usd_micros(/*micros*/ 1_820_000),
|
|
Some("~$1.82".to_string())
|
|
);
|
|
assert_eq!(
|
|
format_estimated_usd_micros(/*micros*/ 995_000),
|
|
Some("~$1.00".to_string())
|
|
);
|
|
assert_eq!(format_estimated_usd_micros(/*micros*/ -1), None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn temporary_thread_usage_failures_have_bounded_retries() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
|
|
for attempt in 0..=THREAD_USAGE_RETRY_DELAYS.len() {
|
|
let request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected retry attempt {attempt}, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Err("thread usage temporarily unavailable".to_string()),
|
|
));
|
|
|
|
if attempt < THREAD_USAGE_RETRY_DELAYS.len() {
|
|
assert!(chat.thread_usage.retry_due_at.is_some());
|
|
chat.thread_usage.retry_due_at = Some(Instant::now());
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
}
|
|
}
|
|
|
|
assert!(chat.thread_usage.retry_due_at.is_none());
|
|
assert!(rx.try_recv().is_err(), "retries must stop after the bound");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn status_history_survives_exhausted_billing_retries() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
let mut request_id = thread_usage_request_id(&mut rx);
|
|
|
|
chat.add_status_output(
|
|
/*refreshing_rate_limits*/ false, /*request_id*/ None,
|
|
);
|
|
let first_status_cell = match rx.try_recv() {
|
|
Ok(AppEvent::InsertHistoryCell(cell)) => cell,
|
|
event => panic!("expected the first /status card, got {event:?}"),
|
|
};
|
|
|
|
for attempt in 0..=THREAD_USAGE_RETRY_DELAYS.len() {
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Err("thread usage temporarily unavailable".to_string()),
|
|
));
|
|
assert_eq!(chat.thread_usage.status_history_handles.len(), 1);
|
|
|
|
if attempt < THREAD_USAGE_RETRY_DELAYS.len() {
|
|
chat.thread_usage.retry_due_at = Some(Instant::now());
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
request_id = thread_usage_request_id(&mut rx);
|
|
}
|
|
}
|
|
|
|
assert_eq!(chat.thread_usage.retry_due_at, None);
|
|
chat.add_status_output(
|
|
/*refreshing_rate_limits*/ false, /*request_id*/ None,
|
|
);
|
|
let retry_status_cell = match rx.try_recv() {
|
|
Ok(AppEvent::InsertHistoryCell(cell)) => cell,
|
|
event => panic!("expected a fresh /status card, got {event:?}"),
|
|
};
|
|
request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 50_000_000,
|
|
Some(2_100_000),
|
|
))),
|
|
));
|
|
|
|
for status_cell in [&first_status_cell, &retry_status_cell] {
|
|
let rendered = status_cell
|
|
.display_lines(/*width*/ 90)
|
|
.into_iter()
|
|
.flat_map(|line| line.spans)
|
|
.map(|span| span.content.into_owned())
|
|
.collect::<String>();
|
|
assert!(rendered.contains("50 credits · ~$2.10"));
|
|
}
|
|
assert!(chat.thread_usage.status_history_handles.is_empty());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn status_history_updates_again_after_billing_settles() {
|
|
for (selected_item, stale_credits_micros, stale_usd_micros) in [
|
|
("estimated-thread-cost", 40_000_000, 1_820_000),
|
|
("thread-credits", 50_000_000, 1_820_000),
|
|
("estimated-thread-cost", 40_000_000, 2_100_000),
|
|
] {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec![selected_item.to_string()]);
|
|
chat.refresh_status_line();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 40_000_000,
|
|
Some(1_820_000),
|
|
))),
|
|
));
|
|
|
|
chat.refresh_thread_usage_after_turn();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
chat.add_status_output(
|
|
/*refreshing_rate_limits*/ false, /*request_id*/ None,
|
|
);
|
|
let status_cell = match rx.try_recv() {
|
|
Ok(AppEvent::InsertHistoryCell(cell)) => cell,
|
|
event => panic!("expected an immediate /status card, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
stale_credits_micros,
|
|
Some(stale_usd_micros),
|
|
))),
|
|
));
|
|
assert_eq!(chat.thread_usage.status_history_handles.len(), 1);
|
|
|
|
chat.thread_usage.settlement_refresh_due_at = Some(Instant::now());
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 50_000_000,
|
|
Some(2_100_000),
|
|
))),
|
|
));
|
|
|
|
let rendered = status_cell
|
|
.display_lines(/*width*/ 90)
|
|
.into_iter()
|
|
.flat_map(|line| line.spans)
|
|
.map(|span| span.content.into_owned())
|
|
.collect::<String>();
|
|
assert!(rendered.contains("50 credits · ~$2.10"));
|
|
assert!(chat.thread_usage.status_history_handles.is_empty());
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn replayed_turn_completions_do_not_start_live_billing_refreshes() {
|
|
for replay_kind in [
|
|
ReplayKind::ResumeInitialMessages,
|
|
ReplayKind::ThreadSnapshot,
|
|
] {
|
|
for (status, error) in [
|
|
(TurnStatus::Completed, None),
|
|
(TurnStatus::Interrupted, None),
|
|
(TurnStatus::Failed, None),
|
|
(
|
|
TurnStatus::Failed,
|
|
Some(TurnError {
|
|
misalignment: None,
|
|
message: "the replayed turn failed".to_string(),
|
|
codex_error_info: Some(CodexErrorInfo::ServerOverloaded),
|
|
additional_details: None,
|
|
}),
|
|
),
|
|
] {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 46_000_000,
|
|
Some(1_820_000),
|
|
))),
|
|
));
|
|
|
|
chat.replay_thread_turns(
|
|
vec![Turn {
|
|
id: "replayed-turn".to_string(),
|
|
root_turn_id: None,
|
|
items: Vec::new(),
|
|
items_view: TurnItemsView::NotLoaded,
|
|
status: status.clone(),
|
|
error,
|
|
started_at: None,
|
|
completed_at: None,
|
|
duration_ms: None,
|
|
}],
|
|
replay_kind,
|
|
);
|
|
|
|
assert!(
|
|
!std::iter::from_fn(|| rx.try_recv().ok())
|
|
.any(|event| matches!(event, AppEvent::RefreshThreadUsage { .. })),
|
|
"{replay_kind:?} replay of {status:?} must not start live billing settlement"
|
|
);
|
|
assert_eq!(chat.thread_usage.settlement_baseline_credits_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_baseline_usd_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_refresh_due_at, None);
|
|
assert!(!chat.thread_usage.replaying_turn_completion);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn replayed_errors_do_not_start_live_billing_refreshes() {
|
|
for replay_kind in [
|
|
ReplayKind::ResumeInitialMessages,
|
|
ReplayKind::ThreadSnapshot,
|
|
] {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 46_000_000,
|
|
Some(1_820_000),
|
|
))),
|
|
));
|
|
|
|
let notification = ServerNotification::Error(ErrorNotification {
|
|
error: TurnError {
|
|
misalignment: None,
|
|
message: "the replayed turn failed".to_string(),
|
|
codex_error_info: Some(CodexErrorInfo::ServerOverloaded),
|
|
additional_details: None,
|
|
},
|
|
will_retry: false,
|
|
thread_id: thread_id.to_string(),
|
|
turn_id: "replayed-turn".to_string(),
|
|
});
|
|
chat.handle_server_notification(notification.clone(), Some(replay_kind));
|
|
assert!(
|
|
!std::iter::from_fn(|| rx.try_recv().ok())
|
|
.any(|event| matches!(event, AppEvent::RefreshThreadUsage { .. })),
|
|
"{replay_kind:?} error replay must not start live billing settlement"
|
|
);
|
|
assert!(!chat.thread_usage.replaying_turn_completion);
|
|
|
|
chat.handle_server_notification(notification, /*replay_kind*/ None);
|
|
assert!(
|
|
std::iter::from_fn(|| rx.try_recv().ok())
|
|
.any(|event| matches!(event, AppEvent::RefreshThreadUsage { .. })),
|
|
"a later live error must still refresh billing"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn transient_zero_cost_preserves_fresh_credits_and_breakdowns() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec![
|
|
"thread-credits".to_string(),
|
|
"estimated-thread-cost".to_string(),
|
|
]);
|
|
chat.local_settings.tui.terminal_title = chat.local_settings.tui.status_line.clone();
|
|
chat.refresh_status_surfaces();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 46_000_000,
|
|
Some(1_820_000),
|
|
))),
|
|
));
|
|
|
|
chat.refresh_thread_usage_after_turn();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
let fresh_usage = ThreadUsage {
|
|
groups: vec![ThreadUsageBreakdownGroup {
|
|
model: Some("gpt-5.4".to_string()),
|
|
reasoning_effort: Some("high".to_string()),
|
|
speed: Some("fast".to_string()),
|
|
estimated_usage_credits_micros: 50_000_000,
|
|
net_new_input_tokens: Some(80),
|
|
cached_input_tokens: Some(20),
|
|
input_tokens: Some(100),
|
|
output_tokens: Some(40),
|
|
total_tokens: Some(140),
|
|
}],
|
|
..thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 50_000_000,
|
|
Some(0),
|
|
)
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(fresh_usage.clone())),
|
|
));
|
|
|
|
assert_eq!(
|
|
chat.estimated_thread_usage(),
|
|
Some(&ThreadUsage {
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
..fresh_usage
|
|
})
|
|
);
|
|
assert_eq!(
|
|
chat.status_line_text(),
|
|
Some("50 credits · ~$1.82".to_string())
|
|
);
|
|
assert_eq!(
|
|
chat.last_terminal_title,
|
|
Some("50 credits | ~$1.82".to_string())
|
|
);
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn transient_zero_credits_preserves_credits_only_estimates() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["thread-credits".to_string()]);
|
|
chat.local_settings.tui.terminal_title = chat.local_settings.tui.status_line.clone();
|
|
chat.refresh_status_surfaces();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
let previous_usage = ThreadUsage {
|
|
groups: vec![ThreadUsageBreakdownGroup {
|
|
model: Some("gpt-5.4".to_string()),
|
|
reasoning_effort: Some("high".to_string()),
|
|
speed: Some("fast".to_string()),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
net_new_input_tokens: Some(80),
|
|
cached_input_tokens: Some(20),
|
|
input_tokens: Some(100),
|
|
output_tokens: Some(40),
|
|
total_tokens: Some(140),
|
|
}],
|
|
..thread_usage(
|
|
thread_id, /*estimated_usage_credits_micros*/ 46_000_000,
|
|
/*estimated_usage_usd_micros*/ None,
|
|
)
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(previous_usage.clone())),
|
|
));
|
|
|
|
chat.refresh_thread_usage_after_turn();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id, /*estimated_usage_credits_micros*/ 0,
|
|
/*estimated_usage_usd_micros*/ None,
|
|
))),
|
|
));
|
|
|
|
assert_eq!(chat.estimated_thread_usage(), Some(&previous_usage));
|
|
assert_eq!(chat.status_line_text(), Some("46 credits".to_string()));
|
|
assert_eq!(chat.last_terminal_title, Some("46 credits".to_string()));
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn cost_settlement_waits_for_new_or_missing_usd_estimates() {
|
|
for (plan_type, previous_usd_micros, stale_usd_micros) in [
|
|
(PlanType::Business, Some(1_820_000), Some(1_820_000)),
|
|
(PlanType::Business, None, None),
|
|
(PlanType::Business, None, Some(0)),
|
|
(PlanType::Ent26, Some(1_820_000), Some(1_820_000)),
|
|
(PlanType::Ent26, None, None),
|
|
(PlanType::Ent26, None, Some(0)),
|
|
(PlanType::Ent26, Some(1_820_000), Some(0)),
|
|
] {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
let [previous_usage, stale_usage, settled_usage] = [
|
|
(40_000_000, previous_usd_micros),
|
|
(50_000_000, stale_usd_micros),
|
|
(50_000_000, Some(2_100_000)),
|
|
]
|
|
.map(|(credits, cost)| {
|
|
let mut usage = thread_usage(thread_id, credits, cost);
|
|
usage.native_usage_usd_micros = cost;
|
|
if plan_type == PlanType::Ent26 {
|
|
usage.estimated_usage_credits_micros = 0;
|
|
usage.estimated_usage_usd_micros = Some(0);
|
|
}
|
|
usage
|
|
});
|
|
let previous_credits_micros = previous_usage.estimated_usage_credits_micros;
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(plan_type);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.local_settings.tui.terminal_title =
|
|
(plan_type == PlanType::Ent26).then(|| vec!["thread-credits".to_string()]);
|
|
chat.refresh_status_line();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(previous_usage)),
|
|
));
|
|
|
|
chat.refresh_thread_usage_after_turn();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(stale_usage)),
|
|
));
|
|
if plan_type == PlanType::Ent26 {
|
|
assert_eq!(
|
|
chat.status_line_text(),
|
|
stale_usd_micros.and_then(format_estimated_usd_micros)
|
|
);
|
|
}
|
|
assert_eq!(
|
|
chat.thread_usage.settlement_baseline_credits_micros,
|
|
Some(previous_credits_micros)
|
|
);
|
|
assert_eq!(
|
|
chat.thread_usage.settlement_baseline_usd_micros,
|
|
previous_usd_micros
|
|
);
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
|
|
chat.thread_usage.settlement_refresh_due_at = Some(Instant::now());
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(settled_usage)),
|
|
));
|
|
|
|
assert_eq!(chat.status_line_text(), Some("~$2.10".to_string()));
|
|
assert_eq!(chat.thread_usage.settlement_baseline_credits_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_baseline_usd_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_refresh_due_at, None);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn credits_only_settlement_completes_without_usd_estimates() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.terminal_title = Some(vec!["thread-credits".to_string()]);
|
|
chat.refresh_terminal_title();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id, /*estimated_usage_credits_micros*/ 40_000_000,
|
|
/*estimated_usage_usd_micros*/ None,
|
|
))),
|
|
));
|
|
|
|
chat.refresh_thread_usage_after_turn();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id, /*estimated_usage_credits_micros*/ 50_000_000,
|
|
/*estimated_usage_usd_micros*/ None,
|
|
))),
|
|
));
|
|
|
|
assert_eq!(chat.last_terminal_title, Some("50 credits".to_string()));
|
|
assert_eq!(chat.thread_usage.settlement_baseline_credits_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_baseline_usd_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_refresh_due_at, None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn combined_billing_surfaces_wait_for_credits_and_cost() {
|
|
for (status_line_item, title_item) in [
|
|
("thread-credits", "estimated-thread-cost"),
|
|
("estimated-thread-cost", "thread-credits"),
|
|
] {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec![status_line_item.to_string()]);
|
|
chat.local_settings.tui.terminal_title = Some(vec![title_item.to_string()]);
|
|
chat.refresh_status_surfaces();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 40_000_000,
|
|
Some(1_820_000),
|
|
))),
|
|
));
|
|
|
|
chat.refresh_thread_usage_after_turn();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 50_000_000,
|
|
Some(1_820_000),
|
|
))),
|
|
));
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
|
|
chat.thread_usage.settlement_refresh_due_at = Some(Instant::now());
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(thread_usage(
|
|
thread_id,
|
|
/*estimated_usage_credits_micros*/ 50_000_000,
|
|
Some(2_100_000),
|
|
))),
|
|
));
|
|
|
|
assert_eq!(chat.thread_usage.settlement_baseline_credits_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_baseline_usd_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_refresh_due_at, None);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn billing_surfaces_render_for_every_supported_enterprise_plan() {
|
|
for plan_type in [
|
|
PlanType::Business,
|
|
PlanType::EnterpriseCbpUsageBased,
|
|
PlanType::EnterpriseCbpAutomation,
|
|
PlanType::Ent26,
|
|
] {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(plan_type);
|
|
chat.local_settings.tui.status_line = Some(vec![
|
|
"thread-credits".to_string(),
|
|
"estimated-thread-cost".to_string(),
|
|
]);
|
|
chat.local_settings.tui.terminal_title = chat.local_settings.tui.status_line.clone();
|
|
chat.refresh_status_surfaces();
|
|
let request_id = thread_usage_request_id(&mut rx);
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: Some(750_000),
|
|
..thread_usage(
|
|
thread_id,
|
|
if plan_type == PlanType::Ent26 {
|
|
0
|
|
} else {
|
|
5_200_000
|
|
},
|
|
Some(if plan_type == PlanType::Ent26 {
|
|
0
|
|
} else {
|
|
210_000
|
|
}),
|
|
)
|
|
})),
|
|
));
|
|
|
|
assert!(rx.try_recv().is_err(), "billing surfaces share a request");
|
|
let snapshot = serde_json::to_value(chat.statusline_state()).unwrap();
|
|
let expected_cost = if plan_type == PlanType::Ent26 {
|
|
serde_json::json!({"total_cost_usd": 0.75})
|
|
} else {
|
|
serde_json::json!({"total_cost_usd": 0.21, "total_credits": 5.2})
|
|
};
|
|
assert_eq!(snapshot["thread"]["usage"]["cost"], expected_cost);
|
|
if plan_type == PlanType::Ent26 {
|
|
insta::assert_snapshot!(chat.status_line_text().unwrap(), @"~$0.75");
|
|
insta::assert_snapshot!(chat.last_terminal_title.as_ref().unwrap(), @"~$0.75");
|
|
chat.add_status_output(
|
|
/*refreshing_rate_limits*/ false, /*request_id*/ None,
|
|
);
|
|
let status_cell = match rx.try_recv() {
|
|
Ok(AppEvent::InsertHistoryCell(cell)) => cell,
|
|
event => panic!("expected /status card, got {event:?}"),
|
|
};
|
|
let usage_row = status_cell
|
|
.display_lines(/*width*/ 90)
|
|
.into_iter()
|
|
.map(|line| line.to_string())
|
|
.find(|line| line.contains("Thread usage:"))
|
|
.unwrap();
|
|
insta::assert_snapshot!(usage_row, @" Thread usage: ~$0.75");
|
|
continue;
|
|
}
|
|
|
|
assert_eq!(
|
|
chat.status_line_text(),
|
|
Some("5.2 credits · ~$0.21".to_string())
|
|
);
|
|
assert_eq!(
|
|
chat.last_terminal_title,
|
|
Some("5.2 credits | ~$0.21".to_string())
|
|
);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn early_draw_rearms_thread_usage_retry_after_immediate_redraw() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
|
|
let request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected initial thread usage request, got {event:?}"),
|
|
};
|
|
let (frame_requester, mut frame_rx) = FrameRequester::test_channel();
|
|
chat.frame_requester = frame_requester;
|
|
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Err("thread usage temporarily unavailable".to_string()),
|
|
));
|
|
let retry_due_at = chat
|
|
.thread_usage
|
|
.retry_due_at
|
|
.expect("temporary failures schedule a retry");
|
|
let scheduled = std::iter::from_fn(|| frame_rx.try_recv().ok()).collect::<Vec<_>>();
|
|
assert!(scheduled.iter().any(|deadline| *deadline >= retry_due_at));
|
|
assert!(scheduled.iter().any(|deadline| *deadline < retry_due_at));
|
|
|
|
chat.pre_draw_tick();
|
|
|
|
let rearmed = std::iter::from_fn(|| frame_rx.try_recv().ok())
|
|
.find(|deadline| *deadline >= retry_due_at)
|
|
.expect("the immediate redraw must re-arm the pending billing retry");
|
|
assert!(rearmed.duration_since(retry_due_at) < Duration::from_secs(/*secs*/ 1));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn unchanged_thread_usage_has_bounded_settlement_refreshes() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
|
|
let initial_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected initial thread usage request, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
initial_request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
|
|
let (mut owner, _, _, _) = make_chatwidget_manual_with_sender().await;
|
|
owner.park_voice();
|
|
chat.resume_background_voice(&mut owner);
|
|
for attempt in 0..=THREAD_USAGE_SETTLEMENT_DELAYS.len() {
|
|
let request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected settlement attempt {attempt}, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
|
|
if attempt < THREAD_USAGE_SETTLEMENT_DELAYS.len() {
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
chat.thread_usage.settlement_refresh_due_at = Some(Instant::now());
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
}
|
|
}
|
|
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_none());
|
|
assert!(
|
|
chat.thread_usage
|
|
.settlement_baseline_credits_micros
|
|
.is_none()
|
|
);
|
|
assert!(rx.try_recv().is_err(), "settlement polling must stop");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn early_draw_rearms_thread_usage_settlement_after_immediate_redraw() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
|
|
let initial_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected initial thread usage request, got {event:?}"),
|
|
};
|
|
let usage = ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
groups: Vec::new(),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
initial_request_id,
|
|
Ok(ThreadUsageOutcome::Available(usage.clone())),
|
|
));
|
|
|
|
let (frame_requester, mut frame_rx) = FrameRequester::test_channel();
|
|
chat.frame_requester = frame_requester;
|
|
chat.refresh_thread_usage_after_turn();
|
|
let settlement_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected immediate post-turn thread usage request, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
settlement_request_id,
|
|
Ok(ThreadUsageOutcome::Available(usage)),
|
|
));
|
|
let settlement_due_at = chat
|
|
.thread_usage
|
|
.settlement_refresh_due_at
|
|
.expect("unchanged post-turn billing schedules a settlement refresh");
|
|
let scheduled = std::iter::from_fn(|| frame_rx.try_recv().ok()).collect::<Vec<_>>();
|
|
assert!(
|
|
scheduled
|
|
.iter()
|
|
.any(|deadline| *deadline >= settlement_due_at)
|
|
);
|
|
assert!(
|
|
scheduled
|
|
.iter()
|
|
.any(|deadline| *deadline < settlement_due_at)
|
|
);
|
|
|
|
chat.pre_draw_tick();
|
|
|
|
let rearmed = std::iter::from_fn(|| frame_rx.try_recv().ok())
|
|
.find(|deadline| *deadline >= settlement_due_at)
|
|
.expect("the immediate redraw must re-arm the pending billing settlement");
|
|
assert!(rearmed.duration_since(settlement_due_at) < Duration::from_secs(/*secs*/ 1));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn thread_usage_settlement_requires_request_started_after_turn_completion() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.refresh_status_line();
|
|
|
|
let initial_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected initial thread usage request, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
initial_request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 40_000_000,
|
|
estimated_usage_usd_micros: Some(1_600_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
|
|
chat.thread_usage.status_requested = true;
|
|
chat.request_thread_usage();
|
|
let pre_turn_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected pre-turn thread usage request, got {event:?}"),
|
|
};
|
|
chat.refresh_thread_usage_after_turn();
|
|
assert_eq!(chat.thread_usage.settlement_request_id, None);
|
|
assert_eq!(
|
|
chat.thread_usage.settlement_baseline_credits_micros,
|
|
Some(40_000_000)
|
|
);
|
|
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
pre_turn_request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
let post_turn_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected a request started after turn completion, got {event:?}"),
|
|
};
|
|
assert_eq!(
|
|
chat.thread_usage.settlement_baseline_credits_micros,
|
|
Some(46_000_000)
|
|
);
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
post_turn_request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
|
|
chat.thread_usage.settlement_refresh_due_at = Some(Instant::now());
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
let settlement_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected another post-turn settlement request, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
settlement_request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 50_000_000,
|
|
estimated_usage_usd_micros: Some(2_100_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
assert_eq!(chat.thread_usage.settlement_baseline_credits_micros, None);
|
|
assert_eq!(chat.thread_usage.settlement_refresh_due_at, None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn deselecting_billing_surfaces_cancels_polling_and_preserves_status_cache() {
|
|
let (mut chat, _sender, mut rx, _op_rx) = make_chatwidget_manual_with_sender().await;
|
|
let thread_id = ThreadId::new();
|
|
chat.thread_id = Some(thread_id);
|
|
chat.has_codex_backend_auth = true;
|
|
chat.plan_type = Some(PlanType::Business);
|
|
chat.local_settings.tui.status_line = Some(vec!["estimated-thread-cost".to_string()]);
|
|
chat.local_settings.tui.terminal_title = Some(vec!["thread-credits".to_string()]);
|
|
chat.refresh_status_surfaces();
|
|
|
|
let initial_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected initial thread usage request, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
initial_request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
|
|
chat.thread_usage.status_requested = true;
|
|
chat.request_thread_usage();
|
|
assert!(chat.thread_usage.status_requested);
|
|
let status_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected explicit /status refresh, got {event:?}"),
|
|
};
|
|
assert!(chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
status_request_id,
|
|
Err("thread usage temporarily unavailable".to_string()),
|
|
));
|
|
assert!(!chat.thread_usage.status_requested);
|
|
assert!(chat.thread_usage.retry_due_at.is_some());
|
|
|
|
chat.refresh_thread_usage_after_turn();
|
|
let settlement_request_id = match rx.try_recv() {
|
|
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
|
|
event => panic!("expected post-turn thread usage refresh, got {event:?}"),
|
|
};
|
|
assert!(chat.thread_usage.settlement_refresh_due_at.is_some());
|
|
|
|
chat.local_settings.tui.status_line = Some(Vec::new());
|
|
chat.local_settings.tui.terminal_title = Some(Vec::new());
|
|
chat.refresh_status_surfaces();
|
|
|
|
assert!(!chat.thread_usage.status_requested);
|
|
assert!(!chat.thread_usage.requested);
|
|
assert_eq!(chat.thread_usage.pending_request_id, None);
|
|
assert_eq!(chat.thread_usage.retry_due_at, None);
|
|
assert_eq!(chat.thread_usage.settlement_refresh_due_at, None);
|
|
assert_eq!(chat.thread_usage.settlement_baseline_credits_micros, None);
|
|
assert_eq!(
|
|
chat.estimated_thread_usage(),
|
|
Some(&ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 46_000_000,
|
|
estimated_usage_usd_micros: Some(1_820_000),
|
|
groups: Vec::new(),
|
|
})
|
|
);
|
|
assert!(!chat.finish_thread_usage_refresh(
|
|
thread_id,
|
|
settlement_request_id,
|
|
Ok(ThreadUsageOutcome::Available(ThreadUsage {
|
|
native_usage_usd_micros: None,
|
|
thread_id: thread_id.to_string(),
|
|
estimated_usage_credits_micros: 50_000_000,
|
|
estimated_usage_usd_micros: Some(2_100_000),
|
|
groups: Vec::new(),
|
|
})),
|
|
));
|
|
chat.refresh_thread_usage_if_settlement_due();
|
|
assert!(rx.try_recv().is_err(), "deselected billing must not poll");
|
|
}
|