import { vi, describe, expect, it } from 'vitest';
import { axios } from 'app/test/vitest/helper';
import { downloadFile } from 'src/composables/downloadFile';
import { useSession } from 'src/composables/useSession';

const session = useSession();
const token = session.getToken();

describe('downloadFile', () => {
    it('should open a new window to download the file', async () => {
        const url = 'http://localhost:9000';

        vi.spyOn(axios, 'get').mockResolvedValueOnce({ data: url });

        const mockWindowOpen = vi.spyOn(window, 'open');

        await downloadFile(1);

        expect(mockWindowOpen).toHaveBeenCalledWith(
            `${url}/api/dms/1/downloadFile?access_token=${token}`
        );

        mockWindowOpen.mockRestore();
    });
});