"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const backupAppData_1 = require("./backupAppData");
const types_1 = require("./types");
const deviceCore = __importStar(require("@ledgerhq/device-core"));
jest.mock("@ledgerhq/hw-transport");
jest.mock("@ledgerhq/device-core", () => ({
    isAppStorageInfo: jest.fn().mockResolvedValue(true),
    getAppStorageInfo: jest.fn().mockResolvedValue({
        size: 6,
        dataVersion: "test",
        hasSettings: true,
        hasData: true,
        hash: "test",
    }),
    backupAppStorage: jest.fn().mockResolvedValue(Buffer.from("ledger")),
}));
describe("backupAppData", () => {
    let transport;
    let appName;
    beforeEach(() => {
        // Initialize the transport and app name before each test
        transport = {};
        appName = "MyApp";
    });
    afterEach(() => {
        jest.clearAllMocks();
    });
    it("should backup the app data by emitting relative events sequentially", async () => {
        const backupObservable = (0, backupAppData_1.backupAppData)(transport, appName);
        const events = [];
        // Subscribe to the observable to receive the backup events
        backupObservable.subscribe((event) => {
            events.push(event);
        });
        const firstValue = await (0, rxjs_1.firstValueFrom)(backupObservable);
        expect(firstValue).toEqual({
            type: types_1.BackupAppDataEventType.AppDataInfoFetched,
            data: { size: 6, dataVersion: "test", hasSettings: true, hasData: true, hash: "test" },
        });
        const lastValue = await (0, rxjs_1.lastValueFrom)(backupObservable);
        expect(lastValue).toEqual({
            type: types_1.BackupAppDataEventType.AppDataBackedUp,
            data: "bGVkZ2Vy", // base64 encoded "ledger"
        });
        expect(events).toContainEqual({
            type: types_1.BackupAppDataEventType.Progress,
            data: expect.any(Number),
        });
        expect(events).toHaveLength(3);
    });
    it("should emit specific event when data size is zero", async () => {
        // Mock the getAppStorageInfo function to return an app data size of 0
        jest.spyOn(deviceCore, "getAppStorageInfo").mockResolvedValue({
            size: 0,
            dataVersion: "test",
            hasSettings: false,
            hasData: false,
            hash: "test",
        });
        const backupObservable = (0, backupAppData_1.backupAppData)(transport, appName);
        const events = [];
        // Subscribe to the observable to receive the backup events
        backupObservable.subscribe((event) => {
            events.push(event);
        });
        const firstValue = await (0, rxjs_1.firstValueFrom)(backupObservable);
        expect(firstValue).toEqual({
            type: types_1.BackupAppDataEventType.AppDataInfoFetched,
            data: { size: 0, dataVersion: "test", hasSettings: false, hasData: false, hash: "test" },
        });
        const lastValue = await (0, rxjs_1.lastValueFrom)(backupObservable);
        expect(lastValue).toEqual({
            type: types_1.BackupAppDataEventType.NoAppDataToBackup,
        });
        expect(events).toHaveLength(2);
    });
    it("should throw an error when the chunk data is empty", async () => {
        jest.spyOn(deviceCore, "backupAppStorage").mockResolvedValue(Buffer.from(""));
        const backupObservable = (0, backupAppData_1.backupAppData)(transport, appName);
        (0, rxjs_1.lastValueFrom)(backupObservable).catch(e => {
            expect(e).toBeInstanceOf(types_1.BackupAppDataError);
            expect(e.message).toBe("Chunk data is empty");
        });
    });
    it("should throw an error when the app data size mismatch", async () => {
        jest.spyOn(deviceCore, "getAppStorageInfo").mockResolvedValue({
            size: 5,
            dataVersion: "test",
            hasSettings: true,
            hasData: true,
            hash: "test",
        });
        jest.spyOn(deviceCore, "backupAppStorage").mockResolvedValue(Buffer.from("ledger"));
        const backupObservable = (0, backupAppData_1.backupAppData)(transport, appName);
        (0, rxjs_1.lastValueFrom)(backupObservable).catch(e => {
            expect(e).toBeInstanceOf(types_1.BackupAppDataError);
            expect(e.message).toBe("App data size mismatch");
        });
    });
});
//# sourceMappingURL=backupAppData.test.js.map