#[cfg(test)]
mod tests {
use super::*;
use bevy::prelude::*;
use avian2d::prelude::*;
use crate::components::*;
use crate::constants::*;
fn setup_test_world() -> (World, PaddleEntities, GameRng) {
let mut world = World::new();
// Create paddle entities
let left_paddle = world.spawn((
LeftPaddle,
LinearVelocity(Vec2::ZERO),
Transform::from_xyz(-WINDOW_WIDTH / 2.0 + 50.0, 0.0, 0.0),
)).id();
let right_paddle = world.spawn((
RightPaddle,
LinearVelocity(Vec2::ZERO),
Transform::from_xyz(WINDOW_WIDTH / 2.0 - 50.0, 0.0, 0.0),
)).id();
let paddle_entities = PaddleEntities { left: left_paddle, right: right_paddle };
let rng = GameRng::default();
(world, paddle_entities, rng)
}
#[test]
fn test_ball_bounces_right_after_hitting_left_paddle() {
let (mut world, paddle_entities, mut rng) = setup_test_world();
// Create ball moving left
let mut ball_velocity = LinearVelocity(Vec2::new(-200.0, 50.0));
// Create collision with left paddle
let mut colliding_entities = CollidingEntities::default();
colliding_entities.insert(paddle_entities.left);
// Set up paddle query
let paddle_query = world.query::<&LinearVelocity>();
// Apply collision effects
apply_paddle_collision_effects(
&mut ball_velocity,
&colliding_entities,
&paddle_query,
&paddle_entities,
&mut rng,
);
// Ball should now be moving right (positive X)
assert!(ball_velocity.0.x > 0.0,
"Ball should bounce right after hitting left paddle, but X velocity is: {}",
ball_velocity.0.x);
assert!(ball_velocity.0.x >= BALL_MIN_X_SPEED_RATIO * BALL_SPEED,
"Ball should maintain minimum X speed");
}
#[test]
fn test_ball_bounces_left_after_hitting_right_paddle() {
let (mut world, paddle_entities, mut rng) = setup_test_world();
// Create ball moving right
let mut ball_velocity = LinearVelocity(Vec2::new(200.0, -30.0));
// Create collision with right paddle
let mut colliding_entities = CollidingEntities::default();
colliding_entities.insert(paddle_entities.right);
let paddle_query = world.query::<&LinearVelocity>();
apply_paddle_collision_effects(
&mut ball_velocity,
&colliding_entities,
&paddle_query,
&paddle_entities,
&mut rng,
);
// Ball should now be moving left (negative X)
assert!(ball_velocity.0.x < 0.0,
"Ball should bounce left after hitting right paddle, but X velocity is: {}",
ball_velocity.0.x);
}
#[test]
fn test_paddle_velocity_influences_ball() {
let (mut world, paddle_entities, mut rng) = setup_test_world();
// Set left paddle moving upward
world.entity_mut(paddle_entities.left)
.get_mut::<LinearVelocity>()
.unwrap()
.0 = Vec2::new(0.0, 100.0);
let mut ball_velocity = LinearVelocity(Vec2::new(-200.0, 0.0));
let mut colliding_entities = CollidingEntities::default();
colliding_entities.insert(paddle_entities.left);
let original_y = ball_velocity.0.y;
let paddle_query = world.query::<&LinearVelocity>();
apply_paddle_collision_effects(
&mut ball_velocity,
&colliding_entities,
&paddle_query,
&paddle_entities,
&mut rng,
);
// Ball's Y velocity should be influenced by paddle movement
assert!(ball_velocity.0.y > original_y,
"Ball Y velocity should be influenced by upward paddle movement");
}
#[test]
fn test_scoring_detection() {
let mut game_state = GameState { left_score: 0, right_score: 0 };
let mut ball_velocity = LinearVelocity(Vec2::new(-200.0, 0.0));
let mut ball_transform = Transform::from_xyz(-WINDOW_WIDTH / 2.0 - BALL_RESET_MARGIN - 10.0, 0.0, 0.0);
let mut rng = GameRng::default();
let scored = check_and_handle_scoring(
&mut ball_velocity,
&mut ball_transform,
&mut game_state,
&mut rng,
);
assert!(scored, "Should detect scoring when ball is past left boundary");
assert_eq!(game_state.right_score, 1, "Right player should score");
assert_eq!(ball_transform.translation, Vec3::ZERO, "Ball should reset to center");
}
#[test]
fn test_paddle_boundary_clamping() {
let mut transform = Transform::from_xyz(0.0, WINDOW_HEIGHT, 0.0); // Way above boundary
let mut velocity = LinearVelocity(Vec2::new(0.0, PADDLE_SPEED));
let keyboard_input = ButtonInput::<KeyCode>::default();
let time = Time::default();
update_paddle_velocity(&mut velocity, &mut transform, &keyboard_input, &time, true);
let max_y = WINDOW_HEIGHT / 2.0 - PADDLE_HEIGHT / 2.0 - WALL_HEIGHT;
assert!(transform.translation.y <= max_y,
"Paddle should be clamped within boundaries, but Y is: {}",
transform.translation.y);
}
// Keep the constant tests as they are useful
#[test]
fn test_minimum_x_velocity_constants() {
assert!(BALL_MIN_X_SPEED_RATIO > 0.0);
assert!(BALL_MIN_X_SPEED_RATIO <= 1.0);
assert!(BALL_SPEED > 0.0);
}
#[test]
fn test_collision_cooldown_constants() {
assert!(COLLISION_COOLDOWN_MS > 0.0);
assert!(COLLISION_COOLDOWN_MS >= 100.0);
assert!(COLLISION_COOLDOWN_MS <= 500.0);
}
}