import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createPhotoShareLink } from '../photosApi';
const fetchMock = vi.fn();
describe('photosApi', () => {
beforeEach(() => {
fetchMock.mockReset();
global.fetch = fetchMock as unknown as typeof fetch;
document.head.innerHTML = '';
localStorage.setItem('device-id', 'device-123');
});
it('creates a share link with CSRF headers', async () => {
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ slug: 'demo', url: 'http://example.com/share/demo' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
);
const payload = await createPhotoShareLink('token', 123);
expect(payload.url).toBe('http://example.com/share/demo');
expect(fetchMock).toHaveBeenCalledTimes(1);
const [, options] = fetchMock.mock.calls[0];
const headers = options?.headers as Record;
expect(headers['X-CSRF-TOKEN']).toBe('csrf-token-demo');
expect(headers['X-XSRF-TOKEN']).toBe('csrf-token-demo');
expect(headers['X-Device-Id']).toBe('device-123');
});
});