"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const chai_1 = require("chai");
const bentleyjs_core_1 = require("@bentley/bentleyjs-core");
const geometry_core_1 = require("@bentley/geometry-core");
const imodelhub_client_1 = require("@bentley/imodelhub-client");
const imodeljs_frontend_1 = require("@bentley/imodeljs-frontend");
const frontend_1 = require("@bentley/oidc-signin-tool/lib/frontend");
const TestContext_1 = require("./setup/TestContext");
/* eslint-disable deprecation/deprecation */
describe("IModel Read/Write Connection", () => {
    let accessToken;
    let testContext;
    before(async function () {
        testContext = await TestContext_1.TestContext.instance();
        if (!testContext.settings.runiModelWriteRpcTests)
            this.skip();
        accessToken = testContext.adminUserAccessToken;
        imodeljs_frontend_1.IModelApp.authorizationClient = new frontend_1.TestFrontendAuthorizationClient(accessToken);
    });
    after(async function () {
        if (!testContext.settings.runiModelWriteRpcTests)
            return;
        const iModelId = testContext.iModelForWrite.iModelId;
        const requestContext = await imodeljs_frontend_1.AuthorizedFrontendRequestContext.create();
        const briefcases = await imodeljs_frontend_1.IModelHubFrontend.iModelClient.briefcases.get(requestContext, iModelId, new imodelhub_client_1.BriefcaseQuery().ownedByMe());
        if (briefcases.length > 16) {
            bentleyjs_core_1.Logger.logInfo("TestUtility", `Reached limit of maximum number of briefcases for ${iModelId}. Purging all briefcases.`);
            const promises = new Array();
            briefcases.forEach((briefcase) => {
                promises.push(imodeljs_frontend_1.IModelHubFrontend.iModelClient.briefcases.delete(requestContext, iModelId, briefcase.briefcaseId));
            });
            await Promise.all(promises);
        }
    });
    it("should successfully open an IModelConnection for read/write", async () => {
        const contextId = testContext.iModelForWrite.contextId;
        const iModelId = testContext.iModelForWrite.iModelId;
        const iModel = await imodeljs_frontend_1.RemoteBriefcaseConnection.open(contextId, iModelId, bentleyjs_core_1.OpenMode.ReadWrite);
        chai_1.expect(iModel).to.exist.and.be.not.empty;
        chai_1.expect(iModel.getRpcProps()).to.exist.and.be.not.empty;
        await iModel.close();
    });
    it("should successfully close an open read/write IModelConnection", async () => {
        const contextId = testContext.iModelForWrite.contextId;
        const iModelId = testContext.iModelForWrite.iModelId;
        const iModel = await imodeljs_frontend_1.RemoteBriefcaseConnection.open(contextId, iModelId, bentleyjs_core_1.OpenMode.ReadWrite);
        chai_1.expect(iModel).to.exist;
        return chai_1.expect(iModel.close()).to.eventually.be.fulfilled;
    });
    it("should successfully update the project extents", async () => {
        const contextId = testContext.iModelForWrite.contextId;
        const iModelId = testContext.iModelForWrite.iModelId;
        const iModel = await imodeljs_frontend_1.RemoteBriefcaseConnection.open(contextId, iModelId, bentleyjs_core_1.OpenMode.ReadWrite);
        const originalExtents = iModel.projectExtents;
        const newExtents = geometry_core_1.Range3d.create(originalExtents.low, originalExtents.high);
        newExtents.low.x -= 50;
        newExtents.low.y -= 25;
        newExtents.low.z -= 189;
        newExtents.high.x += 1087;
        newExtents.high.y += 19;
        newExtents.high.z += .001;
        await iModel.updateProjectExtents(newExtents);
        await iModel.saveChanges();
        const updatediModel = await imodeljs_frontend_1.RemoteBriefcaseConnection.open(contextId, iModelId, bentleyjs_core_1.OpenMode.ReadWrite);
        const updatedExtents = geometry_core_1.Range3d.fromJSON(updatediModel.projectExtents);
        chai_1.assert.isTrue(newExtents.isAlmostEqual(updatedExtents), "Project extents successfully updated in database");
        await iModel.close();
        await updatediModel.close();
    });
    it("should successfully save a thumbnail", async () => {
        const contextId = testContext.iModelForWrite.contextId;
        const iModelId = testContext.iModelForWrite.iModelId;
        const iModel = await imodeljs_frontend_1.RemoteBriefcaseConnection.open(contextId, iModelId, bentleyjs_core_1.OpenMode.ReadWrite);
        const viewList = await iModel.views.getViewList({});
        chai_1.assert.isAtLeast(viewList.length, 1);
        const viewId = viewList[0].id;
        const thumbnail = await iModel.views.getThumbnail(viewId);
        chai_1.assert.equal(thumbnail.format, "png");
        chai_1.assert.equal(thumbnail.height, 768);
        chai_1.assert.equal(thumbnail.width, 768);
        chai_1.assert.equal(thumbnail.image.length, 19086);
        chai_1.assert.equal(thumbnail.image[3], 71);
        chai_1.assert.equal(thumbnail.image[18061], 160);
        thumbnail.format = "png";
        thumbnail.height = 100;
        thumbnail.width = 200;
        thumbnail.image = new Uint8Array(301); // try with odd number of bytes
        thumbnail.image.fill(33);
        await iModel.views.saveThumbnail(viewId, thumbnail);
        const thumbnail2 = await iModel.views.getThumbnail(viewId);
        chai_1.assert.equal(thumbnail2.format, "png");
        chai_1.assert.equal(thumbnail2.height, 100);
        chai_1.assert.equal(thumbnail2.width, 200);
        chai_1.assert.equal(thumbnail2.image.length, 301);
        chai_1.assert.equal(thumbnail2.image[3], 33);
        await iModel.close();
    });
});
//# sourceMappingURL=IModelWrite.test.js.map