JA
apps/winzik / tests / PluginsTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Form\PluginsForm;/** The plug-ins of 2001: Clavier (the keys) and LED (the three lights follow the sound). */final class PluginsTest extends PlayerTestCase{    public function testTheKeysAreThePluginClavier(): void    {        $this->startPlayer();        self::assertSame('clavier', $this->wz->plugin());        $this->form->openPlugins();        $this->game->run(1);        $plugins = $this->game->scene();        self::assertInstanceOf(PluginsForm::class, $plugins);        self::assertSame(['Plugins Clavier', 'Plugins LED'], $plugins->widget('plugins')->props['items']);        $plugins->click($plugins->widget('stop'));        $this->game->run(1);        self::assertSame('PLUGINS : DESACTIVE', $plugins->widget('state')->props['text']);        $plugins->click($plugins->widget('quit'));        $this->game->run(1);        $this->form->type('x');     // (Space and the media keys work always: ShortcutsTest)        self::assertSame('stopped', $this->wz->player->state, 'without the plug-in the keys do nothing');    }    public function testTheFirstStartSaysItReallyKicks(): void    {        $this->startPlayer();        self::assertTrue($this->wz->firstStart, 'no settings file yet');        self::assertFileExists($this->wz->path('assets/sfx/winzik.ogg'));        self::assertFalse($this->wz->greet(), 'said once, at the start');        $this->wz->save();        $this->wz->reset();        self::assertFalse($this->wz->firstStart, 'the next time: the settings are there, no hello');    }    public function testTheLedsGreetThenFollowTheSound(): void    {        $this->startPlayer();        $this->form->openPlugins();        $this->game->run(1);        $plugins = $this->game->scene();        $plugins->widget('plugins')->select(1);        $plugins->click($plugins->widget('start'));        $plugins->click($plugins->widget('quit'));        $this->game->run(1);        self::assertSame('led', $this->wz->plugin());        $this->game->wait(0.3);        self::assertSame([1, 1, 1], $this->wz->leds(), 'the greeting of #1plugs.exe');        $this->game->wait(2);        self::assertSame([0, 0, 0], $this->wz->leds(), 'silence: all off');        $this->wz->setNoise(fn () => 1.0);        $this->wz->setVolume(100);        $this->wz->play();        $this->game->wait(0.5);        self::assertSame([1, 1, 1], $this->wz->leds(), 'loud: all on');        $this->game->shot('leds');        $this->wz->usePlugin('clavier');        self::assertNotNull($this->wz->leds(), 'they blink before going out');        $this->game->wait(4);        self::assertNull($this->wz->leds());        $this->wz->setNoise(fn () => mt_rand() / mt_getrandmax());    }    public function testAnUpdateIsAcceptedOnlyWhenSignedNewerAndForThisComputer(): void    {        $this->startPlayer();        $key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_EC, 'curve_name' => 'prime256v1']);        self::assertNotFalse($key);        $public = (string)openssl_pkey_get_details($key)['key'];        $engine = $this->form->engineOrNull();        self::assertNotNull($engine);        $updater = new \App\Core\AutoUpdate($engine->http(), 'https://example.org/update.json', '0.9.87', 'winzik', $public, __FILE__);        $make = function (string $version, string $id = 'winzik') use ($key): string {            $signed = json_encode(['id' => $id, 'version' => $version, 'notes' => 'n', 'platforms' => [                'linux-x86_64' => ['url' => 'https://example.org/winzik-linux-x86_64', 'sha256' => str_repeat('a', 64), 'size' => 3]]]);            openssl_sign((string)$signed, $sig, $key, OPENSSL_ALGO_SHA256);            return (string)json_encode(['signed' => $signed, 'signature' => base64_encode($sig)]);        };        self::assertSame('https://example.org/winzik-linux-x86_64', $updater->parse($make('0.9.88'), 'linux-x86_64')['url'] ?? null);        self::assertSame('0.9.88', $updater->version);        self::assertNull($updater->parse($make('0.9.87'), 'linux-x86_64'), 'the same version: nothing');        self::assertNull($updater->parse($make('0.9.88'), 'windows-x86_64'), 'not for this system');        foreach ([fn () => $updater->parse($make('1.0', 'other'), 'linux-x86_64'),                  fn () => $updater->parse(str_replace('0.9.88', '0.9.99', $make('0.9.88')), 'linux-x86_64')] as $bad) {            try {                $bad();                self::fail('accepted');            } catch (\RuntimeException) {                self::assertTrue(true);            }        }        self::assertFalse((new \App\Core\AutoUpdate($engine->http(), 'x', '1', 'winzik', $public, null))->enabled(), 'only the executable updates itself');    }    public function testQuitterInTheMenuReallyQuits(): void    {        $this->startPlayer();        $labels = [];        foreach ($this->desk->menuItems() as $item) {            if (($item['label'] ?? '') === 'Quitter') {                $labels[] = 'Quitter';                ($item['run'])();            }        }        self::assertSame(['Quitter'], $labels);        $this->game->wait(1);        self::assertFalse($this->desk->main->visible(), 'tests are headless: WinZik goes to the tray instead of closing the window');    }}