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

75 lines
3.3 KiB
PHP

<?php
use App\Actions\EndLevy;
use App\Actions\Levy;
use App\Actions\Recruit;
use App\Models\GameEvent;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
it('drafts villagers, who stop working and return when the levy ends', function () {
$village = startVillage();
$before = shown($village);
app(Levy::class)(owner($village), $village->id, 24);
$state = shown($village);
expect($state['levy'])->toBe(['militia' => 24, 'until' => '2026-10-29T18:00:00.000Z', 'seconds' => 21600])
->and($state['population'])->toMatchArray(['villagers' => 24, 'workers' => 0])
->and($state['rates']['food'])->toBeLessThan($before['rates']['food']);
$this->travel(6)->hours();
$this->artisan('game:events --once')->assertSuccessful();
$state = shown($village);
expect($state['levy'])->toMatchArray(['militia' => 0, 'until' => null])
->and($state['population']['workers'])->toBe($before['population']['workers'])
->and(GameEvent::where('type', GameEvent::LEVY_ENDS)->whereNull('processed_at')->count())->toBe(0);
});
it('settles the village at the moment the militia return', function () {
$village = startVillage();
$before = shown($village);
app(Levy::class)(owner($village), $village->id, 24);
$levied = shown($village)['rates']['food'];
// No worker runs: showing the village catches the levy end up first.
$this->travel(7)->hours();
expect(shown($village)['resources']['food'])
->toEqualWithDelta($before['resources']['food'] + 6 * $levied + 1 * $before['rates']['food'], 0.01);
});
it('ends early on request', function () {
$village = startVillage();
app(Levy::class)(owner($village), $village->id, 10);
$this->travel(1)->hours();
app(EndLevy::class)(owner($village), $village->id);
expect(shown($village)['levy'])->toMatchArray(['militia' => 0, 'until' => null])
->and(GameEvent::where('type', GameEvent::LEVY_ENDS)->whereNull('processed_at')->count())->toBe(0)
->and(rule(fn () => app(EndLevy::class)(owner($village), $village->id)))->toBe('not_found');
});
it('refuses a second levy, more than the villagers and nobody', function () {
$village = startVillage();
expect(rule(fn () => app(Levy::class)(owner($village), $village->id, 25)))->toBe('villagers')
->and(rule(fn () => app(Levy::class)(owner($village), $village->id, 0)))->toBe('count');
app(Levy::class)(owner($village), $village->id, 5);
expect(rule(fn () => app(Levy::class)(owner($village), $village->id, 5)))->toBe('levy_active');
});
it('trains soldiers only from villagers who are not drafted', function () {
$village = startVillage();
$barracks = $village->buildings()->create(['type' => 'barracks', 'level' => 0]);
$village->update(['wood' => 1000, 'clay' => 1000, 'iron' => 1000]);
app(Levy::class)(owner($village), $village->id, 20);
$spear = collect(collect(shown($village)['military'])->firstWhere('type', 'barracks')['units'])->firstWhere('unit', 'spear');
expect($spear['max'])->toBe(4)
->and(rule(fn () => app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 5)))->toBe('villagers');
app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 4);
expect(shown($village)['population']['training'])->toBe(4);
});