"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/Resolution/ManifestResolution', () => {
    const testsSubPath = 'Cdm/Resolution/ManifestResolutionTest';
    const schemaDocsPath = testHelper_1.testHelper.schemaDocumentsPath;
    /**
     * Test if a manifest resolves correctly a referenced entity declaration
     */
    it('TestReferencedEntityDeclarationResolution', () => __awaiter(void 0, void 0, void 0, function* () {
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.mount('cdm', new Storage_1.LocalAdapter(schemaDocsPath));
        cdmCorpus.setEventCallback(() => { }, internal_1.cdmStatusLevel.error);
        cdmCorpus.storage.defaultNamespace = 'cdm';
        const manifest = new internal_1.CdmManifestDefinition(cdmCorpus.ctx, 'manifest');
        manifest.entities.push('Account', 'cdm:/core/applicationCommon/foundationCommon/crmCommon/accelerators/healthCare/electronicMedicalRecords/Account.cdm.json/Account');
        const referencedEntity = new internal_1.CdmReferencedEntityDeclarationDefinition(cdmCorpus.ctx, 'Address');
        referencedEntity.entityPath =
            'cdm:/core/applicationCommon/foundationCommon/crmCommon/accelerators/healthCare/electronicMedicalRecords/electronicMedicalRecords.manifest.cdm.json/Address';
        manifest.entities.push(referencedEntity);
        cdmCorpus.storage.fetchRootFolder('cdm').documents
            .push(manifest);
        const resolvedManifest = yield manifest.createResolvedManifestAsync('resolvedManifest', undefined);
        expect(resolvedManifest.entities.length)
            .toBe(2);
        expect(resolvedManifest.entities.allItems[0].entityPath)
            .toBe('core/applicationCommon/foundationCommon/crmCommon/accelerators/healthCare/electronicMedicalRecords/resolved/Account.cdm.json/Account');
        expect(resolvedManifest.entities.allItems[1].entityPath)
            .toBe('cdm:/core/applicationCommon/foundationCommon/crmCommon/accelerators/healthCare/electronicMedicalRecords/electronicMedicalRecords.manifest.cdm.json/Address');
    }), 26000);
    /**
     * Test that resolving a manifest that hasn't been added to a folder doesn't throw any exceptions.
     */
    it('TestResolvingManifestNotInFolder', () => __awaiter(void 0, void 0, void 0, function* () {
        let failed = false;
        try {
            var expectedLogCodes = new Set([internal_1.cdmLogCode.ErrResolveManifestFailed]);
            const cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubPath, 'TestResolvingManifestNotInFolder', undefined, false, expectedLogCodes, true);
            const manifest = cdmCorpus.MakeObject(internal_1.cdmObjectType.manifestDef, 'test');
            const entity = cdmCorpus.MakeObject(internal_1.cdmObjectType.entityDef, 'entity');
            const document = cdmCorpus.MakeObject(internal_1.cdmObjectType.documentDef, `entity${internal_1.CdmConstants.cdmExtension}`);
            document.definitions.push(entity);
            // Don't add the document containing the entity to a folder either.
            manifest.entities.push(entity);
            yield manifest.createResolvedManifestAsync('resolved', undefined);
            testHelper_1.testHelper.expectCdmLogCodeEquality(cdmCorpus, internal_1.cdmLogCode.ErrResolveManifestFailed, true);
        }
        catch (e) {
            failed = true;
        }
        expect(failed)
            .toBeFalsy();
    }));
    /**
     * Test that saving a resolved manifest will not cause original logical entity doc to be marked dirty.
     */
    it('TestLinkedResolvedDocSavingNotDirtyingLogicalEntities', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubPath, 'TestLinkedResolvedDocSavingNotDirtyingLogicalEntities');
        const manifestAbstract = corpus.MakeObject(internal_1.cdmObjectType.manifestDef, 'default');
        manifestAbstract.imports.push('cdm:/foundations.cdm.json');
        manifestAbstract.entities.push('B', 'local:/B.cdm.json/B');
        corpus.storage.fetchRootFolder('output').documents.push(manifestAbstract);
        yield manifestAbstract.createResolvedManifestAsync('default-resolved', '{n}/{n}.cdm.json');
        expect(!corpus.storage.namespaceFolders.get('local').documents.allItems[0].isDirty
            && !corpus.storage.namespaceFolders.get('local').documents.allItems[1].isDirty)
            .toBeTruthy();
    }));
    /**
     * Test that correct error is shown when trying to create a resolved manifest with a name that already exists
     */
    it('TestResolvingManifestWithSameName', () => __awaiter(void 0, void 0, void 0, function* () {
        var expectedLogCodes = new Set([internal_1.cdmLogCode.ErrResolveManifestExists]);
        var corpus = testHelper_1.testHelper.getLocalCorpus(testsSubPath, 'TestResolvingManifestWithSameName', undefined, false, expectedLogCodes);
        const manifest = corpus.MakeObject(internal_1.cdmObjectType.manifestDef, 'test');
        corpus.storage.namespaceFolders.get('local').documents.push(manifest);
        const resManifest = yield manifest.createResolvedManifestAsync(manifest.name, '{n}/{n}.cdm.json');
        expect(resManifest)
            .toBeUndefined();
        testHelper_1.testHelper.expectCdmLogCodeEquality(corpus, internal_1.cdmLogCode.ErrResolveManifestExists, true);
    }));
    /**
     * Test that manifest containing entities having dependency on each other for polymorphic sources resolves.
     */
    it('TestResolveManifestWithInterdependentPolymorphicSource', () => __awaiter(void 0, void 0, void 0, function* () {
        var corpus = testHelper_1.testHelper.getLocalCorpus(testsSubPath, 'TestResolveManifestWithInterdependentPolymorphicSource');
        const manifest = yield corpus.fetchObjectAsync('local:/Input.manifest.cdm.json');
        const resolvedManifest = yield manifest.createResolvedManifestAsync('resolved', undefined);
        expect(resolvedManifest.entities.length)
            .toBe(2);
        expect(resolvedManifest.entities.allItems[0].entityPath.toLowerCase())
            .toBe('resolved/group.cdm.json/group');
        expect(resolvedManifest.entities.allItems[1].entityPath.toLowerCase())
            .toBe('resolved/groupmember.cdm.json/groupmember');
    }));
});
//# sourceMappingURL=ManifestResolution.test.js.map