"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const internal_1 = require("../../internal");
const Storage_1 = require("../../Storage");
const testHelper_1 = require("../testHelper");
describe('Cdm.Storage.StorageConfig', () => {
    /**
     * Test path between TestDataPath and TestName.
     */
    const testsSubpath = 'Storage';
    it('TestLoadingAndSavingConfig', () => __awaiter(void 0, void 0, void 0, function* () {
        const testInputPath = testHelper_1.testHelper.getInputFolderPath(testsSubpath, 'TestLoadingAndSavingConfig');
        const testOutputPath = testHelper_1.testHelper.getExpectedOutputFolderPath(testsSubpath, 'TestLoadingAndSavingConfig');
        // Create a corpus to load the config.
        const cdmCorpus = getLocalCorpus(testInputPath, testOutputPath);
        cdmCorpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.error);
        // tslint:disable-next-line: no-backbone-get-set-outside-model
        const config = yield cdmCorpus.storage.fetchAdapter('local')
            .readAsync('/config.json');
        const differentCorpus = new internal_1.CdmCorpusDefinition();
        differentCorpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.error);
        differentCorpus.storage.mountFromConfig(config);
        const resultConfig = differentCorpus.storage.fetchConfig();
        // tslint:disable-next-line: no-backbone-get-set-outside-model
        const outputConfig = yield cdmCorpus.storage.fetchAdapter('target')
            .readAsync('/config.json');
        testHelper_1.testHelper.assertObjectContentEquality(JSON.parse(outputConfig), JSON.parse(resultConfig));
    }));
    it('TestLoadingConfigAndTryingToFetchManifest', () => __awaiter(void 0, void 0, void 0, function* () {
        const testInputPath = testHelper_1.testHelper.getInputFolderPath(testsSubpath, 'TestLoadingConfigAndTryingToFetchManifest');
        // Create a corpus to load the config.
        const cdmCorpus = getLocalCorpus(testInputPath);
        cdmCorpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.error);
        // tslint:disable-next-line: no-backbone-get-set-outside-model
        const config = yield cdmCorpus.storage.fetchAdapter('local')
            .readAsync('/config-TypeScript.json');
        const differentCorpus = new internal_1.CdmCorpusDefinition();
        differentCorpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.error);
        const unrecognizedAdapters = differentCorpus.storage.mountFromConfig(config, true);
        const cdmManifest = yield differentCorpus.fetchObjectAsync('model.json', cdmCorpus.storage.fetchRootFolder('local'));
        expect(cdmManifest)
            .toBeDefined();
        expect(unrecognizedAdapters.length)
            .toBe(1);
    }));
    it('TestSystemAndResourceAdapters', () => __awaiter(void 0, void 0, void 0, function* () {
        // Create a corpus to load the config.
        const outputFolder = testHelper_1.testHelper.getExpectedOutputFolderPath(testsSubpath, 'TestSystemAndResourceAdapters');
        const cdmCorpus = getLocalCorpus(outputFolder);
        cdmCorpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.error);
        const differentCorpus = new internal_1.CdmCorpusDefinition();
        differentCorpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.error);
        differentCorpus.storage.unMount('cdm');
        differentCorpus.storage.defaultNamespace = 'local';
        const resultConfig = differentCorpus.storage.fetchConfig();
        // tslint:disable-next-line: no-backbone-get-set-outside-model
        const outputConfig = yield cdmCorpus.storage.namespaceAdapters.get('local')
            .readAsync('/config.json');
        testHelper_1.testHelper.assertObjectContentEquality(JSON.parse(outputConfig), JSON.parse(resultConfig));
    }));
});
function getLocalCorpus(testFilesInputRoot, testFilesOutputRoot) {
    const corpus = new internal_1.CdmCorpusDefinition();
    corpus.storage.defaultNamespace = 'local';
    corpus.storage.mount('local', new Storage_1.LocalAdapter(testFilesInputRoot));
    if (testFilesOutputRoot !== undefined) {
        corpus.storage.mount('target', new Storage_1.LocalAdapter(testFilesOutputRoot));
    }
    return corpus;
}
//# sourceMappingURL=storageConfig.test.js.map