"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");
const modelJsonUnitTestLocalAdapter_1 = require("../modelJsonUnitTestLocalAdapter");
describe('Cdm/ManifestDefinitionTests', () => {
    const testsSubpath = 'Cdm/ManifestDefinition';
    /**
    * Tests if the imports on the resolved manifest are relative to the resolved manifest location.
    */
    it('testResolvedManifestImport', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'testResolvedManifestImport');
        // Make sure that we are not picking up the default namespace while testing.
        corpus.storage.defaultNamespace = 'remote';
        const documentName = 'localImport.cdm.json';
        const localFolder = corpus.storage.fetchRootFolder('local');
        // Create a manifest that imports a document on the same folder.
        const manifest = new internal_1.CdmManifestDefinition(corpus.ctx, 'default');
        manifest.imports.push(documentName);
        localFolder.documents.push(manifest);
        const document = new internal_1.CdmDocumentDefinition(corpus.ctx, documentName);
        localFolder.documents.push(document);
        // Resolve the manifest into a different folder.
        const resolvedManifest = yield manifest.createResolvedManifestAsync('output:/default.manifest.cdm.json', undefined);
        // Checks if the import path on the resolved manifest points to the original location.
        expect(resolvedManifest.imports.length)
            .toBe(1);
        expect(resolvedManifest.imports.allItems[0].corpusPath)
            .toBe(`local:/${documentName}`);
    }));
    /**
     * Tests if the copy function creates copies of the sub objects
     */
    it('testManifestCopy', () => {
        const corpus = testHelper_1.testHelper.getLocalCorpus('', 'testManifestCopy', undefined, undefined, undefined, true);
        const manifest = new internal_1.CdmManifestDefinition(corpus.ctx, 'name');
        const entityName = 'entity';
        const subManifestName = 'subManifest';
        const relationshipName = 'relName';
        const traitName = "traitName";
        const entityDec = manifest.entities.push(entityName);
        const subManifest = manifest.subManifests.push(subManifestName);
        const relationship = manifest.relationships.push(relationshipName);
        const trait = manifest.exhibitsTraits.push(traitName);
        const copy = manifest.copy();
        copy.entities.allItems[0].entityName = 'newEntity';
        copy.subManifests.allItems[0].manifestName = 'newSubManifest';
        copy.relationships.allItems[0].name = 'newRelName';
        copy.exhibitsTraits.allItems[0].namedReference = 'newTraitName';
        expect(entityDec.entityName)
            .toBe(entityName);
        expect(subManifest.manifestName)
            .toBe(subManifestName);
        expect(relationship.name)
            .toBe(relationshipName);
        expect(trait.namedReference)
            .toBe(traitName);
    });
    /**
    * Tests if FileStatusCheckAsync() works properly for manifest loaded from model.json.
    */
    it('testModelJsonManifestFileStatusCheckAsync', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'testModelJsonManifestFileStatusCheckAsync');
        const modeljsonAdapter = new modelJsonUnitTestLocalAdapter_1.ModelJsonUnitTestLocalAdapter(corpus.storage.namespaceAdapters.get('local').root);
        corpus.storage.mount('modeljson', modeljsonAdapter);
        corpus.storage.defaultNamespace = 'modeljson';
        const manifest = yield corpus.fetchObjectAsync('modeljson:/' + internal_1.CdmConstants.modelJsonExtension);
        expect(manifest.isVirtual())
            .toBeTruthy();
        expect(manifest.entities.allItems[0] instanceof internal_1.CdmReferencedEntityDeclarationDefinition)
            .toBeTruthy();
        expect(manifest.entities.allItems[0].isVirtual())
            .toBeTruthy();
        expect(manifest.entities.allItems[1] instanceof internal_1.CdmLocalEntityDeclarationDefinition)
            .toBeTruthy();
        expect(manifest.entities.allItems[1].isVirtual())
            .toBeTruthy();
        const timeBeforeLoad = new Date();
        const oldManifestLastFileModifiedTime = manifest._fileSystemModifiedTime;
        expect(manifest.lastFileStatusCheckTime)
            .toBeUndefined();
        expect(manifest.entities.allItems[0].lastFileStatusCheckTime)
            .toBeUndefined();
        expect(manifest.entities.allItems[1].lastFileStatusCheckTime)
            .toBeUndefined();
        expect(manifest.lastFileModifiedTime)
            .toBeUndefined();
        expect(manifest.entities.allItems[0].lastFileModifiedTime)
            .toBeUndefined();
        expect(manifest.entities.allItems[1].lastFileModifiedTime)
            .toBeUndefined();
        expect(oldManifestLastFileModifiedTime < timeBeforeLoad)
            .toBeTruthy();
        yield new Promise(f => setTimeout(f, 1000));
        yield manifest.fileStatusCheckAsync();
        const newManifestLastFileStatusCheckTime = manifest.lastFileStatusCheckTime;
        const newRefEntityLastFileStatusCheckTime = manifest.entities.allItems[0].lastFileStatusCheckTime;
        const newLocalEntityLastFileStatusCheckTime = manifest.entities.allItems[1].lastFileStatusCheckTime;
        expect(manifest.lastFileModifiedTime)
            .not
            .toBeUndefined();
        expect(manifest.entities.allItems[0].lastFileModifiedTime)
            .not
            .toBeUndefined();
        expect(manifest.entities.allItems[1].lastFileModifiedTime)
            .not
            .toBeUndefined();
        expect(newManifestLastFileStatusCheckTime > timeBeforeLoad)
            .toBeTruthy();
        expect(newLocalEntityLastFileStatusCheckTime > timeBeforeLoad)
            .toBeTruthy();
        expect(newRefEntityLastFileStatusCheckTime > timeBeforeLoad)
            .toBeTruthy();
    }));
});
//# sourceMappingURL=ManifestDefinition.test.js.map