"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const Project_1 = require("../app/Project");
const CartoApp_1 = require("../app/CartoApp");
const NodeStorage_1 = require("../local/NodeStorage");
const Database_1 = require("../minecraft/Database");
const LocalEnvironment_1 = require("../local/LocalEnvironment");
const ProjectInfoSet_1 = require("../info/ProjectInfoSet");
const IProjectInfoData_1 = require("../info/IProjectInfoData");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const ProjectExporter_1 = require("../app/ProjectExporter");
const WorldLevelDat_1 = require("../minecraft/WorldLevelDat");
const fs = require("fs");
const Utilities_1 = require("../core/Utilities");
CartoApp_1.default.hostType = CartoApp_1.HostType.testLocal;
let carto = undefined;
let localEnv = undefined;
let scenariosFolder = undefined;
let resultsFolder = undefined;
localEnv = new LocalEnvironment_1.default(false);
(async () => {
    CartoApp_1.default.localFolderExists = _localFolderExists;
    CartoApp_1.default.ensureLocalFolder = _ensureLocalFolder;
    const scenariosStorage = new NodeStorage_1.default(NodeStorage_1.default.ensureEndsWithDelimiter(__dirname) + "/../../test/", "scenarios");
    scenariosFolder = scenariosStorage.rootFolder;
    await scenariosFolder.ensureExists();
    const resultsStorage = new NodeStorage_1.default(NodeStorage_1.default.ensureEndsWithDelimiter(__dirname) + "/../../test/", "results");
    resultsFolder = resultsStorage.rootFolder;
    await resultsFolder.ensureExists();
    CartoApp_1.default.prefsStorage = new NodeStorage_1.default(localEnv.utilities.testWorkingPath + "prefs" + NodeStorage_1.default.platformFolderDelimiter, "");
    CartoApp_1.default.projectsStorage = new NodeStorage_1.default(localEnv.utilities.testWorkingPath + "projects" + NodeStorage_1.default.platformFolderDelimiter, "");
    CartoApp_1.default.packStorage = new NodeStorage_1.default(localEnv.utilities.testWorkingPath + "packs" + NodeStorage_1.default.platformFolderDelimiter, "");
    CartoApp_1.default.worldStorage = new NodeStorage_1.default(localEnv.utilities.testWorkingPath + "worlds" + NodeStorage_1.default.platformFolderDelimiter, "");
    CartoApp_1.default.deploymentStorage = new NodeStorage_1.default(localEnv.utilities.testWorkingPath + "deployment" + NodeStorage_1.default.platformFolderDelimiter, "");
    CartoApp_1.default.workingStorage = new NodeStorage_1.default(localEnv.utilities.testWorkingPath + "working" + NodeStorage_1.default.platformFolderDelimiter, "");
    const coreStorage = new NodeStorage_1.default(__dirname + "/../../public/data/content/", "");
    Database_1.default.contentFolder = coreStorage.rootFolder;
    await CartoApp_1.default.init();
    carto = CartoApp_1.default.carto;
    if (!carto) {
        return;
    }
    await carto.load();
    Database_1.default.local = localEnv.utilities;
    carto.local = localEnv.utilities;
    carto.onStatusAdded.subscribe(handleStatusAdded);
    run();
})();
function handleStatusAdded(carto, status) {
    console.log(status.message);
}
function _ensureLocalFolder(path) {
    const ls = new NodeStorage_1.default(path, "");
    return ls.rootFolder;
}
async function _localFolderExists(path) {
    const ls = new NodeStorage_1.default(path, "");
    return await ls.rootFolder.exists();
}
async function _loadProject(name) {
    if (!carto || !scenariosFolder || !resultsFolder) {
        chai_1.assert.fail("Not properly initialized");
    }
    const project = new Project_1.default(carto, name, null);
    project.autoDeploymentMode = Project_1.ProjectAutoDeploymentMode.noAutoDeployment;
    project.localFolderPath = __dirname + "/../../../samplecontent/" + name + "/";
    await project.inferProjectItemsFromFiles();
    return project;
}
function removeResultFolder(scenarioName) {
    if (resultsFolder) {
        const path = StorageUtilities_1.default.ensureEndsWithDelimiter(resultsFolder.fullPath) +
            StorageUtilities_1.default.ensureEndsWithDelimiter(scenarioName);
        // guard against being called at a "more root" file path
        if (fs.existsSync(path) && Utilities_1.default.countChar(path, NodeStorage_1.default.platformFolderDelimiter) > 5) {
            try {
                fs.rmSync(path, {
                    recursive: true,
                });
            }
            catch (e) {
                console.log("Error occurred during rmSync on '" + path + "'");
                throw e;
            }
        }
    }
}
describe("simple", async () => {
    it("has expected structure", async () => {
        const project = await _loadProject("simple");
        (0, chai_1.expect)(project.items.length).to.equal(6);
    });
    it("report file matches", async () => {
        const project = await _loadProject("simple");
        const pis = new ProjectInfoSet_1.default(project, IProjectInfoData_1.ProjectInfoSuite.allExceptAddOn, ["JSON"]); // don't do schema tests as results may change via network'ed schema def changes over time.
        await pis.generateForProject();
        const dataObject = pis.getDataObject();
        await ensureJsonMatchesScenario(dataObject, "simple");
    });
});
describe("deployJs", async () => {
    before((done) => {
        removeResultFolder("deployJs");
        done();
    });
    it("has expected structure", async () => {
        const project = await _loadProject("deployJs");
        (0, chai_1.expect)(project.items.length).to.equal(4);
    });
    it("deploy outputs match", async () => {
        const project = await _loadProject("deployJs");
        if (!carto || !resultsFolder) {
            return;
        }
        const worldSettings = {
            generator: WorldLevelDat_1.Generator.infinite,
            gameType: WorldLevelDat_1.GameType.survival,
            randomSeed: "3000",
            lastPlayed: BigInt(new Date(2023, 0, 1).getTime()),
        };
        const resultsOutFolder = resultsFolder.ensureFolder("deployJs");
        await resultsOutFolder.ensureExists();
        await ProjectExporter_1.default.deployProjectAndGeneratedWorldTo(carto, project, worldSettings, resultsOutFolder);
        await folderMatches("deployJs", ["level.dat", "level.dat_old"]);
    });
});
async function folderMatches(scenarioName, excludeList) {
    if (!scenariosFolder || !resultsFolder) {
        chai_1.assert.fail("Not properly initialized");
    }
    const scenarioOutFolder = resultsFolder.ensureFolder(scenarioName);
    await scenarioOutFolder.ensureExists();
    const scenarioFolder = scenariosFolder.ensureFolder(scenarioName);
    const isEqual = await StorageUtilities_1.default.folderContentsEqual(scenarioFolder, scenarioOutFolder, excludeList, true, [
        '"uuid":',
        '"pack_id":',
        '"version":',
        "generator_version",
        "generatorVersion",
    ]);
    (0, chai_1.assert)(isEqual.result, "Folder '" + scenarioFolder.fullPath + "' does not match for scenario '" + scenarioName + "'. " + isEqual.reason);
}
async function ensureJsonMatchesScenario(obj, scenarioName) {
    if (!scenariosFolder || !resultsFolder) {
        chai_1.assert.fail("Not properly initialized");
    }
    const dataObjectStr = JSON.stringify(obj, null, 2);
    const scenarioOutFolder = resultsFolder.ensureFolder(scenarioName);
    await scenarioOutFolder.ensureExists();
    const outFile = scenarioOutFolder.ensureFile("report.json");
    outFile.setContent(dataObjectStr);
    await outFile.saveContent();
    const scenarioFile = scenariosFolder.ensureFolder(scenarioName).ensureFile("report.json");
    const exists = await scenarioFile.exists();
    (0, chai_1.assert)(exists, "report.json file for scenario '" + scenarioName + "' does not exist.");
    const isEqual = await StorageUtilities_1.default.fileContentsEqual(scenarioFile, outFile, true, ["generatorVersion"]);
    (0, chai_1.assert)(isEqual, "report.json file '" + scenarioFile.fullPath + "' does not match for scenario '" + scenarioName + "'");
}

//# sourceMappingURL=../maps/test/CartoTest.js.map
