import { detectLoginWall, extractHtmlImageCandidates, fetchOgImageFromUrl } from '../../utils/shareIntent'; describe('shareIntent login wall handling', () => { const originalFetch = global.fetch; afterEach(() => { global.fetch = originalFetch; jest.restoreAllMocks(); }); const mockFetchHtml = (html: string, ok = true) => { global.fetch = jest.fn().mockResolvedValue({ ok, text: async () => html, }) as unknown as typeof fetch; }; describe('detectLoginWall', () => { it('detects Instagram login wall markers', () => { expect(detectLoginWall('Log in')).toBe(true); expect(detectLoginWall('{"page":"LoginAndSignupPage"}')).toBe(true); expect(detectLoginWall('
'); const result = await fetchOgImageFromUrl('https://www.instagram.com/p/abc/'); expect(result).toEqual({ failureReason: 'login_wall' }); }); it('returns no_image when the page has no images and no login markers', async () => { mockFetchHtml('Nothing here
'); const result = await fetchOgImageFromUrl('https://example.com/post'); expect(result).toEqual({ failureReason: 'no_image' }); }); it('returns no_image when the response is not ok', async () => { mockFetchHtml('', false); const result = await fetchOgImageFromUrl('https://example.com/missing'); expect(result).toEqual({ failureReason: 'no_image' }); }); it('returns no_image when the fetch throws', async () => { global.fetch = jest.fn().mockRejectedValue(new Error('network down')) as unknown as typeof fetch; const result = await fetchOgImageFromUrl('https://example.com/offline'); expect(result).toEqual({ failureReason: 'no_image' }); }); }); });