54 lines
3.0 KiB
JavaScript
54 lines
3.0 KiB
JavaScript
import { test, expect } from 'bun:test';
|
|
import { mkdtempSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
|
|
const root = mkdtempSync(join(tmpdir(), 'ytp-video-catalog-'));
|
|
process.env.DB_PATH = join(root, 'test.db');
|
|
const db = await import('./db.js');
|
|
const catalog = await import('./video-catalog.js');
|
|
await db.initDb();
|
|
|
|
test('catalog migration backfills canonical URLs and rich metadata, excluding upload ids', async () => {
|
|
await db.db.execute('DELETE FROM catalog_schema_migrations WHERE version=1');
|
|
await db.db.execute({ sql: 'INSERT INTO video_meta (id,card,hay,seen,updated_at) VALUES (?,?,?,?,?)',
|
|
args: ['abcDEF12345', JSON.stringify({ id: 'abcDEF12345', title: 'Café Worship', channel: 'Choir', channelId: 'UC123', channelUrl: 'https://www.youtube.com/@choir', tags: ['live'], publishedAt: '2024-01-01', language: 'en' }), 'cafe worship choir', 1, 1] });
|
|
await db.db.execute({ sql: 'INSERT INTO video_meta (id,card,hay,seen,updated_at) VALUES (?,?,?,?,?)',
|
|
args: ['upl_abcdef12', JSON.stringify({ id: 'upl_abcdef12', title: 'Upload' }), 'upload', 1, 1] });
|
|
await db.migrateVideoMeta({ pageSize: 1 });
|
|
await db.migrateVideoMeta({ pageSize: 1 });
|
|
const youtube = (await db.db.execute("SELECT * FROM video_meta WHERE id='abcDEF12345'")).rows[0];
|
|
const upload = (await db.db.execute("SELECT * FROM video_meta WHERE id='upl_abcdef12'")).rows[0];
|
|
expect(youtube.url).toBe('https://www.youtube.com/watch?v=abcDEF12345');
|
|
expect(youtube.channel_id).toBe('UC123');
|
|
expect(youtube.channel_url).toBe('https://www.youtube.com/@choir');
|
|
expect(youtube.published).toBe('2024-01-01');
|
|
expect(youtube.lang).toBe('en');
|
|
expect(youtube.hay).toContain('cafe');
|
|
expect(JSON.parse(youtube.card).url).toBe(youtube.url);
|
|
expect(upload.url).toBeNull();
|
|
});
|
|
|
|
test('ingest stores canonical source URL and channel metadata on the card and row', async () => {
|
|
await catalog.ingest([{ id: 'xyzABC12345', title: 'Música', channel: 'Choir', channel_id: 'UC456', channel_url: 'https://www.youtube.com/@choir', tags: ['Praise'], description: 'Sunday service' }], 'deep');
|
|
const row = (await db.db.execute("SELECT * FROM video_meta WHERE id='xyzABC12345'")).rows[0];
|
|
const card = JSON.parse(row.card);
|
|
expect(row.url).toBe('https://www.youtube.com/watch?v=xyzABC12345');
|
|
expect(row.channel_id).toBe('UC456');
|
|
expect(card.url).toBe(row.url);
|
|
expect(row.hay).toContain('musica');
|
|
expect(row.hay).toContain('praise');
|
|
});
|
|
|
|
test('accent-insensitive matching ranks title phrase ahead of channel match', async () => {
|
|
await catalog.ingest([
|
|
{ id: 'phrase12345', title: 'Café Worship Live', channel: 'Other' },
|
|
{ id: 'channel1234', title: 'Sunday Set', channel: 'Cafe Worship Live' },
|
|
{ id: 'words123456', title: 'Worship in the Cafe', channel: 'Other' },
|
|
], 'deep');
|
|
const search = await import('./search-cache.js');
|
|
const results = await search.searchVideos('cafe worship live');
|
|
expect(results[0].id).toBe('phrase12345');
|
|
expect(results[0].url).toBe('https://www.youtube.com/watch?v=phrase12345');
|
|
});
|