98 lines
7.0 KiB
TypeScript
98 lines
7.0 KiB
TypeScript
/** 발음 정답 다시 만들기 (2026-11-04, docs/13): old rule readings → the checked 한글 표기표, in the DB and in saved quizzes. */
|
|
import { expect, test } from 'bun:test';
|
|
import { openDb } from './db';
|
|
import { tempDbPath } from './test-utils';
|
|
import { Catalog } from './collector/catalog';
|
|
import { redoReadings } from './songs/readings-redo';
|
|
|
|
function aliases(db: ReturnType<typeof openDb>, id: number, part: string) {
|
|
return db
|
|
.query<{ alias: string }, [number, string]>('SELECT alias FROM song_aliases WHERE song_id = ? AND part = ? ORDER BY alias')
|
|
.all(id, part)
|
|
.map((r) => r.alias);
|
|
}
|
|
|
|
test('DB songs: the old reader\'s readings and the singer bracket go, checked spellings come, an admin alias stays', () => {
|
|
const db = openDb(tempDbPath());
|
|
const c = new Catalog(db);
|
|
const [rod] = c.upsert({ title: 'Heaven', artists: [{ name: 'Rod Stewart' }], source: 't', ref: '1' });
|
|
const [crank] = c.upsert({ title: 'Crank That', artists: [{ name: 'Soulja Boy' }], source: 't', ref: '2' });
|
|
const add = db.query('INSERT INTO song_aliases (song_id, part, alias, source) VALUES (?, ?, ?, ?)');
|
|
// what the 2026-11-04 cleanup wrote with the old rule reader
|
|
add.run(rod!, 'artist', '로드 스투어트', 'auto');
|
|
add.run(rod!, 'title', '헤벤', 'auto');
|
|
add.run(rod!, 'artist', '로드', 'admin');
|
|
db.query("INSERT INTO title_reviews (song_id, kind, status, old_title, old_artist, new_title, new_artist, title_answers, artist_answers, created_at) VALUES (?, 'title', 'applied', ?, ?, ?, ?, '[]', '[]', 0)").run(
|
|
crank!,
|
|
'Crank That (Soulja Boy)',
|
|
'Soulja Boy',
|
|
'Crank That',
|
|
'Soulja Boy',
|
|
);
|
|
for (const a of ['Crank That (Soulja Boy)', 'Crank That Soulja Boy', 'Soulja Boy', '솔야 보이', '크랭크 댓', '크랭크 댓 솔야 보이']) add.run(crank!, 'title', a, 'auto');
|
|
add.run(crank!, 'artist', '솔야 보이', 'auto');
|
|
|
|
const dry = redoReadings(db, { dryRun: true });
|
|
expect(dry.songs.changed).toBe(2);
|
|
expect(aliases(db, rod!, 'artist')).toEqual(['로드', '로드 스투어트']);
|
|
|
|
redoReadings(db);
|
|
expect(aliases(db, rod!, 'artist')).toEqual(['로드', '로드 스튜어트', '로드스튜어트']);
|
|
expect(aliases(db, rod!, 'title')).toEqual(['헤븐']);
|
|
// the old title as collected stays; the singer in its bracket and every reading of it go
|
|
expect(aliases(db, crank!, 'title')).toEqual(['Crank That (Soulja Boy)', '크랭크 댓', '크랭크댓']);
|
|
expect(aliases(db, crank!, 'artist')).toEqual(['솔자 보이', '솔자보이']);
|
|
// a second run changes nothing
|
|
expect(redoReadings(db).songs.changed).toBe(0);
|
|
// a 표기표 fix later: a spelling the table had before (not the rule reader's) goes only when that table is passed
|
|
const before = { words: { heaven: ['헤에븐'] }, titles: {}, artists: {} };
|
|
db.query("INSERT INTO song_aliases (song_id, part, alias, source) VALUES (?, 'title', '헤에븐', 'auto')").run(rod!);
|
|
expect(redoReadings(db).songs.changed).toBe(0);
|
|
expect(redoReadings(db, { oldTables: [before] }).songs.dropped).toBe(1);
|
|
expect(aliases(db, rod!, 'title')).toEqual(['헤븐']);
|
|
});
|
|
|
|
test('saved quizzes: generated readings replaced, typed answers kept, 이지선다 quizzes left alone', () => {
|
|
const db = openDb(tempDbPath());
|
|
db.query("INSERT INTO users (id, kind, nickname, created_at, last_seen_at) VALUES ('u', 'member', 'u', 0, 0)").run();
|
|
const normal = (db.query("INSERT INTO custom_quizzes (owner_id, name, description, visibility, settings_json, kind, created_at, updated_at) VALUES ('u', 'q', '', 'private', '{}', 'normal', 0, 0) RETURNING id").get() as { id: number }).id;
|
|
const duel = (db.query("INSERT INTO custom_quizzes (owner_id, name, description, visibility, settings_json, kind, created_at, updated_at) VALUES ('u', 'd', '', 'private', '{}', 'duel', 0, 0) RETURNING id").get() as { id: number }).id;
|
|
const item = db.query(
|
|
"INSERT INTO custom_quiz_items (quiz_id, pos, video_id, title, artist, title_answers, artist_answers) VALUES (?, ?, 'aaaaaaaaaaa', ?, ?, ?, ?) RETURNING id",
|
|
);
|
|
const a = (item.get(normal, 0, 'Dreamers', 'Jung Kook', JSON.stringify(['드리머즈 노래']), JSON.stringify(['융 쿠크', '정꾹이'])) as { id: number }).id;
|
|
const b = (item.get(duel, 0, 'Dreamers', 'Jung Kook', '[]', '[]') as { id: number }).id;
|
|
const r = redoReadings(db);
|
|
expect(r.quizItems.changed).toBe(1);
|
|
const got = (id: number) => db.query<{ t: string; a: string }, [number]>('SELECT title_answers t, artist_answers a FROM custom_quiz_items WHERE id = ?').get(id)!;
|
|
expect(JSON.parse(got(a).t)).toEqual(['드리머즈 노래', '드리머스', '드리머즈']);
|
|
expect(JSON.parse(got(a).a)).toEqual(['정꾹이', '정국']);
|
|
expect(got(b)).toEqual({ t: '[]', a: '[]' });
|
|
});
|
|
|
|
test('제목 정리 rows still waiting get the new spellings too: applying one later does not bring the old reading back', async () => {
|
|
const { applyTitleReviews } = await import('./songs/title-review');
|
|
const db = openDb(tempDbPath());
|
|
const [id] = new Catalog(db).upsert({ title: 'Heaven (feat. X)', artists: [{ name: 'Rod Stewart' }], source: 't', ref: '1' });
|
|
db.query(
|
|
"INSERT INTO title_reviews (song_id, kind, status, old_title, old_artist, new_title, new_artist, title_answers, artist_answers, created_at) VALUES (?, 'title', 'pending', 'Heaven (feat. X)', 'Rod Stewart', 'Heaven', 'Rod Stewart', ?, ?, 0)",
|
|
).run(id!, JSON.stringify(['헤벤']), JSON.stringify(['로드 스투어트']));
|
|
expect(redoReadings(db).pendingReviews).toBe(1);
|
|
expect(applyTitleReviews(db, { ids: [id!] }, 'admin').applied).toBe(1);
|
|
expect(aliases(db, id!, 'title')).toEqual(['Heaven (feat. X)', '헤븐']);
|
|
expect(aliases(db, id!, 'artist')).toEqual(['로드 스튜어트', '로드스튜어트']);
|
|
});
|
|
|
|
test('saved quizzes: an answer the song DB has from a person (quiz save / admin) stays even when it equals an old generated reading', () => {
|
|
const db = openDb(tempDbPath());
|
|
db.query("INSERT INTO users (id, kind, nickname, created_at, last_seen_at) VALUES ('u', 'member', 'u', 0, 0)").run();
|
|
const quiz = (db.query("INSERT INTO custom_quizzes (owner_id, name, description, visibility, settings_json, kind, created_at, updated_at) VALUES ('u', 'q', '', 'private', '{}', 'normal', 0, 0) RETURNING id").get() as { id: number }).id;
|
|
const [song] = new Catalog(db).upsert({ title: 'Heaven', artists: [{ name: '에일리' }], source: 't', ref: '1' });
|
|
db.query("INSERT INTO song_aliases (song_id, part, alias, source) VALUES (?, 'title', '헤벤', 'quiz')").run(song!);
|
|
const it = (db.query("INSERT INTO custom_quiz_items (quiz_id, pos, song_id, video_id, title, artist, title_answers, artist_answers) VALUES (?, 0, ?, 'aaaaaaaaaaa', 'Heaven', '에일리', ?, '[]') RETURNING id").get(quiz, song!, JSON.stringify(['헤벤'])) as { id: number }).id;
|
|
redoReadings(db);
|
|
expect(JSON.parse(db.query<{ t: string }, [number]>('SELECT title_answers t FROM custom_quiz_items WHERE id = ?').get(it)!.t)).toEqual(['헤벤', '헤븐']);
|
|
// the song's own typed alias is never touched either
|
|
expect(aliases(db, song!, 'title')).toEqual(['헤벤', '헤븐']);
|
|
});
|