#[cfg(test)]
mod tests {
use super::;
use bevy::prelude::;
use avian2d::prelude::;
use crate::components::;
use crate::constants::;
use crate::systems::;
#[test]
fn test_ball_direction_logic_left_paddle() {
// Test that ball moving left should bounce right after hitting left paddle
let mut ball_velocity = LinearVelocity(Vec2::new(-200.0, 50.0)); // Moving left
let colliding_entities = CollidingEntities::default();
// Simulate what should happen: ball moving left hits left paddle, should bounce right
let original_direction = ball_velocity.0.x.signum(); // -1.0 (left)
assert_eq!(original_direction, -1.0, "Ball should be moving left initially");
// After collision, X should be positive (right)
let expected_direction = 1.0; // Should bounce right
assert_ne!(original_direction, expected_direction, "Direction should change after collision");
}
#[test]
fn test_ball_direction_logic_right_paddle() {
// Test that ball moving right should bounce left after hitting right paddle
let mut ball_velocity = LinearVelocity(Vec2::new(200.0, -30.0)); // Moving right
let original_direction = ball_velocity.0.x.signum(); // 1.0 (right)
assert_eq!(original_direction, 1.0, "Ball should be moving right initially");
// After collision, X should be negative (left)
let expected_direction = -1.0; // Should bounce left
assert_ne!(original_direction, expected_direction, "Direction should change after collision");
}
#[test]
fn test_minimum_x_velocity_constants() {
// Test that our constants make sense
assert!(BALL_MIN_ABSOLUTE_X_SPEED > 0.0, "Minimum X speed should be positive");
assert!(BALL_MIN_ABSOLUTE_X_SPEED >= 100.0, "Minimum X speed should be reasonable (>=100)");
assert!(BALL_SPEED > BALL_MIN_ABSOLUTE_X_SPEED, "Ball speed should be greater than minimum X speed");
}
#[test]
fn test_collision_cooldown_constants() {
// Test collision cooldown makes sense
assert!(COLLISION_COOLDOWN_MS > 0.0, "Collision cooldown should be positive");
assert!(COLLISION_COOLDOWN_MS >= 100.0, "Collision cooldown should be at least 100ms");
assert!(COLLISION_COOLDOWN_MS <= 500.0, "Collision cooldown shouldn't be too long");
}
#[test]
fn test_wall_boundary_constants() {
// Test wall boundary constants
assert!(WALL_BOUNDARY_MARGIN > 0.0, "Wall boundary margin should be positive");
assert!(WALL_BOUNDARY_MARGIN < 50.0, "Wall boundary margin should be reasonable");
}
#[test]
fn test_ball_velocity_enforcement() {
// Test velocity enforcement logic manually
let mut velocity = Vec2::new(10.0, 200.0); // Very low X, high Y
// Simulate what monitor_ball_state should do
if velocity.x.abs() < BALL_MIN_ABSOLUTE_X_SPEED {
let direction = if velocity.x >= 0.0 { 1.0 } else { -1.0 };
velocity.x = direction * BALL_MIN_ABSOLUTE_X_SPEED;
}
assert!(velocity.x.abs() >= BALL_MIN_ABSOLUTE_X_SPEED,
"X velocity should be enforced to minimum: {} >= {}",
velocity.x.abs(), BALL_MIN_ABSOLUTE_X_SPEED);
}
#[test]
fn test_direction_correction_logic() {
// Test the core direction logic that was causing issues
// Scenario 1: Ball moving left (-200), hits left paddle, should bounce right (+)
let ball_moving_left = -200.0;
let correct_bounce_direction = if ball_moving_left < 0.0 { 1.0 } else { -1.0 };
assert_eq!(correct_bounce_direction, 1.0, "Ball moving left should bounce right");
// Scenario 2: Ball moving right (+200), hits right paddle, should bounce left (-)
let ball_moving_right = 200.0;
let correct_bounce_direction = if ball_moving_right < 0.0 { 1.0 } else { -1.0 };
assert_eq!(correct_bounce_direction, -1.0, "Ball moving right should bounce left");
}
#[test]
fn test_prevent_self_goal_logic() {
// Test logic to prevent self goals
// Ball near left side moving left should bounce right after paddle hit
let ball_x_velocity = -100.0; // Moving left towards left goal
// After hitting left paddle, it must move right to prevent self goal
let after_collision_direction = 1.0; // Must be positive (right)
let corrected_velocity = after_collision_direction * BALL_MIN_ABSOLUTE_X_SPEED;
assert!(corrected_velocity > 0.0, "Ball must move away from left goal after hitting left paddle");
assert!(corrected_velocity >= BALL_MIN_ABSOLUTE_X_SPEED, "Ball must have minimum speed");
}
#[test]
fn test_anti_stuck_ratio_logic() {
// Test the ratio logic that prevents vertical bouncing
let velocity = Vec2::new(5.0, 250.0); // Very low X, high Y (stuck scenario)
let x_to_y_ratio = velocity.x.abs() / (velocity.y.abs() + 0.01);
assert!(x_to_y_ratio < 0.3, "This velocity should trigger anti-stuck logic");
// Simulate what should happen
let boosted_x = velocity.x.signum() * BALL_MIN_ABSOLUTE_X_SPEED * 1.2;
let reduced_y = velocity.y * 0.7;
let new_ratio = boosted_x.abs() / (reduced_y.abs() + 0.01);
assert!(new_ratio > x_to_y_ratio, "New ratio should be better than original");
}
}
Are these test testing actually testing their purpose