PT
apps/winzik / tests / LyricsFeaturesTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Core\Market;use App\Core\WinZik;use App\Media\LyricsSearch;use App\Media\LyricsTranslator;use App\Media\Process;use App\Model\Language;use App\Model\Lyrics;use JumPHP\Http\HttpResponse;use Tests\Lyrics\FakeLyricsHttp;/** * The lyrics found on the Internet (LRCLIB, the marketplace, the old site, Claude), the lyrics without times, their * translations stored in the .LYR (Claude Code, else MyMemory), the play mode "only the songs with lyrics": no network, * no real Claude (fakes). */final class LyricsFeaturesTest extends PlayerTestCase{    private const PLAIN = "Sous le ciel de la ville\nLes lumières s'allument\n\nEt la nuit nous emporte\nLes lumières s'allument";    private const ENGLISH = "I walk alone tonight\nAnd the city is my friend\n\nYou know that I will stay\nAnd the city is my friend";    /** @var list<list<string>> the commands given to "claude" */    private array $claudeCalls = [];    /** The songs played here leave no listens behind them (the tests after count their own). */    protected function tearDown(): void    {        if (isset($this->wz)) {            $this->wz->stop();            @unlink($this->wz->storagePath('stats.json'));            (new \ReflectionProperty(WinZik::class, 'stats'))->setValue($this->wz, null);        }        parent::tearDown();    }    /** The lyrics of WinZik 2001 as the versions before this one read them (Lyrics::parse of 0.10.5). @return list<array{0: float, 1: string}> */    private static function oldParse(string $content): array    {        $rows = preg_split('/\r\n|\r|\n/', $content) ?: [];        $lines = [];        $time = null;        foreach (array_slice($rows, 1) as $row) {            if (preg_match('/^\s*\[\s*(-?[0-9]+(?:[.,][0-9]*)?)\s*\]\s*$/', $row, $m)) {                $time = (float)str_replace(',', '.', $m[1]);                continue;            }            if ($time !== null) {                $lines[] = [$time, rtrim($row)];                $time = null;            }        }        return $lines;    }    private function fakeInternet(\Closure $route, string|false $claude = false, ?\Closure $claudeAnswer = null): FakeLyricsHttp    {        $engine = $this->form->engineOrNull();        self::assertNotNull($engine);        $http = new FakeLyricsHttp($engine->bridge, $route);        $runner = function (array $cmd) use ($claudeAnswer): Process {            $this->claudeCalls[] = $cmd;            return Process::finished($cmd, $claudeAnswer !== null ? $claudeAnswer($cmd) : [], $claudeAnswer !== null ? 0 : 1);        };        $this->wz->useLyricsServices($http, \Closure::fromCallable($runner), $claude);        LyricsTranslator::forgetCache();        return $http;    }    private static function json(mixed $data): HttpResponse    {        return new HttpResponse(200, (string)json_encode($data), ['content-type' => 'application/json']);    }    /** The song "partage.mp3" playing, without lyrics of its own (its var/lyr copy removed). */    private function playPartage(): string    {        $this->wz->playIndex(3);        $file = (string)$this->wz->playlist->current();        self::assertStringEndsWith('partage.mp3', $file);        @unlink($this->wz->lyricsCachePath($file));        $this->wz->refreshLyrics();        self::assertNull($this->wz->lyrics);        return $file;    }    public function testTheLyrFileKeepsItsTranslationsAndTheOlderWinZikStillReadsIt(): void    {        $lyrics = new Lyrics([[1.5, 'I walk alone tonight'], [4.0, 'And the city is my friend'], [7.25, '<00:07.25>You <00:07.80>know']]);        $lyrics->language = 'en';        $lyrics->setTranslations([0 => 'Je marche seul ce soir', 1 => 'Et la ville est mon amie', 2 => 'Tu sais'], 'fr');        $lyr = $lyrics->toLyr();        self::assertStringContainsString("[TRADUCTION fr]\r\n1=Je marche seul ce soir\r\n", $lyr);        $back = Lyrics::parse($lyr);        self::assertNotNull($back);        self::assertTrue($back->synced);        self::assertSame($lyrics->lines, $back->lines);        self::assertSame('en', $back->language);        self::assertSame('fr', $back->translationLang);        self::assertSame('Et la ville est mon amie', $back->translation(1));        self::assertTrue($back->hasTranslation('fr'));        self::assertFalse($back->hasTranslation('de'));        // the WinZik of before reads the same lines, and nothing of the translation        self::assertSame($lyrics->lines, self::oldParse($lyr), 'an older WinZik skips the new rows');        // a file of 2001 (quoted rows, Windows-1252) reads as before: no translation        $old = Lyrics::load(__DIR__ . '/../assets/music/winzik.lyr');        self::assertNotNull($old);        self::assertTrue($old->synced);        self::assertSame([], $old->translations);        // a file whose times are not in order: the translations follow their lines        $mixed = Lyrics::parse("[LYR]\r\n[ 5 ]\r\nB\r\n[ 1 ]\r\nA\r\n[TRADUCTION fr]\r\n1=b\r\n2=a\r\n");        self::assertSame([[1.0, 'A'], [5.0, 'B']], $mixed?->lines);        self::assertSame([0 => 'a', 1 => 'b'], $mixed?->translations);    }    public function testLyricsWithoutTimesAreLinesSpreadOverTheSong(): void    {        $lyrics = Lyrics::fromText(self::PLAIN . "\n\n\n");        self::assertNotNull($lyrics);        self::assertFalse($lyrics->synced);        self::assertCount(5, $lyrics->lines, 'the blank between the verses kept, once');        $lyr = $lyrics->toLyr();        self::assertStringStartsWith("[LYR]\r\n[NON SYNCHRONISEES]\r\nSous le ciel de la ville\r\n", $lyr);        self::assertStringNotContainsString('[ 0', $lyr, 'no time written');        self::assertSame([], self::oldParse($lyr), 'an older WinZik sees no lyrics (it never shows wrong times)');        $back = Lyrics::parse($lyr);        self::assertNotNull($back);        self::assertFalse($back->synced);        self::assertSame($lyrics->texts(), $back->texts());        $back->spread(200.0);        self::assertSame(-1, $back->indexAt(5.0), 'an intro before the first line');        self::assertSame(0, $back->indexAt(13.0));        self::assertSame(4, $back->indexAt(199.0));        for ($i = 1; $i < count($back->lines); $i++) {            self::assertGreaterThan($back->lines[$i - 1][0], $back->lines[$i][0]);        }        // an LRC pasted is synchronised        self::assertTrue(Lyrics::fromText("[00:01.00]Un\n[00:02.00]Deux")?->synced);        self::assertNull(Lyrics::fromText("  \n \n"));        self::assertSame('fr', Language::detect(self::PLAIN . "\nJe suis là pour toi et tu es avec moi"));        self::assertSame('en', Language::detect(self::ENGLISH));        self::assertSame('', Language::detect('Oh oh'));    }    public function testASearchGathersLrclibTheMarketplaceAndClaudeSyncedFirst(): void    {        $this->startPlayer();        $lrclib = [            ['id' => 1, 'trackName' => 'Partage', 'artistName' => 'WinZik Band', 'albumName' => 'Demo', 'duration' => 120, 'instrumental' => false,                'plainLyrics' => self::PLAIN, 'syncedLyrics' => null],            ['id' => 2, 'trackName' => 'Partage', 'artistName' => 'WinZik Band', 'albumName' => 'Live', 'duration' => 121, 'instrumental' => false,                'plainLyrics' => 'Un', 'syncedLyrics' => "[00:01.00] On partage la musique\n[00:04.00] Des paroles qui tombent du ciel"],            ['id' => 3, 'trackName' => 'Partage (instrumental)', 'artistName' => 'WinZik Band', 'instrumental' => true, 'plainLyrics' => '', 'syncedLyrics' => ''],        ];        $http = $this->fakeInternet(function (string $method, string $url) use ($lrclib): HttpResponse {            return match (true) {                str_starts_with($url, 'https://lrclib.net/api/search?') => self::json($lrclib),                str_starts_with($url, 'https://lrclib.net/api/get/77') => self::json(['id' => 77, 'trackName' => 'Partage', 'artistName' => 'WinZik Band',                    'plainLyrics' => "Une autre version\nDes paroles trouvées par Claude", 'syncedLyrics' => null]),                $url === 'https://market.test/lyrics?q=' . rawurlencode('WinZik Band Partage') => self::json(['items' => [['id' => 'm1', 'artist' => 'WinZik Band', 'title' => 'Partage']]]),                $url === 'https://market.test/lyrics/m1' => new HttpResponse(200, "[LYR]\r\n[ 2 ]\r\nLa version de la marketplace\r\n"),                default => new HttpResponse(404, '<html>none</html>'),            };        }, '/fake/claude', fn (array $cmd) => ['Voici :', 'LRCLIB:77', 'URL:http://192.168.1.1/x.lrc', 'URL:https://localhost/a.lrc']);        (new \ReflectionProperty(WinZik::class, 'market'))->setValue($this->wz, new Market($http, 'https://market.test'));        $search = $this->wz->searchLyrics('WinZik Band', 'Partage');        self::assertNotNull($search);        self::assertSame('searching', $search->sources['claude'], 'Claude works in the background');        $this->game->run(3);        self::assertTrue($search->done(), $search->describe());        self::assertSame(['lrclib' => 'found', 'market' => 'found', 'archive' => 'none', 'claude' => 'found'], $search->sources);        $sources = array_map(fn (array $c) => $c['source'] . ($c['synced'] ? '+sync' : ''), $search->candidates);        self::assertSame([true, true, false, false], array_map(fn (array $c) => $c['synced'], $search->candidates), 'the synchronised lyrics first');        self::assertContains('lrclib', $sources);        self::assertContains('market+sync', $sources);        self::assertContains('claude', $sources);        self::assertCount(4, $search->candidates, 'the instrumental left out');        $first = array_values(array_filter($search->candidates, fn (array $c) => $c['source'] === 'lrclib' && $c['synced']))[0];        self::assertSame('On partage la musique / Des paroles qui tombent du ciel', $first['preview']);        self::assertSame(2, $first['lines']);        self::assertSame('Live', $first['album']);        // Claude: only WebSearch / WebFetch, no MCP server; its addresses of this network are never fetched        $cmd = $this->claudeCalls[0];        self::assertSame(['--tools', 'WebSearch,WebFetch', '--allowedTools', 'WebSearch,WebFetch', '--strict-mcp-config'], array_slice($cmd, 3));        self::assertStringContainsString('Partage', $cmd[2]);        foreach ($http->urls() as $url) {            self::assertStringNotContainsString('192.168', $url);            self::assertStringNotContainsString('localhost', $url);        }        // nothing personal sent: a generic client name        foreach ($http->requests as $r) {            self::assertStringNotContainsString('@', (string)($r['headers']['User-Agent'] ?? 'WinZik'));        }    }    public function testACandidateImportedKeepsItsTimesOrBecomesLinesToScroll(): void    {        $this->startPlayer();        $this->fakeInternet(fn () => new HttpResponse(404, ''));        $file = $this->playPartage();        $synced = LyricsSearch::fromLrclib(['trackName' => 'Partage', 'artistName' => 'WinZik Band', 'syncedLyrics' => "[00:01.00] On partage\n[00:03.00] la musique"], 'lrclib');        self::assertNotNull($synced);        self::assertSame($this->wz->lyricsCachePath($file), $this->wz->importLyrics($synced));        self::assertTrue($this->wz->lyricsSynced());        self::assertSame([[1.0, 'On partage'], [3.0, 'la musique']], $this->wz->lyrics?->lines);        self::assertTrue($this->wz->hasLyrics($file));        $plain = LyricsSearch::fromLrclib(['trackName' => 'Partage', 'artistName' => 'WinZik Band', 'plainLyrics' => self::PLAIN], 'lrclib');        self::assertNotNull($plain);        self::assertFalse($plain['synced']);        $this->wz->importLyrics($plain);        self::assertNotNull($this->wz->lyrics);        self::assertFalse($this->wz->lyricsSynced(), 'lines to scroll by hand');        self::assertStringContainsString('[NON SYNCHRONISEES]', (string)file_get_contents($this->wz->lyricsCachePath($file)));        self::assertSame('fr', $this->wz->lyrics->language, 'its language guessed when saved');        // spread over the song: the estimate moves with the position, the lyrics bar shows a line        $duration = $this->wz->player->duration();        self::assertGreaterThan(0.0, $duration);        self::assertSame(-1, $this->wz->estimatedLine(0.0));        self::assertSame(0, $this->wz->estimatedLine($duration * 0.07));        self::assertSame(4, $this->wz->estimatedLine($duration * 0.99));        $this->wz->seek($duration * 0.5);        self::assertNotSame(Lyrics::NONE, $this->wz->lyricLine());        // shifted: nothing to shift        $before = (string)file_get_contents($this->wz->lyricsCachePath($file));        $this->wz->shiftLyrics(0.5);        self::assertSame($before, (string)file_get_contents($this->wz->lyricsCachePath($file)));        // the karaoke still finds a line and its syllables        [$syllables] = \App\Model\Karaoke::timing($this->wz->lyrics->lines[0][1], $this->wz->lyrics->lines[0][0], $this->wz->lyrics->lines[1][0]);        self::assertNotSame([], $syllables);        // the lyrics edited (synchronised in the editor): the translations of the lines that stayed are kept        $edited = $this->wz->lyrics;        $edited->setTranslations([0 => 'Under the city sky'], 'en');        $this->wz->saveLyrics($file, $edited, false);        $synced = new Lyrics([[2.0, 'Sous le ciel de la ville'], [5.0, 'Une ligne nouvelle']]);        $this->wz->saveLyrics($file, $synced);        self::assertTrue($this->wz->lyricsSynced());        self::assertSame('Under the city sky', $this->wz->lyrics->translation(0));        self::assertSame('', $this->wz->lyrics->translation(1));        @unlink($this->wz->lyricsCachePath($file));    }    public function testClaudeCodeTranslatesTheWholeSongLineByLineIntoTheLyr(): void    {        $this->startPlayer();        $this->fakeInternet(fn () => new HttpResponse(500, ''), '/fake/claude', function (array $cmd): array {            preg_match_all('/^(\d+)\|(.*)$/m', $cmd[2], $m, PREG_SET_ORDER);            return array_map(fn (array $l) => $l[1] . '|FR ' . $l[2], $m);        });        $file = $this->playPartage();        $this->wz->saveLyrics($file, Lyrics::fromText(self::ENGLISH) ?? new Lyrics());        self::assertTrue($this->wz->needsTranslation());        self::assertFalse($this->wz->hasTranslation());        $task = $this->wz->translate();        self::assertSame('translating', $this->wz->translationState()['state']);        self::assertSame('claude', $this->wz->translationState()['engine']);        $this->game->run(3);        self::assertTrue($task->isOk(), (string)$task->error()?->getMessage());        self::assertSame('done', $this->wz->translationState()['state']);        self::assertSame(1.0, $this->wz->translationState()['progress']);        self::assertTrue($this->wz->hasTranslation());        self::assertSame('FR I walk alone tonight', $this->wz->lyrics?->translation(0));        self::assertSame('', $this->wz->lyrics?->translation(2), 'the blank line stays blank');        self::assertSame('FR And the city is my friend', $this->wz->translations()[4], 'a chorus line translated once, given to both');        self::assertStringContainsString("[TRADUCTION fr]\r\n", (string)file_get_contents($this->wz->lyricsCachePath($file)));        // the song's lines once each, no tool, no MCP server        $cmd = $this->claudeCalls[0];        self::assertSame(['--tools', '', '--strict-mcp-config'], array_slice($cmd, 3));        self::assertSame(1, substr_count($cmd[2], 'And the city is my friend'));        self::assertStringContainsString('exactement 3 lignes', $cmd[2]);        // read again from the file: still there        $this->wz->refreshLyrics();        self::assertTrue($this->wz->hasTranslation($file, 'fr'));        self::assertFalse($this->wz->lyricsSynced());        @unlink($this->wz->lyricsCachePath($file));    }    public function testWithoutClaudeTheFreeTranslationIsAskedPolitelyInBlocks(): void    {        $this->startPlayer();        $long = [];        for ($i = 1; $i <= 30; $i++) {            $long[] = "This is the line number $i of a long song that goes on";        }        $calls = 0;        $http = $this->fakeInternet(function (string $method, string $url) use (&$calls): HttpResponse {            parse_str((string)parse_url($url, PHP_URL_QUERY), $q);            $calls++;            $rows = explode("\n", (string)$q['q']);            if ($calls === 1) {                // the first block comes back merged: it is asked again line by line                return self::json(['responseData' => ['translatedText' => 'tout en une ligne'], 'responseStatus' => 200, 'quotaFinished' => false]);            }            return self::json(['responseData' => ['translatedText' => implode("\n", array_map(fn ($r) => 'FR &#39;' . $r, $rows))], 'responseStatus' => 200, 'quotaFinished' => false]);        });        $file = $this->playPartage();        $this->wz->saveLyrics($file, Lyrics::fromText(implode("\n", $long)) ?? new Lyrics());        $task = $this->wz->translate();        $this->game->run(3);        self::assertTrue($task->isOk(), (string)$task->error()?->getMessage());        self::assertSame('mymemory', $this->wz->translationState()['engine']);        self::assertSame("FR 'This is the line number 1 of a long song that goes on", $this->wz->translations()[0], 'HTML entities decoded');        self::assertCount(30, $this->wz->translations());        foreach ($http->requests as $r) {            self::assertStringStartsWith(LyricsTranslator::MYMEMORY . '?', $r['url']);            parse_str((string)parse_url($r['url'], PHP_URL_QUERY), $q);            self::assertLessThanOrEqual(500, strlen((string)$q['q']), 'MyMemory takes 500 bytes at most');            self::assertSame('en|fr', $q['langpair']);            self::assertArrayNotHasKey('de', $q, 'never an e-mail');            self::assertSame('WinZik', $r['headers']['User-Agent']);        }        self::assertGreaterThan(5, count($http->requests));        // the quota over: a clear error, nothing written        $this->fakeInternet(fn () => self::json(['responseData' => ['translatedText' => 'MYMEMORY WARNING'], 'responseStatus' => 429, 'quotaFinished' => true]));        $task = $this->wz->translate(null, 'de');        $this->game->run(2);        self::assertNotNull($task->error());        self::assertStringContainsString('quota', $this->wz->translationState()['text']);        self::assertTrue($this->wz->hasTranslation(null, 'fr'), 'the French one kept');        @unlink($this->wz->lyricsCachePath($file));    }    public function testOnlyTheSongsWithLyricsArePlayedWhenAsked(): void    {        $this->startPlayer();        $this->fakeInternet(fn () => new HttpResponse(404, ''));        // winzik.mp3 has its .lyr; partage.mp3 gets lyrics without times; the two others none        $items = $this->wz->playlist->items;        $partage = $items[3];        foreach ($items as $f) {            @unlink($this->wz->lyricsCachePath($f));        }        $this->wz->refreshLyrics();        $this->wz->saveLyrics($partage, Lyrics::fromText(self::PLAIN) ?? new Lyrics());        self::assertSame([true, false, false, true], array_map(fn ($f) => $this->wz->hasLyrics($f), $items));        self::assertSame(2, $this->wz->songsWithLyrics());        $this->wz->setLyricsOnly(true);        self::assertTrue($this->wz->playsLyricsOnly());        $this->wz->playIndex(0);        $this->wz->next();        self::assertSame(3, $this->wz->playlist->active, 'archettes and chaine-hifi skipped');        $this->wz->next();        self::assertSame(0, $this->wz->playlist->active, 'round the list');        $this->wz->previous();        self::assertSame(3, $this->wz->playlist->active);        // Hasard: only them        $this->wz->playlist->shuffle = true;        for ($i = 0; $i < 10; $i++) {            $this->wz->next();            self::assertContains($this->wz->playlist->active, [0, 3]);        }        // the end of a song: the next one with lyrics        $this->wz->playlist->shuffle = false;        $this->wz->playIndex(3);        $this->wz->seek(max(0.0, $this->wz->player->duration() - 0.05));        $this->game->run(200);        self::assertSame(0, $this->wz->playlist->active, 'the end of the song went to the next one with lyrics (round the list)');        // a click on a song without lyrics plays it (the user chose it)        $this->wz->playIndex(1);        self::assertSame(1, $this->wz->playlist->active);        // none left with lyrics: it says so and stops        @unlink($this->wz->lyricsCachePath($partage));        $this->wz->playlist->items = [$items[1], $items[2]];        $this->wz->playlist->active = 0;        $this->wz->refreshLyrics();        $this->wz->next();        self::assertStringContainsString('Aucun morceau de la playlist n\'a de paroles', $this->wz->notice);        self::assertNotSame(\App\Audio\Player::PLAYING, $this->wz->player->state);        $this->wz->setLyricsOnly(false);        $this->wz->next();        self::assertSame(1, $this->wz->playlist->active);    }    public function testTheDotOfThePlaylistStartsASearchForThatSong(): void    {        $this->startPlayer();        $this->fakeInternet(fn (string $m, string $url) => str_contains($url, 'lrclib.net/api/search')            ? self::json([['id' => 9, 'trackName' => 'Archettes City', 'artistName' => 'X', 'plainLyrics' => "Une ligne\nDeux lignes"]])            : new HttpResponse(404, ''));        $file = $this->wz->playlist->items[1];        @unlink($this->wz->lyricsCachePath($file));        $this->wz->refreshLyrics();        $help = $this->wz->lyricsHelp($file);        self::assertSame($file, $help['file']);        self::assertFalse($help['hasLyrics']);        self::assertNotSame('', $help['title']);        self::assertNotNull($help['search']);        self::assertSame($this->wz->lyricsSearch, $help['search']);        $this->game->run(2);        self::assertTrue($help['search']->done());        self::assertCount(1, $help['search']->candidates);        // imported for that song (not the one playing)        self::assertNotNull($this->wz->importLyrics($help['search']->candidates[0], $file));        self::assertTrue($this->wz->hasLyrics($file));        self::assertFalse($this->wz->lyricsHelp($file)['synced']);        @unlink($this->wz->lyricsCachePath($file));    }}