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('
')).toBe(true); expect(detectLoginWall('')).toBe(true); }); it('does not flag regular post pages', () => { expect(detectLoginWall('')).toBe(false); }); }); describe('extractHtmlImageCandidates', () => { const baseUrl = 'https://www.instagram.com/p/abc/'; it('decodes HTML entities in og:image URLs so signed CDN params survive', () => { const html = ''; const [candidate] = extractHtmlImageCandidates(html, baseUrl); expect(candidate).toBe('https://scontent.cdninstagram.com/v/t51/img.jpg?stp=dst-jpg_s640x640&_nc_ohc=token&oh=hash&oe=expiry'); expect(candidate).not.toContain('&'); }); it('skips inline base64 placeholder images from HTML', () => { const html = ''; const candidates = extractHtmlImageCandidates(html, baseUrl); expect(candidates).toEqual(['https://cdn.example.com/post.jpg']); }); it('prefers the og:image over small profile pictures', () => { const html = [ '', '', ].join(''); const candidates = extractHtmlImageCandidates(html, baseUrl); expect(candidates[0]).toContain('s640x640'); }); }); describe('fetchOgImageFromUrl', () => { it('returns login_wall when the page is a login wall without usable images', async () => { mockFetchHtml('accounts/login
'); 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' }); }); }); });