"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 Persistence_1 = require("../../../Persistence");
const DocumentPersistence_1 = require("../../../Persistence/CdmFolder/DocumentPersistence");
const testHelper_1 = require("../../testHelper");
describe('Persistence.ModelJson.ModelJsonExtensibility', () => {
    /* tslint:disable:no-backbone-get-set-outside-model */
    /**
     * Test path between TestDataPath and TestName.
     */
    const testsSubpath = 'Persistence/ModelJson/ModelJsonExtensibility';
    const doesWriteTestDebuggingFiles = false;
    /**
     * Reads a Model.Json, converting to CdmManifestDef and then converting back to Model.Json format.
     * Checks all the results of the operations against snapshots.
     */
    it('TestModelJsonExtensibility', () => __awaiter(void 0, void 0, void 0, function* () {
        // Workflow of this test:
        // Model.json (file) => Model (class) => Manifest => Model -- check result
        const cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestModelJsonExtensibility');
        const cdmManifest = yield cdmCorpus.fetchObjectAsync('model.json', cdmCorpus.storage.fetchRootFolder('local'));
        const obtainedModel = yield Persistence_1.ModelJson.ManifestPersistence.toData(cdmManifest, undefined, undefined);
        // The imports were generated during processing and are not present in the original file.
        obtainedModel['cdm:imports'] = undefined;
        if (doesWriteTestDebuggingFiles) {
            testHelper_1.testHelper.writeActualOutputFileContent(testsSubpath, 'TestModelJsonExtensibility', 'SerializerTesting-model.json', JSON.stringify(obtainedModel));
        }
        const originalModelJsonSerialized = testHelper_1.testHelper.getExpectedOutputFileContent(testsSubpath, 'TestModelJsonExtensibility', 'SerializerTesting-model.json');
        const originalModelJson = JSON.parse(originalModelJsonSerialized);
        testHelper_1.testHelper.assertObjectContentEquality(originalModelJson, obtainedModel);
    }));
    /**
     * Reads Model.Json, converts to manifest and compares files from obtained manifest to stored files.
     */
    it('TestModelJsonExtensibilityManifestDocs', () => __awaiter(void 0, void 0, void 0, function* () {
        const cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestModelJsonExtensibilityManifestDocs');
        const manifest = yield cdmCorpus.fetchObjectAsync('model.json', cdmCorpus.storage.fetchRootFolder('local'));
        const folderObject = cdmCorpus.storage.fetchRootFolder('default');
        const serializedManifest = JSON.stringify(Persistence_1.CdmFolder.ManifestPersistence.toData(manifest, undefined, undefined));
        if (doesWriteTestDebuggingFiles) {
            folderObject.documents.allItems.forEach((doc) => {
                const docContent = DocumentPersistence_1.DocumentPersistence.toData(doc, undefined, undefined);
                const serializedDocument = JSON.stringify(docContent);
                testHelper_1.testHelper.writeActualOutputFileContent(testsSubpath, 'TestModelJsonExtensibilityManifestDocs', doc.name, serializedDocument);
            });
            testHelper_1.testHelper.writeActualOutputFileContent(testsSubpath, 'TestModelJsonExtensibilityManifestDocs', manifest.name, serializedManifest);
        }
        for (const doc of folderObject.documents.allItems) {
            // manifest shows up twice. once as a manifest and again as the model.json conversion cached
            if (doc.name === manifest.name) {
                continue;
            }
            const serializedDocument = JSON.stringify(DocumentPersistence_1.DocumentPersistence.toData(doc, undefined, undefined));
            const expectedOutputDocument = testHelper_1.testHelper.getExpectedOutputFileContent(testsSubpath, 'TestModelJsonExtensibilityManifestDocs', doc.name);
            testHelper_1.testHelper.assertSameObjectWasSerialized(expectedOutputDocument, serializedDocument);
        }
        const expectedOutput = testHelper_1.testHelper.getExpectedOutputFileContent(testsSubpath, 'TestModelJsonExtensibilityManifestDocs', manifest.name);
        testHelper_1.testHelper.assertSameObjectWasSerialized(expectedOutput, serializedManifest);
    }));
});
//# sourceMappingURL=modelJsonExtensibility.test.js.map