65 lines
3.2 KiB
PHP
65 lines
3.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use App\Actions\Chat;
|
||
|
|
use App\Actions\Tribes;
|
||
|
|
use Carbon\CarbonImmutable;
|
||
|
|
|
||
|
|
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
|
||
|
|
|
||
|
|
function chat(): Chat
|
||
|
|
{
|
||
|
|
return app(Chat::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
it('talks on the world channel and counts what is unread', function () {
|
||
|
|
$a = startVillage();
|
||
|
|
$b = startVillage();
|
||
|
|
$sent = chat()->send(owner($a), $a->world_id, 'world', ' Hello, neighbours! ');
|
||
|
|
expect($sent)->toMatchArray(['body' => 'Hello, neighbours!', 'player' => ['id' => $a->player_id, 'name' => $a->player->name]]);
|
||
|
|
|
||
|
|
expect(chat()->channels(owner($b), $b->world_id))->toBe([['channel' => 'world', 'with' => null, 'unread' => 1]])
|
||
|
|
->and(chat()->channels(owner($a), $a->world_id)[0]['unread'])->toBe(0);
|
||
|
|
$history = chat()->history(owner($b), $b->world_id, 'world');
|
||
|
|
expect($history['messages'])->toHaveCount(1)->and($history['more'])->toBeFalse()
|
||
|
|
->and(chat()->channels(owner($b), $b->world_id)[0]['unread'])->toBe(0);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('keeps tribe talk inside the tribe', function () {
|
||
|
|
$a = startVillage();
|
||
|
|
$b = startVillage();
|
||
|
|
expect(rule(fn () => chat()->send(owner($a), $a->world_id, 'tribe', 'Hi')))->toBe('no_tribe');
|
||
|
|
|
||
|
|
app(Tribes::class)->found(owner($a), $a->world_id, 'Dorfrat', 'DRF');
|
||
|
|
chat()->send(owner($a), $a->world_id, 'tribe', 'Council at dawn');
|
||
|
|
expect(collect(chat()->channels(owner($a), $a->world_id))->pluck('channel')->all())->toBe(['world', 'tribe'])
|
||
|
|
->and(chat()->history(owner($a), $a->world_id, 'tribe')['messages'][0]['body'])->toBe('Council at dawn')
|
||
|
|
->and(rule(fn () => chat()->history(owner($b), $b->world_id, 'tribe')))->toBe('no_tribe');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('opens a conversation between two players by name', function () {
|
||
|
|
$a = startVillage();
|
||
|
|
$b = startVillage();
|
||
|
|
$c = startVillage();
|
||
|
|
$open = chat()->open(owner($a), $a->world_id, $b->player->name);
|
||
|
|
expect($open['channel'])->toBe("dm:{$b->player_id}");
|
||
|
|
|
||
|
|
chat()->send(owner($a), $a->world_id, $open['channel'], 'Truce?');
|
||
|
|
$theirs = collect(chat()->channels(owner($b), $b->world_id))->firstWhere('channel', "dm:{$a->player_id}");
|
||
|
|
expect($theirs)->toBe(['channel' => "dm:{$a->player_id}", 'with' => ['id' => $a->player_id, 'name' => $a->player->name], 'unread' => 1])
|
||
|
|
->and(chat()->history(owner($b), $b->world_id, "dm:{$a->player_id}")['messages'][0]['body'])->toBe('Truce?')
|
||
|
|
->and(collect(chat()->channels(owner($c), $c->world_id))->pluck('channel')->all())->toBe(['world'])
|
||
|
|
->and(rule(fn () => chat()->open(owner($a), $a->world_id, 'Nobody')))->toBe('no_player');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('refuses empty, long and too quick messages', function () {
|
||
|
|
$a = startVillage();
|
||
|
|
expect(rule(fn () => chat()->send(owner($a), $a->world_id, 'world', ' ')))->toBe('chat_length')
|
||
|
|
->and(rule(fn () => chat()->send(owner($a), $a->world_id, 'world', str_repeat('a', 501))))->toBe('chat_length');
|
||
|
|
|
||
|
|
chat()->send(owner($a), $a->world_id, 'world', 'one');
|
||
|
|
expect(rule(fn () => chat()->send(owner($a), $a->world_id, 'world', 'two')))->toBe('chat_slow');
|
||
|
|
$this->travel(2)->seconds();
|
||
|
|
chat()->send(owner($a), $a->world_id, 'world', 'two');
|
||
|
|
expect(chat()->history(owner($a), $a->world_id, 'world')['messages'])->toHaveCount(2);
|
||
|
|
});
|