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

123 lines
5.7 KiB
PHP
Raw Permalink Normal View History

<?php
use App\Actions\CancelRecruit;
use App\Actions\QueueDemolish;
use App\Actions\Recruit;
use App\Models\GameEvent;
use App\Models\Village;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
/** A start village with a barracks at level 1 and a full storage. */
function withBarracks(): array
{
$village = startVillage();
$barracks = $village->buildings()->create(['type' => 'barracks', 'level' => 1]);
$village->update(['wood' => 1000, 'clay' => 1000, 'iron' => 1000]);
return [$village->fresh(), $barracks];
}
function barracksQueue(Village $village): array
{
return collect(shown($village)['military'])->firstWhere('type', 'barracks')['queue'];
}
it('turns villagers into units at once and trains them one by one', function () {
[$village, $barracks] = withBarracks();
app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 10);
$state = shown($village);
expect($state['resources']['wood'])->toBe(500.0)
->and($state['population'])->toMatchArray(['villagers' => 14, 'upkeep' => 24, 'soldiers' => 0, 'training' => 10])
->and($state['rates']['food'])->toBe(6.0)
->and(barracksQueue($village)[0])->toMatchArray([
'unit' => 'spear', 'count' => 10, 'done' => 0, 'seconds_per_unit' => 1020,
'starts_at' => '2026-10-29T12:00:00.000Z', 'finishes_at' => '2026-10-29T14:50:00.000Z',
]);
$this->travel(1020 * 3 + 10)->seconds();
$state = shown($village);
expect((array) $state['troops'])->toBe(['spear' => 3])
->and($state['population'])->toMatchArray(['soldiers' => 3, 'training' => 7, 'villagers' => 14]);
$this->travel(2)->hours();
$this->artisan('game:events --once')->assertSuccessful();
$state = shown($village);
expect((array) $state['troops'])->toBe(['spear' => 10])
->and(barracksQueue($village))->toBe([])
->and(GameEvent::where('type', GameEvent::RECRUIT_FINISHED)->whereNull('processed_at')->count())->toBe(0);
});
it('refunds the untrained units when cancelled and gives the villagers back', function () {
[$village, $barracks] = withBarracks();
app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 10);
$this->travel(1020 * 2 + 5)->seconds();
$order = barracksQueue($village)[0]['id'];
app(CancelRecruit::class)(owner($village), $village->id, $order);
$state = shown($village);
expect((array) $state['troops'])->toBe(['spear' => 2])
->and($state['population'])->toMatchArray(['villagers' => 22, 'training' => 0])
->and($state['resources']['wood'])->toEqualWithDelta(500 + 8 * 50 + 5 * 2045 / 3600, 0.01)
->and(barracksQueue($village))->toBe([]);
});
it('queues orders at one barracks one after another, and moves them up on cancel', function () {
[$village, $barracks] = withBarracks();
app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 2);
app(Recruit::class)(owner($village), $village->id, $barracks->id, 'sword', 3);
$queue = barracksQueue($village);
expect($queue[1]['starts_at'])->toBe($queue[0]['finishes_at']);
app(CancelRecruit::class)(owner($village), $village->id, $queue[0]['id']);
expect(barracksQueue($village)[0])->toMatchArray(['unit' => 'sword', 'starts_at' => '2026-10-29T12:00:00.000Z']);
});
it('runs a queue per barracks, side by side', function () {
[$village, $first] = withBarracks();
$second = $village->buildings()->create(['type' => 'barracks', 'level' => 1]);
app(Recruit::class)(owner($village), $village->id, $first->id, 'spear', 2);
app(Recruit::class)(owner($village), $village->id, $second->id, 'spear', 2);
$starts = collect(shown($village)['military'])->flatMap(fn ($m) => array_column($m['queue'], 'starts_at'))->all();
expect($starts)->toBe(['2026-10-29T12:00:00.000Z', '2026-10-29T12:00:00.000Z']);
});
it('refuses missing requirements, too few villagers and the wrong building', function () {
[$village, $barracks] = withBarracks();
$archer = collect(collect(shown($village)['military'])->firstWhere('type', 'barracks')['units'])->firstWhere('unit', 'archer');
expect($archer)->toMatchArray(['blocked' => 'requires', 'requires' => [['type' => 'barracks', 'level' => 3]], 'max' => 0])
->and(rule(fn () => app(Recruit::class)(owner($village), $village->id, $barracks->id, 'archer', 1)))->toBe('requires')
->and(rule(fn () => app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 25)))->toBe('villagers')
->and(rule(fn () => app(Recruit::class)(owner($village), $village->id, $barracks->id, 'light_cavalry', 1)))->toBe('not_found');
});
it('says how many units fit, by resources and villagers', function () {
[$village] = withBarracks();
$units = collect(collect(shown($village)['military'])->firstWhere('type', 'barracks')['units'])->keyBy('unit');
// 24 villagers, and 1000 wood / 50 = 20 spears, 1000 iron / 70 = 14 swords.
expect($units['spear']['max'])->toBe(20)
->and($units['sword']['max'])->toBe(14);
});
it('sends units in training back to work, refunded, when their barracks is torn down', function () {
[$village, $barracks] = withBarracks();
app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 4);
// Taking level 1 down removes it after 5 minutes, before the first spear (17 minutes) is done.
app(QueueDemolish::class)(owner($village), $village->id, $barracks->id);
$this->travel(1)->hours();
$state = shown($village);
expect(collect($state['buildings'])->firstWhere('type', 'barracks'))->toBeNull()
->and($state['population']['training'])->toBe(0)
->and((array) $state['troops'])->toBe([])
->and($state['population']['villagers'])->toBe(24);
});