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 && (