Some checks failed
extensions-split / split (push) Failing after 6s
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
245 lines
10 KiB
PHP
245 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use JumPHP\Bridge\JsBridge;
|
|
use JumPHP\Kernel\GameConfig;
|
|
use JumPHP\Net\RuntimeLink;
|
|
use JumPHP\Runtime\RuntimeClient;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** The client's state machine, fed with the page's 'runtime' events (no browser, no window). */
|
|
final class RuntimeClientTest extends TestCase
|
|
{
|
|
private JsBridge $bridge;
|
|
|
|
private function client(): RuntimeClient
|
|
{
|
|
$this->bridge = new JsBridge();
|
|
return new RuntimeClient($this->bridge, new GameConfig(['id' => 'sql', 'title' => 'SQL']), true);
|
|
}
|
|
|
|
private function page(array $event): void
|
|
{
|
|
$this->bridge->emit('runtime', [$event]);
|
|
}
|
|
|
|
public function testItStartsUnknownAndCheckingMovesItOn(): void
|
|
{
|
|
$c = $this->client();
|
|
self::assertSame(RuntimeLink::UNKNOWN, $c->state());
|
|
$changes = [];
|
|
$c->on('change', function (string $s) use (&$changes): void { $changes[] = $s; });
|
|
$task = $c->check();
|
|
self::assertSame(RuntimeLink::CHECKING, $c->state());
|
|
$this->page(['ev' => 'hello', 'port' => 47822, 'runtime' => '0.1.0', 'os' => 'linux', 'granted' => false, 'apps' => []]);
|
|
self::assertSame(RuntimeLink::RUNNING, $c->state());
|
|
self::assertSame(RuntimeLink::RUNNING, $task->result());
|
|
self::assertSame('ws://127.0.0.1:47822/v1', $c->bridgeUrl());
|
|
self::assertSame('0.1.0', $c->version());
|
|
self::assertSame('linux', $c->os());
|
|
self::assertSame([RuntimeLink::CHECKING, RuntimeLink::RUNNING], $changes);
|
|
}
|
|
|
|
public function testABlockedBrowserIsToldApartFromAMissingRuntime(): void
|
|
{
|
|
$c = $this->client();
|
|
$c->check();
|
|
$this->page(['ev' => 'blocked', 'why' => 'no']);
|
|
self::assertSame(RuntimeLink::UNSUPPORTED, $c->state());
|
|
self::assertTrue($c->isBlocked());
|
|
$c->check();
|
|
$this->page(['ev' => 'missing']);
|
|
self::assertSame(RuntimeLink::MISSING, $c->state());
|
|
self::assertFalse($c->isBlocked());
|
|
}
|
|
|
|
public function testAutoConnectLooksAgainWhenMissingAndAsksOnceWhenFound(): void
|
|
{
|
|
$this->bridge = new class extends JsBridge {
|
|
/** @var list<string> */
|
|
public array $ops = [];
|
|
public function send(string $type, array $data = []): bool
|
|
{
|
|
$this->ops[] = $data['op'] ?? $type;
|
|
return true;
|
|
}
|
|
};
|
|
$c = new RuntimeClient($this->bridge, new GameConfig(['id' => 'sql', 'title' => 'SQL']), true);
|
|
$c->autoConnect();
|
|
$this->page(['ev' => 'missing']);
|
|
$c->update(9.0); // looks again by itself
|
|
self::assertSame(['probe', 'probe'], $this->bridge->ops);
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => []]);
|
|
self::assertSame(['probe', 'probe', 'pair'], $this->bridge->ops, 'found but not allowed: asks once');
|
|
$this->page(['ev' => 'paired', 'granted' => false]);
|
|
self::assertSame(RuntimeLink::REFUSED, $c->state());
|
|
$c->check();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => []]);
|
|
self::assertSame(RuntimeLink::REFUSED, $c->state(), 'a refusal is respected');
|
|
self::assertNotContains('pair', array_slice($this->bridge->ops, 3));
|
|
}
|
|
|
|
public function testAPermissionQuestionInTheBrowserIsNotMissing(): void
|
|
{
|
|
$c = $this->client();
|
|
$c->check();
|
|
$c->update(5.0);
|
|
$this->page(['ev' => 'pending']);
|
|
$c->update(30.0);
|
|
self::assertSame(RuntimeLink::CHECKING, $c->state(), 'the probe waits for the answer of the user');
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => []]);
|
|
self::assertSame(RuntimeLink::RUNNING, $c->state());
|
|
$c->check();
|
|
$this->page(['ev' => 'pending']);
|
|
$c->update(70.0);
|
|
self::assertSame(RuntimeLink::MISSING, $c->state(), 'but not forever');
|
|
}
|
|
|
|
public function testAnAuthorisedApplicationIsReady(): void
|
|
{
|
|
$c = $this->client();
|
|
$c->check();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'granted' => true, 'apps' => ['other', 'sql']]);
|
|
self::assertSame(RuntimeLink::READY, $c->state());
|
|
}
|
|
|
|
public function testNothingListeningMeansMissing(): void
|
|
{
|
|
$c = $this->client();
|
|
$task = $c->check();
|
|
$this->page(['ev' => 'missing']);
|
|
self::assertSame(RuntimeLink::MISSING, $c->state());
|
|
self::assertSame(RuntimeLink::MISSING, $task->result());
|
|
}
|
|
|
|
public function testASilentPageMeansMissingAfterTheTimeout(): void
|
|
{
|
|
$c = $this->client();
|
|
$c->check();
|
|
$c->update(3.0);
|
|
self::assertSame(RuntimeLink::CHECKING, $c->state());
|
|
$c->update(4.0);
|
|
self::assertSame(RuntimeLink::MISSING, $c->state());
|
|
}
|
|
|
|
public function testABrowserThatBlocksIsUnsupported(): void
|
|
{
|
|
$c = $this->client();
|
|
$c->check();
|
|
$this->page(['ev' => 'blocked', 'why' => 'mixed content']);
|
|
self::assertSame(RuntimeLink::UNSUPPORTED, $c->state());
|
|
}
|
|
|
|
public function testPairingAsksTheRuntimeAndFollowsTheAnswer(): void
|
|
{
|
|
$c = $this->client();
|
|
self::assertFalse($c->pair('sql', 'SQL')->result(), 'not running: nothing to ask');
|
|
$c->check();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => []]);
|
|
$answer = null;
|
|
$c->pair('sql', 'SQL')->then(function ($granted) use (&$answer): void { $answer = $granted; });
|
|
$this->page(['ev' => 'paired', 'app' => 'sql', 'granted' => true, 'always' => true]);
|
|
self::assertTrue($answer);
|
|
self::assertSame(RuntimeLink::READY, $c->state());
|
|
|
|
$c = $this->client();
|
|
$c->check();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => []]);
|
|
$c->pair('sql', 'SQL');
|
|
$this->page(['ev' => 'paired', 'app' => 'sql', 'granted' => false]);
|
|
self::assertSame(RuntimeLink::REFUSED, $c->state());
|
|
// looking again does not hide the refusal: the user must ask again
|
|
$c->check();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => []]);
|
|
self::assertSame(RuntimeLink::REFUSED, $c->state());
|
|
}
|
|
|
|
public function testItAsksThePageToProbeOnce(): void
|
|
{
|
|
$c = $this->client();
|
|
$c->check();
|
|
$c->check();
|
|
self::assertSame(RuntimeLink::CHECKING, $c->state());
|
|
}
|
|
|
|
public function testNotInABrowserItIsAlwaysUnsupported(): void
|
|
{
|
|
$c = new RuntimeClient(new JsBridge(), new GameConfig(), false);
|
|
self::assertSame(RuntimeLink::UNSUPPORTED, $c->state());
|
|
self::assertSame(RuntimeLink::UNSUPPORTED, $c->check()->result());
|
|
self::assertFalse($c->pair('a', 'A')->result());
|
|
}
|
|
|
|
public function testItKeepsLookingEvenWhenReadyAndAfterAFailure(): void
|
|
{
|
|
$this->bridge = new class extends JsBridge {
|
|
/** @var list<string> */
|
|
public array $ops = [];
|
|
public function send(string $type, array $data = []): bool
|
|
{
|
|
$this->ops[] = $data['op'] ?? $type;
|
|
return true;
|
|
}
|
|
};
|
|
$c = new RuntimeClient($this->bridge, new GameConfig(['id' => 'sql', 'title' => 'SQL']), true);
|
|
$c->autoConnect();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => ['sql']]);
|
|
self::assertSame(RuntimeLink::READY, $c->state());
|
|
$c->update(10.0);
|
|
self::assertSame(['probe'], $this->bridge->ops, 'calm while ready: not yet');
|
|
$c->update(11.0);
|
|
self::assertSame(['probe', 'probe'], $this->bridge->ops, 'looks again every 20 seconds even when ready');
|
|
$this->page(['ev' => 'missing']);
|
|
self::assertSame(RuntimeLink::MISSING, $c->state(), 'the runtime stopped: the badge follows');
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => ['sql']]);
|
|
$c->reportFailure();
|
|
self::assertSame(['probe', 'probe', 'probe'], $this->bridge->ops, 'a failed connection makes it look at once');
|
|
}
|
|
|
|
public function testTheUsersChoiceOfTheProxyIsHonouredByTheRoute(): void
|
|
{
|
|
$c = $this->client();
|
|
$c->check();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => ['sql']]);
|
|
self::assertSame('ws://127.0.0.1:47820/v1', \JumPHP\Net\BridgeRoute::resolve('auto', $c, 'wss://server/tcp'));
|
|
$c->setUseServer(true);
|
|
self::assertSame('wss://server/tcp', \JumPHP\Net\BridgeRoute::resolve('auto', $c, 'wss://server/tcp'));
|
|
self::assertSame('ws://127.0.0.1:47820/v1', \JumPHP\Net\BridgeRoute::resolve('local', $c, 'wss://server/tcp'), 'local mode means local');
|
|
}
|
|
|
|
public function testAnAndroidAppThatDoesNotAnswerIsWokenFromTheServer(): void
|
|
{
|
|
$c = $this->client();
|
|
$woken = [];
|
|
$c->setWaker(function (string $token) use (&$woken): void { $woken[] = $token; });
|
|
$learned = [];
|
|
$c->on('push', function (string $t) use (&$learned): void { $learned[] = $t; });
|
|
$this->page(['ev' => 'env', 'os' => 'android']);
|
|
$c->check();
|
|
$this->page(['ev' => 'hello', 'port' => 47820, 'apps' => ['sql'], 'push' => 'TOKEN-1']);
|
|
self::assertSame(['TOKEN-1'], $learned, 'the app gives its token in its hello');
|
|
self::assertSame([], $woken, 'it answers: nothing to wake');
|
|
$c->check();
|
|
$this->page(['ev' => 'missing']);
|
|
self::assertSame(['TOKEN-1'], $woken, 'it does not answer any more: the server is asked to wake it');
|
|
$c->check();
|
|
$this->page(['ev' => 'missing']);
|
|
self::assertCount(1, $woken, 'not again within 90 seconds');
|
|
$c->update(95.0);
|
|
$c->check();
|
|
$this->page(['ev' => 'missing']);
|
|
self::assertCount(2, $woken);
|
|
}
|
|
|
|
public function testWithoutAPhoneTokenNothingIsAsked(): void
|
|
{
|
|
$c = $this->client();
|
|
$woken = 0;
|
|
$c->setWaker(function () use (&$woken): void { $woken++; });
|
|
$this->page(['ev' => 'env', 'os' => 'android']);
|
|
$c->check();
|
|
$this->page(['ev' => 'missing']);
|
|
self::assertSame(0, $woken);
|
|
}
|
|
}
|