<?phpnamespace Tests;use App\Core\WinZik;use App\Media\Process;use App\Media\Tools;/** A program run by WinZik that ends only when the test says so (ffmpeg making the sound of a film), well or badly. */final class HeldProcess extends Process{ public bool $released = false; public bool $good = true; /** @param list<string> $command */ public function __construct(array $command, private ?\Closure $then = null) { parent::__construct($command, '', ''); } public function poll(): array { if ($this->released && $this->good && $this->then !== null) { ($this->then)(); $this->then = null; } return []; } public function done(): bool { return $this->released; } public function ok(): bool { return $this->released && $this->good; } public function error(): string { return $this->good ? '' : 'film illisible'; }}/** * What must never go wrong: the settings file with a file name of an old disk (Latin-1), the lyrics of two songs of the * same name, the song playing removed, a film being made when its line goes, a film that cannot be made, a whole list * of files gone (a disk unplugged), the place in the song kept while it plays. */final class RobustnessTest extends PlayerTestCase{ /** @var list<HeldProcess> the sound of each film asked to ffmpeg */ private array $films = []; /** WinZik with an ffmpeg whose film sounds wait for the test (release()); a film of its own for each test. */ private function startWithFfmpeg(string $name): string { $this->startPlayer(); $tools = new Tools($this->wz->storagePath('bin'), $this->wz->storagePath('tmp')); $tools->state = ['yt-dlp' => 'ready', 'ffmpeg' => 'ready']; $tools->path = ['yt-dlp' => '/fake/yt-dlp', 'ffmpeg' => '/fake/ffmpeg']; $wav = $this->wz->path('assets/music/chaine-hifi.wav'); $runner = function (array $cmd) use ($wav): Process { if (str_ends_with(implode(' ', $cmd), 'audio.wav')) { $target = (string)end($cmd); return $this->films[] = new HeldProcess($cmd, fn () => copy($wav, $target)); } return Process::finished($cmd, [], 1); }; $this->wz->useMedia($tools, \Closure::fromCallable($runner)); $film = $this->wz->storagePath('test/films/' . $name . '.mp4'); @mkdir(dirname($film), 0777, true); file_put_contents($film, 'film'); return $film; } private function release(bool $good = true): void { self::assertNotEmpty($this->films, 'ffmpeg was asked for the sound of the film'); foreach ($this->films as $p) { $p->good = $good; $p->released = true; } } public function testALatin1FileNameKeepsTheSettingsFileWholeAndComesBackAsItWas(): void { $this->startPlayer(); $latin1 = '/media/vieux disque/Chanson d' . "\xe9" . 't' . "\xe9" . '.mp3'; // "d\xe9t\xe9": Latin-1, not UTF-8 self::assertFalse(mb_check_encoding($latin1, 'UTF-8')); $this->wz->playlist->add($latin1); $this->wz->options['lastDir'] = '/media/vieux disque/' . "\xc9" . 't' . "\xe9"; $this->wz->ui['playlist']['order'] = ['assets/music/winzik.mp3', $latin1]; $this->wz->eqSongs[$latin1] = array_fill(0, 10, 3.0); $this->wz->options['note'] = 'b64:pas du base64'; // a real name that starts like the mark $this->wz->save(); $file = $this->wz->storagePath(WinZik::SETTINGS); $json = (string)file_get_contents($file); self::assertNotSame('', $json, 'the settings file is never emptied'); self::assertIsArray(json_decode($json, true), 'and it stays JSON'); $this->wz->reset(); // the next start: the file read again self::assertContains($latin1, $this->wz->playlist->items, 'the song of the old disk is still in the list'); self::assertSame('/media/vieux disque/' . "\xc9" . 't' . "\xe9", $this->wz->options['lastDir']); self::assertSame(['assets/music/winzik.mp3', $latin1], $this->wz->ui['playlist']['order']); self::assertSame(array_fill(0, 10, 3.0), $this->wz->eqSongs[$latin1] ?? null, 'its equalizer too'); self::assertSame('b64:pas du base64', $this->wz->options['note']); } public function testAValueJsonCannotHoldLeavesTheSettingsOfBeforeAsTheyWere(): void { $this->startPlayer(); $this->wz->save(); $file = $this->wz->storagePath(WinZik::SETTINGS); $before = (string)file_get_contents($file); $this->wz->options['broken'] = NAN; // json_encode cannot write it $this->wz->save(); self::assertSame($before, (string)file_get_contents($file), 'not replaced by an empty file'); unset($this->wz->options['broken']); } public function testTheQueueOfDownloadsKeepsALatin1Name(): void { $this->startPlayer(); $file = $this->wz->storagePath('test/queue.json'); $queue = [['url' => 'https://www.youtube.com/watch?v=aaa', 'title' => 'Caf' . "\xe9", 'format' => 'mp3', 'key' => 'k']]; self::assertTrue(WinZik::writeJsonFile($file, $queue)); self::assertSame($queue, WinZik::readJsonFile($file)); } public function testTwoSongsOfTheSameNameInTwoFoldersKeepTheirOwnLyrics(): void { $this->startPlayer(); $song = $this->wz->path('assets/music/partage.mp3'); $a = $this->wz->storagePath('test/Album A/01 - Intro.mp3'); $b = $this->wz->storagePath('test/Album B/01 - Intro.mp3'); foreach ([$a, $b] as $f) { @mkdir(dirname($f), 0777, true); copy($song, $f); } // B has its own lyrics next to it; the lyrics downloaded for A by an older WinZik are in var/lyr under the bare name file_put_contents(dirname($b) . '/01 - Intro.lyr', "[LYR]\n[ 1.0 ]\nB\n"); $old = $this->wz->storagePath('lyr/01 - Intro.lyr'); @mkdir(dirname($old), 0777, true); file_put_contents($old, "[LYR]\n[ 1.0 ]\nA\n"); self::assertSame(dirname($b) . '/01 - Intro.lyr', $this->wz->findLyricsFile($b), 'next to the song first'); self::assertSame($old, $this->wz->findLyricsFile($a), 'the copy of before is still read'); // lyrics got now are kept under the song's own name in var/lyr self::assertNotSame($this->wz->lyricsCachePath($a), $this->wz->lyricsCachePath($b)); self::assertStringContainsString('01 - Intro-', basename($this->wz->lyricsCachePath($a))); file_put_contents($this->wz->lyricsCachePath($a), "[LYR]\n[ 1.0 ]\nA2\n"); self::assertSame($this->wz->lyricsCachePath($a), $this->wz->findLyricsFile($a)); self::assertSame(dirname($b) . '/01 - Intro.lyr', $this->wz->findLyricsFile($b), 'never the lyrics of the other one'); unlink($old); } public function testLyricsFromTheMarketplaceOrAFileChosenGoToTheSongsOwnCopy(): void { $this->startPlayer(); $song = $this->wz->path('assets/music/partage.mp3'); $a = $this->wz->storagePath('test/Album A/02 - Outro.mp3'); $b = $this->wz->storagePath('test/Album B/02 - Outro.mp3'); foreach ([$a, $b] as $f) { @mkdir(dirname($f), 0777, true); copy($song, $f); } $this->wz->playlist->clear(); $this->wz->addFiles([$a]); $this->wz->playIndex(0); $this->game->run(2); $bare = $this->wz->storagePath('lyr/02 - Outro.lyr'); // the marketplace's "Installer": the song playing only, never var/lyr/<name>.lyr shared by every "02 - Outro" $this->form->openMarket('lyrics'); $this->game->run(2); $market = $this->game->scene(); self::assertInstanceOf(\App\Form\MarketForm::class, $market); (new \ReflectionMethod($market, 'installLyrics'))->invoke($market, [], "[LYR]\n[ 1.0 ]\nMarket\n"); self::assertFileExists($this->wz->lyricsCachePath($a)); self::assertFileDoesNotExist($bare); self::assertSame($this->wz->lyricsCachePath($a), $this->wz->findLyricsFile($a)); self::assertNull($this->wz->findLyricsFile($b), 'the other song of that name has none'); $market->click($market->widget('quit')); $this->game->run(1); // a .LYR chosen in "Ajouter des fichiers": for the songs of the list of that name, over the copy of before $this->wz->addFiles([$b]); $chosen = sys_get_temp_dir() . '/wz-lyr-' . bin2hex(random_bytes(3)) . '/02 - Outro.lyr'; @mkdir(dirname($chosen), 0777, true); file_put_contents($chosen, "[LYR]\n[ 1.0 ]\nChosen\n"); $this->clickControl($this->desk->playlist, 'add'); $files = $this->game->scene(); self::assertInstanceOf(\App\Form\FilesForm::class, $files); (new \ReflectionMethod($files, 'imported'))->invoke($files, [$chosen]); foreach ([$a, $b] as $f) { self::assertSame($this->wz->lyricsCachePath($f), $this->wz->findLyricsFile($f)); self::assertStringContainsString('Chosen', (string)file_get_contents($this->wz->lyricsCachePath($f))); } self::assertFileDoesNotExist($bare); @unlink($chosen); @rmdir(dirname($chosen)); foreach ([$a, $b] as $f) { @unlink($this->wz->lyricsCachePath($f)); } } public function testRemovingTheSongPlayingPlaysTheNextOne(): void { $this->startPlayer(); $this->wz->playIndex(0); $this->game->run(5); self::assertSame('playing', $this->wz->player->state); $next = $this->wz->playlist->items[1]; $this->wz->removeLines([0]); $this->game->run(2); self::assertSame($next, $this->wz->playlist->current()); self::assertSame($this->wz->path($next), $this->wz->player->file); self::assertSame('playing', $this->wz->player->state, 'the music goes on with the next song'); // stopped: the next one is only made ready $this->wz->stop(); $this->wz->removeLines([0]); $this->game->run(2); self::assertSame('stopped', $this->wz->player->state); self::assertSame($this->wz->path((string)$this->wz->playlist->current()), $this->wz->player->file); } public function testAFilmBeingMadeWhenItsLineGoesNeverStartsAfterwards(): void { $film = $this->startWithFfmpeg('retire'); $this->wz->playlist->add($film); $this->wz->playIndex($this->wz->playlist->count() - 1); $this->game->run(2); self::assertSame($film, $this->wz->waitVideo, 'ffmpeg makes its sound'); $this->wz->removeLines(range(0, $this->wz->playlist->count() - 1)); self::assertSame('', $this->wz->waitVideo); $this->release(); $this->game->run(10); self::assertNull($this->wz->player->file, 'the film removed does not play once ready'); self::assertSame('stopped', $this->wz->player->state); } public function testAnEmptyFlsListStopsTheFilmBeingMade(): void { $film = $this->startWithFfmpeg('liste-vide'); $this->wz->playlist->add($film); $this->wz->playIndex($this->wz->playlist->count() - 1); $this->game->run(2); $fls = $this->wz->storagePath('test/vide.fls'); file_put_contents($fls, "[FLS]\n"); self::assertTrue($this->wz->loadFls($fls)); self::assertSame(0, $this->wz->playlist->count()); $this->release(); $this->game->run(10); self::assertNull($this->wz->player->file); self::assertSame('stopped', $this->wz->player->state); } public function testAFilmThatCannotBeMadeWhileTheListPlaysOnGoesToTheNextSong(): void { $film = $this->startWithFfmpeg('illisible'); $this->wz->playlist->clear(); $this->wz->playlist->add($film); $this->wz->playlist->add('assets/music/partage.mp3'); $this->wz->playIndex(0); $this->game->run(2); $this->release(false); // ffmpeg fails $this->game->run(5); self::assertSame(1, $this->wz->playlist->active, 'the next song'); self::assertSame('playing', $this->wz->player->state, 'the music did not stop'); } public function testAListOfFilesAllGoneIsSkippedOneAFrameThenSaysSo(): void { $this->startPlayer(); $this->wz->playlist->clear(); for ($i = 0; $i < 40; $i++) { $this->wz->playlist->add("/media/disque-debranche/morceau-$i.mp3"); } $this->wz->playIndex(0); self::assertSame(0, $this->wz->playlist->active, 'not the whole list in one go (WinZik froze)'); $this->game->run(1); self::assertLessThanOrEqual(2, $this->wz->playlist->active, 'one file a frame'); $this->game->run(60); self::assertSame('Aucun fichier lisible dans la playlist', $this->wz->notice); self::assertNotSame('playing', $this->wz->player->state); $active = $this->wz->playlist->active; $this->game->run(10); self::assertSame($active, $this->wz->playlist->active, 'and it stops trying'); } public function testOneFileGoneIsSkippedAndTheNextSongPlays(): void { $this->startPlayer(); $this->wz->playlist->clear(); $this->wz->playlist->add('/media/disque-debranche/perdu.mp3'); $this->wz->playlist->add('assets/music/partage.mp3'); $this->wz->playIndex(0); $this->game->run(3); self::assertSame(1, $this->wz->playlist->active); self::assertSame('playing', $this->wz->player->state); } public function testThePlaceInTheSongIsSavedEveryFewSecondsWhileItPlays(): void { $this->startPlayer(); $this->wz->playIndex(0); $this->game->wait(2 * WinZik::POSITION_EVERY); self::assertSame('playing', $this->wz->player->state); $saved = json_decode((string)file_get_contents($this->wz->storagePath(WinZik::SETTINGS)), true); self::assertIsArray($saved); self::assertSame('playing', $saved['state']); // saved at most POSITION_EVERY seconds ago (+ the 2 seconds a change waits before it is written) self::assertGreaterThan(0.5, (float)$saved['position']); self::assertGreaterThanOrEqual($this->wz->player->position - WinZik::POSITION_EVERY - 2.5, (float)$saved['position'], 'a crash resumes near there'); }}