"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', (done) => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestResolveSymbolReference');
        corpus.setEventCallback((level, msg) => {
            done.fail(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);
        done();
    }));
    /**
     * Tests if ComputeLastModifiedTimeAsync doesn't log errors related to reference validation.
     */
    it('testComputeLastModifiedTimeAsync', (done) => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestComputeLastModifiedTimeAsync');
        corpus.setEventCallback((level, msg) => {
            done.fail(new Error(msg));
        }, internal_1.cdmStatusLevel.error);
        yield corpus.computeLastModifiedTimeAsync('local:/default.manifest.cdm.json');
        done();
    }));
    /**
     * Tests the FetchObjectAsync function with the lazy imports load.
     */
    it('TestLazyLoadImports', (done) => __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.
            done.fail(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', null, resOpt);
        done();
    }));
    /**
     * Tests if a document that was fetched with lazy load and imported by another document is property indexed when needed.
     */
    it('testLazyLoadCreateResolvedEntity', (done) => __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.
            done.fail(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', null, resOpt);
        var entA = yield corpus.fetchObjectAsync('local:/entA.cdm.json/entA', null, 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();
        done();
    }));
    /**
     * Tests the FetchObjectAsync function with the imports load strategy set to load.
     */
    it('TestLoadImports', (done) => __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 {
                done.fail(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 {
                done.fail(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);
        done();
    }));
    /**
     * 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', (done) => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestResolveConstSymbolReference');
        corpus.setEventCallback((level, msg) => {
            done.fail(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);
        done();
    }));
});
//# sourceMappingURL=Corpus.test.js.map