Skip to content

Commit

Permalink
Merge pull request #415 from lanedirt/401-bug-fix-flaky-espionage-dis…
Browse files Browse the repository at this point in the history
…patch-mission-test

Fix flaxy espionage fleet dispatch tests caused by conflicting missions
  • Loading branch information
lanedirt authored Oct 25, 2024
2 parents 3c10dcb + be6686e commit 6882b2a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
6 changes: 3 additions & 3 deletions tests/AccountTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ protected function getSecondPlayerId(): int
protected function getNearbyForeignPlanet(): PlanetService
{
// Find a planet of another player that is close to the current player by checking the same galaxy
// and up to 10 systems away.
// and up to 15 systems away.
$planet_id = \DB::table('planets')
->where('user_id', '!=', $this->currentUserId)
->where('galaxy', $this->planetService->getPlanetCoordinates()->galaxy)
->whereBetween('system', [$this->planetService->getPlanetCoordinates()->system - 10, $this->planetService->getPlanetCoordinates()->system + 10])
->whereBetween('system', [$this->planetService->getPlanetCoordinates()->system - 15, $this->planetService->getPlanetCoordinates()->system + 15])
->inRandomOrder()
->limit(1)
->pluck('id');
Expand All @@ -188,7 +188,7 @@ protected function getNearbyForeignPlanet(): PlanetService
$planet_id = \DB::table('planets')
->where('user_id', '!=', $this->currentUserId)
->where('galaxy', $this->planetService->getPlanetCoordinates()->galaxy)
->whereBetween('system', [$this->planetService->getPlanetCoordinates()->system - 10, $this->planetService->getPlanetCoordinates()->system + 10])
->whereBetween('system', [$this->planetService->getPlanetCoordinates()->system - 15, $this->planetService->getPlanetCoordinates()->system + 15])
->inRandomOrder()
->limit(1)
->pluck('id');
Expand Down
47 changes: 31 additions & 16 deletions tests/Feature/FleetDispatch/FleetDispatchEspionageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Tests\Feature\FleetDispatch;

use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Carbon;
use OGame\GameObjects\Models\Units\UnitCollection;
use OGame\Models\EspionageReport;
use OGame\Models\Message;
use OGame\Models\Resources;
use OGame\Services\DebrisFieldService;
use OGame\Services\FleetMissionService;
Expand Down Expand Up @@ -131,10 +129,11 @@ public function testDispatchFleetUpdatePlanet(): void
// Get current updated timestamp of the target planet.
$foreignPlanetUpdatedAt = $foreignPlanet->getUpdatedAt();

// Increase time by 10 hours to ensure the mission is done.
$currentTime = Carbon::now();
$currentTime->addHours(10);
Carbon::setTestNow($currentTime);
// Increase time by 10 hours from the foreign planet's last update time to ensure the mission is done
// and time has passed since the last update. This regardless of whether other tests have affected the
// foreign planet before and potentially mutated time themselves as well.
$missionCompletionTime = $foreignPlanetUpdatedAt->copy()->addHours(10);
Carbon::setTestNow($missionCompletionTime);

// Do a request to trigger the update logic.
$response = $this->get('/overview');
Expand All @@ -145,7 +144,7 @@ public function testDispatchFleetUpdatePlanet(): void
$foreignPlanet->reloadPlanet();

// Assert that target planet has been updated.
$this->assertLessThan($foreignPlanet->getUpdatedAt()->timestamp, $foreignPlanetUpdatedAt->timestamp, 'Target planet was not updated after espionage mission has arrived. Check target planet update logic on mission arrival.');
$this->assertNotEquals($foreignPlanet->getUpdatedAt()->timestamp, $foreignPlanetUpdatedAt->timestamp, 'Target planet was not updated after espionage mission has arrived. Check target planet update logic on mission arrival.');

// Assert that the updated resources are visible in the espionage report.
// Get the latest espionage report message from the database.
Expand All @@ -166,18 +165,27 @@ public function testDispatchFleetDebrisEspionageReport(): void
$foreignPlanet = $this->sendMissionToOtherPlayer($unitCollection, new Resources(0, 0, 0, 0));

// Add debris field to the foreign planet.
// First check if it already exists, if so, delete it to make sure the debris field contains
// the exact resources that we expect.
$debrisField = resolve(DebrisFieldService::class);
if ($debrisField->loadForCoordinates($foreignPlanet->getPlanetCoordinates())) {
$debrisField->delete();
}

// Create a new debris field for the foreign planet with an exact amount of resources
// that we later test for.
$debrisField->loadOrCreateForCoordinates($foreignPlanet->getPlanetCoordinates());
$debrisField->appendResources(new Resources(1337, 443, 259, 0));
$debrisField->save();

// Get current updated timestamp of the target planet.
$foreignPlanetUpdatedAt = $foreignPlanet->getUpdatedAt();

// Increase time by 10 hours to ensure the mission is done.
$currentTime = Carbon::now();
$currentTime->addHours(10);
Carbon::setTestNow($currentTime);
// Increase time by 10 hours from the foreign planet's last update time to ensure the mission is done
// and time has passed since the last update. This regardless of whether other tests have affected the
// foreign planet before and potentially mutated time themselves as well.
$missionCompletionTime = $foreignPlanetUpdatedAt->copy()->addHours(10);
Carbon::setTestNow($missionCompletionTime);

// Do a request to trigger the update logic.
$response = $this->get('/overview');
Expand All @@ -188,7 +196,7 @@ public function testDispatchFleetDebrisEspionageReport(): void
$foreignPlanet->reloadPlanet();

// Assert that target planet has been updated.
$this->assertLessThan($foreignPlanet->getUpdatedAt()->timestamp, $foreignPlanetUpdatedAt->timestamp, 'Target planet was not updated after espionage mission has arrived. Check target planet update logic on mission arrival.');
$this->assertNotEquals($foreignPlanet->getUpdatedAt()->timestamp, $foreignPlanetUpdatedAt->timestamp, 'Target planet was not updated after espionage mission has arrived. Check target planet update logic on mission arrival.');

// Assert that the updated resources are visible in the espionage report.
// Get the latest espionage report message from the database.
Expand All @@ -202,7 +210,6 @@ public function testDispatchFleetDebrisEspionageReport(): void

/**
* Verify that dispatching a fleet launches a return trip.
* @throws BindingResolutionException
* @throws Exception
*/
public function testDispatchFleetReturnTrip(): void
Expand Down Expand Up @@ -286,11 +293,15 @@ public function testDispatchFleetReturnShown(): void
// Assert that we see both the parent and the return mission.
$response->assertSee('data-return-flight="false"', false);
$response->assertSee('data-return-flight="true"', false);

// Cancel the fleet mission, so it doesn't interfere with other tests.
$fleetMissionService = resolve(FleetMissionService::class, ['player' => $this->planetService->getPlayer()]);
$fleetMission = $fleetMissionService->getActiveFleetMissionsForCurrentPlayer()->first();
$fleetMissionService->cancelMission($fleetMission);
}

