import { backupAppDataUseCase } from "./backupAppDataUseCase";
import { concat, delay, firstValueFrom, lastValueFrom, of } from "rxjs";
import { BackupAppDataError, BackupAppDataEventType, } from "./types";
import { DeviceModelId } from "@ledgerhq/devices";
describe("backupAppDataUseCase", () => {
    const storageProviderMock = {
        getItem: jest.fn(),
        setItem: jest.fn(),
        removeItem: jest.fn(),
    };
    const appName = "MyApp";
    const deviceModelId = DeviceModelId.stax;
    it("should transfer the AppDataInfoFetched event when no backup found", async () => {
        const backupAppDataFnMock = jest.fn(() => of({
            type: BackupAppDataEventType.AppDataInfoFetched,
            data: {},
        }));
        const expectedEvent = {
            type: BackupAppDataEventType.AppDataInfoFetched,
            data: {},
        };
        const backupAppDataUseCaseObservable = backupAppDataUseCase(appName, deviceModelId, storageProviderMock, backupAppDataFnMock);
        const firstValue = await firstValueFrom(backupAppDataUseCaseObservable);
        expect(firstValue).toEqual(expectedEvent);
    });
    it("should return AppDataAlreadyBackedUp event when backup found in storage", async () => {
        jest.spyOn(storageProviderMock, "getItem").mockResolvedValue({
            appDataInfo: { hash: "hashfortest" },
        });
        const backupAppDataFnMock = jest.fn(() => of({
            type: BackupAppDataEventType.AppDataInfoFetched,
            data: { hash: "hashfortest" },
        }));
        const expectedEvent = {
            type: BackupAppDataEventType.AppDataAlreadyBackedUp,
        };
        const backupAppDataUseCaseObservable = backupAppDataUseCase(appName, deviceModelId, storageProviderMock, backupAppDataFnMock);
        const firstValue = await firstValueFrom(backupAppDataUseCaseObservable);
        expect(firstValue).toEqual(expectedEvent);
    });
    it("should store the app data and return AppDataBackedUp event with empty string as data", async () => {
        const backupAppDataFnMock = jest.fn().mockReturnValue(concat(of({
            type: BackupAppDataEventType.AppDataInfoFetched,
            data: { size: 100 },
        }), of({
            type: BackupAppDataEventType.AppDataBackedUp,
            data: "mockeddatafortest",
        }).pipe(delay(3))));
        const expectedEvent = {
            type: BackupAppDataEventType.AppDataBackedUp,
            data: "",
        };
        const backupAppDataUseCaseObservable = backupAppDataUseCase(appName, deviceModelId, storageProviderMock, backupAppDataFnMock);
        // Initial event should be AppDataInfoFetched
        const firstValue = await firstValueFrom(backupAppDataUseCaseObservable);
        expect(firstValue).toEqual({
            type: BackupAppDataEventType.AppDataInfoFetched,
            data: { size: 100 },
        });
        // test store the app data
        const lastValue = await lastValueFrom(backupAppDataUseCaseObservable);
        expect(storageProviderMock.setItem).toHaveBeenCalledWith("stax-MyApp", {
            appDataInfo: { size: 100 },
            appData: "mockeddatafortest",
        });
        expect(lastValue).toEqual(expectedEvent);
    });
    it("should throw an error when an invalid event type is received", async () => {
        const backupAppDataFnMock = jest.fn(() => of({
            type: "InvalidEventType",
        }));
        const backupAppDataUseCaseObservable = backupAppDataUseCase(appName, deviceModelId, storageProviderMock, backupAppDataFnMock);
        await firstValueFrom(backupAppDataUseCaseObservable).catch(e => {
            expect(e).toBeInstanceOf(BackupAppDataError);
            expect(e.message).toBe("Invalid event type");
        });
    });
    it("should transfer the events when NoAppDataToBackup / Progress event received ", async () => {
        const expectedEvents = [
            {
                type: BackupAppDataEventType.Progress,
                data: 0.5,
            },
            {
                type: BackupAppDataEventType.Progress,
                data: 0.75,
            },
            {
                type: BackupAppDataEventType.NoAppDataToBackup,
            },
        ];
        for (const event of expectedEvents) {
            const backupAppDataFnMock = jest.fn(() => of(event));
            const backupAppDataUseCaseObservable = backupAppDataUseCase(appName, deviceModelId, storageProviderMock, backupAppDataFnMock);
            const firstValue = await firstValueFrom(backupAppDataUseCaseObservable);
            expect(firstValue).toEqual(event);
        }
    });
});
//# sourceMappingURL=backupAppDataUseCase.test.js.map