winzik/engine/tests/Runtime/RuntimeMessagesTest.php

95 lines
3.8 KiB
PHP

<?php
namespace JumPHP\Tests\Runtime;
use JumPHP\Bridge\JsBridge;
use JumPHP\Kernel\GameConfig;
use JumPHP\Runtime\ManualRuntime;
use JumPHP\Runtime\RuntimeClient;
use PHPUnit\Framework\TestCase;
/** The runtime's other messages (RuntimeMessages): features from the hello, a request and its answer, a timeout. */
final class RuntimeMessagesTest extends TestCase
{
/** @var list<array<string, mixed>> what the client asked the page */
private array $sent = [];
private JsBridge $bridge;
private function client(): RuntimeClient
{
$sent = &$this->sent;
$this->bridge = new class($sent) extends JsBridge {
/** @param list<array<string, mixed>> $sent */
public function __construct(private array &$sent)
{
}
public function send(string $type, array $data = []): bool
{
$this->sent[] = ['t' => $type] + $data;
return true;
}
};
return new RuntimeClient($this->bridge, new GameConfig(['id' => 'pong', 'title' => 'Pong']), true);
}
private function page(array $event): void
{
$this->bridge->emit('runtime', [$event]);
}
public function testTheHelloSaysWhatTheRuntimeCanDo(): void
{
$c = $this->client();
self::assertFalse($c->supports('notify'));
$c->check();
$this->page(['ev' => 'hello', 'port' => 47820, 'runtime' => '0.3.0', 'os' => 'android', 'apps' => [], 'features' => ['notify']]);
self::assertTrue($c->supports('notify'));
$c->check();
$this->page(['ev' => 'missing']);
self::assertFalse($c->supports('notify'));
}
public function testARequestIsSentToThePageAndSettledByItsReply(): void
{
$c = $this->client();
$task = $c->request(['t' => 'notify-status', 'app' => 'pong'], ['notify-status'], 5.0);
$last = end($this->sent);
self::assertSame('request', $last['op']);
self::assertSame(['t' => 'notify-status', 'app' => 'pong'], $last['message']);
self::assertSame(['notify-status'], $last['expect']);
self::assertSame(5000, $last['timeout']);
self::assertFalse($task->isDone());
$this->page(['ev' => 'reply', 'id' => 999, 'message' => ['t' => 'notify-status']]); // someone else's
self::assertFalse($task->isDone());
$this->page(['ev' => 'reply', 'id' => $last['id'], 'message' => ['t' => 'notify-status', 'app' => 'pong', 'granted' => true, 'subscription' => 'ns_1']]);
self::assertSame('ns_1', $task->result()['subscription']);
}
public function testARequestNobodyAnswersTimesOut(): void
{
$c = $this->client();
$task = $c->request(['t' => 'notify-status', 'app' => 'pong'], ['notify-status'], 1.0);
$c->update(0.6);
self::assertFalse($task->isDone());
$c->update(0.6);
self::assertSame(['t' => 'error', 'code' => 'timeout', 'message' => 'the JumPHP runtime did not answer'], $task->result());
}
public function testOutsideABrowserARequestIsUnsupported(): void
{
$c = new RuntimeClient(new JsBridge(), new GameConfig(['id' => 'pong']), false);
self::assertSame('unsupported', $c->request(['t' => 'x'], ['y'])->result()['code']);
}
public function testTheManualRuntimeAnswersWithItsResponder(): void
{
$m = new ManualRuntime();
self::assertSame('unsupported', $m->request(['t' => 'x'], ['y'])->result()['code']);
$m->features = ['notify'];
$m->responder = fn (array $msg) => ['t' => 'notify-unsubscribed', 'app' => $msg['app']];
self::assertTrue($m->supports('notify'));
self::assertSame('pong', $m->request(['t' => 'notify-unsubscribe', 'app' => 'pong'], ['notify-unsubscribed'])->result()['app']);
self::assertCount(2, $m->requests);
}
}