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 3f48a74
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
34 changes: 33 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 Expand Up @@ -235,6 +238,35 @@ protected function getNearbyForeignPlanet(): PlanetService
}
}

/**
* Creates a new clean planet for a foreign player in a nearby system.
* This is useful for testing interactions with a fresh hostile planet that has no history.
*
* @return PlanetService
*/
protected function getNearbyForeignCleanPlanet(): PlanetService
{
// First get a nearby foreign planet to obtain its player
$foreignPlanet = $this->getNearbyForeignPlanet();
$foreignPlayer = $foreignPlanet->getPlayer();

// Get a random empty coordinate near the current planet
$coordinate = $this->getNearbyEmptyCoordinate();

// Create a new planet at this coordinate for the foreign player
$planetServiceFactory = resolve(PlanetServiceFactory::class);
$newPlanet = $planetServiceFactory->createAdditionalPlanetForPlayer($foreignPlayer, $coordinate);

if ($newPlanet === null) {
$this->fail('Failed to create a new clean planet');
}

// Reload the foreign player to include the new planet
$foreignPlayer->load($foreignPlayer->getId());

return $newPlanet;
}

/**
* Gets a nearby foreign moon for the current user. This is useful for testing interactions between two players.
*
Expand Down
22 changes: 12 additions & 10 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 @@ -428,21 +433,17 @@ public function testDispatchFleetCombatUnitsLostAndResourceGained(): void

$unitCollection = new UnitCollection();
$unitCollection->addUnit(ObjectService::getUnitObjectByMachineName('light_fighter'), 200);
$foreignPlanet = $this->sendMissionToOtherPlayerPlanet($unitCollection, new Resources(0, 0, 0, 0));

// Clear existing units from foreign planet.
$foreignPlanet->removeUnits($foreignPlanet->getShipUnits(), true);
$foreignPlanet->removeUnits($foreignPlanet->getDefenseUnits(), true);
$foreignPlanet = $this->sendMissionToOtherPlayerCleanPlanet($unitCollection, new Resources(0, 0, 0, 0));

// 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 +452,10 @@ public function testDispatchFleetCombatUnitsLostAndResourceGained(): void
$response = $this->get('/overview');
$response->assertStatus(200);

// Assert that attacker has less than 200 light fighters after battle.
// 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
16 changes: 16 additions & 0 deletions tests/FleetDispatchTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ protected function sendMissionToOtherPlayerPlanet(UnitCollection $units, Resourc
return $nearbyForeignPlanet;
}

/**
* Send a fleet to a new clean planet of another player.
*
* @param UnitCollection $units
* @param Resources $resources
* @param int $assertStatus
* @return PlanetService
*/
protected function sendMissionToOtherPlayerCleanPlanet(UnitCollection $units, Resources $resources, int $assertStatus = 200): PlanetService
{
$nearbyForeignCleanPlanet = $this->getNearbyForeignCleanPlanet();

$this->dispatchFleet($nearbyForeignCleanPlanet->getPlanetCoordinates(), $units, $resources, PlanetType::Planet, $assertStatus);
return $nearbyForeignCleanPlanet;
}

/**
* Send a fleet to a moon of another player.
*
Expand Down

0 comments on commit 3f48a74

Please sign in to comment.