test: the lowest farm level that feeds a village, and the farm migration: raise, refund, idempotent, no-op on SQLite, capped at max level

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-03 09:41:12 +01:00
parent 6e022f7d1f
commit c272ecb5f2
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,80 @@
<?php
use App\Actions\JoinWorld;
use App\Actions\QueueUpgrade;
use App\Models\GameEvent;
use App\Models\User;
use App\Models\Village;
use App\Models\World;
use Carbon\CarbonImmutable;
use Database\Seeders\WorldSeeder;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-11-03 12:00:00')));
function farmsMigration(): object
{
return require database_path('migrations/2026_11_03_180000_farms_feed_their_heads.php');
}
/** A dev world village with more soldiers than its farm 1 feeds, as the old rule allowed. */
function overgrown(): Village
{
(new WorldSeeder)->run();
$village = app(JoinWorld::class)(User::factory()->create(), World::where('slug', 'dev')->value('id'));
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'spear', 'count' => 100]);
return $village->fresh();
}
function farmOf(Village $village): int
{
return $village->buildings()->where('type', 'farm')->value('level');
}
it('raises the farm to the lowest level that feeds the soldiers and the workers', function () {
// 100 spearmen and 5 workers: farm 7 leaves 80% of 114.4 = 91 heads, farm 8 leaves
// 114 for 100 spearmen and 7 workers (the farm needs 4 at level 8).
$village = overgrown();
$fine = startVillage();
expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([$village->id => 8]);
$state = shown($village);
expect(farmOf($village))->toBe(8)
->and($village->fresh()->points)->toBe(10 + 6 + 18)
->and($state['population'])->toMatchArray(['villagers' => 14, 'workers' => 7])
->and($state['rates']['food'])->toBeGreaterThan(0)
->and(farmOf($fine))->toBe(1);
});
it('cancels orders queued on the raised farm with a full refund', function () {
$village = overgrown();
$farm = $village->buildings->firstWhere('type', 'farm');
app(QueueUpgrade::class)(owner($village), $village->id, $farm->id);
expect($village->fresh()->wood)->toBe(443.0);
farmsMigration()->raiseFarms(now()->toImmutable());
$village->refresh();
expect($village->wood)->toBe(500.0)
->and($village->orders()->count())->toBe(0)
->and(GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)->whereNull('processed_at')->count())->toBe(0)
->and(farmOf($village))->toBe(8);
});
it('changes nothing on a second run, and nothing at all on SQLite', function () {
$village = overgrown();
farmsMigration()->up();
expect(farmOf($village))->toBe(1);
farmsMigration()->raiseFarms(now()->toImmutable());
expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([])
->and(farmOf($village))->toBe(8);
});
it('raises a farm no higher than its max level', function () {
$village = overgrown();
$village->troops()->update(['count' => 100_000]);
expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([$village->id => 30]);
});

View File

@ -98,6 +98,20 @@ it('counts idle villagers: not drafted, not needed by the buildings, not reserve
->and($short->idle())->toBe(0); ->and($short->idle())->toBe(0);
}); });
it('finds the lowest farm level that feeds the soldiers and the workers of every building', function () {
$population = new Population(gameConfig());
$start = [building(1, 'headquarters'), building(2, 'storage'), building(3, 'farm')];
// 100 heads of soldiers: farm 7 leaves 80% of 114.4 = 91, farm 8 leaves 114 for 100 + 7 workers.
expect($population->lowestFarmLevel($start, 3, 100))->toBe(8)
->and($population->lowestFarmLevel($start, 3, 0))->toBe(1)
// A level 30 camp needs 53: farm 5 feeds 58 villagers for 59 workers, farm 6 73.
->and($population->lowestFarmLevel([...$start, building(4, 'timber_camp', 30)], 3, 0))->toBe(6)
->and($population->lowestFarmLevel($start, 3, 100_000))->toBe(30)
// Never lower than the farm stands.
->and($population->lowestFarmLevel([building(1, 'farm', 12)], 1, 0))->toBe(12);
});
it('holds the same heads on every world speed', function () { it('holds the same heads on every world speed', function () {
$buildings = [building(1, 'farm', 5), building(2, 'headquarters', 3)]; $buildings = [building(1, 'farm', 5), building(2, 'headquarters', 3)];
$slow = (new Population(gameConfig()))->workforce($buildings, soldierUpkeep: 12); $slow = (new Population(gameConfig()))->workforce($buildings, soldierUpkeep: 12);