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

98 lines
4.8 KiB
PHP

<?php
use App\Actions\Account;
use App\Actions\JoinWorld;
use App\Actions\ListWorlds;
use App\Actions\SendTroops;
use App\Actions\Tribes;
use App\Engine\Abandon;
use App\Engine\Nobility;
use App\Models\GameEvent;
use App\Models\Movement;
use App\Models\Player;
use App\Models\Tribe;
use App\Models\TribeMember;
use App\Models\User;
use App\Models\Village;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
it('changes the password only with the current one', function () {
$village = startVillage(User::factory()->create(['password' => 'old-secret']));
$user = owner($village);
expect(rule(fn () => app(Account::class)->password($user, 'wrong', 'new-secret')))->toBe('wrong_password')
->and(rule(fn () => app(Account::class)->password($user, 'old-secret', 'short')))->toBe('password_short');
app(Account::class)->password($user, 'old-secret', 'new-secret');
$this->postJson('/api/auth/login', ['email' => $user->email, 'password' => 'new-secret'])->assertOk();
expect(app(Account::class)->show($user->fresh())['worlds'][0])->toMatchArray(['villages' => 1, 'tribe' => null]);
});
it('deletes an account: villages turn barbarian, the tribe goes to the next member, support goes home', function () {
$leaving = startVillage(User::factory()->create(['password' => 'secret-pass']));
$mate = startVillage();
app(Tribes::class)->found(owner($leaving), $leaving->world_id, 'Dorfrat', 'DRF');
app(Tribes::class)->invite(owner($leaving), $leaving->world_id, $mate->player->name);
app(Tribes::class)->join(owner($mate), $mate->world_id, Tribe::where('tag', 'DRF')->value('id'));
$leaving->troops()->create(['home_village_id' => $mate->id, 'unit' => 'spear', 'count' => 5]);
$user = owner($leaving);
expect(rule(fn () => app(Account::class)->delete($user, 'wrong')))->toBe('wrong_password');
app(Account::class)->delete($user, 'secret-pass');
$village = $leaving->fresh();
expect($village->player_id)->toBeNull()
->and($village->name)->toBe($leaving->name)
->and(GameEvent::where('village_id', $village->id)->where('type', 'barbarian.grows')->exists())->toBeTrue()
->and(User::find($user->id))->toBeNull()
->and(Player::where('user_id', $user->id)->exists())->toBeFalse()
->and(TribeMember::where('player_id', $mate->player_id)->value('role'))->toBe('leader')
->and(Movement::where('target_id', $mate->id)->where('type', 'return')->exists())->toBeTrue();
});
it('lets a player who lost every village start again with fresh beginner protection', function () {
$fallen = startVillage();
$other = startVillage();
$this->travel(5)->days();
Nobility::of($fallen->fresh())->handOver($fallen->fresh(), $other->player_id, now()->toImmutable());
Village::whereKey($fallen->id)->update(['player_id' => $other->player_id]);
$world = collect(app(ListWorlds::class)(owner($fallen)))->firstWhere('id', $fallen->world_id);
expect($world)->toMatchArray(['village_id' => null, 'fallen' => true]);
$again = app(JoinWorld::class)(owner($fallen), $fallen->world_id);
expect($again->id)->not->toBe($fallen->id)
->and($again->player_id)->toBe($fallen->player_id);
$other->buildings()->create(['type' => 'rally_point', 'level' => 1]);
$other->troops()->create(['home_village_id' => $other->id, 'unit' => 'spear', 'count' => 5]);
expect(rule(fn () => app(SendTroops::class)(owner($other), $other->id, 'attack', $again->x, $again->y, ['spear' => 1], null, null)))->toBe('protected');
});
it('turns the villages of players gone too long barbarian', function () {
config(['game.world.inactive_days' => 14]);
$gone = startVillage();
$here = startVillage();
$never = startVillage();
Player::whereKey($never->player_id)->update(['last_active_at' => null]);
$this->travel(15)->days();
shown($here);
expect(Abandon::inactive(now()->toImmutable()))->toBe(1)
->and($gone->fresh()->player_id)->toBeNull()
->and($here->fresh()->player_id)->not->toBeNull()
->and($never->fresh()->player_id)->not->toBeNull();
});
it('tells a world settings in plain units', function () {
$village = startVillage();
$settings = app(ListWorlds::class)->settings($village->world_id)['settings'];
expect($settings['pace'])->toBe(['speed' => 1.0, 'unit_speed' => 1.0, 'protection' => 5 * 86400, 'inactive' => 0])
->and($settings['village'])->toMatchArray(['space' => 40, 'space_per_hq_level' => 5])
->and($settings['tribes'])->toBe(['max_members' => 25, 'notice' => 86400])
->and($settings['season'])->toMatchArray(['crowns' => 5, 'needed' => 3, 'hold' => 7 * 86400])
->and($settings['raids'])->toBeNull()
->and(rule(fn () => app(ListWorlds::class)->settings(999)))->toBe('not_found');
});