/** 제목 정리 검토 (docs/13): DB songs get cleaned names and generated answers — reviewed first, new songs at once. */ import { expect, test } from 'bun:test'; import { openDb } from './db'; import { tempDbPath, boot, api, discordLogin, guest, TEST_SUPERADMINS } from './test-utils'; import { Catalog } from './collector/catalog'; import { applyTitleReviews, buildTitleReviews, listTitleReviews, skipTitleReviews } from './songs/title-review'; import { isCorrect } from '@mq/shared'; import { quizSong } from './songs/quiz'; const wiki = async (names: string[]) => new Map(names.flatMap((n): [string, string][] => (n === '夜に駆ける' ? [[n, '밤을 달리다']] : n === 'YOASOBI' ? [] : []))); function seed() { const db = openDb(tempDbPath()); const c = new Catalog(db); const add = (title: string, artist: string) => c.upsert({ title, artists: [{ name: artist }], source: 't', ref: title })[0]!; return { db, ids: { bracket: add('사랑을 했다(LOVE SCENARIO)', '아이콘'), feat: add('Finesse (Feat. Cardi B)', 'Bruno Mars'), english: add('Pretender', 'Official髭男dism'), ja: add('夜に駆ける', 'YOASOBI'), plain: add('좋은 날', '아이유'), }, }; } test('first run (--review): everything that would change waits; nothing is written to the songs yet', async () => { const { db, ids } = seed(); const r = await buildTitleReviews(db, { review: true, wiki }); expect(r).toEqual({ songs: 5, same: 1, pending: 4, auto: 0 }); const list = listTitleReviews(db, {}); expect(list.total).toBe(4); const row = (id: number) => list.rows.find((x) => x.songId === id)!; expect(row(ids.bracket)).toMatchObject({ kind: 'title', newTitle: '사랑을 했다', titleAnswers: ['LOVE SCENARIO', '사랑을 했다 LOVE SCENARIO', '러브 시나리오'] }); expect(row(ids.feat)).toMatchObject({ kind: 'title', newTitle: 'Finesse', titleAnswers: ['피네스'] }); // a Japanese artist without a Korean name: 확인 필요 comes first expect(row(ids.english)).toMatchObject({ kind: 'check', newTitle: 'Pretender', titleAnswers: ['프리텐더'], note: '확인 필요' }); expect(row(ids.ja)).toMatchObject({ kind: 'japanese', newTitle: '밤을 달리다', newArtist: 'YOASOBI' }); expect(list.counts).toEqual({ title: 2, japanese: 1, check: 1 }); expect(db.query<{ title: string }, [number]>('SELECT title FROM songs WHERE id = ?').get(ids.bracket)!.title).toBe('사랑을 했다(LOVE SCENARIO)'); // a second run does not ask again expect((await buildTitleReviews(db, { review: true, wiki })).songs).toBe(0); }); test('적용: names and answers written (the old name stays an answer), the game accepts them; 건너뛰기; edited songs are not applied', async () => { const { db, ids } = seed(); await buildTitleReviews(db, { review: true, wiki }); // an admin renamed this one meanwhile db.query("UPDATE songs SET title = 'Finesse!' WHERE id = ?").run(ids.feat); const r = applyTitleReviews(db, { kind: 'title' }, 'admin'); expect(r).toEqual({ applied: 1, failed: [{ id: ids.feat, why: '그사이 제목·가수가 바뀐 곡이에요 (다시 만들기로 새로 확인해 주세요)' }] }); const song = quizSong(db, ids.bracket)!; expect(song.title).toBe('사랑을 했다'); const st = { space: false, number: true, symbol: false }; for (const t of ['사랑을 했다', 'love scenario', '러브 시나리오', '사랑을 했다(LOVE SCENARIO)']) expect([t, isCorrect(t, song, 'title', st)]).toEqual([t, true]); // the Japanese song with an admin's edit of the shown title expect(applyTitleReviews(db, { ids: [ids.ja], edits: [{ id: ids.ja, title: '밤을 달리다!' }] }, 'admin').applied).toBe(1); const ja = quizSong(db, ids.ja)!; expect(ja.title).toBe('밤을 달리다!'); for (const t of ['夜に駆ける', '요루니 카케루', '밤을 달리다']) expect([t, isCorrect(t, ja, 'title', st)]).toEqual([t, true]); expect(skipTitleReviews(db, [ids.english], 'admin')).toBe(1); // only the song renamed meanwhile still waits expect(listTitleReviews(db, {}).rows.map((x) => x.songId)).toEqual([ids.feat]); expect(listTitleReviews(db, { status: 'done' }).total).toBe(3); }); test('new songs (no --review): applied at once, Japanese and 확인 필요 ones wait', async () => { const { db, ids } = seed(); const r = await buildTitleReviews(db, { wiki }); expect(r).toEqual({ songs: 5, same: 1, pending: 2, auto: 2 }); expect(db.query<{ title: string }, [number]>('SELECT title FROM songs WHERE id = ?').get(ids.feat)!.title).toBe('Finesse'); expect(listTitleReviews(db, {}).rows.map((x) => x.songId).sort()).toEqual([ids.english, ids.ja].sort()); }); test('관리자 API: list, apply a kind, skip; guests may not', async () => { const s = boot({ discordExchange: async (code: string) => ({ id: code, username: 'u', global_name: '관리', avatar: null }) }, { DISCORD_CLIENT_ID: 'cid', DISCORD_CLIENT_SECRET: 'sec' }); try { const c = new Catalog(s.db); const [id] = c.upsert({ title: 'Seven (feat. Latto) - Clean Ver.', artists: [{ name: '정국' }], source: 't', ref: '1' }); await buildTitleReviews(s.db, { review: true, wiki }); const cookie = (await discordLogin(s.base, TEST_SUPERADMINS[0])).cookie!; const list = await api(s.base, '/api/admin/title-reviews', { cookie }); expect(list.body.counts).toEqual({ title: 1 }); expect(list.body.rows[0]).toMatchObject({ songId: id, newTitle: 'Seven', titleAnswers: ['세븐'] }); const r = await api(s.base, '/api/admin/title-reviews/apply', { method: 'POST', cookie, body: JSON.stringify({ kind: 'title' }) }); expect(r.body).toEqual({ applied: 1, failed: [] }); expect((await api(s.base, '/api/admin/title-reviews/skip', { method: 'POST', cookie, body: JSON.stringify({ ids: [id] }) })).body).toEqual({ skipped: 0 }); const g = await guest(s.base, '손님'); expect((await api(s.base, '/api/admin/title-reviews', { cookie: g.cookie })).res.status).toBe(403); } finally { await s.stop(); } });