"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 testHelper_1 = require("../testHelper");
/**
 * Test methods for the CdmCorpusDefinition class.
 */
describe('Cdm/CdmCorpusDefinition', () => {
    const testsSubpath = 'Cdm/Corpus';
    /**
     * Tests if a symbol imported with a moniker can be found as the last resource.
     * When resolving symbolEntity with respect to wrtEntity, the symbol fromEntity should be found correctly.
     */
    it('TestResolveSymbolReference', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestResolveSymbolReference');
        corpus.setEventCallback((level, msg) => {
            throw new Error(msg);
        }, internal_1.cdmStatusLevel.warning);
        const wrtEntity = yield corpus.fetchObjectAsync('local:/wrtEntity.cdm.json/wrtEntity');
        const resOpt = new internal_1.resolveOptions(wrtEntity, new internal_1.AttributeResolutionDirectiveSet());
        yield wrtEntity.createResolvedEntityAsync('NewEntity', resOpt);
    }));
    /**
     * Tests if ComputeLastModifiedTimeAsync doesn't log errors related to reference validation.
     */
    it('testComputeLastModifiedTimeAsync', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestComputeLastModifiedTimeAsync');
        corpus.setEventCallback((level, msg) => {
            throw new Error(msg);
        }, internal_1.cdmStatusLevel.error);
        yield corpus.computeLastModifiedTimeAsync('local:/default.manifest.cdm.json');
    }));
    /**
     * Tests if the OM is able to load a data type with a cycle and log an error when that occurs.
     */
    it('testCycleInDataType', () => __awaiter(void 0, void 0, void 0, function* () {
        const expectedLogCodes = new Set([internal_1.cdmLogCode.ErrCycleInObjectDefinition, internal_1.cdmLogCode.ErrResolutionFailure]);
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, "TestCycleInDataType", undefined, undefined, expectedLogCodes);
        // Force the symbols to be resolved.
        const resOpt = new internal_1.resolveOptions();
        resOpt.importsLoadStrategy = internal_1.importsLoadStrategy.load;
        yield corpus.fetchObjectAsync("local:/doc.cdm.json", undefined, resOpt);
        testHelper_1.testHelper.expectCdmLogCodeEquality(corpus, internal_1.cdmLogCode.ErrCycleInObjectDefinition, true);
    }));
    /**
     * Tests the FetchObjectAsync function with the lazy imports load.
     */
    it('TestLazyLoadImports', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestImportsLoadStrategy');
        corpus.setEventCallback((level, msg) => {
            // when the imports are not loaded, there should be no reference validation.
            // no error should be logged.
            throw new Error(msg);
        }, internal_1.cdmStatusLevel.warning);
        // load with deferred imports.
        const resOpt = new internal_1.resolveOptions();
        resOpt.importsLoadStrategy = internal_1.importsLoadStrategy.lazyLoad;
        yield corpus.fetchObjectAsync('local:/doc.cdm.json', undefined, resOpt);
    }));
    /**
     * Tests if a document that was fetched with lazy load and imported by another document is property indexed when needed.
     */
    it('testLazyLoadCreateResolvedEntity', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestLazyLoadCreateResolvedEntity');
        corpus.setEventCallback((level, message) => {
            // when the imports are loaded, there should be no reference validation.
            // no error should be logged.
            throw new Error(message);
        }, internal_1.cdmStatusLevel.warning);
        // load with deferred imports.
        const resOpt = new internal_1.resolveOptions();
        resOpt.importsLoadStrategy = internal_1.importsLoadStrategy.lazyLoad;
        // load entB which is imported by entA document.
        var docB = yield corpus.fetchObjectAsync('local:/entB.cdm.json', undefined, resOpt);
        var entA = yield corpus.fetchObjectAsync('local:/entA.cdm.json/entA', undefined, resOpt);
        expect(entA.inDocument.importPriorities)
            .toBeUndefined();
        expect(docB.importPriorities)
            .toBeUndefined();
        // CreateResolvedEntityAsync will force the entA document to be indexed.
        var resEntA = yield entA.createResolvedEntityAsync('resolved-EntA');
        // in CreateResolvedEntityAsync the documents should be indexed.
        expect(entA.inDocument.importPriorities)
            .not
            .toBeUndefined();
        expect(docB.importPriorities)
            .not
            .toBeUndefined();
        expect(resEntA.inDocument.importPriorities)
            .not
            .toBeUndefined();
    }));
    /**
     * Tests the FetchObjectAsync function with the imports load strategy set to load.
     */
    it('TestLoadImports', () => __awaiter(void 0, void 0, void 0, function* () {
        let errorCount = 0;
        let corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestImportsLoadStrategy');
        corpus.setEventCallback((level, msg) => {
            if (msg.indexOf('Unable to resolve the reference') !== -1) {
                errorCount++;
            }
            else {
                throw new Error(msg);
            }
        }, internal_1.cdmStatusLevel.error);
        // load imports.
        let resOpt = new internal_1.resolveOptions();
        resOpt.importsLoadStrategy = internal_1.importsLoadStrategy.load;
        yield corpus.fetchObjectAsync('local:/doc.cdm.json', null, resOpt);
        expect(errorCount)
            .toEqual(1);
        errorCount = 0;
        corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestImportsLoadStrategy');
        corpus.setEventCallback((level, msg) => {
            if (level === internal_1.cdmStatusLevel.warning && msg.indexOf('Unable to resolve the reference') !== -1) {
                errorCount++;
            }
            else {
                throw new Error(msg);
            }
        }, internal_1.cdmStatusLevel.warning);
        // load imports with shallow validation.
        resOpt = new internal_1.resolveOptions();
        resOpt.importsLoadStrategy = internal_1.importsLoadStrategy.load;
        resOpt.shallowValidation = true;
        yield corpus.fetchObjectAsync('local:/doc.cdm.json', null, resOpt);
        expect(errorCount)
            .toEqual(1);
    }));
    /**
     * Tests if a symbol imported with a moniker can be found as the last resource.
     * When resolving entityReference from wrtConstEntity, constEntity should be found and resolved.
     */
    it('TestResolveConstSymbolReference', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestResolveConstSymbolReference');
        corpus.setEventCallback((level, msg) => {
            throw new Error(msg);
        }, internal_1.cdmStatusLevel.warning);
        const wrtEntity = yield corpus.fetchObjectAsync('local:/wrtConstEntity.cdm.json/wrtConstEntity');
        const resOpt = new internal_1.resolveOptions(wrtEntity, new internal_1.AttributeResolutionDirectiveSet());
        yield wrtEntity.createResolvedEntityAsync('NewEntity', resOpt);
    }));
    /**
     * Tests that errors when trying to cast objects after fetching is handled correctly.
     */
    it('TestIncorrectCastOnFetch', () => __awaiter(void 0, void 0, void 0, function* () {
        const expectedLogCodes = new Set([internal_1.cdmLogCode.ErrInvalidCast]);
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestIncorrectCastOnFetch', undefined, false, expectedLogCodes);
        const manifest = yield corpus.fetchObjectAsync('local:/default.manifest.cdm.json');
        // this function will fetch the entity inside it
        yield corpus.calculateEntityGraphAsync(manifest);
        testHelper_1.testHelper.expectCdmLogCodeEquality(corpus, internal_1.cdmLogCode.ErrInvalidCast, true);
    }));
});
//# sourceMappingURL=Corpus.test.js.map