|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use ai::LLMProvider; |
| 4 | +use warpui::App; |
| 5 | + |
| 6 | +use super::*; |
| 7 | +use crate::ai::credit_availability::AICreditSource; |
| 8 | +use crate::server::server_api::ServerApiProvider; |
| 9 | +use crate::server::server_api::team::MockTeamClient; |
| 10 | +use crate::server::server_api::workspace::MockWorkspaceClient; |
| 11 | +use crate::server::telemetry::context_provider::AppTelemetryContextProvider; |
| 12 | +use crate::workspaces::workspace::{ByoApiKeyPolicy, Workspace, WorkspaceUid}; |
| 13 | + |
| 14 | +fn initialize_app(app: &mut App) { |
| 15 | + initialize_app_with_workspaces(app, vec![]); |
| 16 | +} |
| 17 | + |
| 18 | +fn initialize_app_with_workspaces(app: &mut App, workspaces: Vec<Workspace>) { |
| 19 | + app.add_singleton_model(|_| NetworkStatus::new()); |
| 20 | + app.add_singleton_model(|_| AuthStateProvider::new_for_test()); |
| 21 | + app.add_singleton_model(|_| ServerApiProvider::new_for_test()); |
| 22 | + app.add_singleton_model(AppTelemetryContextProvider::new_context_provider); |
| 23 | + app.add_singleton_model(|ctx| { |
| 24 | + UserWorkspaces::mock( |
| 25 | + Arc::new(MockTeamClient::new()), |
| 26 | + Arc::new(MockWorkspaceClient::new()), |
| 27 | + workspaces, |
| 28 | + ctx, |
| 29 | + ) |
| 30 | + }); |
| 31 | + if app |
| 32 | + .models_of_type::<settings::PrivatePreferences>() |
| 33 | + .is_empty() |
| 34 | + { |
| 35 | + app.update(crate::settings::init_and_register_user_preferences); |
| 36 | + } |
| 37 | + app.update(|ctx| { |
| 38 | + warpui_extras::secure_storage::register_noop("test", ctx); |
| 39 | + ctx.add_singleton_model(ApiKeyManager::new); |
| 40 | + }); |
| 41 | + app.add_singleton_model(|_| crate::pricing::PricingInfoModel::new()); |
| 42 | + app.add_singleton_model(|ctx| { |
| 43 | + AIRequestUsageModel::new_for_test(ServerApiProvider::as_ref(ctx).get_ai_client(), ctx) |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +fn apply_server_availability(app: &mut App, availability: AICreditAvailability) { |
| 48 | + AIRequestUsageModel::handle(app).update(app, |model, ctx| { |
| 49 | + model.apply_server_availability(Ok(availability), ctx); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +fn determine_state(app: &mut App) -> PromptAlertState { |
| 54 | + app.read(PromptAlertView::determine_state) |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn test_server_available_maps_to_no_alert() { |
| 59 | + App::test((), |mut app| async move { |
| 60 | + initialize_app(&mut app); |
| 61 | + apply_server_availability( |
| 62 | + &mut app, |
| 63 | + AICreditAvailability::available_with_source(Some(AICreditSource::BaseLimit)), |
| 64 | + ); |
| 65 | + assert_eq!(determine_state(&mut app), PromptAlertState::NoAlert); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +fn test_server_delinquent_maps_to_delinquency_alert() { |
| 71 | + App::test((), |mut app| async move { |
| 72 | + initialize_app(&mut app); |
| 73 | + apply_server_availability( |
| 74 | + &mut app, |
| 75 | + AICreditAvailability::unavailable(AICreditDenialReason::Delinquent), |
| 76 | + ); |
| 77 | + assert_eq!( |
| 78 | + determine_state(&mut app), |
| 79 | + PromptAlertState::DelinquentDueToPaymentIssue |
| 80 | + ); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +#[test] |
| 85 | +fn test_server_spend_limit_reasons_map_to_spend_limit_alert() { |
| 86 | + App::test((), |mut app| async move { |
| 87 | + initialize_app(&mut app); |
| 88 | + for reason in [ |
| 89 | + AICreditDenialReason::EnterpriseTeamSpendLimitHit, |
| 90 | + AICreditDenialReason::EnterprisePerUserSpendLimitHit, |
| 91 | + AICreditDenialReason::EnterpriseWorkspaceSpendLimitHit, |
| 92 | + ] { |
| 93 | + apply_server_availability(&mut app, AICreditAvailability::unavailable(reason)); |
| 94 | + assert_eq!( |
| 95 | + determine_state(&mut app), |
| 96 | + PromptAlertState::MonthlyOveragesSpendLimitReached, |
| 97 | + "unexpected alert state for {reason:?}", |
| 98 | + ); |
| 99 | + } |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn test_server_out_of_credits_maps_to_request_limit_reached() { |
| 105 | + App::test((), |mut app| async move { |
| 106 | + initialize_app(&mut app); |
| 107 | + // With no workspace overage policy in play, an out-of-credits denial |
| 108 | + // falls through to the generic request limit alert. |
| 109 | + for reason in [ |
| 110 | + AICreditDenialReason::OutOfCredits, |
| 111 | + AICreditDenialReason::Unknown, |
| 112 | + ] { |
| 113 | + apply_server_availability(&mut app, AICreditAvailability::unavailable(reason)); |
| 114 | + assert_eq!( |
| 115 | + determine_state(&mut app), |
| 116 | + PromptAlertState::RequestLimitReached, |
| 117 | + "unexpected alert state for {reason:?}", |
| 118 | + ); |
| 119 | + } |
| 120 | + }); |
| 121 | +} |
| 122 | + |
| 123 | +#[test] |
| 124 | +fn test_legacy_fallback_used_before_first_server_response() { |
| 125 | + App::test((), |mut app| async move { |
| 126 | + initialize_app(&mut app); |
| 127 | + // No server availability applied: the default request limit info has |
| 128 | + // requests remaining, so the legacy derivation reports no alert. |
| 129 | + assert_eq!(determine_state(&mut app), PromptAlertState::NoAlert); |
| 130 | + }); |
| 131 | +} |
| 132 | + |
| 133 | +#[test] |
| 134 | +fn test_server_managed_availability_maps_to_no_alert() { |
| 135 | + App::test((), |mut app| async move { |
| 136 | + initialize_app(&mut app); |
| 137 | + // `available` with no credit source means a server-managed BYO path |
| 138 | + // is configured — definite availability, no local key required. |
| 139 | + apply_server_availability(&mut app, AICreditAvailability::available_with_source(None)); |
| 140 | + assert_eq!(determine_state(&mut app), PromptAlertState::NoAlert); |
| 141 | + }); |
| 142 | +} |
| 143 | + |
| 144 | +#[test] |
| 145 | +fn test_out_of_credits_with_local_key_maps_to_no_alert() { |
| 146 | + App::test((), |mut app| async move { |
| 147 | + let uid = WorkspaceUid::from(crate::server::ids::ServerId::from(1_i64)); |
| 148 | + let mut workspace = Workspace::from_local_cache(uid, "Test Workspace".to_string(), None); |
| 149 | + workspace.billing_metadata.tier.byo_api_key_policy = |
| 150 | + Some(ByoApiKeyPolicy { enabled: true }); |
| 151 | + initialize_app_with_workspaces(&mut app, vec![workspace]); |
| 152 | + |
| 153 | + ApiKeyManager::handle(&app).update(&mut app, |manager, ctx| { |
| 154 | + manager.set_provider_key(LLMProvider::OpenAI, Some("test-key".to_string()), ctx); |
| 155 | + }); |
| 156 | + |
| 157 | + // The server cannot see the locally stored key; the client refines |
| 158 | + // its OUT_OF_CREDITS answer. |
| 159 | + apply_server_availability( |
| 160 | + &mut app, |
| 161 | + AICreditAvailability::unavailable(AICreditDenialReason::OutOfCredits), |
| 162 | + ); |
| 163 | + assert_eq!(determine_state(&mut app), PromptAlertState::NoAlert); |
| 164 | + }); |
| 165 | +} |
0 commit comments