game-village-politics-backend/tests/Feature/SimulatorTest.php

34 lines
1.6 KiB
PHP
Raw Permalink Normal View History

<?php
use App\Actions\SimulateBattle;
use App\Models\World;
use Database\Seeders\WorldSeeder;
it('simulates a battle with the world\'s rules and changes nothing', function () {
(new WorldSeeder)->run();
$world = World::where('slug', 'w1')->value('id');
$result = app(SimulateBattle::class)($world, ['light_cavalry' => 100, 'catapult' => 20, 'axe' => 5], ['spear' => 50, 'militia' => 10], 0, 100, 0, false, 20);
expect($result['won'])->toBeTrue()
->and((array) $result['attacker']['units'])->toBe(['light_cavalry' => 100, 'catapult' => 20])
->and((array) $result['defender']['lost'])->toBe(['spear' => 50, 'militia' => 10])
// 20 catapults shoot 10 food at 60 before the melee: a sixth of each falls.
->and((array) $result['volley']['defender'])->toBe(['spear' => 8, 'militia' => 2])
->and((array) $result['volley']['attacker'])->toBe(['light_cavalry' => 0, 'catapult' => 0])
// 2100 defence against 15000 attack: under 1% falls, no catapult, so 20 take the building down two levels.
->and($result['building'])->toBe(['before' => 20, 'after' => 18])
->and($result['wall'])->toBeNull();
});
it('keeps morale and luck within the world\'s limits', function () {
(new WorldSeeder)->run();
$world = World::where('slug', 'w1')->value('id');
$low = app(SimulateBattle::class)($world, ['spear' => 100], [], 0, 0, -90, false, 0);
// Morale never below 30%, luck never below −25%.
expect($low['power']['attack'])->toBe(round(1000 * 0.3 * 0.75, 1))
->and($low['volley'])->toBeNull();
});