var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { of } from 'rxjs';
import DbApi, { mockPutSyncQueue, mockPutSyncStatus, mockRemoveSyncQueue, mockUpdateSyncQueue, } from '../../../dataSource/indexedDb/__mocks__/dbApiWrapperMock';
import ParticlesResolverQueue from './ParticlesResolverQueue';
jest.mock('src/services/backend/services/sync/utils');
jest.mock('src/services/backend/services/dataSource/indexedDb/dbApiWrapper');
jest.mock('src/services/backend/channels/BroadcastChannelSender');
describe('ParticlesResolverQueue', () => {
    beforeEach(() => {
        mockPutSyncStatus.mockClear();
        mockPutSyncQueue.mockClear();
        DbApi.mockClear();
    });
    it('start should subscribe to isInitialized$ and keep queue unprocessed', () => __awaiter(void 0, void 0, void 0, function* () {
        const waitForParticleResolve = jest.fn();
        const mockItems = [
            {
                id: 'mockId',
                priority: 1,
            },
        ];
        const mockServiceDeps = {
            dbInstance$: of(new DbApi()),
            ipfsInstance$: of(undefined),
            params$: of({
                myAddress: null,
                followings: [],
            }),
            waitForParticleResolve,
        };
        const syncQueue = new ParticlesResolverQueue(mockServiceDeps);
        syncQueue.start().loop$.subscribe({
            next: console.log,
            error: console.error,
        });
        yield syncQueue.enqueue(mockItems);
        expect(mockPutSyncQueue).toHaveBeenCalledWith(mockItems);
        expect(waitForParticleResolve).toHaveBeenCalledTimes(0);
        expect([...syncQueue.queue.keys()]).toEqual([mockItems[0].id]);
        return Promise.resolve();
    }));
    it('processSyncQueue should process the items in the sync queue', (done) => {
        const waitForParticleResolve = jest.fn();
        waitForParticleResolve.mockResolvedValueOnce({
            item: {},
            status: 'completed',
            source: 'db',
        });
        waitForParticleResolve.mockResolvedValueOnce({
            item: {},
            status: 'not_found',
            source: 'db',
        });
        const mockServiceDeps = {
            dbInstance$: of(new DbApi()),
            ipfsInstance$: of({}),
            params$: of({
                myAddress: null,
                followings: [],
            }),
            waitForParticleResolve,
        };
        const syncQueue = new ParticlesResolverQueue(mockServiceDeps);
        const mockItems = [
            {
                id: 'mockToOk',
                priority: 1,
            },
            {
                id: 'mockToNotFound',
                priority: 1,
            },
        ];
        syncQueue.enqueue(mockItems);
        syncQueue.start().loop$.subscribe({
            next: () => {
                expect(mockPutSyncQueue).toHaveBeenCalledWith(mockItems);
                expect(waitForParticleResolve).toHaveBeenCalledTimes(2);
                expect(mockRemoveSyncQueue).toHaveBeenCalledWith(['mockToOk']);
                expect(mockUpdateSyncQueue).toHaveBeenCalledWith([
                    {
                        id: 'mockToNotFound',
                        status: -1,
                    },
                ]);
                expect(syncQueue.queue.size).toEqual(0);
                done();
            },
            error: (err) => done(err),
        });
    });
});
//# sourceMappingURL=ParticlesResolverQueue.test.js.map