use super::helpers::*; #[test] #[should_panic(expected = "Share price cannot be zero")] fn test_validation_fails_zero_rate() { let mut contract = create_test_contract(); // Start vault start_vault(&mut contract); // Try to validate with zero rate - should panic contract.internal_validate_share_price_update(0); } #[test] #[should_panic(expected = "Accountant is currently paused")] fn test_validation_fails_when_paused() { let mut contract = create_test_contract(); // Start vault start_vault(&mut contract); // Pause accountant contract.state.is_accountant_paused = true; // Try to validate - should panic contract.internal_validate_share_price_update(RATE_PRECISION); } #[test] fn test_validation_pauses_on_too_frequent_update() { // Setup with min interval of 1 hour let mut contract = setup_vault_with_price_validation( ONE_HOUR_NS, // min interval ONE_DAY_NS, // max interval 0, // min deviation (disabled) 0, // max deviation (disabled) ); // Start vault let start_time = current_timestamp(); start_vault_at(&mut contract, start_time); // Set previous update time set_previous_update_time(&mut contract, start_time); set_share_price(&mut contract, RATE_PRECISION); // Fast forward only 30 minutes (less than min interval) fast_forward_time(ONE_HOUR_NS / 2); // Try to validate - should pause but not panic contract.internal_validate_share_price_update(RATE_PRECISION); // Verify accountant was paused assert!( contract.state.is_accountant_paused, "Accountant should be paused for too frequent update" ); } #[test] fn test_validation_pauses_on_too_delayed_update() { // Setup with max interval of 1 day let mut contract = setup_vault_with_price_validation( ONE_HOUR_NS, // min interval ONE_DAY_NS, // max interval 0, // min deviation (disabled) 0, // max deviation (disabled) ); // Start vault let start_time = current_timestamp(); start_vault_at(&mut contract, start_time); // Set previous update time set_previous_update_time(&mut contract, start_time); set_share_price(&mut contract, RATE_PRECISION); // Fast forward 2 days (more than max interval) fast_forward_time(ONE_DAY_NS * 2); // Try to validate - should pause but not panic contract.internal_validate_share_price_update(RATE_PRECISION); // Verify accountant was paused assert!( contract.state.is_accountant_paused, "Accountant should be paused for delayed update" ); } #[test] fn test_validation_pauses_on_price_too_small() { // Setup with min deviation of 0.1% (100,000,000 nanopercent) let mut contract = setup_vault_with_price_validation( 0, // min interval (disabled) 0, // max interval (disabled) 100_000_000, // min deviation: 0.1% 0, // max deviation (disabled) ); // Start vault let start_time = current_timestamp(); start_vault_at(&mut contract, start_time); // Set previous rate set_previous_update_time(&mut contract, start_time); set_share_price(&mut contract, RATE_PRECISION); // Fast forward time fast_forward_time(ONE_HOUR_NS); // New rate only 0.05% different (below min deviation) let new_rate = RATE_PRECISION * 10005 / 10000; // 1.0005 // Try to validate - should pause but not panic contract.internal_validate_share_price_update(new_rate); // Verify accountant was paused assert!( contract.state.is_accountant_paused, "Accountant should be paused for price change too small" ); } #[test] fn test_validation_pauses_on_price_too_large() { // Setup with max deviation of 10% (10,000,000,000 nanopercent) let mut contract = setup_vault_with_price_validation( 0, // min interval (disabled) 0, // max interval (disabled) 0, // min deviation (disabled) 10_000_000_000, // max deviation: 10% ); // Start vault let start_time = current_timestamp(); start_vault_at(&mut contract, start_time); // Set previous rate set_previous_update_time(&mut contract, start_time); set_share_price(&mut contract, RATE_PRECISION); // Fast forward time fast_forward_time(ONE_HOUR_NS); // New rate is 15% different (above max deviation) let new_rate = RATE_PRECISION * 115 / 100; // 1.15 // Try to validate - should pause but not panic contract.internal_validate_share_price_update(new_rate); // Verify accountant was paused assert!( contract.state.is_accountant_paused, "Accountant should be paused for price change too large" ); } #[test] fn test_validation_with_large_constraints_passes() { // Create fresh contract with very permissive constraints let mut contract = setup_vault_with_price_validation( 0, // min interval (disabled) u128::MAX, // max interval (essentially disabled) 0, // min deviation (disabled) 100_000_000_000, // max deviation: 100% (very permissive) ); // Start vault let start_time = current_timestamp(); start_vault_at(&mut contract, start_time); // Set previous update time and rate set_previous_update_time(&mut contract, start_time); set_share_price(&mut contract, RATE_PRECISION); // Fast forward a reasonable time fast_forward_time(ONE_HOUR_NS); // Set a rate within the 100% deviation limit (50% change) let new_rate = RATE_PRECISION * 15 / 10; // 1.5x (50% change) // Try to validate - should NOT pause with permissive constraints contract.internal_validate_share_price_update(new_rate); // Verify accountant was NOT paused assert!( !contract.state.is_accountant_paused, "Accountant should not pause with permissive constraints" ); }