PL
apps/winzik / tests / CloudUiTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Cloud\Cloud;use App\Cloud\CloudAccount;use App\Core\WinZik;use App\Form\CloudForm;use App\Form\MainForm;use JumPHP\Gui\Form;use Tests\Cloud\FakeCloudServer;use Tests\Cloud\FakeRelay;/** * The cloud in WinZik's windows of 2001: the account window (Créer un compte, the recovery code, Mes appareils), the menu * of the play list (Ma playlist / Playlist cloud, Mettre dans le cloud), the marks of the lines (☁, % while sent). * The server and the relay are fakes (tests/Cloud); pictures var/screenshots/tests/cloud-*.png. */final class CloudUiTest extends PlayerTestCase{    private FakeCloudServer $server;    private FakeRelay $relay;    private Cloud $cloud;    private function startWithCloud(array $params = []): void    {        $this->startPlayer($params);        $this->server = new FakeCloudServer();        $this->relay = new FakeRelay();        $this->cloud = $this->cloudFor($this->wz, 'pc', 'desktop');        $this->wz->useCloud($this->cloud);    }    private function cloudFor(WinZik $wz, string $name, string $kind): Cloud    {        $dir = $this->wz->storagePath('cloud-test/' . $name);        $account = new CloudAccount(fn (string $m, string $u, string|array|null $b, array $h) => $this->server->request($m, $u, $b, $h),            FakeCloudServer::BASE, "$dir/cloud.json", $kind);        return new Cloud($wz, $account, "$dir/cloud", fn () => $this->relay->session(), null, "$dir/received");    }    private function top(): Form    {        $scene = $this->game->scene();        self::assertInstanceOf(Form::class, $scene);        return $scene;    }    /** A song of the test in the user's list (a copy of the demo song, some bytes of its own). */    private function song(string $name, int $extra = 0): string    {        $file = $this->wz->storagePath("music/$name.mp3");        @mkdir(dirname($file), 0777, true);        copy($this->wz->path('assets/music/winzik.mp3'), $file);        if ($extra > 0) {            file_put_contents($file, random_bytes($extra), FILE_APPEND);        }        return $file;    }    /** The right click on a line of the play list (its number in what is shown). */    private function rightClickLine(int $row): void    {        $pl = $this->desk->playlist;        [$x, $y] = $this->at($pl, 100, \App\Gui\PlaylistWindow::LIST_TOP + $row * \App\Gui\PlaylistWindow::ROW_HEIGHT + 6);        $this->rightClick($x, $y);        self::assertTrue($this->desk->hasMenu());    }    private function clickMenu(string $label): void    {        $menu = $this->desk->menu();        self::assertNotNull($menu);        $at = $menu->child?->rowCenter($label) ?? $menu->rowCenter($label);      // a submenu open beside it first        self::assertNotNull($at, "« $label » is in the menu: " . implode(', ', $this->desk->menuLabels()));        $this->form->clickAt($this->desk->x + $at[0] * $this->desk->scale, $this->desk->y + $at[1] * $this->desk->scale);        $this->game->run(1);    }    public function testTheAccountWindowCreatesTheAccountAndShowsTheRecoveryCodeOnce(): void    {        $this->startWithCloud();        $this->rightClickLine(0);        $this->clickMenu('Mon compte cloud…');        $form = $this->top();        self::assertInstanceOf(CloudForm::class, $form);        self::assertSame('login', $form->tab);        self::assertSame('Ordinateur', $form->widget('deviceName')->props['text'], 'nothing personal by default');        $form->click($form->widget('tabSignup'));        $this->game->run(1);        $form->click($form->widget('pseudo'));        $form->type('ada_78');        $form->click($form->widget('password'));        $form->type('motdepasse1');        $this->game->shot('cloud-signup');        $form->click($form->widget('enter'));        $this->game->run(2);        self::assertSame('code', $form->tab);        self::assertSame('ABCD-EFGH-IJKL-MNOP-QRST', $form->widget('code')->props['text']);        self::assertTrue($this->cloud->signedIn());        $this->game->shot('cloud-recovery-code');        $form->click($form->widget('noted'));        $this->game->run(2);        self::assertSame('devices', $form->tab);        self::assertSame('', $form->widget('code')->props['text'], 'the code is gone from the window');        self::assertStringContainsString('Ordinateur', (string)($form->widget('devices')->props['items'][0] ?? ''));        $this->game->shot('cloud-devices');        // Déconnexion        $form->click($form->widget('signOut'));        $this->game->run(1);        self::assertFalse($this->cloud->signedIn());        self::assertSame('login', $form->tab);        $form->click($form->widget('quit'));        $this->game->run(1);        self::assertInstanceOf(MainForm::class, $this->game->scene());    }    public function testAWrongPasswordSaysSoWithoutSayingWhichOneIsWrong(): void    {        $this->startWithCloud();        $this->server->accounts['ada'] = ['password' => 'motdepasse1', 'recovery' => 'X', 'version' => 0, 'entries' => [], 'lyrics' => []];        $this->form->openCloud();        $this->game->run(1);        $form = $this->top();        $form->click($form->widget('pseudo'));        $form->type('ada');        $form->click($form->widget('password'));        $form->type("faux\n");        $this->game->run(2);        self::assertSame('Pseudo ou mot de passe incorrect.', $form->widget('status')->props['text']);        self::assertFalse($this->cloud->signedIn());    }    public function testTheMenuOfThePlaylistPutsSongsInTheCloudAndSwitchesToTheCloudPlaylist(): void    {        $this->startWithCloud();        $this->server->accounts['ada'] = ['password' => 'motdepasse1', 'recovery' => 'X', 'version' => 0, 'entries' => [], 'lyrics' => []];        $this->cloud->signIn('ada', 'motdepasse1');        $a = $this->song('alpha', 3 << 20);        $b = $this->song('beta', 10);        $this->wz->addFiles([$a, $b]);        $mine = $this->wz->playlist->items;        $first = count($mine) - 2;        $this->game->run(2);        // a line: Mettre dans le cloud > Le PC envoie le MP3        $this->rightClickLine($first);        self::assertContains('Playlist : Ma playlist', $this->desk->menuLabels());        self::assertContains('Playlist : Playlist cloud (mon compte)', $this->desk->menuLabels());        $this->clickMenu('Mettre dans le cloud');        self::assertContains('Le mobile la récupère sur YouTube', $this->desk->menuLabels());        $this->clickMenu('Le PC envoie le MP3');        // the other line: the phone gets it from YouTube        $this->rightClickLine($first + 1);        $this->clickMenu('Mettre dans le cloud');        $this->clickMenu('Le mobile la récupère sur YouTube');        for ($i = 0; $i < 300 && count($this->server->accounts['ada']['entries']) < 2; $i++) {            $this->game->run(1);        }        $entries = $this->server->accounts['ada']['entries'];        self::assertSame(['pc', 'youtube'], array_column(array_column($entries, 'source'), 'type'));        self::assertSame('WinZik Band WinZik', $entries[1]['source']['query'], 'the search of the phone: artist + title of the tag');        self::assertTrue($this->cloud->fileState($a)['cloud']);        // a phone of the account copies the first one: the line shows the % while the PC sends it        $phoneWz = new WinZik();        $phone = $this->cloudFor($phoneWz, 'phone', 'phone');        $phone->wifiOnly = false;        $phone->signIn('ada', 'motdepasse1');        $sending = false;        for ($i = 0; $i < 600 && !$sending; $i++) {            $phone->tick(0.05);            $this->game->run(1);            $sending = ($this->cloud->fileState($a)['sending'] ?? 0) > 0.2;        }        self::assertTrue($sending, 'the PC sends it');        $this->game->shot('cloud-marks');        for ($i = 0; $i < 600 && $phone->rowState((string)$entries[0]['id'])['state'] !== 'here'; $i++) {            $phone->tick(0.05);            $this->game->run(1);        }        self::assertSame('here', $phone->rowState((string)$entries[0]['id'])['state'], $phone->rowState((string)$entries[0]['id'])['text']);        // Playlist : Playlist cloud (mon compte): the cloud's songs, the user's own list kept        $this->rightClickLine(0);        $this->clickMenu('Playlist : Playlist cloud (mon compte)');        self::assertSame('cloud', $this->cloud->view());        self::assertSame([$this->wz->relative($a), $this->wz->relative($b)], $this->wz->playlist->items);        $this->game->run(2);        $this->game->shot('cloud-playlist');        $this->wz->save();        $saved = json_decode((string)file_get_contents($this->wz->storagePath('winzik.json')), true);        self::assertSame($mine, $saved['playlist'], 'what WinZik keeps is the user\'s own list');        $this->rightClickLine(0);        $this->clickMenu('Playlist : Ma playlist');        self::assertSame($mine, $this->wz->playlist->items);        $phone->stop();    }    public function testTheAccountWindowAndTheMenuTurnTheSyncedPlaybackOnAndOff(): void    {        $this->startWithCloud();        $this->server->accounts['ada'] = ['password' => 'motdepasse1', 'recovery' => 'X', 'version' => 0, 'entries' => [], 'lyrics' => []];        $this->cloud->signIn('ada', 'motdepasse1');        $phone = $this->cloudFor(new WinZik(), 'phone', 'phone');        $phone->signIn('ada', 'motdepasse1', 'Mon téléphone');        for ($i = 0; $i < 300 && ($this->cloud->syncCount() !== 2 || $phone->syncCount() !== 2); $i++) {            $phone->tick(1 / 60);            $this->game->run(1);        }        self::assertSame(2, $this->cloud->syncCount());        // the menu of the play list: checked by default, the indicator under it        $this->rightClickLine(0);        self::assertContains('Lecture synchronisée avec mes appareils', $this->desk->menuLabels());        self::assertContains('Synchro · 2 appareils', $this->desk->menuLabels());        $this->game->shot('cloud-sync-menu');        $this->clickMenu('Lecture synchronisée avec mes appareils');        self::assertFalse($this->cloud->playSynced());        self::assertSame('Lecture indépendante : cet appareil joue seul', $this->wz->notice);        // the account window: the box, the line that says how it is        $this->form->openCloud();        $this->game->run(2);        $form = $this->top();        self::assertInstanceOf(CloudForm::class, $form);        self::assertFalse($form->widget('playSync')->props['checked']);        self::assertStringStartsWith('Indépendant', (string)$form->widget('syncState')->props['text']);        $form->click($form->widget('playSync'));        for ($i = 0; $i < 10; $i++) {            $phone->tick(1 / 60);            $this->game->run(1);        }        self::assertTrue($this->cloud->playSynced());        self::assertSame('Synchronisé · 2 appareils', (string)$form->widget('syncState')->props['text']);        $this->game->shot('cloud-sync-window');        $phone->stop();    }    public function testTheOriginalPhoneInterfaceHasTheSameCloudMenu(): void    {        $this->startWithCloud(['phone' => true]);        $this->server->accounts['ada'] = ['password' => 'motdepasse1', 'recovery' => 'X', 'version' => 0, 'entries' => [], 'lyrics' => []];        $this->cloud->signIn('ada', 'motdepasse1');        $this->game->wait(1);        $pl = $this->desk->playlist;        [$x, $y] = $this->at($pl, 100, \App\Gui\PlaylistWindow::LIST_TOP + 6);        $this->desk->openMenu($x, $y);      // what a long press does        $this->game->run(1);        self::assertContains('Playlist : Playlist cloud (mon compte)', $this->desk->menuLabels());        self::assertContains('Mettre dans le cloud', $this->desk->menuLabels());        self::assertContains('Mon compte cloud (ada)…', $this->desk->menuLabels());        $this->game->shot('cloud-original-phone-menu');    }}