//! Test helper utilities for broker integration tests. //! //! Provides configurable test fixtures and utilities that respect environment //! variables while allowing tests to specify their own configurations. use data::brokers::interactive_brokers::IBConfig; /// Creates an IBConfig with test-friendly defaults that can be overridden by environment. /// /// This helper ensures tests work regardless of environment variable settings /// by providing sensible defaults for testing while still respecting env vars /// when explicitly set. /// /// # Default Test Values /// - Host: "127.0.0.1" (or IB_GATEWAY_HOST env var) /// /// - Port: 7497 (paper trading, or IB_GATEWAY_PORT env var) /// - Client ID: 1 (or IB_CLIENT_ID env var) /// /// - Account ID: "DU123456" (or IB_ACCOUNT_ID env var) pub fn test_ib_config() -> IBConfig { IBConfig::default() } /// Creates an IBConfig for paper trading with explicit values. /// /// This bypasses environment variables and uses fixed test values. pub fn test_ib_config_paper() -> IBConfig { IBConfig { host: std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()), port: std::env::var("IB_GATEWAY_PORT") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(7497), client_id: std::env::var("IB_CLIENT_ID") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(1), account_id: std::env::var("IB_ACCOUNT_ID").unwrap_or_else(|_| "DU123456".to_string()), connection_timeout: 30, heartbeat_interval: 30, max_reconnect_attempts: 5, request_timeout: 10, } } /// Creates an IBConfig for live trading with explicit values. /// /// This bypasses environment variables and uses fixed test values. pub fn test_ib_config_live() -> IBConfig { IBConfig { host: std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()), port: 7496, // Live trading port (hardcoded for test) client_id: std::env::var("IB_CLIENT_ID") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(1), account_id: "U123456".to_string(), // Live account format connection_timeout: 30, heartbeat_interval: 30, max_reconnect_attempts: 5, request_timeout: 10, } } /// Creates an IBConfig for IB Gateway with explicit values. pub fn test_ib_config_gateway() -> IBConfig { IBConfig { host: std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()), port: 4001, // IB Gateway port (hardcoded for test) client_id: std::env::var("IB_CLIENT_ID") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(1), account_id: std::env::var("IB_ACCOUNT_ID").unwrap_or_else(|_| "DU123456".to_string()), connection_timeout: 30, heartbeat_interval: 30, max_reconnect_attempts: 5, request_timeout: 10, } } /// Gets the expected host from environment or default. pub fn expected_host() -> String { std::env::var("IB_GATEWAY_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()) } /// Gets the expected port from environment or default (paper trading). pub fn expected_port() -> u16 { std::env::var("IB_GATEWAY_PORT") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(7497) } /// Gets the expected client ID from environment or default. pub fn expected_client_id() -> i32 { std::env::var("IB_CLIENT_ID") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(1) } /// Gets the expected account ID from environment or default. pub fn expected_account_id() -> String { std::env::var("IB_ACCOUNT_ID").unwrap_or_else(|_| "DU123456".to_string()) } #[cfg(test)] mod tests { use super::*; #[test] fn test_helper_configs_are_valid() { let paper = test_ib_config_paper(); assert!(!paper.host.is_empty()); assert!(paper.port > 0); let live = test_ib_config_live(); assert_eq!(live.port, 7496); assert!(live.account_id.starts_with("U")); let gateway = test_ib_config_gateway(); assert_eq!(gateway.port, 4001); } #[test] fn test_expected_values() { let host = expected_host(); assert!(!host.is_empty()); let port = expected_port(); assert!(port > 0); let client_id = expected_client_id(); assert!(client_id >= 0 && client_id <= 32767); } }