/**
* Verify that canceling/recalling an active mission works.
* @throws BindingResolutionException
* @throws Exception
*/
public function testDispatchFleetRecallMission(): void
Expand All @@ -311,7 +322,7 @@ public function testDispatchFleetRecallMission(): void
// Send fleet to a nearby foreign planet.
$unitCollection = new UnitCollection();
$unitCollection->addUnit($this->planetService->objects->getUnitObjectByMachineName('espionage_probe'), 1);
$foreignPlanet = $this->sendMissionToOtherPlayer($unitCollection, new Resources(0, 0, 0, 0));
$this->sendMissionToOtherPlayer($unitCollection, new Resources(0, 0, 0, 0));

// Get just dispatched fleet mission ID from database.
$fleetMissionService = resolve(FleetMissionService::class, ['player' => $this->planetService->getPlayer()]);
Expand Down Expand Up @@ -372,7 +383,6 @@ public function testDispatchFleetRecallMission(): void

/**
* Verify that canceling/recalling an active mission twice results in an error.
* @throws BindingResolutionException
* @throws Exception
*/
public function testDispatchFleetRecallMissionTwiceError(): void
Expand Down Expand Up @@ -454,5 +464,10 @@ public function testMiniFleetDispatchMethod(): void
$response->assertJsonFragment(['friendly' => 1]);

$this->get('/ajax/fleet/eventlist/fetch')->assertStatus(200);

// Cancel the fleet mission, so it doesn't interfere with other tests.
$fleetMissionService = resolve(FleetMissionService::class, ['player' => $this->planetService->getPlayer()]);
$fleetMission = $fleetMissionService->getActiveFleetMissionsForCurrentPlayer()->first();
$fleetMissionService->cancelMission($fleetMission);
}
}
6 changes: 6 additions & 0 deletions tests/FleetDispatchTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ protected function sendMissionToSecondPlanetDebrisField(UnitCollection $units, R

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

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

0 comments on commit 6882b2a

Please sign in to comment.