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

30 lines
1.2 KiB
PHP
Raw 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])
// 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));
});