<?phpnamespace Tests;use App\Cloud\Cloud;use App\Cloud\CloudAccount;use App\Core\WinZik;use App\Form\MobileForm;use App\Mobile\MobileView;use JumPHP\Gui\Pointer;use JumPHP\Testing\GameTestCase;use Tests\Cloud\FakeCloudServer;use Tests\Cloud\FakeRelay;/** * The cloud on the phone (390 x 844 dp): Plus > Mon compte (cloud) (the account made, its recovery code shown once, the * devices, sign in, sign out), the Playlist tab's switch « Ce téléphone | Cloud », each song of the cloud playlist with its * state, a song copied from the PC, a song put in the cloud from the phone, the options of the copies. * The server and the relay are the fakes of tests/Cloud; the PC is a second WinZik of its own. * Pictures: var/screenshots/tests/mobile-cloud-*.png. */final class MobileCloudTest extends GameTestCase{ private MobileForm $form; private MobileView $view; private WinZik $wz; private FakeCloudServer $server; private FakeRelay $relay; private Cloud $cloud; private string $tmp; /** @var list<Cloud> the other devices, ticked with each frame */ private array $others = []; protected function tearDown(): void { foreach ($this->others as $c) { $c->stop(); } if (isset($this->tmp)) { exec('rm -rf ' . escapeshellarg($this->tmp)); } parent::tearDown(); } private function startPhone(): void { $this->game->start(MobileForm::class, ['fresh' => true, 'phone' => true]); $form = $this->game->scene(); self::assertInstanceOf(MobileForm::class, $form); $this->form = $form; $view = $form->widget('view'); self::assertInstanceOf(MobileView::class, $view); $this->view = $view; $this->wz = $form->winzik(); $this->wz->playlist->shuffle = false; $this->tmp = sys_get_temp_dir() . '/winzik-mcloud-' . bin2hex(random_bytes(4)); @mkdir($this->tmp, 0777, true); $this->server = new FakeCloudServer(); $this->relay = new FakeRelay(); $this->cloud = $this->device($this->wz, 'phone', 'phone'); $this->wz->useCloud($this->cloud); $this->wz->notice = ''; $this->game->run(3); } private function device(WinZik $wz, string $name, string $kind): Cloud { $account = new CloudAccount(fn (string $m, string $u, string|array|null $b, array $h) => $this->server->request($m, $u, $b, $h), FakeCloudServer::BASE, "$this->tmp/$name/cloud.json", $kind); return new Cloud($wz, $account, "$this->tmp/$name/cloud", fn () => $this->relay->session(), null, "$this->tmp/$name/received"); } /** The PC of the account (another WinZik), signed in, ticked with the phone's frames. */ private function pc(): Cloud { $pc = $this->device(new WinZik(), 'pc', 'desktop'); $pc->signIn('ada', 'motdepasse1'); $this->others[] = $pc; return $pc; } private function account(): void { $this->server->accounts['ada'] = ['password' => 'motdepasse1', 'recovery' => 'X', 'version' => 0, 'entries' => [], 'lyrics' => []]; } /** Frames of the phone (and of the other devices) until $until says yes. */ private function frames(\Closure $until, int $max = 1500): void { for ($i = 0; $i < $max; $i++) { foreach ($this->others as $c) { $c->tick(1 / 60); } $this->game->run(1); if ($until()) { return; } } self::fail('not reached after ' . $max . ' frames'); } private function tap(string $id, mixed $data = null, int $frames = 2): void { $at = $this->view->at($id, $data); self::assertNotNull($at, "$id " . json_encode($data) . ' is on the screen (' . implode(', ', $this->view->touchable()) . ')'); $this->form->clickAt($at[0], $at[1]); $this->game->run($frames); } private function tapScrolled(string $id, mixed $data = null, int $frames = 2): void { $key = 'page:' . $this->view->screen; for ($i = 0; $i < 40 && $this->view->at($id, $data) === null; $i++) { $this->view->scroll[$key] = ($this->view->scroll[$key] ?? 0.0) + 120; $this->game->run(1); } $this->tap($id, $data, $frames); } /** A field of the page tapped, emptied, typed in. */ private function typeIn(string $field, string $text): void { $this->tapScrolled('field', $field); $this->view->fields[$field] = ''; $this->form->type($text); $this->game->run(1); } private function shot(string $name): void { $this->game->run(1); $file = $this->game->shot($name); $image = @imagecreatefrompng($file); if ($image === false) { return; } [$x, $y, $w, $h] = $this->view->screenRect(); $crop = imagecrop($image, ['x' => (int)$x, 'y' => (int)$y, 'width' => (int)round($w), 'height' => (int)round($h)]); if ($crop !== false) { imagepng($crop, $file); } } private function song(string $name, int $extra = 0): string { $file = "$this->tmp/pc-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; } /** An entry written on the server by another device. */ private function serverEntry(string $id, string $title, array $source, bool $lyrics = false): void { $this->server->accounts['ada']['entries'][] = ['id' => $id, 'title' => $title, 'artist' => 'Les Givrés', 'duration' => 184, 'size' => 4000000, 'sha256' => str_repeat('a', 64), 'addedBy' => 'bbbbbbbbbbbb', 'source' => $source, 'lyrics' => $lyrics, 'at' => '2026-11-20T10:00:00Z']; $this->server->accounts['ada']['version']++; } private function openAccountPage(): void { $this->tap('tab', 'more', 12); $this->tap('goto', 'cloud', 12); self::assertSame('cloud', $this->view->screen); } public function testCreatingTheAccountShowsTheRecoveryCodeOnceThenTheDevices(): void { $this->startPhone(); $this->openAccountPage(); self::assertFalse($this->cloud->signedIn()); self::assertSame('Téléphone', $this->view->fields['cdDevice'], 'nothing personal by default'); $this->tap('cdMode', 'signup'); $this->typeIn('cdLogin', 'ada_78'); $this->typeIn('cdPass', 'motdepasse1'); $this->view->field = ''; $this->view->scroll['page:cloud'] = 0.0; $this->shot('mobile-cloud-signup'); $this->tapScrolled('cdEnter', null, 4); self::assertTrue($this->cloud->signedIn()); self::assertSame('ABCD-EFGH-IJKL-MNOP-QRST', $this->view->cloudCode); $this->shot('mobile-cloud-code'); $saved = (string)file_get_contents("$this->tmp/phone/cloud.json"); self::assertStringNotContainsString('ABCD-EFGH', $saved, 'the code is never kept'); $this->tap('cdCopy'); $this->tap('cdNoted', null, 4); self::assertSame('', $this->view->cloudCode, 'shown once'); // the PC of the account: the devices, this one first $pc = $this->device(new WinZik(), 'pc', 'desktop'); $pc->signIn('ada_78', 'motdepasse1', 'PC du salon'); $this->others[] = $pc; $this->view->tap('cloudSync', null); $this->frames(fn () => count($this->cloud->devices()) === 2); $names = array_column($this->cloud->devices(), 'name'); self::assertSame(['Téléphone', 'PC du salon'], $names); $this->game->run(2); $this->shot('mobile-cloud-account'); // the PC signed out from the phone (a confirmation first) $this->tapScrolled('cdRevoke', $pc->deviceId(), 12); self::assertSame('cloudRevoke', $this->view->sheet); $this->tap('cdRevokeYes', null, 12); $this->frames(fn () => count($this->cloud->devices()) === 1); $revoked = array_filter($this->server->tokens, fn (array $d) => $d['id'] === $pc->deviceId() && !$d['revoked']); self::assertSame([], $revoked); // this phone leaves the account $this->tapScrolled('cdSignOut', null, 3); self::assertFalse($this->cloud->signedIn()); self::assertContains('cdEnter', $this->view->touchable()); } public function testAWrongPasswordSaysSoThenTheRightOneSignsIn(): void { $this->startPhone(); $this->account(); $this->openAccountPage(); $this->typeIn('cdLogin', 'ada'); $this->typeIn('cdPass', "faux\n"); $this->game->run(3); self::assertSame('Pseudo ou mot de passe incorrect.', $this->view->cloudStatus); self::assertFalse($this->cloud->signedIn()); $this->view->scroll['page:cloud'] = 0.0; $this->shot('mobile-cloud-error'); $this->typeIn('cdPass', "motdepasse1\n"); $this->game->run(3); self::assertTrue($this->cloud->signedIn()); self::assertSame('', $this->view->cloudCode, 'no code on a sign in'); } public function testTheCloudPlaylistShowsEachSongsStateAndCopiesOneFromThePc(): void { $this->startPhone(); $this->account(); $pc = $this->pc(); $this->cloud->signIn('ada', 'motdepasse1'); $this->cloud->autoCopy = false; // the Playlist tab: the switch, signed in, nothing in the cloud yet $this->tap('tab', 'playlist'); $this->tap('cloudView', 'cloud'); self::assertSame('cloud', $this->cloud->view()); $mine = $this->cloud->localPlaylist()?->items; self::assertNotEmpty($mine, 'the phone\'s own list is kept'); // the PC puts two songs in (one with its lyrics); other devices put songs of every kind $a = $this->song('alpha', 3 << 20); file_put_contents(substr($a, 0, -4) . '.lyr', "[LYR]\n[ 0.5 ]\nUne ligne du PC\n[ 3.0 ]\nPuis une autre\n"); $b = $this->song('beta', 20); $this->frames(fn () => ($pc->link?->has($this->cloud->deviceId()) ?? false) && ($this->cloud->link?->has($pc->deviceId()) ?? false)); self::assertSame(2, $pc->putFromPc([$a, $b])); $this->frames(fn () => count($this->server->accounts['ada']['entries']) === 2 && $pc->playlist->ops === []); $this->serverEntry('abcdef12-0000-4000-8000-000000000001', 'Loin du PC', ['type' => 'pc', 'device' => 'bbbbbbbbbbbb']); $this->serverEntry('abcdef12-0000-4000-8000-000000000002', 'Clip du dimanche', ['type' => 'youtube', 'videoId' => 'dQw4w9WgXcQ', 'query' => 'x']); $this->serverEntry('abcdef12-0000-4000-8000-000000000003', 'Chanson de YouTube', ['type' => 'youtube', 'videoId' => '', 'query' => 'Les Givrés Chanson']); $this->serverEntry('abcdef12-0000-4000-8000-000000000004', 'Copie ratée', ['type' => 'pc', 'device' => $pc->deviceId()]); $this->tap('cloudSync'); $this->frames(fn () => count($this->cloud->entries()) === 6); $ids = array_column($this->cloud->entries(), 'id'); [$idA, $idB] = $ids; self::assertSame('absent', $this->cloud->rowState($idA)['state']); self::assertSame('offline', $this->cloud->rowState('abcdef12-0000-4000-8000-000000000001')['state']); // a tap on the song not here: it is copied from the PC (copying, then here) $this->tap('cloudRow', $idA, 1); $copying = false; $this->frames(function () use ($idA, &$copying): bool { $s = $this->cloud->rowState($idA); $copying = $copying || ($s['state'] === 'copying' && $s['progress'] > 0.2); return $copying; }); $this->shot('mobile-cloud-copying'); $this->frames(fn () => $this->cloud->rowState($idA)['state'] === 'here'); self::assertSame(hash_file('sha256', $a), hash_file('sha256', (string)$this->cloud->playlist->fileOf($idA))); // its lyrics came down with it $file = (string)$this->cloud->playlist->fileOf($idA); $this->frames(fn () => is_file($this->wz->lyricsCachePath($file))); // each state on its line (YouTube ↓ and an error are made here: no yt-dlp in the tests) $youtube = $this->wz->youtube; $this->wz->youtube = null; $this->cloud->youtubeJobs['abcdef12-0000-4000-8000-000000000002'] = ['url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'since' => 0.0]; $this->cloud->failed['abcdef12-0000-4000-8000-000000000004'] = ['state' => 'error', 'error' => 'Le PC n\'a plus ce fichier.']; $this->wz->notice = ''; $this->game->run(2); $states = array_column($this->cloud->rows(), 'state'); self::assertSame(['here', 'absent', 'offline', 'youtube', 'absent', 'error'], $states); foreach ($ids as $id) { self::assertNotNull($this->view->at('cloudRow', $id), "the line of $id"); } self::assertNotNull($this->view->at('cloudRetry', 'abcdef12-0000-4000-8000-000000000004'), 'Réessayer'); self::assertNotNull($this->view->at('cloudAll'), 'Tout copier'); $this->shot('mobile-cloud-playlist'); unset($this->cloud->youtubeJobs['abcdef12-0000-4000-8000-000000000002']); $this->wz->youtube = $youtube; // the song here: its sheet (Lire, Paroles, Retirer du cloud); Paroles = the karaoke of the PC's lyrics $this->tap('cloudMore', $idA, 12); self::assertSame('cloudActions', $this->view->sheet); self::assertNotNull($this->view->at('cloudAct', 'lyrics')); $this->shot('mobile-cloud-actions'); $this->tap('cloudAct', 'lyrics', 12); self::assertSame('karaoke', $this->view->screen); self::assertSame($file, $this->wz->player->file); $this->frames(fn () => str_contains(implode("\n", array_column($this->wz->lyrics?->lines ?? [], 1)), 'Une ligne du PC'), 300); $this->shot('mobile-cloud-lyrics'); $this->form->key(Pointer::ESCAPE); $this->game->wait(0.4); // the other one: Retirer du cloud (confirmed) $this->tap('cloudMore', $idB, 12); $this->tap('cloudAct', 'remove', 12); $this->tap('cloudRemoveYes', null, 12); $this->frames(fn () => !in_array($idB, array_column($this->server->accounts['ada']['entries'], 'id'), true)); // back to « Ce téléphone »: the phone's own list $this->tap('cloudView', 'local'); self::assertSame('local', $this->cloud->view()); self::assertSame($mine, $this->wz->playlist->items); } public function testASongOfThePhoneGoesInTheCloudForYouTube(): void { $this->startPhone(); $this->account(); $this->cloud->signIn('ada', 'motdepasse1'); $this->tap('tab', 'playlist'); self::assertNotNull($this->view->at('cloudView', 'cloud'), 'the switch is there'); $this->shot('mobile-cloud-switch'); $this->tap('rowMore', 0, 12); self::assertNotNull($this->view->at('act', 'cloudPut')); $this->shot('mobile-cloud-put'); $this->tap('act', 'cloudPut', 12); $this->frames(fn () => count($this->server->accounts['ada']['entries']) === 1); $entry = $this->server->accounts['ada']['entries'][0]; self::assertSame('youtube', $entry['source']['type'], 'from the phone: the other devices get it from YouTube'); self::assertNotSame('', $entry['source']['query']); self::assertTrue($this->cloud->fileState($this->wz->playlist->items[0])['cloud']); // its sheet now says so $this->tap('rowMore', 0, 12); self::assertNotNull($this->view->at('act', 'none'), 'Déjà dans le cloud'); } public function testTheCloudTabSignedOutLeadsToTheAccountAndTheSettingsAreSaved(): void { $this->startPhone(); $this->account(); $this->tap('tab', 'playlist'); $this->tap('cloudView', 'cloud'); self::assertTrue($this->view->inCloudView()); $this->shot('mobile-cloud-signedout'); $this->tap('goto', 'cloud', 4); self::assertSame('cloud', $this->view->screen); $this->typeIn('cdLogin', 'ada'); $this->typeIn('cdPass', "motdepasse1\n"); $this->game->run(3); self::assertTrue($this->cloud->signedIn()); self::assertSame('cloud', $this->cloud->view(), 'the cloud tab asked: the cloud playlist once signed in'); // the options of the copies self::assertTrue($this->cloud->autoCopy, 'a phone copies by itself by default'); self::assertTrue($this->cloud->wifiOnly); $this->tapScrolled('cdAuto'); $this->tapScrolled('cdWifi'); $this->shot('mobile-cloud-options'); $settings = json_decode((string)file_get_contents("$this->tmp/phone/cloud/settings.json"), true); self::assertFalse($settings['autoCopy']); self::assertFalse($settings['wifiOnly']); self::assertSame($this->cloud->receiveDir, $settings['receiveDir']); // the menu of the cloud playlist $this->back(); $this->tap('headerMore', null, 12); self::assertSame('cloudMenu', $this->view->sheet); $this->tap('cdAuto'); self::assertTrue($this->cloud->autoCopy); $this->shot('mobile-cloud-menu'); } public function testTheSyncedPlaybackShowsOnThePlayerAndTheAccountPageTurnsItOff(): void { $this->startPhone(); $this->account(); $this->cloud->signIn('ada', 'motdepasse1', 'Mon téléphone'); $pc = $this->pc(); $this->frames(fn () => $this->cloud->syncCount() === 2 && $pc->syncCount() === 2); // the player of the phone says it plays with the PC; a tap opens the account $this->tap('tab', 'player', 4); self::assertSame('Synchro · 2 appareils', $this->cloud->syncLabel()); $this->shot('mobile-cloud-sync-player'); $this->tap('goto', 'cloud', 12); self::assertSame('cloud', $this->view->screen); for ($i = 0; $i < 40 && $this->view->at('cdSync') === null; $i++) { $this->view->scroll['page:cloud'] = ($this->view->scroll['page:cloud'] ?? 0.0) + 120; $this->game->run(1); } $this->view->scroll['page:cloud'] = max(0.0, ($this->view->scroll['page:cloud'] ?? 0.0) - 200); $this->game->run(1); $this->shot('mobile-cloud-sync-on'); self::assertTrue($this->cloud->playSynced(), 'synchronised by default'); $this->tapScrolled('cdSync'); self::assertFalse($this->cloud->playSynced()); self::assertFalse(json_decode((string)file_get_contents("$this->tmp/phone/cloud/settings.json"), true)['playSync']); $this->frames(fn () => $pc->syncCount() === 1, 30); $this->shot('mobile-cloud-sync-off'); self::assertSame('', $this->cloud->syncLabel()); $this->tapScrolled('cdSync'); self::assertTrue($this->cloud->playSynced()); } private function back(): void { $this->form->key(Pointer::ESCAPE); $this->game->wait(0.4); }}