From c018bc5a9df8ab0823caaf728e97d93791fb0c9e Mon Sep 17 00:00:00 2001
From: EJClaw
Date: Sun, 18 Oct 2026 20:36:27 +0900
Subject: [PATCH] =?UTF-8?q?=EA=B4=80=EB=A6=AC=EC=9E=90=20=ED=8E=B8?=
=?UTF-8?q?=EC=A7=91=20=EB=B3=B4=ED=98=B8:=20=EC=9E=90=EB=8F=99=20?=
=?UTF-8?q?=EB=A7=A4=EC=B9=AD=EC=9D=80=20=EA=B4=80=EB=A6=AC=EC=9E=90?=
=?UTF-8?q?=EA=B0=80=20=EC=A0=95=ED=95=9C=20=EC=98=81=EC=83=81=EC=9D=84=20?=
=?UTF-8?q?=EB=8D=AE=EC=96=B4=EC=93=B0=EC=A7=80=20=EC=95=8A=EC=9D=8C(?=
=?UTF-8?q?=EC=93=B0=EA=B8=B0=20=EC=8B=9C=EC=A0=90=20=EC=A1=B0=EA=B1=B4?=
=?UTF-8?q?=C2=B7=EC=8B=A4=EC=A0=9C=20=EA=B0=B1=EC=8B=A0=EB=A7=8C=20?=
=?UTF-8?q?=EC=A7=91=EA=B3=84),=20=EB=8B=A4=EC=8B=9C=20=EA=B0=80=EC=A0=B8?=
=?UTF-8?q?=EC=99=80=EB=8F=84=20=EA=B4=80=EB=A6=AC=EC=9E=90=EA=B0=80=20?=
=?UTF-8?q?=EC=A0=95=ED=95=9C=20=EC=9E=A5=EB=A5=B4(=EB=B9=88=20=EC=84=A0?=
=?UTF-8?q?=ED=83=9D=20=ED=8F=AC=ED=95=A8)=20=EC=9C=A0=EC=A7=80,=20?=
=?UTF-8?q?=EC=95=88=20=EB=B0=94=EA=BE=BC=20=EC=9C=A0=ED=8A=9C=EB=B8=8C=20?=
=?UTF-8?q?=EC=A3=BC=EC=86=8C=EB=8A=94=20=EB=B3=B4=EB=82=B4=EC=A7=80=20?=
=?UTF-8?q?=EC=95=8A=EC=9D=8C=20+=20=ED=9B=84=EB=B3=B4=20=ED=99=95?=
=?UTF-8?q?=EC=A0=95=20=EB=B2=84=ED=8A=BC,=20=ED=9A=8C=EA=B7=80=20?=
=?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=203=EA=B0=9C=C2=B7e2e?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-Authored-By: Claude Opus 5.5
---
apps/server/src/collector/admin-guard.test.ts | 60 +++++++++++++++++++
apps/server/src/collector/catalog.ts | 4 +-
apps/server/src/collector/match-youtube.ts | 22 ++++---
.../src/db/migrations/003_genres_source.sql | 2 +
apps/server/src/songs/store.ts | 4 +-
apps/web/src/pages/admin/SongsPage.tsx | 28 ++++++++-
e2e/login.e2e.ts | 12 ++++
7 files changed, 121 insertions(+), 11 deletions(-)
create mode 100644 apps/server/src/collector/admin-guard.test.ts
create mode 100644 apps/server/src/db/migrations/003_genres_source.sql
diff --git a/apps/server/src/collector/admin-guard.test.ts b/apps/server/src/collector/admin-guard.test.ts
new file mode 100644
index 0000000..5b1505e
--- /dev/null
+++ b/apps/server/src/collector/admin-guard.test.ts
@@ -0,0 +1,60 @@
+/** Regressions: automatic collectors must never undo an admin's edits (review 2026-10-18). */
+import { afterEach, expect, test } from 'bun:test';
+import { openDb } from '../db';
+import { Catalog } from './catalog';
+import { matchYoutube } from './match-youtube';
+import { updateSong } from '../songs/store';
+import { TEST_SUPERADMINS, api, boot, discordLogin, tempDbPath } from '../test-utils';
+
+test('an admin URL saved while the matcher is searching is not overwritten', async () => {
+ const db = openDb(tempDbPath());
+ const [id] = new Catalog(db).upsert({ title: 'Love wins all', artists: [{ name: '아이유' }], durationSec: 272, source: 't', ref: '1' });
+ const stats = await matchYoutube(
+ db,
+ {
+ searchSongs: async () => {
+ // the admin saves a video while the search is in flight
+ updateSong(db, id, { youtubeId: 'ADMINvideo1' }, { author: 'IU - Topic', title: 'Love wins all' });
+ return [{ videoId: 'Wu5FY8J6P8Q', title: 'Love wins all', artists: '아이유', album: null, durationSec: 272 }];
+ },
+ },
+ { delayMs: 0 },
+ async () => ({ state: 'ok', author: 'auto', title: 'auto' }),
+ );
+ const row = db.query<{ youtube_id: string; youtube_source: string }, [number]>('SELECT youtube_id, youtube_source FROM songs WHERE id = ?').get(id)!;
+ expect(row).toEqual({ youtube_id: 'ADMINvideo1', youtube_source: 'admin' });
+ expect(stats.matched).toBe(0);
+});
+
+test('re-import does not bring back genres the admin removed (also an empty choice)', () => {
+ const db = openDb(tempDbPath());
+ const c = new Catalog(db);
+ const song = { melonId: 5, title: '광', artists: [{ name: 'SG워너비' }], genres: ['ballad', 'kpop'], source: 'norabose', ref: '5' };
+ const [a] = c.upsert(song);
+ updateSong(db, a, { genres: ['rock'] });
+ c.upsert(song);
+ const genres = (id: number) => db.query<{ genre_id: string }, [number]>('SELECT genre_id FROM song_genres WHERE song_id = ? ORDER BY genre_id').all(id).map((r) => r.genre_id);
+ expect(genres(a)).toEqual(['rock']);
+ updateSong(db, a, { genres: [] });
+ c.upsert(song);
+ expect(genres(a)).toEqual([]);
+});
+
+let s: ReturnType | null = null;
+afterEach(async () => {
+ await s?.stop();
+ s = null;
+});
+
+test('saving only a note on a song without video keeps it in the automatic search', async () => {
+ s = boot({ discordExchange: async (code) => ({ id: code, username: 'u', global_name: '관리', avatar: null }) }, { DISCORD_CLIENT_ID: 'c', DISCORD_CLIENT_SECRET: 's' });
+ const [id] = new Catalog(s.db).upsert({ title: '없는영상', artists: [{ name: '누구' }], source: 't', ref: '1' });
+ const sa = await discordLogin(s.base, TEST_SUPERADMINS[0]);
+ // what the editor used to send for a song with no video: youtube: null alongside the note
+ const r = await api(s.base, `/api/admin/songs/${id}`, { method: 'PATCH', cookie: sa.cookie, body: JSON.stringify({ note: '메모', youtube: null }) });
+ expect(r.res.status).toBe(200);
+ expect(r.body.song.youtube_source).toBeNull();
+ let searches = 0;
+ await matchYoutube(s.db, { searchSongs: async () => (searches++, []) }, { delayMs: 0 });
+ expect(searches).toBe(1);
+});
diff --git a/apps/server/src/collector/catalog.ts b/apps/server/src/collector/catalog.ts
index 2c45d00..289b9e2 100644
--- a/apps/server/src/collector/catalog.ts
+++ b/apps/server/src/collector/catalog.ts
@@ -62,7 +62,9 @@ export class Catalog {
this.db.query('INSERT OR IGNORE INTO song_artists (song_id, artist_id, role) VALUES (?, ?, ?)').run(id, this.artistId(a.name, a.melonArtistId), 'main');
});
}
- for (const g of s.genres ?? []) this.db.query('INSERT OR IGNORE INTO song_genres (song_id, genre_id, source) VALUES (?, ?, ?)').run(id, g, s.source);
+ // Genres an admin has set (even to none) are final: imports only add genres to songs nobody edited.
+ const adminGenres = this.db.query<{ g: string | null }, [number]>('SELECT genres_source g FROM songs WHERE id = ?').get(id)?.g === 'admin';
+ if (!adminGenres) for (const g of s.genres ?? []) this.db.query('INSERT OR IGNORE INTO song_genres (song_id, genre_id, source) VALUES (?, ?, ?)').run(id, g, s.source);
if (s.lyrics) this.db.query('INSERT OR IGNORE INTO lyrics (song_id, text, source, fetched_at) VALUES (?, ?, ?, ?)').run(id, s.lyrics, s.source, now);
this.db.query('INSERT OR IGNORE INTO song_sources (song_id, source, ref) VALUES (?, ?, ?)').run(id, s.source, s.ref);
return [id, created];
diff --git a/apps/server/src/collector/match-youtube.ts b/apps/server/src/collector/match-youtube.ts
index 9faab99..fe5681c 100644
--- a/apps/server/src/collector/match-youtube.ts
+++ b/apps/server/src/collector/match-youtube.ts
@@ -41,13 +41,20 @@ export async function matchYoutube(db: Database, ytm: Pick
db
.query(
`UPDATE songs SET youtube_id = ?, youtube_status = ?, youtube_source = ?, youtube_channel = ?, youtube_title = ?, embeddable = 1,
- youtube_checked_at = ?, updated_at = ? WHERE id = ?`,
+ youtube_checked_at = ?, updated_at = ?
+ WHERE id = ? AND COALESCE(youtube_source, '') <> 'admin' AND youtube_status IN ('none','candidate')`,
)
- .run(v.videoId, status, source, v.author, v.title, Date.now(), Date.now(), id);
+ .run(v.videoId, status, source, v.author, v.title, Date.now(), Date.now(), id).changes === 1;
+ const adminOwned = (id: number) =>
+ db.query<{ n: number }, [number]>("SELECT COUNT(*) n FROM songs WHERE id = ? AND COALESCE(youtube_source, '') = 'admin'").get(id)!.n > 0;
try {
for (const [i, s] of rows.entries()) {
let results: YtmSong[];
@@ -72,13 +79,15 @@ export async function matchYoutube(db: Database, ytm: Pick(null);
const [form, setForm] = useState({ title: '', artist: '', year: '', level: '', youtube: '', highlight: '', titleAliases: '', artistAliases: '', note: '', hidden: false, genres: [] as string[] });
const [busy, setBusy] = useState(false);
+ /** The address as loaded: `youtube` is only sent when the admin changed it (review 2026-10-18). */
+ const [loadedYoutube, setLoadedYoutube] = useState('');
const load = useCallback(() => {
apiCall('GET', `/api/admin/songs/${id}`)
.then((x) => {
setD(x);
const lv = x.levels.find((l) => l.scope === 'all');
+ setLoadedYoutube(x.song.youtube_id ? `https://youtu.be/${x.song.youtube_id}` : '');
setForm({
title: x.song.title,
artist: x.song.artist,
@@ -279,14 +282,13 @@ function SongEditor({ id, genres, onClose, onSaved }: { id: number; genres: Genr
e.preventDefault();
setBusy(true);
try {
- const curYt = d.song.youtube_id ? `https://youtu.be/${d.song.youtube_id}` : '';
await apiCall('PATCH', `/api/admin/songs/${id}`, {
title: form.title,
artist: form.artist,
year: form.year ? Number(form.year) : null,
level: form.level ? Number(form.level) : null,
genres: form.genres,
- ...(form.youtube.trim() !== curYt || d.song.youtube_status !== 'matched' ? { youtube: form.youtube.trim() || null } : {}),
+ ...(form.youtube.trim() !== loadedYoutube ? { youtube: form.youtube.trim() || null } : {}),
highlightSec: form.highlight ? Number(form.highlight) : null,
titleAliases: split(form.titleAliases),
artistAliases: split(form.artistAliases),
@@ -381,6 +383,28 @@ function SongEditor({ id, genres, onClose, onSaved }: { id: number; genres: Genr
{YT_LABEL[d.song.youtube_status][0]}{' '}
{d.song.youtube_channel && {d.song.youtube_channel} · {d.song.youtube_title}}
+ {d.song.youtube_status === 'candidate' && d.song.youtube_id && (
+
+ )}
{videoId && (