"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const types_1 = require("@superblocksteam/types");
const index_1 = require("./index");
const pageDsl = {
    version: 8,
    widgetId: '0',
    children: []
};
const appSettings = {};
const appConfiguration = {
    routes: {}
};
const hashTreeV1 = {
    v: 1,
    page: {
        hash: 'ZMpaDRZaIl1QxyEwrjoPUQJbkcCtTsNXbVySIGlcgVg=',
        version: 8
    },
    settings: {
        components: 'RBNvo1WzZ4oRRq0W9+hknpT7T8If536DEMBg9hyq/4o=',
        rest: 'RBNvo1WzZ4oRRq0W9+hknpT7T8If536DEMBg9hyq/4o='
    }
};
const hashTreeV2 = {
    v: 2,
    pages: {
        'fake-page-id': {
            hash: 'ZMpaDRZaIl1QxyEwrjoPUQJbkcCtTsNXbVySIGlcgVg=',
            version: 8
        }
    },
    settings: {
        components: 'RBNvo1WzZ4oRRq0W9+hknpT7T8If536DEMBg9hyq/4o=',
        rest: 'RBNvo1WzZ4oRRq0W9+hknpT7T8If536DEMBg9hyq/4o='
    },
    configuration: {
        version: 1,
        hash: 'boT9TuU6iU3HiVIO8vh8YU+SgXAozIHLSuhRcux09F8='
    }
};
(0, vitest_1.describe)('createApplicationSignatureTree', () => {
    (0, vitest_1.it)('returns a v2 signature tree', async () => {
        const calculatedHashTree = await (0, index_1.createApplicationSignatureTree)({
            pages: { 'fake-page-id': pageDsl },
            appSettings,
            configuration: appConfiguration
        });
        (0, vitest_1.expect)(calculatedHashTree).toEqual(hashTreeV2);
    });
});
(0, vitest_1.describe)('verifyApplicationHashTree', () => {
    (0, vitest_1.describe)('v1', () => {
        (0, vitest_1.it)('verifies a v1 signature tree', async () => {
            const result = await (0, index_1.verifyApplicationHashTree)({ hashTree: hashTreeV1, pages: { 'fake-page-id': pageDsl }, appSettings });
            (0, vitest_1.expect)(result).toBe(true);
        });
        (0, vitest_1.it)('returns false if the page hash does not match', async () => {
            const badHashTree = { ...hashTreeV1, page: { ...hashTreeV1.page, hash: 'bad-hash' } };
            const result = await (0, index_1.verifyApplicationHashTree)({ hashTree: badHashTree, pages: { 'fake-page-id': pageDsl }, appSettings });
            (0, vitest_1.expect)(result).toBe(false);
        });
        (0, vitest_1.it)('returns false if the settings hash does not match', async () => {
            const badHashTree = { ...hashTreeV1, settings: { ...hashTreeV1.settings, components: 'bad-hash' } };
            const result = await (0, index_1.verifyApplicationHashTree)({ hashTree: badHashTree, pages: { 'fake-page-id': pageDsl }, appSettings });
            (0, vitest_1.expect)(result).toBe(false);
        });
    });
    (0, vitest_1.describe)('v2', () => {
        (0, vitest_1.it)('verifies a v2 signature tree', async () => {
            const result = await (0, index_1.verifyApplicationHashTree)({ hashTree: hashTreeV2, pages: { 'fake-page-id': pageDsl }, appSettings });
            (0, vitest_1.expect)(result).toBe(true);
        });
        (0, vitest_1.it)('returns false if the page hash does not match', async () => {
            const badHashTree = { ...hashTreeV2, pages: { 'fake-page-id': { ...hashTreeV2.pages['fake-page-id'], hash: 'bad-hash' } } };
            const result = await (0, index_1.verifyApplicationHashTree)({ hashTree: badHashTree, pages: { 'fake-page-id': pageDsl }, appSettings });
            (0, vitest_1.expect)(result).toBe(false);
        });
        (0, vitest_1.it)('returns false if the settings hash does not match', async () => {
            const badHashTree = { ...hashTreeV2, settings: { ...hashTreeV2.settings, components: 'bad-hash' } };
            const result = await (0, index_1.verifyApplicationHashTree)({ hashTree: badHashTree, pages: { 'fake-page-id': pageDsl }, appSettings });
            (0, vitest_1.expect)(result).toBe(false);
        });
    });
});
(0, vitest_1.describe)('get algorithm name', () => {
    vitest_1.it.each([
        {
            name: 'unspecified algorithm',
            algorithm: types_1.UtilsV1.Signature_Algorithm.UNSPECIFIED,
            expected: 'ALGORITHM_UNSPECIFIED'
        },
        {
            name: 'ED25519 algorithm',
            algorithm: types_1.UtilsV1.Signature_Algorithm.ED25519,
            expected: 'ALGORITHM_ED25519'
        }
    ])('should return $expected for $name', ({ algorithm, expected }) => {
        const actual = (0, index_1.getAlgorithmName)(algorithm);
        (0, vitest_1.expect)(actual).toEqual(expected);
    });
});
//# sourceMappingURL=index.test.js.map