/**
 * E2E integration test for the yt-dlp Instagram extractor.
 *
 * Makes real network calls (yt-dlp + Instagram CDN). Requires:
 *   - yt-dlp installed on PATH
 *   - Network access to instagram.com
 *   - EXTRACTOR_E2E=1 env var (safety guard to avoid running in normal test runs)
 *
 * Run with:
 *   EXTRACTOR_E2E=1 npm test -- src/tests/instagram-extractor.e2e.spec.ts
 */

import { describe, it, expect } from 'vitest';
import { extractTextAndThumbnail } from '$lib/server/instagram-extractor';

const E2E = !!process.env.EXTRACTOR_E2E;

describe.skipIf(!E2E)('instagram-extractor E2E (requires yt-dlp + network)', () => {
	// Public reels that have previously been in the app queue
	const TEST_REELS = [
		{
			url: 'https://www.instagram.com/reel/DX4XEDZt3qT/',
			expectKeyword: 'pizza'
		},
		{
			url: 'https://www.instagram.com/reel/DUtHm2EiD26/',
			expectKeyword: 'noodles'
		}
	];

	for (const { url, expectKeyword } of TEST_REELS) {
		it(`extracts caption from ${url}`, async () => {
			const events: { type: string; message: string }[] = [];
			const result = await extractTextAndThumbnail(url, (e) =>
				events.push(e as { type: string; message: string })
			);

			expect(result.bodyText.length).toBeGreaterThan(20);
			expect(result.bodyText.toLowerCase()).toContain(expectKeyword);

			if (result.thumbnail !== null) {
				expect(result.thumbnail).toMatch(/^data:image\//);
			}

			expect(events.some((e) => e.type === 'complete')).toBe(true);
			expect(events.some((e) => e.type === 'status' && e.message.includes('yt-dlp'))).toBe(true);
		}, 90_000);
	}
});
