import { beforeEach, describe, expect, it, vi } from 'vitest';
import { deleteMediaItems } from './mediaItems';

describe('deleteMediaItems', () => {
  beforeEach(() => {
    window.localStorage.clear();
    vi.restoreAllMocks();
  });

  it('DELETEs /api/v1/media-items with the ids in the body', async () => {
    const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 204 }));

    await deleteMediaItems([1, 2, 3]);

    const [url, init] = fetchSpy.mock.calls[0];
    expect(url).toBe('/api/v1/media-items');
    expect((init?.method ?? 'GET').toUpperCase()).toBe('DELETE');
    expect(JSON.parse(init?.body as string)).toEqual({ ids: [1, 2, 3] });
  });

  it('rejects with the ApiError status on failure', async () => {
    vi.spyOn(window, 'fetch').mockResolvedValue(
      new Response(JSON.stringify({ status: 422, title: 'Validation failed' }), {
        headers: { 'Content-Type': 'application/json' },
        status: 422
      })
    );

    await expect(deleteMediaItems([])).rejects.toMatchObject({ status: 422 });
  });
});
