diff --git a/apps/server/src/answers/answers.test.ts b/apps/server/src/answers/answers.test.ts index 237b202..a296d9e 100644 --- a/apps/server/src/answers/answers.test.ts +++ b/apps/server/src/answers/answers.test.ts @@ -5,6 +5,9 @@ import { kanaToHangul, romajiToHangul } from './ja-ko'; import { readJapanese } from './ja-reading'; import { wikiLookup } from './wiki'; import { songAnswersBatch } from '.'; +import { openDb } from '../db'; +import { tempDbPath } from '../test-utils'; +import { startImport } from '../quizzes/playlist'; test('영어 → 한글: 외래어 표기 규칙과 흔히 쓰는 표기', () => { const cases: Record = { @@ -106,3 +109,48 @@ test('곡 정답: cleaned title, other names, 한글 발음; J-POP shown by its const [asIs] = await songAnswersBatch(null, [{ title: '夜に駆ける', artist: 'YOASOBI' }]); expect(asIs).toMatchObject({ title: '夜に駆ける', titleAnswers: ['요루니 카케루', '요루니 가케루'] }); }); + +test('a Japanese title already shown by its bracket\'s Korean name still asks Wikipedia first (the original decides)', async () => { + const wiki = async (names: string[]) => new Map(names.includes('夜に駆ける') ? [['夜に駆ける', '밤을 달리다']] : []); + const [a] = await songAnswersBatch(null, [{ title: '夜に駆ける (밤에 달리다)', artist: 'YOASOBI' }], { korean: true, wiki }); + expect(a!.title).toBe('밤을 달리다'); + expect(a!.titleAnswers).toEqual(expect.arrayContaining(['夜に駆ける', '밤에 달리다', '요루니 카케루', '요루니 가케루'])); + expect(a!.note).toBeNull(); + // no Wikipedia article: the bracket's Korean name is shown + const [b] = await songAnswersBatch(null, [{ title: '夜に駆ける (밤에 달리다)', artist: 'YOASOBI' }], { korean: true, wiki: async () => new Map() }); + expect(b).toMatchObject({ title: '밤에 달리다', note: null }); + expect(b!.titleAnswers).toEqual(expect.arrayContaining(['夜に駆ける', '요루니 카케루'])); +}); + +test('플레이리스트 가져오기: the answers come from the title as found (brackets, Japanese original), the row shows it cleaned', async () => { + const db = openDb(tempDbPath()); + const playlist = { + loadType: 'playlist', + data: { + info: { name: 'p' }, + tracks: [ + { info: { identifier: 'lovescen001', title: '사랑을 했다(LOVE SCENARIO)', author: 'iKON - Topic', length: 210000, sourceName: 'youtube' } }, + { info: { identifier: 'yoruni00001', title: '夜に駆ける', author: 'YOASOBI - Topic', length: 260000, sourceName: 'youtube' } }, + ], + }, + }; + const job = await startImport( + { + db, + lavalink: { url: 'http://ll.test', password: 'x' }, + search: async () => [], + embed: async () => ({ state: 'ok', author: 'x', title: 'x' }), + delayMs: 0, + fetchImpl: (async () => Response.json(playlist)) as unknown as typeof fetch, + wiki: async (names) => new Map(names.includes('夜に駆ける') ? [['夜に駆ける', '밤을 달리다']] : []), + }, + 'me', + 'https://youtube.com/playlist?list=x', + ); + if (typeof job === 'string') throw new Error(job); + for (let i = 0; i < 200 && job.status === 'running'; i++) await Bun.sleep(20); + expect(job.status).toBe('done'); + expect(job.rows[0]).toMatchObject({ title: '사랑을 했다', titleAnswers: ['LOVE SCENARIO', '사랑을 했다 LOVE SCENARIO', '러브 시나리오'] }); + expect(job.rows[1]!.title).toBe('밤을 달리다'); + expect(job.rows[1]!.titleAnswers).toEqual(expect.arrayContaining(['夜に駆ける', '요루니 카케루', '요루니 가케루'])); +}); diff --git a/apps/server/src/answers/index.ts b/apps/server/src/answers/index.ts index bd598f6..779a697 100644 --- a/apps/server/src/answers/index.ts +++ b/apps/server/src/answers/index.ts @@ -108,9 +108,17 @@ export async function songAnswersBatch(db: Database | null, songs: { title: stri } if (!wikiNames.has(name) && !bracket) notes.push('한글 이름 자동 변환'); push.push(name); + // the bracket's Korean name stays an answer when Wikipedia's wins + if (bracket && bracket !== ko) push.push(bracket); return ko; }; - if (opts.korean && hasJapanese(title)) title = korean(title, c.aliases.find((a) => HANGUL.test(a) && !hasJapanese(a)), extraTitle); + // the Japanese title: shown as is, or already swapped for its bracket's Korean name by cleanTitle + // ("夜に駆ける (밤에 달리다)") — Wikipedia still goes first, so the order is decided on the original + const jaTitle = hasJapanese(c.title) ? c.title : c.aliases.find((a) => hasJapanese(a)); + if (opts.korean && jaTitle) { + const bracket = hasJapanese(c.title) ? c.aliases.find((a) => HANGUL.test(a) && !hasJapanese(a)) : c.title; + title = korean(jaTitle, bracket, extraTitle); + } if (opts.korean && hasJapanese(artist)) artist = korean(artist, undefined, extraArtist); const titleAnswers = uniq([...c.aliases, ...extraTitle, ...fromDb.title, ...[c.title, ...c.aliases, ...extraTitle, ...fromDb.title].flatMap(say)], [title]); const names = artistNames(s.artist); diff --git a/apps/server/src/quizzes/playlist.ts b/apps/server/src/quizzes/playlist.ts index 552268c..3674f99 100644 --- a/apps/server/src/quizzes/playlist.ts +++ b/apps/server/src/quizzes/playlist.ts @@ -138,6 +138,7 @@ async function runImport(d: ImportDeps, job: ImportJob, tracks: LavalinkTrack[]) 'SELECT id, title, artist, youtube_id, youtube_status, duration_sec FROM songs WHERE match_key = ? AND deleted_at IS NULL ORDER BY youtube_status = \'matched\' DESC LIMIT 1', ); const aliases = aliasesOf(d.db); + const rawTitles: string[] = []; for (const [i, t] of tracks.entries()) { const dur = t.length ? Math.round(t.length / 1000) : null; const yt = t.sourceName === 'youtube'; @@ -177,6 +178,8 @@ async function runImport(d: ImportDeps, job: ImportJob, tracks: LavalinkTrack[]) if (i < tracks.length - 1) await Bun.sleep(d.delayMs ?? 700); } // shown title without brackets / feat. / version marks (the answers come at the end, docs/13) + // the title as found goes to the answers below (its brackets hold other names), the row shows it cleaned meanwhile + rawTitles.push(row.title); row.title = cleanTitle(row.title).title; job.rows.push(row); job.done = i + 1; @@ -184,7 +187,7 @@ async function runImport(d: ImportDeps, job: ImportJob, tracks: LavalinkTrack[]) // answers for every song at once: 한글 발음, 일본어 읽기 (one process), J-POP 한글 이름 (위키백과) const answers = await songAnswersBatch( d.db, - job.rows.map((r) => ({ title: r.title, artist: r.artist, songId: r.songId })), + job.rows.map((r, i) => ({ title: rawTitles[i]!, artist: r.artist, songId: r.songId })), { korean: true, wiki: d.wiki, read: d.read }, ); job.rows.forEach((r, i) => {