game-village-politics-backend/tests/Unit/Game/DiplomacyTest.php

55 lines
2.7 KiB
PHP

<?php
use App\Game\Tribes\Diplomacy;
use Carbon\CarbonImmutable;
$now = CarbonImmutable::parse('2026-11-01 12:00:00');
$row = fn (int $from, int $to, string $state, bool $confirmed = true, ?string $endsAt = null) => [
'tribe_id' => $from, 'other_tribe_id' => $to, 'state' => $state, 'confirmed' => $confirmed, 'ends_at' => $endsAt ? CarbonImmutable::parse($endsAt) : null,
];
it('needs both tribes to confirm an alliance or a NAP', function () use ($now, $row) {
expect(Diplomacy::relation(1, 2, [$row(1, 2, 'ally')], $now))->toBe('none')
->and(Diplomacy::relation(1, 2, [$row(1, 2, 'ally'), $row(2, 1, 'ally', false)], $now))->toBe('none')
->and(Diplomacy::relation(1, 2, [$row(1, 2, 'ally'), $row(2, 1, 'ally')], $now))->toBe('ally')
->and(Diplomacy::relation(2, 1, [$row(1, 2, 'nap'), $row(2, 1, 'nap')], $now))->toBe('nap')
->and(Diplomacy::relation(1, 2, [$row(1, 2, 'ally'), $row(2, 1, 'nap')], $now))->toBe('none');
});
it('keeps a pact until its notice ends', function () use ($now, $row) {
$ending = fn (string $at) => [$row(1, 2, 'ally', true, $at), $row(2, 1, 'ally', true, $at)];
expect(Diplomacy::relation(1, 2, $ending('2026-11-01 12:00:01'), $now))->toBe('ally')
->and(Diplomacy::relation(1, 2, $ending('2026-11-01 12:00:00'), $now))->toBe('none');
});
it('lets one side declare enmity', function () use ($now, $row) {
expect(Diplomacy::relation(1, 2, [$row(2, 1, 'enemy')], $now))->toBe('enemy')
->and(Diplomacy::relation(2, 1, [$row(2, 1, 'enemy')], $now))->toBe('enemy');
});
it('knows the own tribe and players without one', function () use ($now) {
expect(Diplomacy::relation(3, 3, [], $now))->toBe('tribe')
->and(Diplomacy::relation(null, 3, [], $now))->toBe('none')
->and(Diplomacy::protects('tribe'))->toBeTrue()
->and(Diplomacy::protects('nap'))->toBeTrue()
->and(Diplomacy::protects('enemy'))->toBeFalse()
->and(Diplomacy::protects('none'))->toBeFalse();
});
it('takes a day of notice at world speed', function () {
expect((new Diplomacy(gameConfig()))->noticeSeconds())->toBe(86400)
->and((new Diplomacy(gameConfig(['world' => ['speed' => 100]])))->noticeSeconds())->toBe(864);
});
it('checks names and tags', function () {
expect(Diplomacy::validName('Dorfrat'))->toBeTrue()
->and(Diplomacy::validName('Die Rote Hand'))->toBeTrue()
->and(Diplomacy::validName('ab'))->toBeFalse()
->and(Diplomacy::validName(' padded '))->toBeFalse()
->and(Diplomacy::validTag('DRF'))->toBeTrue()
->and(Diplomacy::validTag('X'))->toBeFalse()
->and(Diplomacy::validTag('TOOLONG'))->toBeFalse()
->and(Diplomacy::validTag('a b'))->toBeFalse();
});