import {getLogoUrl, getMediaPrincipalUrl} from './mediaService';
import http from '@/http';

const mockedHttp = http as jest.Mocked<typeof http>;

describe('mediaService', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('shouldReturnTheMainMediaUrlOfTheEntity', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {url: 'https://media.tamm-kreiz.bzh/cover.jpg', credit: null}});

    expect(await getMediaPrincipalUrl('annuaire_entree', 42, 'v6_annuaire_cover_4_3')).toBe('https://media.tamm-kreiz.bzh/cover.jpg');
    expect(mockedHttp.get).toHaveBeenCalledWith('/media/annuaire_entree/42/MediaPrincipal?format=v6_annuaire_cover_4_3');
  });

  it('shouldReturnNullWhenTheApiServesAPlaceholderImage', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {url: 'https://dummyimage.com/600x800/f60/fff&text=Bagad'}});

    expect(await getMediaPrincipalUrl('annuaire_entree', 42, 'v6_annuaire_cover_4_3')).toBeNull();
  });

  it('shouldReturnNullWhenTheApiFails', async () => {
    mockedHttp.get.mockRejectedValueOnce(new Error('HTTP 404'));

    expect(await getMediaPrincipalUrl('evenement', '9', 'v6_affiche_800')).toBeNull();
  });

  it('shouldReturnNullWithoutUrl', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {}});

    expect(await getMediaPrincipalUrl('evenement', 9, 'v6_affiche_800')).toBeNull();
  });

  it('shouldReturnTheLogoUrlOfAnEntity', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {url: 'https://media.tamm-kreiz.bzh/logo.png'}});

    expect(await getLogoUrl('evenement_ensemble', 5)).toBe('https://media.tamm-kreiz.bzh/logo.png');
    expect(mockedHttp.get).toHaveBeenCalledWith('/media/evenement_ensemble/5/Logo');
  });

  it('shouldReturnNullForAPlaceholderLogo', async () => {
    mockedHttp.get.mockResolvedValueOnce({status: 200, data: {url: 'https://dummyimage.com/300x300/f60/fff&text=x'}});

    expect(await getLogoUrl('annuaire_entree', 42)).toBeNull();
  });
});
