import { test, expect } from 'bun:test'; import { existsSync, readFileSync } from 'node:fs'; import { createYtdlpPool } from './ytdlp-pool.js'; // The worker imports yt_dlp from the release ZIPAPP (a python script, starts with // "#!"). `npm run setup` downloads yt-dlp_linux, a compiled ELF Python can't import, // so an ELF (or nothing) means these tests skip. const isZipapp = (p) => { try { return readFileSync(p).subarray(0, 2).toString() === '#!'; } catch { return false; } }; const YTDLP = [process.env.YTDLP_PATH, '../bin/yt-dlp', Bun.which('yt-dlp')].find((p) => p && existsSync(p) && isZipapp(p)) || ''; const quiet = { warn() {}, info() {} }; test.skipIf(!YTDLP)('runs requests and reports yt-dlp errors without dying', async () => { const pool = createYtdlpPool({ ytdlpPath: YTDLP, size: 1, log: quiet }); try { const v = await pool.run(['--version']); expect(v.trim()).toMatch(/^\d{4}\.\d{2}\.\d{2}/); const err = await pool.run(['--definitely-not-a-flag']).catch((e) => e); expect(err).toBeInstanceOf(Error); expect(err.poolInfra).toBeFalsy(); expect((await pool.run(['--version'])).trim()).toBe(v.trim()); } finally { pool.close(); } }, 30000); test('a broken python disables the pool and rejects as infra', async () => { const pool = createYtdlpPool({ python: '/nonexistent/python3', size: 1, log: quiet }); const err = await pool.run(['--version']).catch((e) => e); expect(err.poolInfra).toBe(true); await new Promise((r) => setTimeout(r, 3500)); expect(pool.disabled).toBe(true); expect((await pool.run(['--version']).catch((e) => e)).poolInfra).toBe(true); pool.close(); }, 15000); test.skipIf(!YTDLP)('workers are replaced after maxRequests and by recycle(), without failing requests', async () => { const pool = createYtdlpPool({ ytdlpPath: YTDLP, size: 1, maxRequests: 1, log: quiet }); try { const a = await pool.run(['--version']); const b = await pool.run(['--version']); // served by a fresh worker expect(b).toBe(a); pool.recycle(); expect(await pool.run(['--version'])).toBe(a); expect(pool.disabled).toBe(false); } finally { pool.close(); } }, 30000);