From a2f4e01092c7f55a4630eca992bf5c06d4287a73 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Tue, 3 Nov 2026 09:38:20 +0100 Subject: [PATCH] test: heads on a speed 100 world, the food rate and starvation at world speed, idle villagers for new buildings and upgrades, reservations, farms exempt, demolish and levy, lint in heads Co-Authored-By: Claude Opus 5.5 --- tests/Feature/JoinWorldTest.php | 2 +- tests/Feature/VillagersTest.php | 153 ++++++++++++++++++++++++++ tests/Unit/Game/BuildingRulesTest.php | 27 +++++ tests/Unit/Game/ConfigLintTest.php | 15 +++ tests/Unit/Game/PopulationTest.php | 29 +++++ tests/Unit/Game/ProductionTest.php | 12 ++ 6 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/VillagersTest.php diff --git a/tests/Feature/JoinWorldTest.php b/tests/Feature/JoinWorldTest.php index 1c17797..2e08c43 100644 --- a/tests/Feature/JoinWorldTest.php +++ b/tests/Feature/JoinWorldTest.php @@ -87,7 +87,7 @@ it('shows a start village to its owner only', function () { ->and($state['continent'])->toBe(Continent::of($village->x, $village->y)) ->and($state['rates'])->toBe(['wood' => 5.0, 'clay' => 5.0, 'iron' => 5.0, 'food' => 6.0]) ->and($state['storage_cap'])->toEqual(1000) - ->and($state['population'])->toEqual(['food_budget' => 30, 'upkeep' => 24, 'villagers' => 24, 'workers' => 5, 'soldiers' => 0, 'training' => 0]) + ->and($state['population'])->toEqual(['food_budget' => 30, 'upkeep' => 24, 'villagers' => 24, 'workers' => 5, 'idle' => 19, 'reserved' => 0, 'soldiers' => 0, 'training' => 0]) ->and($state['builders'])->toBe(2) ->and(collect($state['buildable'])->firstWhere('type', 'headquarters')['blocked'])->toBe('max_per_village') ->and(collect($state['buildable'])->firstWhere('type', 'wall')['blocked'])->toBeNull() diff --git a/tests/Feature/VillagersTest.php b/tests/Feature/VillagersTest.php new file mode 100644 index 0000000..e8b0bae --- /dev/null +++ b/tests/Feature/VillagersTest.php @@ -0,0 +1,153 @@ + $this->travelTo(CarbonImmutable::parse('2026-11-03 12:00:00'))); + +/** A start village on the speed 100 dev world. */ +function devVillage(): Village +{ + (new WorldSeeder)->run(); + + return app(JoinWorld::class)(User::factory()->create(), World::where('slug', 'dev')->value('id')); +} + +/** A start village (24 villagers, 5 of them working) with extra buildings and soldiers at home. */ +function crowded(array $buildings = [], array $troops = []): Village +{ + $village = startVillage(); + foreach ($buildings as [$type, $level]) { + $village->buildings()->create(['type' => $type, 'level' => $level]); + } + foreach ($troops as $unit => $count) { + $village->troops()->create(['home_village_id' => $village->id, 'unit' => $unit, 'count' => $count]); + } + + return $village->fresh(); +} + +it('holds the same heads on the speed 100 dev world, with food made and eaten 100 times as fast', function () { + $fast = shown(devVillage()); + $slow = shown(startVillage()); + + expect($fast['population'])->toEqual($slow['population']) + ->and($fast['population'])->toMatchArray(['food_budget' => 30, 'upkeep' => 24, 'villagers' => 24, 'idle' => 19]) + ->and($fast['rates']['food'])->toEqual(100 * $slow['rates']['food']) + ->and($fast['rates']['food'])->toEqual(600); +}); + +it('trains no more on the dev world than its heads allow, and starves at world speed', function () { + $village = devVillage(); + $barracks = $village->buildings()->create(['type' => 'barracks', 'level' => 1]); + + expect(rule(fn () => app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 25)))->toBe('villagers'); + + // 10 light cavalry eat 30 heads, all that a farm 1 feeds: no villagers are left, the + // farm makes a tenth of its 3000 food/h and the riders eat 3000. + $village->troops()->create(['home_village_id' => $village->id, 'unit' => 'light_cavalry', 'count' => 10]); + $state = shown($village->fresh()); + $rate = 3000 * exp(-2.3) - 3000; + expect($state['population'])->toMatchArray(['villagers' => 0, 'upkeep' => 30]) + ->and($state['rates']['food'])->toEqualWithDelta($rate, 0.01) + ->and(CarbonImmutable::parse($state['starvation']['food_empty_at'])->getTimestampMs()) + ->toEqualWithDelta(now()->getTimestampMs() + 300 / -$rate * 3_600_000, 1000); + + // 300 food last 400 s. The grace is a day at speed 1, 864 s at speed 100; then just + // enough riders desert. + $this->travel(7)->minutes(); + $this->artisan('game:events --once')->assertSuccessful(); + $this->travel(864)->seconds(); + $this->artisan('game:events --once')->assertSuccessful(); + $state = shown($village); + expect($state['troops']->light_cavalry)->toBeLessThan(10) + ->and($state['rates']['food'])->toBeGreaterThanOrEqual(0); +}); + +it('refuses a new building without idle villagers for its workers, and the queue reserves them', function () { + // 24 villagers − 17 spearmen = 7, the start buildings need 5: 2 are idle. + $village = crowded(troops: ['spear' => 17]); + $state = shown($village); + $options = collect($state['buildable'])->keyBy('type'); + + expect($state['population'])->toMatchArray(['villagers' => 7, 'idle' => 2, 'reserved' => 0]) + ->and($options['barracks'])->toMatchArray(['villagers' => 3, 'blocked' => 'villagers']) + ->and($options['timber_camp'])->toMatchArray(['villagers' => 2, 'blocked' => null]) + ->and($options['farm'])->toMatchArray(['villagers' => 0, 'blocked' => null]) + ->and($options['hiding_place'])->toMatchArray(['villagers' => 0, 'blocked' => null]) + ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'barracks')))->toBe('villagers'); + + app(QueueBuild::class)(owner($village), $village->id, 'timber_camp'); + $state = shown($village); + $options = collect($state['buildable'])->keyBy('type'); + expect($state['population'])->toMatchArray(['idle' => 0, 'reserved' => 2]) + ->and($options['clay_pit']['blocked'])->toBe('villagers') + ->and($options['farm']['blocked'])->toBeNull() + ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'clay_pit')))->toBe('villagers'); + + // A farm needs no idle villagers: it brings more than it employs. + app(QueueBuild::class)(owner($village), $village->id, 'farm'); + + // Cancelling frees the reserved workers at once. + app(CancelOrder::class)(owner($village), $village->id, $state['queue'][0]['id']); + expect(shown($village)['population'])->toMatchArray(['idle' => 2, 'reserved' => 0]); +}); + +it('refuses an upgrade without idle villagers until the farm grows', function () { + // A level 2 timber camp needs 2 more: 24 − 17 spearmen = 7 villagers for 7 workers. + $village = crowded([['timber_camp', 2]], ['spear' => 17]); + $state = shown($village); + $camp = collect($state['buildings'])->firstWhere('type', 'timber_camp'); + $farm = collect($state['buildings'])->firstWhere('type', 'farm'); + + // Level 3 needs round(2 × 1.12²) = 3 workers, one more. + expect($state['population']['idle'])->toBe(0) + ->and($camp['upgrade'])->toMatchArray(['target_level' => 3, 'villagers' => 1, 'blocked' => 'villagers']) + ->and($farm['upgrade'])->toMatchArray(['target_level' => 2, 'villagers' => 0, 'blocked' => null]) + ->and(rule(fn () => app(QueueUpgrade::class)(owner($village), $village->id, $camp['id'])))->toBe('villagers'); + + // Farm 2 feeds 37.5 heads: 30 − 17 = 13 villagers for 7 workers. + app(QueueUpgrade::class)(owner($village), $village->id, $farm['id']); + $this->travel(144)->seconds(); + $state = shown($village); + expect($state['population'])->toMatchArray(['villagers' => 13, 'idle' => 6]) + ->and(collect($state['buildings'])->firstWhere('type', 'timber_camp')['upgrade']['blocked'])->toBeNull(); + + app(QueueUpgrade::class)(owner($village), $village->id, $camp['id']); + expect(shown($village)['population'])->toMatchArray(['idle' => 5, 'reserved' => 1]); +}); + +it('frees the workers of a demolished building when the demolition finishes', function () { + // Barracks 1 needs 3: 24 − 16 spearmen = 8 villagers for 8 workers. + $village = crowded([['barracks', 1]], ['spear' => 16]); + $barracks = collect(shown($village)['buildings'])->firstWhere('type', 'barracks'); + + app(QueueDemolish::class)(owner($village), $village->id, $barracks['id']); + expect(shown($village)['population']['idle'])->toBe(0); + + $this->travel(5)->minutes(); + $state = shown($village); + expect(collect($state['buildings'])->firstWhere('type', 'barracks'))->toBeNull() + ->and($state['population']['idle'])->toBe(3) + ->and(collect($state['buildable'])->firstWhere('type', 'timber_camp')['blocked'])->toBeNull(); +}); + +it('does not count drafted militia as idle', function () { + $village = startVillage(); + app(Levy::class)(owner($village), $village->id, 20); + $state = shown($village); + + expect($state['population']['idle'])->toBe(0) + ->and(collect($state['buildable'])->firstWhere('type', 'timber_camp')['blocked'])->toBe('villagers') + ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'timber_camp')))->toBe('villagers'); +}); diff --git a/tests/Unit/Game/BuildingRulesTest.php b/tests/Unit/Game/BuildingRulesTest.php index f112503..ff0752a 100644 --- a/tests/Unit/Game/BuildingRulesTest.php +++ b/tests/Unit/Game/BuildingRulesTest.php @@ -146,3 +146,30 @@ it('shows what a copy does at its share', function () { ->and($rules->effects('barracks', 1, 0.5))->toBe([['key' => 'recruit_speed', 'value' => -50]]) ->and($rules->effects('barracks', 6, 0.5))->toBe([['key' => 'recruit_speed', 'value' => -38]]); }); + +it('feeds heads by farm level at world speed 1, on every world speed', function () { + $rules = new BuildingRules(gameConfig()); + $fast = new BuildingRules(gameConfig(['world' => ['speed' => 100]])); + + expect($rules->feeds('farm', 1))->toBe(30.0) + ->and($rules->feeds('farm', 3))->toBe(30 * 1.25 ** 2) + ->and($fast->feeds('farm', 3))->toBe(30 * 1.25 ** 2) + ->and($fast->output('farm', 3)['per_hour'])->toBe(30 * 1.25 ** 2 * 100) + ->and($rules->feeds('timber_camp', 5))->toBe(0.0) + ->and($rules->isFarm('farm'))->toBeTrue() + ->and($rules->isFarm('storage'))->toBeFalse(); +}); + +it('needs idle villagers for the workers a level adds, and none for farms', function () { + $rules = new BuildingRules(gameConfig()); + + // Timber camp workers: round(2 × 1.12^(n−1)) = 2, 2, 3 at levels 1 to 3. + expect($rules->extraWorkers('timber_camp', 1))->toBe(2) + ->and($rules->extraWorkers('timber_camp', 2))->toBe(0) + ->and($rules->extraWorkers('timber_camp', 3))->toBe(1) + ->and($rules->extraWorkers('barracks', 1))->toBe(3) + ->and($rules->extraWorkers('hiding_place', 4))->toBe(0) + ->and($rules->extraWorkers('timber_camp', 0))->toBe(0) + ->and($rules->extraWorkers('farm', 1))->toBe(0) + ->and($rules->extraWorkers('farm', 21))->toBe(0); +}); diff --git a/tests/Unit/Game/ConfigLintTest.php b/tests/Unit/Game/ConfigLintTest.php index e7aac8c..2019bcb 100644 --- a/tests/Unit/Game/ConfigLintTest.php +++ b/tests/Unit/Game/ConfigLintTest.php @@ -16,6 +16,21 @@ it('reports a farm whose workers eat more than it makes', function () { expect(ConfigLint::check($config))->toContain('[farm] level 1 feeds fewer people than it needs as workers.'); }); +it('checks farms in heads, so a fast world does not hide a farm that feeds too few', function () { + // 2 food/h at speed 1 for 2 workers: 200 food/h at speed 100 hides nothing. + $config = gameConfig(['world' => ['speed' => 100], 'buildings' => ['farm' => ['production' => ['base' => 2]]]]); + + expect(ConfigLint::check($config))->toContain('[farm] level 1 feeds fewer people than it needs as workers.'); +}); + +it('reports when one farm at max level cannot staff one of every building at max level', function () { + // Farm 30 feeds 19387 heads; at a 1% share that is 193 villagers for 343 workers. + $config = gameConfig(['economy' => ['villager_share' => 0.01]]); + + expect(ConfigLint::check($config)) + ->toContain('One of every building at max level needs 343 workers, more than the 193 villagers one farm at max level feeds.'); +}); + it('reports a storage level that its predecessor cannot pay for', function () { $config = gameConfig(['buildings' => ['storage' => ['cost' => ['wood' => 2000]]]]); diff --git a/tests/Unit/Game/PopulationTest.php b/tests/Unit/Game/PopulationTest.php index d333c3c..7d52429 100644 --- a/tests/Unit/Game/PopulationTest.php +++ b/tests/Unit/Game/PopulationTest.php @@ -81,3 +81,32 @@ it('counts buildings that need nobody as fully staffed', function () { expect($workforce->villagers)->toBe(0) ->and($workforce->ratio(1))->toBe(1.0); }); + +it('counts idle villagers: not drafted, not needed by the buildings, not reserved', function () { + // 24 villagers; headquarters 2, storage 1 and farm 2 need 5. + $buildings = [building(1, 'headquarters'), building(2, 'storage'), building(3, 'farm')]; + $population = new Population(gameConfig()); + $workforce = $population->workforce($buildings); + $drafted = $population->workforce($buildings, militia: 20); + // A level 30 camp needs 53 alone: understaffed, and nobody is idle. + $short = $population->workforce([...$buildings, building(4, 'timber_camp', 30)]); + + expect($workforce->idle())->toBe(19) + ->and($workforce->idle(4))->toBe(15) + ->and($workforce->idle(30))->toBe(0) + ->and($drafted->idle())->toBe(0) + ->and($short->idle())->toBe(0); +}); + +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); + $fast = (new Population(gameConfig(['world' => ['speed' => 100]])))->workforce($buildings, soldierUpkeep: 12); + + // Farm 5 feeds 30 × 1.25^4 = 73.24 heads; 80% is 58.6, soldiers take 12. + expect($fast->foodBudget)->toBe($slow->foodBudget) + ->and($fast->foodBudget)->toEqualWithDelta(73.24, 0.01) + ->and($fast->villagers)->toBe($slow->villagers) + ->and($fast->villagers)->toBe(46) + ->and($fast->upkeep)->toEqual(46 + 12); +}); diff --git a/tests/Unit/Game/ProductionTest.php b/tests/Unit/Game/ProductionTest.php index 9513606..60a44c3 100644 --- a/tests/Unit/Game/ProductionTest.php +++ b/tests/Unit/Game/ProductionTest.php @@ -56,3 +56,15 @@ it('settles stored amounts by rate and elapsed hours within the cap', function ( expect(Settle::advance($stored, $rates, 1000.0, 0.5)) ->toBe(['wood' => 115.0, 'clay' => 1000.0, 'iron' => 0.0, 'food' => 50.0]); }); + +it('scales the food rate with world speed, both what farms make and what heads eat', function () { + $config = gameConfig(['world' => ['speed' => 100]]); + $buildings = [building(1, 'headquarters'), building(2, 'storage'), building(3, 'farm')]; + $workforce = (new Population($config))->workforce($buildings); + $rates = (new Production($config))->rates($buildings, $workforce); + + // 24 villagers (heads) on every speed; the farm makes 3000 food/h and they eat 2400. + expect($workforce->villagers)->toBe(24) + ->and($rates['food'])->toEqual((30 - 24) * 100) + ->and($rates['wood'])->toEqual(5 * 100); +});