Files
music_quiz/apps/server/src/collector/collector.test.ts
EJClaw cec9f8bdc9 3단계 노래 DB: 곡 스키마(장르·기준별 단계·가사·후보), 노라보세·스타맵 가져오기, 유튜브 원곡 찾기(쿠키 노래 검색 + 로그인 없는 Topic 대체), 관리자 노래·수집 화면
- 노라보세: 8,572곡(멜론 ID·연도·최고순위·장르·5단계 난이도·가사), 500 나는 묶음은 반씩 나눠 재요청, 원본 캐시로 재실행 안전
- 스타맵 803곡: 같은 곡은 합치고(363) 새 곡 440, 맵 제작자 연도(올해)는 버림
- 유튜브: 제목·가수·길이 점수로 확정/후보, 퍼가기 금지 영상 제외, 검색 방식별로 시도 기록
- 관리자: 노래 검색·장르·연도·난이도·유튜브 상태·출처 필터, 편집 패널(미리듣기·후보 선택·하이라이트·별칭·숨김), 수집 기록
- 테스트 32개, e2e에 노래 편집 단계 추가

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 20:31:43 +09:00

114 lines
6.6 KiB
TypeScript

import { expect, test } from 'bun:test';
import { mkdtempSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { openDb } from '../db';
import { Catalog } from './catalog';
import { genresOf, provisionalLevel } from './norabose';
import { importSta } from './sta';
import { matchYoutube } from './match-youtube';
import { isConfident, scoreResult } from './score';
import { parseDuration, parseSearch, type YtmSong } from './ytmusic';
import { catalogStats, listSongs } from '../songs/store';
import { tempDbPath } from '../test-utils';
test('norabose genre bits and provisional levels', () => {
expect(genresOf(1)).toEqual(['ballad', 'kpop']);
expect(genresOf(512 | 2).sort()).toEqual(['dance', 'pop']);
expect(genresOf(8192)).toEqual(['jpop']);
expect(provisionalLevel(0b00001, 3)).toBe(1);
expect(provisionalLevel(0b00001, 40)).toBe(2);
expect(provisionalLevel(0b10100, 50)).toBe(6);
expect(provisionalLevel(0, 1)).toBeNull();
});
test('YouTube Music search parsing', () => {
const row = (videoId: string, title: string, meta: string) => ({
musicResponsiveListItemRenderer: {
playlistItemData: { videoId },
flexColumns: [
{ musicResponsiveListItemFlexColumnRenderer: { text: { runs: [{ text: title }] } } },
{ musicResponsiveListItemFlexColumnRenderer: { text: { runs: meta.split(/( • )/).map((text) => ({ text })) } } },
],
},
});
const r = parseSearch({ contents: [{ x: [row('Wu5FY8J6P8Q', 'Love wins all', '아이유 • The Winning • 4:32'), row('aaaaaaaaaaa', 'no meta', '')] }] });
expect(r[0]).toEqual({ videoId: 'Wu5FY8J6P8Q', title: 'Love wins all', artists: '아이유', album: 'The Winning', durationSec: 272 });
expect(parseDuration('1:02:03')).toBe(3723);
});
test('scoring: title+artist confident, MR/cover not, wrong length not', () => {
const song = { title: '광 (狂)', artist: 'SG워너비', durationSec: 237 };
const yt = (title: string, artists: string, durationSec: number): YtmSong => ({ videoId: 'x', title, artists, album: null, durationSec });
expect(isConfident(scoreResult(song, yt('광 (狂)', 'SG워너비', 237), 0))).toBe(true);
expect(isConfident(scoreResult(song, yt('광', 'SG Wannabe', 236), 1))).toBe(true); // title + exact length
expect(isConfident(scoreResult(song, yt('광 (Inst.)', 'SG워너비', 237), 0))).toBe(false);
expect(isConfident(scoreResult(song, yt('광', 'SG워너비', 300), 0))).toBe(false);
expect(isConfident(scoreResult(song, yt('다른 노래', 'SG워너비', 237), 0))).toBe(false);
});
function freshDb() {
return openDb(tempDbPath());
}
test('upsert merges the same song from two sources and never duplicates', () => {
const db = freshDb();
const c = new Catalog(db);
const [a, newA] = c.upsert({ melonId: 100, title: '사건의 지평선', artists: [{ name: '윤하', melonArtistId: 7 }], year: 2022, genres: ['ballad', 'kpop'], source: 'norabose', ref: '100' });
const [b, newB] = c.upsert({ title: '사건의지평선', artists: [{ name: '윤하' }], year: 2026, genres: ['kpop'], source: 'sta', ref: '5' });
expect(newA).toBe(true);
expect(newB).toBe(false);
expect(b).toBe(a);
const s = db.query<{ year: number }, []>('SELECT year FROM songs').get()!;
expect(s.year).toBe(2022); // the second source only fills gaps
expect(catalogStats(db).sources).toEqual({ norabose: 1, sta: 1 });
c.autoLevel(a, 'all', 3);
db.query("UPDATE song_levels SET level = 9, source = 'admin' WHERE song_id = ?").run(a);
c.autoLevel(a, 'all', 4);
expect(db.query<{ level: number }, []>('SELECT level FROM song_levels').get()!.level).toBe(9); // admin level kept
});
test('sta import skips map-maker years and merges duplicates', async () => {
const db = freshDb();
new Catalog(db).upsert({ melonId: 1, title: 'Candy Man', artists: [{ name: '브라운아이드걸스' }], year: 2009, source: 'norabose', ref: '1' });
const dir = mkdtempSync(join(process.env.TMPDIR ?? '/tmp', 'mq-sta-'));
const seed = join(dir, 'sta.json');
writeFileSync(seed, JSON.stringify({ songs: [
{ id: 1, title: 'Candy Man', artist: '브라운아이드걸스', year: 2009, found_in: ['a'] },
{ id: 2, title: 'Vitamin ME', artist: '프로미스나인', year: new Date().getFullYear(), found_in: ['a'] },
] }));
expect(await importSta(db, seed)).toEqual({ total: 2, created: 1 });
const v = db.query<{ year: number | null }, [string]>('SELECT year FROM songs WHERE title = ?').get('Vitamin ME')!;
expect(v.year).toBeNull();
});
test('YouTube matching: confident → matched, unsure → candidate fallback, noembed skipped', async () => {
const db = freshDb();
const c = new Catalog(db);
const [s1] = c.upsert({ title: 'Love wins all', artists: [{ name: '아이유' }], durationSec: 272, source: 't', ref: '1' });
const [s2] = c.upsert({ title: '한사람을 위해', artists: [{ name: '안재모' }], durationSec: 293, source: 't', ref: '2' });
db.query("INSERT INTO youtube_candidates (song_id, video_id, score, source) VALUES (?, 'fanvideo001', 0, 'norabose')").run(s2);
const [s3] = c.upsert({ title: '없는노래', artists: [{ name: '누구' }], source: 't', ref: '3' });
const results: Record<string, YtmSong[]> = {
'아이유 Love wins all': [
{ videoId: 'blockedvid1', title: 'Love wins all', artists: '아이유', album: null, durationSec: 272 },
{ videoId: 'Wu5FY8J6P8Q', title: 'Love wins all', artists: '아이유', album: 'The Winning', durationSec: 272 },
],
'안재모 한사람을 위해': [{ videoId: 'othersong01', title: '다른 노래', artists: '안재모', album: null, durationSec: 200 }],
'누구 없는노래': [],
};
const stats = await matchYoutube(
db,
{ searchSongs: async (q) => results[q] ?? [] },
{ delayMs: 0 },
async (id) => (id === 'blockedvid1' ? { state: 'noembed' } : { state: 'ok', author: id === 'Wu5FY8J6P8Q' ? 'IU - Topic' : 'fan', title: id }),
);
expect(stats).toEqual({ matched: 1, candidate: 1, none: 1 });
const row = (id: number) => db.query<{ youtube_id: string | null; youtube_status: string; youtube_channel: string | null }, [number]>('SELECT youtube_id, youtube_status, youtube_channel FROM songs WHERE id = ?').get(id)!;
expect(row(s1)).toEqual({ youtube_id: 'Wu5FY8J6P8Q', youtube_status: 'matched', youtube_channel: 'IU - Topic' });
expect(row(s2)).toMatchObject({ youtube_id: 'fanvideo001', youtube_status: 'candidate' });
expect(row(s3).youtube_status).toBe('none');
// Re-run skips songs tried recently.
expect(await matchYoutube(db, { searchSongs: async () => { throw new Error('should not search'); } }, { delayMs: 0 })).toEqual({ matched: 0, candidate: 0, none: 0 });
expect(listSongs(db, { youtube: 'matched' }).total).toBe(1);
});