Skip to content

Commit

Permalink
Fix flaky attack mission test
Browse files Browse the repository at this point in the history
  • Loading branch information
lanedirt committed Jan 1, 2025
1 parent 6e3efae commit 3b51a2f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
5 changes: 4 additions & 1 deletion tests/AccountTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ protected function setUp(): void
// Set default test time to 2024-01-01 00:00:00 to ensure all tests have the same starting point.
$this->travelTo(Carbon::create(2024, 1, 1, 0, 0, 0));

// Set default server settings for all tests.
$settingsService = resolve(SettingsService::class);
$settingsService->set('economy_speed', 8);

// Set amount of planets to be created for the user because planet switching
// is a part of the test suite.
$settingsService = resolve(SettingsService::class);
$settingsService->set('registration_planet_amount', $this->userPlanetAmount);

// Create a new user and login so we can access ingame features.
Expand Down
23 changes: 18 additions & 5 deletions tests/Feature/FleetDispatch/FleetDispatchAttackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ public function testDispatchFleetRecallMissionTwiceError(): void
*/
public function testDispatchFleetCombatUnitsLostAndResourceGained(): void
{
// Disable all resource generation in server settings to ensure we're not affected by it
// when comparing resources before and after battle.
$settingsService = resolve(SettingsService::class);
$settingsService->set('economy_speed', 0);

// Send fleet to a nearby foreign planet.
// Attack with 200 light fighters, defend with 100 rocket launchers.
// We expect attacker to win in +/- 4 rounds, while losing 10-50 light fighters.
Expand All @@ -437,12 +442,12 @@ public function testDispatchFleetCombatUnitsLostAndResourceGained(): void
// Give the foreign planet some units to defend itself.
$foreignPlanet->addUnit('rocket_launcher', 100);

// Increase time by 24 hours to ensure the mission is done and fleets have returned.
$this->travel(24)->hours();

// Reload application to make sure the planet is not cached.
$this->reloadApplication();

// Increase time by 2 hours to ensure the mission is done.
$this->travel(2)->hours();

// Get amount of resources of the foreign planet before the battle.
$attackerResourcesBefore = $this->planetService->getResources();
$foreignPlanetResourcesBefore = $foreignPlanet->getResources();
Expand All @@ -451,9 +456,17 @@ public function testDispatchFleetCombatUnitsLostAndResourceGained(): void
$response = $this->get('/overview');
$response->assertStatus(200);

// Assert that attacker has less than 200 light fighters after battle.
// Load the combat report from the database and assert that it exists for the target planet.
$battleReport = BattleReport::orderBy('id', 'desc')->first();

$this->assertEquals($foreignPlanet->getPlanetCoordinates()->galaxy, $battleReport->planet_galaxy, 'Battle report planet galaxy does not match.');
$this->assertEquals($foreignPlanet->getPlanetCoordinates()->system, $battleReport->planet_system, 'Battle report planet system does not match.');
$this->assertEquals($foreignPlanet->getPlanetCoordinates()->position, $battleReport->planet_position, 'Battle report planet position does not match.');

// Assert that attacker has more than 0 but less than 200 light fighters after battle.
$this->planetService->reloadPlanet();
$this->assertLessThan(200, $this->planetService->getObjectAmount('light_fighter'), 'Attacker still has 150 light fighters after battle while it was expected they lost some.');
$this->assertGreaterThan(0, $this->planetService->getObjectAmount('light_fighter'), 'Attacker has no light fighters after battle while it was expected some should have survived and returned.');
$this->assertLessThan(200, $this->planetService->getObjectAmount('light_fighter'), 'Attacker still has 200 light fighters after battle while it was expected they lost some.');

// Assert that the defender has lost all units.
$foreignPlanet->reloadPlanet();
Expand Down

0 comments on commit 3b51a2f

Please sign in to comment.