import { pollImportJob, isTerminalJobStatus } from '../importJobPoller';
import { JobStatus, } from '@wix/bex-utils/@wix/ambassador-hub-v1-job/types';
const noWait = () => Promise.resolve();
describe('isTerminalJobStatus', () => {
    it('is true for completed / partially successful / failed', () => {
        expect(isTerminalJobStatus(JobStatus.COMPLETED)).toBe(true);
        expect(isTerminalJobStatus(JobStatus.PARTIALLY_SUCCESSFUL)).toBe(true);
        expect(isTerminalJobStatus(JobStatus.FAILED)).toBe(true);
    });
    it('is false for in-progress / initializing / undefined', () => {
        expect(isTerminalJobStatus(JobStatus.IN_PROGRESS)).toBe(false);
        expect(isTerminalJobStatus(JobStatus.INITIALIZING)).toBe(false);
        expect(isTerminalJobStatus(undefined)).toBe(false);
    });
});
describe('pollImportJob', () => {
    it('polls until terminal and returns the terminal job', async () => {
        const jobs = [
            { id: 'j', status: JobStatus.INITIALIZING },
            { id: 'j', status: JobStatus.IN_PROGRESS },
            { id: 'j', status: JobStatus.COMPLETED },
        ];
        const getJob = jest.fn(async () => jobs.shift());
        const result = await pollImportJob(getJob, 'j', { wait: noWait });
        expect(getJob).toHaveBeenCalledTimes(3);
        expect(result?.status).toBe(JobStatus.COMPLETED);
    });
    it('reports progress for every fetched job', async () => {
        const jobs = [
            { id: 'j', status: JobStatus.IN_PROGRESS, progress: { current: 1 } },
            { id: 'j', status: JobStatus.COMPLETED, progress: { current: 2 } },
        ];
        const getJob = jest.fn(async () => jobs.shift());
        const onProgress = jest.fn();
        await pollImportJob(getJob, 'j', { wait: noWait, onProgress });
        expect(onProgress).toHaveBeenCalledTimes(2);
        expect(onProgress.mock.calls[1][0].progress.current).toBe(2);
    });
    it('stops and returns null when cancelled before the first poll', async () => {
        const getJob = jest.fn();
        const result = await pollImportJob(getJob, 'j', {
            wait: noWait,
            isCancelled: () => true,
        });
        expect(result).toBeNull();
        expect(getJob).not.toHaveBeenCalled();
    });
    it('stops between polls when cancelled mid-run', async () => {
        let cancelled = false;
        const getJob = jest.fn(async () => {
            cancelled = true; // cancel after the first fetch
            return { id: 'j', status: JobStatus.IN_PROGRESS };
        });
        const result = await pollImportJob(getJob, 'j', {
            wait: noWait,
            isCancelled: () => cancelled,
        });
        expect(result).toBeNull();
        expect(getJob).toHaveBeenCalledTimes(1);
    });
});
//# sourceMappingURL=importJobPoller.spec.js.map