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

66 lines
2.8 KiB
PHP
Raw Normal View History

<?php
use App\Actions\JoinWorld;
use App\Actions\ListWorlds;
use App\Actions\ShowVillage;
use App\Game\Map\Continent;
use App\Models\User;
use App\Models\World;
it('starts a village with HQ, storage and farm at level 0', function () {
$village = startVillage();
// On the spawn ring: start radius 6 ± 2 around 500|500.
expect(hypot($village->x - 500, $village->y - 500))->toBeBetween(3.0, 9.0)
->and($village->only('space', 'points'))->toBe(['space' => 40, 'points' => 21])
->and($village->resources())->toBe(['wood' => 500.0, 'clay' => 500.0, 'iron' => 500.0, 'food' => 300.0])
->and($village->buildings->map->only('type', 'level')->all())->toBe([
['type' => 'headquarters', 'level' => 0],
['type' => 'storage', 'level' => 0],
['type' => 'farm', 'level' => 0],
]);
});
it('returns the same village when joining again', function () {
$village = startVillage();
expect(app(JoinWorld::class)(owner($village), $village->world_id)->id)->toBe($village->id);
});
it('spawns players on the ring, never next to another village', function () {
$villages = collect(range(1, 6))->map(fn () => startVillage(User::factory()->create()));
foreach ($villages as $a) {
foreach ($villages as $b) {
if ($a->isNot($b)) {
expect(max(abs($a->x - $b->x), abs($a->y - $b->y)))->toBeGreaterThanOrEqual(2);
}
}
}
});
it('lists worlds with their speed and the player village', function () {
$village = startVillage();
$dev = World::where('slug', 'dev')->first();
expect(app(ListWorlds::class)(owner($village)))->toBe([
['id' => $village->world_id, 'name' => 'World 1', 'slug' => 'w1', 'speed' => 1.0, 'village_id' => $village->id],
['id' => $dev->id, 'name' => 'Dev world', 'slug' => 'dev', 'speed' => 100.0, 'village_id' => null],
]);
});
it('shows a start village to its owner only', function () {
$village = startVillage();
$state = shown($village);
expect($state['space'])->toBe(['used' => 17, 'max' => 40])
->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['builders'])->toBe(2)
->and(collect($state['buildable'])->firstWhere('type', 'headquarters')['blocked'])->toBe('max_per_village')
->and(collect($state['buildable'])->firstWhere('type', 'wall')['blocked'])->toBeNull()
->and(rule(fn () => app(ShowVillage::class)(User::factory()->create(), $village->id)))->toBe('not_found');
});