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

166 lines
7.6 KiB
PHP

<?php
use App\Actions\CancelOrder;
use App\Actions\QueueBuild;
use App\Actions\QueueDemolish;
use App\Actions\QueueUpgrade;
use App\Actions\SetPriority;
use App\Models\GameEvent;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-28 12:00:00')));
it('settles resources by rate and elapsed time, up to the storage cap', function () {
$village = startVillage();
$this->travel(2)->hours();
expect(shown($village)['resources'])->toBe(['wood' => 510.0, 'clay' => 510.0, 'iron' => 510.0, 'food' => 300.0]);
$this->travel(200)->hours();
expect(shown($village)['resources']['wood'])->toBe(1000.0);
});
it('pays up front, builds at the first free slot and finishes on time', function () {
$village = startVillage();
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
$state = shown($village);
expect($state['resources']['wood'])->toBe(450.0)
->and($state['queue'][0])->toMatchArray([
'type' => 'timber_camp', 'action' => 'build', 'x' => 7, 'y' => 0, 'builder' => 1,
'starts_at' => '2026-10-28T12:00:00.000Z', 'finishes_at' => '2026-10-28T12:01:30.000Z',
]);
$this->travel(89)->seconds();
expect(shown($village)['buildings'])->toHaveCount(3);
$this->travel(1)->seconds();
$state = shown($village);
expect($state['queue'])->toBe([])
->and(collect($state['buildings'])->firstWhere('type', 'timber_camp'))->toMatchArray(['level' => 0, 'x' => 7, 'y' => 0])
->and($state['rates']['wood'])->toBe(35.0);
});
it('builds at a chosen cell and refuses taken cells', function () {
$village = startVillage();
app(QueueBuild::class)(owner($village), $village->id, 'hiding_place', 9, 4);
expect(shown($village)['queue'][0])->toMatchArray(['x' => 9, 'y' => 4])
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'rally_point', 9, 4)))->toBe('no_space')
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'farm', 1, 1)))->toBe('no_space');
});
it('refuses a second headquarters, a wall without HQ 1 and what it cannot pay', function () {
$village = startVillage();
$village->update(['wood' => 10]);
expect(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'headquarters')))->toBe('max_per_village')
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'wall')))->toBe('requires')
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'storage')))->toBe('resources');
});
it('makes a later instance of a type cost more', function () {
$village = startVillage();
$farms = collect(shown($village)['buildable'])->firstWhere('type', 'farm');
expect($farms['cost'])->toBe(['wood' => 68, 'clay' => 60, 'iron' => 45]);
});
it('keeps a third order waiting and starts it when a builder frees up', function () {
$village = startVillage();
$user = owner($village);
app(QueueBuild::class)($user, $village->id, 'timber_camp'); // 90 s
app(QueueBuild::class)($user, $village->id, 'hiding_place'); // 60 s
app(QueueBuild::class)($user, $village->id, 'rally_point'); // 60 s, waits
expect(collect(shown($village)['queue'])->pluck('builder')->all())->toBe([1, 2, null]);
// Caught up long after: the rally point started when the hiding place finished.
$this->travel(10)->minutes();
expect(shown($village)['queue'])->toBe([])
->and(GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)->orderBy('id')->pluck('fire_at')->map->format('H:i:s')->all())
->toBe(['12:01:30', '12:01:00', '12:02:00']);
});
it('queues upgrades of one building one after another', function () {
$village = startVillage();
$hq = $village->buildings->firstWhere('type', 'headquarters');
app(QueueUpgrade::class)(owner($village), $village->id, $hq->id);
app(QueueUpgrade::class)(owner($village), $village->id, $hq->id);
$state = shown($village);
expect(collect($state['queue'])->map(fn (array $o) => Arr::only($o, ['target_level', 'builder']))->all())->toBe([
['target_level' => 1, 'builder' => 1],
['target_level' => 2, 'builder' => null],
])
->and(collect($state['buildings'])->firstWhere('type', 'headquarters')['upgrade']['target_level'])->toBe(3)
->and($state['resources']['wood'])->toBe(500.0 - 113 - 143);
// HQ 1 takes 144 s; HQ 2 then builds with HQ 1's speed-up: 172.8 / 1.05 → 165 s.
$this->travel(144 + 165)->seconds();
expect(collect(shown($village)['buildings'])->firstWhere('type', 'headquarters')['level'])->toBe(2);
});
it('cancels an order, the orders that build on it, and refunds them in full', function () {
$village = startVillage();
$hq = $village->buildings->firstWhere('type', 'headquarters');
app(QueueUpgrade::class)(owner($village), $village->id, $hq->id);
app(QueueUpgrade::class)(owner($village), $village->id, $hq->id);
$first = shown($village)['queue'][0]['id'];
app(CancelOrder::class)(owner($village), $village->id, $first);
$state = shown($village);
expect($state['queue'])->toBe([])
->and($state['resources']['wood'])->toBe(500.0)
->and(GameEvent::whereNull('processed_at')->count())->toBe(0);
});
it('demolishes with a refund on finish and removes a building below level 0', function () {
$village = startVillage();
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
$this->travel(90)->seconds();
$camp = collect(shown($village)['buildings'])->firstWhere('type', 'timber_camp');
app(QueueDemolish::class)(owner($village), $village->id, $camp['id']);
expect(shown($village)['queue'][0])->toMatchArray(['action' => 'demolish', 'target_level' => -1]);
$woodBefore = shown($village)['resources']['wood'];
$this->travel(45)->seconds();
$state = shown($village);
expect(collect($state['buildings'])->firstWhere('type', 'timber_camp'))->toBeNull()
->and($state['resources']['wood'])->toEqualWithDelta($woodBefore + 25 + 35 * 45 / 3600, 0.01);
});
it('keeps core buildings at level 0', function () {
$village = startVillage();
$hq = $village->buildings->firstWhere('type', 'headquarters');
expect(collect(shown($village)['buildings'])->firstWhere('type', 'headquarters')['demolish'])->toBeNull()
->and(rule(fn () => app(QueueDemolish::class)(owner($village), $village->id, $hq->id)))->toBe('core_building');
});
it('moves workers to the buildings with the highest priority', function () {
// 30 villagers: the farm takes 2, the clay pit at priority 9 the other 28.
$village = startVillage();
$village->buildings()->create(['type' => 'timber_camp', 'level' => 30, 'x' => 7, 'y' => 0]);
$clay = $village->buildings()->create(['type' => 'clay_pit', 'level' => 30, 'x' => 0, 'y' => 3]);
app(SetPriority::class)(owner($village), $village->id, $clay->id, 12);
$buildings = collect(shown($village)['buildings'])->keyBy('type');
expect($buildings['clay_pit']['workers'])->toBe(['assigned' => 28, 'required' => 60])
->and($buildings['clay_pit']['priority'])->toBe(9)
->and($buildings['timber_camp']['workers']['assigned'])->toBe(0)
->and($buildings['farm']['workers']['assigned'])->toBe(2);
});
it('lets the worker finish orders of idle villages', function () {
$village = startVillage();
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
$this->travel(2)->minutes();
$this->artisan('game:events --once')->assertSuccessful();
expect($village->buildings()->where('type', 'timber_camp')->exists())->toBeTrue()
->and(GameEvent::whereNull('processed_at')->count())->toBe(0);
});