From c272ecb5f2d223b2ba9415b1de91b21b3dcd52e1 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Tue, 3 Nov 2026 09:41:12 +0100 Subject: [PATCH] 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 --- tests/Feature/FarmsFeedHeadsTest.php | 80 ++++++++++++++++++++++++++++ tests/Unit/Game/PopulationTest.php | 14 +++++ 2 files changed, 94 insertions(+) create mode 100644 tests/Feature/FarmsFeedHeadsTest.php diff --git a/tests/Feature/FarmsFeedHeadsTest.php b/tests/Feature/FarmsFeedHeadsTest.php new file mode 100644 index 0000000..07d0dbe --- /dev/null +++ b/tests/Feature/FarmsFeedHeadsTest.php @@ -0,0 +1,80 @@ + $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]); +}); diff --git a/tests/Unit/Game/PopulationTest.php b/tests/Unit/Game/PopulationTest.php index 7d52429..1416b55 100644 --- a/tests/Unit/Game/PopulationTest.php +++ b/tests/Unit/Game/PopulationTest.php @@ -98,6 +98,20 @@ it('counts idle villagers: not drafted, not needed by the buildings, not reserve ->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 () { $buildings = [building(1, 'farm', 5), building(2, 'headquarters', 3)]; $slow = (new Population(gameConfig()))->workforce($buildings, soldierUpkeep: 12);