|
| 1 | +// Tests for the concrete BundleSimulator implementation |
| 2 | +mod common; |
| 3 | + |
| 4 | +use common::builders::*; |
| 5 | +use common::fixtures::*; |
| 6 | +use common::mocks::*; |
| 7 | +use tips_simulator::{core::BundleSimulator, core::BundleSimulatorImpl, SimulationError}; |
| 8 | +use uuid::Uuid; |
| 9 | + |
| 10 | +#[tokio::test] |
| 11 | +async fn test_bundle_simulator_impl_successful_flow() { |
| 12 | + // Setup - exercise the concrete BundleSimulator implementation |
| 13 | + let bundle_id = Uuid::new_v4(); |
| 14 | + let expected_result = SimulationResultBuilder::successful() |
| 15 | + .with_ids(Uuid::new_v4(), bundle_id) |
| 16 | + .with_gas_used(250_000) |
| 17 | + .with_execution_time_us(2500) |
| 18 | + .build(); |
| 19 | + |
| 20 | + let engine = MockSimulationEngine::new().with_result(expected_result.clone()); |
| 21 | + let publisher = MockSimulationPublisher::new(); |
| 22 | + let simulator = BundleSimulatorImpl::new(engine.clone(), publisher.clone()); |
| 23 | + |
| 24 | + let request = SimulationRequestBuilder::new() |
| 25 | + .with_bundle_id(bundle_id) |
| 26 | + .with_bundle(bundles::single_tx_bundle()) |
| 27 | + .build(); |
| 28 | + |
| 29 | + // Act - test the actual BundleSimulator trait implementation |
| 30 | + let result = simulator.simulate(&request).await; |
| 31 | + |
| 32 | + // Assert |
| 33 | + assert!(result.is_ok()); |
| 34 | + assert_eq!(engine.simulation_count(), 1); |
| 35 | + assert_eq!(publisher.published_count(), 1); |
| 36 | + |
| 37 | + let published = publisher.get_published(); |
| 38 | + assert_eq!(published[0].bundle_id, bundle_id); |
| 39 | + assert_eq!(published[0].gas_used, Some(250_000)); |
| 40 | + assert!(published[0].success); |
| 41 | +} |
| 42 | + |
| 43 | +#[tokio::test] |
| 44 | +async fn test_bundle_simulator_impl_engine_failure() { |
| 45 | + // Test that the concrete BundleSimulator handles engine failures |
| 46 | + let bundle_id = Uuid::new_v4(); |
| 47 | + let engine = MockSimulationEngine::new().fail_next_with(SimulationError::OutOfGas); |
| 48 | + let publisher = MockSimulationPublisher::new(); |
| 49 | + let simulator = BundleSimulatorImpl::new(engine.clone(), publisher.clone()); |
| 50 | + |
| 51 | + let request = SimulationRequestBuilder::new() |
| 52 | + .with_bundle_id(bundle_id) |
| 53 | + .build(); |
| 54 | + |
| 55 | + // Act |
| 56 | + let result = simulator.simulate(&request).await; |
| 57 | + |
| 58 | + // Assert - simulate() should succeed even if the engine simulation fails |
| 59 | + assert!(result.is_ok()); |
| 60 | + assert_eq!(engine.simulation_count(), 1); |
| 61 | + assert_eq!(publisher.published_count(), 1); |
| 62 | + |
| 63 | + let published = publisher.get_published(); |
| 64 | + assert!(!published[0].success); |
| 65 | + assert!(published[0].error_reason.is_some()); |
| 66 | +} |
| 67 | + |
| 68 | +#[tokio::test] |
| 69 | +async fn test_bundle_simulator_impl_publisher_failure() { |
| 70 | + // Test that the concrete BundleSimulator handles publisher failures gracefully |
| 71 | + let engine = MockSimulationEngine::new(); |
| 72 | + let publisher = MockSimulationPublisher::new().fail_next(); |
| 73 | + let simulator = BundleSimulatorImpl::new(engine.clone(), publisher.clone()); |
| 74 | + |
| 75 | + let request = SimulationRequestBuilder::new().build(); |
| 76 | + |
| 77 | + // Act - should complete without error even if publisher fails |
| 78 | + let result = simulator.simulate(&request).await; |
| 79 | + |
| 80 | + // Assert |
| 81 | + assert!(result.is_ok()); |
| 82 | + assert_eq!(engine.simulation_count(), 1); |
| 83 | + assert_eq!(publisher.published_count(), 0); // Publisher failed |
| 84 | +} |
| 85 | + |
| 86 | +#[tokio::test] |
| 87 | +async fn test_bundle_simulator_impl_multiple_simulations() { |
| 88 | + // Test the concrete BundleSimulator with multiple sequential simulations |
| 89 | + let engine = MockSimulationEngine::new(); |
| 90 | + let publisher = MockSimulationPublisher::new(); |
| 91 | + let simulator = BundleSimulatorImpl::new(engine.clone(), publisher.clone()); |
| 92 | + |
| 93 | + // Run multiple simulations with different bundle IDs |
| 94 | + for i in 0..3 { |
| 95 | + let bundle_id = Uuid::new_v4(); |
| 96 | + let result = SimulationResultBuilder::successful() |
| 97 | + .with_ids(Uuid::new_v4(), bundle_id) |
| 98 | + .with_gas_used(100_000 + i * 50_000) |
| 99 | + .build(); |
| 100 | + |
| 101 | + let _ = engine.clone().with_result(result); |
| 102 | + |
| 103 | + let request = SimulationRequestBuilder::new() |
| 104 | + .with_bundle_id(bundle_id) |
| 105 | + .with_bundle( |
| 106 | + TestBundleBuilder::new() |
| 107 | + .with_simple_transaction(&[i as u8, 0x01, 0x02]) |
| 108 | + .build(), |
| 109 | + ) |
| 110 | + .build(); |
| 111 | + |
| 112 | + let sim_result = simulator.simulate(&request).await; |
| 113 | + assert!(sim_result.is_ok()); |
| 114 | + } |
| 115 | + |
| 116 | + // Verify all simulations were processed |
| 117 | + assert_eq!(engine.simulation_count(), 3); |
| 118 | + assert_eq!(publisher.published_count(), 3); |
| 119 | + |
| 120 | + let published = publisher.get_published(); |
| 121 | + assert_eq!(published.len(), 3); |
| 122 | + for (i, result) in published.iter().enumerate() { |
| 123 | + assert!(result.success); |
| 124 | + assert_eq!(result.gas_used, Some(100_000 + i as u64 * 50_000)); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[tokio::test] |
| 129 | +async fn test_bundle_simulator_impl_various_error_types() { |
| 130 | + // Test the concrete BundleSimulator with different types of simulation errors |
| 131 | + let errors = vec![ |
| 132 | + SimulationError::Revert { |
| 133 | + reason: "Contract reverted".to_string(), |
| 134 | + }, |
| 135 | + SimulationError::InvalidNonce { |
| 136 | + tx_index: 1, |
| 137 | + expected: 10, |
| 138 | + actual: 5, |
| 139 | + }, |
| 140 | + SimulationError::InsufficientBalance { |
| 141 | + tx_index: 0, |
| 142 | + required: alloy_primitives::U256::from(1000000), |
| 143 | + available: alloy_primitives::U256::from(500000), |
| 144 | + }, |
| 145 | + SimulationError::StateAccessError { |
| 146 | + message: "RPC timeout".to_string(), |
| 147 | + }, |
| 148 | + SimulationError::Timeout, |
| 149 | + ]; |
| 150 | + |
| 151 | + for (_i, error) in errors.into_iter().enumerate() { |
| 152 | + let engine = MockSimulationEngine::new().fail_next_with(error.clone()); |
| 153 | + let publisher = MockSimulationPublisher::new(); |
| 154 | + let simulator = BundleSimulatorImpl::new(engine.clone(), publisher.clone()); |
| 155 | + |
| 156 | + let request = SimulationRequestBuilder::new() |
| 157 | + .with_bundle_id(Uuid::new_v4()) |
| 158 | + .build(); |
| 159 | + |
| 160 | + let result = simulator.simulate(&request).await; |
| 161 | + assert!(result.is_ok()); |
| 162 | + |
| 163 | + let published = publisher.get_published(); |
| 164 | + assert_eq!(published.len(), 1); |
| 165 | + assert!(!published[0].success); |
| 166 | + |
| 167 | + let error_msg = published[0].error_reason.as_ref().unwrap(); |
| 168 | + match error { |
| 169 | + SimulationError::Revert { .. } => assert!(error_msg.contains("reverted")), |
| 170 | + SimulationError::InvalidNonce { .. } => assert!(error_msg.contains("nonce")), |
| 171 | + SimulationError::InsufficientBalance { .. } => assert!(error_msg.contains("balance")), |
| 172 | + SimulationError::StateAccessError { .. } => assert!(error_msg.contains("State access")), |
| 173 | + SimulationError::Timeout => assert!(error_msg.contains("timed out")), |
| 174 | + _ => {} |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
0 commit comments