import { test, expect, afterAll } from 'bun:test'; import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; const root = mkdtempSync(join(tmpdir(), 'ytp-search-cache-')); process.env.DB_PATH = join(root, 'test.db'); const sc = await import('./search-cache.js'); const { initDb } = await import('./db.js'); await initDb(); afterAll(() => rmSync(root, { recursive: true, force: true })); const cards = (n) => Array.from({ length: n }, (_, i) => ({ id: 'v' + i, title: 't' + i })); test('put/get round-trips, case-insensitive, capped at 200', async () => { await sc.put('Hello World', cards(300)); const r = await sc.get('hello world'); expect(r.results.length).toBe(200); expect(await sc.get('nope')).toBeNull(); }); test('trim evicts least-recently-used rows over the row cap', async () => { for (let i = 0; i < 6; i++) await sc.put('q' + i, cards(3)); const dropped = await sc.trim({ maxRows: 3 }); expect(dropped).toBeGreaterThan(0); expect((await sc.stats()).queries).toBe(3); }); test('known videos are remembered and found by every word', async () => { await sc.rememberVideos([ { id: 'aaa11111111', title: 'Way Maker - Sinach', channel: 'Worship' }, { id: 'bbb22222222', title: 'Way Maker (live)', channel: 'Other' }, { id: 'ccc33333333', title: 'Goodness of God', channel: 'Worship' }, ]); expect((await sc.searchVideos('way maker')).map((c) => c.id).sort()).toEqual(['aaa11111111', 'bbb22222222']); expect((await sc.searchVideos('maker worship')).map((c) => c.id)).toEqual(['aaa11111111']); expect(await sc.searchVideos('100%')).toEqual([]); });