$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 () { // Farm 10 fed 80% of 22,350 under the old rule; farm 30 feeds 15,509 heads today. $village = overgrown(); $village->buildings()->where('type', 'farm')->update(['level' => 10]); $village->troops()->update(['count' => 16_000]); expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([$village->id => 30]); }); it('leaves a village alone that was short of villagers under the old rule already', function () { // On world speed 1 both rules agree: a level 30 camp next to farm 1 was understaffed before. $short = startVillage(); $short->buildings()->create(['type' => 'timber_camp', 'level' => 30]); // On the dev world, 3000 spearmen outeat even the old farm 1's 2400. $hungry = overgrown(); $hungry->troops()->update(['count' => 3000]); expect(farmsMigration()->raiseFarms(now()->toImmutable()))->toBe([]) ->and(farmOf($short))->toBe(1) ->and(farmOf($hungry))->toBe(1); });