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

76 lines
3.3 KiB
PHP
Raw Normal View History

<?php
use App\Game\ConfigLint;
use App\Game\Quests\Facts;
use App\Game\Quests\QuestBook;
function book(): QuestBook
{
return new QuestBook(gameConfig());
}
function line(array $progress, string $line): array
{
return collect($progress)->firstWhere('line', $line);
}
it('starts every line at its first quest', function () {
$progress = book()->progress([], new Facts(['headquarters' => 1]));
expect(array_column($progress, 'line'))->toBe(['construction', 'army', 'village'])
->and(line($progress, 'construction')['quest'])->toMatchArray(['key' => 'hq_2', 'complete' => false])
->and(line($progress, 'construction')['quest']['goals'][0])
->toBe(['kind' => 'building', 'subject' => 'headquarters', 'have' => 1, 'need' => 2, 'met' => false, 'hint' => 'headquarters']);
});
it('completes a quest when every goal is met, and moves on once it is claimed', function () {
$facts = new Facts(['headquarters' => 2, 'storage' => 2, 'farm' => 1]);
expect(book()->claimable('hq_2', [], $facts))->toBeTrue()
->and(book()->claimable('storage_farm_2', [], $facts))->toBeFalse();
$line = line(book()->progress(['hq_2'], $facts), 'construction');
expect($line)->toMatchArray(['done' => 1, 'total' => 8])
->and($line['quest']['key'])->toBe('storage_farm_2')
->and(array_column($line['quest']['goals'], 'met'))->toBe([true, false]);
});
it('measures counters and instances, capping progress at the goal', function () {
$facts = new Facts([], ['timber_camp' => 1], ['recruited.spear' => 9, 'village.renamed' => 1]);
$army = line(book()->progress(['barracks'], $facts), 'army');
$village = line(book()->progress(['rename', 'priority', 'look_around', 'hiding_place'], $facts), 'village');
expect($army['quest']['goals'][0])->toMatchArray(['have' => 5, 'need' => 5, 'met' => true, 'hint' => 'barracks'])
->and($village['quest']['goals'][0])->toMatchArray(['kind' => 'instances', 'have' => 1, 'need' => 2, 'met' => false]);
});
it('only claims the current quest of a line', function () {
$facts = new Facts(['headquarters' => 3]);
expect(book()->claimable('hq_3', [], $facts))->toBeFalse()
->and(book()->claimableCount([], $facts))->toBe(1);
});
it('ends a line once every quest is claimed', function () {
$all = array_keys(gameConfig()->get('quests.lines.army'));
expect(line(book()->progress($all, new Facts), 'army'))->toMatchArray(['done' => 8, 'quest' => null]);
});
it('pays back a share of a level cost', function () {
expect(book()->levelReward(['wood' => 113, 'clay' => 100, 'iron' => 88]))->toBe(['wood' => 23, 'clay' => 20, 'iron' => 18]);
});
it('reports quests that can never be completed', function () {
$config = gameConfig(['quests' => ['lines' => ['army' => ['wall_3' => ['goals' => [
['building', 'wall', 99], ['counter', 'cats.petted', 1, 'village'], ['instances', 'headquarters', 2], ['building', 'castle', 1],
]]]]]]);
expect(ConfigLint::check($config))->toContain(
'Quest [army.wall_3] needs [wall] level 99, above its maximum.',
'Quest [army.wall_3] counts [cats.petted], which no action bumps.',
'Quest [army.wall_3] needs 2 [headquarters] in one village, which never fit.',
'Quest [army.wall_3] needs unknown building [castle].',
);
});