<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
import { getSiteLogo } from '@launch/api/DataApi';

const fallbackUrl =
	'https://images.extendify-cdn.com/demo-content/logos/ext-custom-logo-default.webp';

const originalFetch = global.fetch;

describe('getSiteLogo', () => {
	beforeEach(() => {
		global.fetch = jest.fn();

		window.extSharedData = Object.assign({}, window.extSharedData, {
			siteId: 'test-site',
			partnerId: 'test-partner',
			showAILogo: true,
		});
	});

	afterEach(() => {
		jest.clearAllMocks();
		global.fetch = originalFetch;
	});

	it('returns logoUrl on successful fetch and valid JSON', async () => {
		global.fetch.mockResolvedValueOnce({
			ok: true,
			json: () => ({ logoUrl: 'https://cdn/image.png' }),
		});

		const result = await getSiteLogo('test-logo');
		expect(result).toBe('https://cdn/image.png');
		expect(global.fetch).toHaveBeenCalledTimes(1);
	});

	it('returns fallback when showAILogo is false', async () => {
		global.window.extSharedData = {
			siteId: 'test-site',
			partnerId: 'test-partner',
			showAILogo: false,
		};

		const result = await getSiteLogo('test-logo');
		expect(result).toBe(fallbackUrl);
		expect(global.fetch).not.toHaveBeenCalled();
	});

	it('throws error when required parameters are missing', async () => {
		global.window.extSharedData = {
			siteId: '',
			partnerId: 'test-partner',
			showAILogo: true,
		};

		await expect(getSiteLogo('test-logo')).rejects.toThrow(
			'Missing required parameter (siteId, partnerId or objectName)',
		);
	});

	it('returns fallback on failed fetch and retry (non-ok response)', async () => {
		global.fetch
			.mockRejectedValueOnce(new Error('Network error'))
			.mockResolvedValueOnce({ ok: false });

		const result = await getSiteLogo('test-logo');
		expect(result).toBe(fallbackUrl);
		expect(global.fetch).toHaveBeenCalledTimes(2);
	});

	it('returns fallback on JSON parse error', async () => {
		global.fetch.mockResolvedValueOnce({
			ok: true,
			json: () => {
				throw new Error('Invalid JSON');
			},
		});

		const result = await getSiteLogo('test-logo');
		expect(result).toBe(fallbackUrl);
	});

	describe('returns fallback when objectName is falsy', () => {
		it('returns fallback when objectName is null', async () => {
			const result = await getSiteLogo(null);
			expect(result).toBe(fallbackUrl);
			expect(global.fetch).not.toHaveBeenCalled();
		});

		it('returns fallback when objectName is undefined', async () => {
			const result = await getSiteLogo(undefined);
			expect(result).toBe(fallbackUrl);
			expect(global.fetch).not.toHaveBeenCalled();
		});

		it('returns fallback when objectName is an empty string', async () => {
			const result = await getSiteLogo('');
			expect(result).toBe(fallbackUrl);
			expect(global.fetch).not.toHaveBeenCalled();
		});
	});
});
