"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 AttributeResolutionDirectiveSet_1 = require("../../../Utilities/AttributeResolutionDirectiveSet");
const resolveOptions_1 = require("../../../Utilities/resolveOptions");
const testHelper_1 = require("../../testHelper");
describe('Cdm/Resolution/Cache', () => {
    /**
     * The path between TestDataPath and TestName.
     */
    const testsSubpath = 'Cdm/Resolution/CacheTest';
    /**
     * use the same test data for all tests
     */
    const testNamePath = 'TestMaxDepth';
    /**
     * Test when cached value hit the max depth, we are now getting
     * attributes where max depth should not be reached
     */
    it('TestMaxDepthCached', () => __awaiter(void 0, void 0, void 0, function* () {
        const cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testNamePath);
        const aEnt = yield cdmCorpus.fetchObjectAsync('A.cdm.json/A');
        const bEnt = yield cdmCorpus.fetchObjectAsync('B.cdm.json/B');
        const cEnt = yield cdmCorpus.fetchObjectAsync('C.cdm.json/C');
        const dEnt = yield cdmCorpus.fetchObjectAsync('D.cdm.json/D');
        // when resolving A, B should be cached and it should exclude any attributes from D because
        // it is outside of the maxDepth
        const resA = yield aEnt.createResolvedEntityAsync('resA', new resolveOptions_1.resolveOptions(aEnt.inDocument, new AttributeResolutionDirectiveSet_1.AttributeResolutionDirectiveSet(new Set(['normalized', 'structured']))));
        // ensure that when resolving B on its own, attributes from D are included
        const resB = yield bEnt.createResolvedEntityAsync('resB', new resolveOptions_1.resolveOptions(bEnt.inDocument, new AttributeResolutionDirectiveSet_1.AttributeResolutionDirectiveSet(new Set(['normalized', 'structured']))));
        // check the attributes found in D from resolving A
        const bAttInA = resA.attributes.allItems[1].explicitReference;
        const cAttInA = bAttInA.members.allItems[1].explicitReference;
        const dAttInA = cAttInA.members.allItems[1].explicitReference;
        expect(dAttInA.members.length)
            .toBe(1);
        // check that the attribute in D is a foreign key atttribute
        const dIdAttFromA = dAttInA.members.allItems[0];
        expect(dIdAttFromA.name)
            .toBe('dId');
        expect(dIdAttFromA.appliedTraits.item('is.linkedEntity.identifier'))
            .not
            .toBeUndefined();
        // check the attributes found in D from resolving B
        const cAttInB = resB.attributes.allItems[1].explicitReference;
        const dAttInB = cAttInB.members.allItems[1].explicitReference;
        expect(dAttInB.members.length)
            .toBe(2);
        // check that the attribute in D is not a foreign key atttribute
        const dIdAttFromB = dAttInB.members.allItems[0];
        expect(dIdAttFromB.name)
            .toBe('dId');
        expect(dIdAttFromB.appliedTraits.item('is.linkedEntity.identifier'))
            .toBeUndefined();
    }));
    /**
     * Test when cached value did not hit max depth and we are
     * now getting attributes where max depth should be hit
     */
    it('TestNonMaxDepthCached', () => __awaiter(void 0, void 0, void 0, function* () {
        const cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testNamePath);
        const aEnt = yield cdmCorpus.fetchObjectAsync('A.cdm.json/A');
        const bEnt = yield cdmCorpus.fetchObjectAsync('B.cdm.json/B');
        const cEnt = yield cdmCorpus.fetchObjectAsync('C.cdm.json/C');
        const dEnt = yield cdmCorpus.fetchObjectAsync('D.cdm.json/D');
        // when resolving B, it should include any attributes from D because
        // it is outside of the maxDepth
        const resB = yield bEnt.createResolvedEntityAsync('resB', new resolveOptions_1.resolveOptions(bEnt.inDocument, new AttributeResolutionDirectiveSet_1.AttributeResolutionDirectiveSet(new Set(['normalized', 'structured']))));
        // ensure that when resolving A, attributes from D are excluded because they are beyond the max depth
        const resA = yield aEnt.createResolvedEntityAsync('resA', new resolveOptions_1.resolveOptions(aEnt.inDocument, new AttributeResolutionDirectiveSet_1.AttributeResolutionDirectiveSet(new Set(['normalized', 'structured']))));
        // check the attributes found in D from resolving A
        const bAttInA = resA.attributes.allItems[1].explicitReference;
        const cAttInA = bAttInA.members.allItems[1].explicitReference;
        const dAttInA = cAttInA.members.allItems[1].explicitReference;
        expect(dAttInA.members.length)
            .toBe(1);
        // check that the attribute in D is a foreign key atttribute
        const dIdAttFromA = dAttInA.members.allItems[0];
        expect(dIdAttFromA.name)
            .toBe('dId');
        expect(dIdAttFromA.appliedTraits.item('is.linkedEntity.identifier'))
            .not
            .toBeUndefined();
        // check the attributes found in D from resolving B
        const cAttInB = resB.attributes.allItems[1].explicitReference;
        const dAttInB = cAttInB.members.allItems[1].explicitReference;
        expect(dAttInB.members.length)
            .toBe(2);
        // check that the attribute in D is not a foreign key atttribute and all other attributes from D exist
        const dIdAttFromB = dAttInB.members.allItems[0];
        expect(dIdAttFromB.name)
            .toBe('dId');
        expect(dIdAttFromB.appliedTraits.item('is.linkedEntity.identifier'))
            .toBeUndefined();
    }));
});
//# sourceMappingURL=Cache.test.js.map