"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
var __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
const bentleyjs_core_1 = require("@bentley/bentleyjs-core");
const imodeljs_frontend_1 = require("@bentley/imodeljs-frontend");
const frontend_1 = require("@bentley/oidc-signin-tool/lib/frontend");
const TestContext_1 = require("../setup/TestContext");
const expect = chai.expect;
chai.use(chaiAsPromised);
describe("Basic Scenarios", async () => {
    let testContext;
    before(async () => {
        testContext = await TestContext_1.TestContext.instance();
        const accessToken = testContext.adminUserAccessToken;
        imodeljs_frontend_1.IModelApp.authorizationClient = new frontend_1.TestFrontendAuthorizationClient(accessToken);
    });
    async function openIModelAndQueryPage(contextId, iModelId, openMode) {
        const iModel = await imodeljs_frontend_1.RemoteBriefcaseConnection.open(contextId, iModelId, openMode); // eslint-disable-line deprecation/deprecation
        expect(iModel).to.exist;
        expect(iModel.elements).to.exist;
        const elements = iModel.elements;
        const elementProps = await elements.getProps(elements.rootSubjectId);
        expect(elementProps).to.exist;
        expect(elementProps.length).to.equal(1);
    }
    it("should successfully open a new IModel with changesets for read and Get Properties for an Element TestCase:819342", async () => {
        const contextId = testContext.contextId;
        const openMode = bentleyjs_core_1.OpenMode.Readonly;
        const iModelId = testContext.iModelWithChangesets.iModelId;
        await openIModelAndQueryPage(contextId, iModelId, openMode);
    });
    // imodeljs does not allow this -- changesetid must be non-empty for routing purposes.
    it.skip("should successfully open a new IModel without changesets for read and Get Properties for an Element TestCase:872675", async () => {
        const contextId = testContext.contextId;
        const openMode = bentleyjs_core_1.OpenMode.Readonly;
        const iModelId = testContext.iModelWithChangesets.iModelId;
        await openIModelAndQueryPage(contextId, iModelId, openMode);
    });
    it("should open iModel and Execute Query TestCase:819343", async () => {
        var e_1, _a;
        const iModel = await testContext.iModelWithChangesets.getConnection();
        const rows = [];
        try {
            for (var _b = __asyncValues(iModel.query("SELECT ECInstanceId AS id FROM BisCore.Element", undefined, 10)), _c; _c = await _b.next(), !_c.done;) {
                const row = _c.value;
                rows.push(row);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) await _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        expect(rows).not.to.be.empty;
    });
    /* This test is wrong. If two users open the same imodel using the same mode in the same backend, then they
          will share a single briefcase in the backend. When either user closes the imodel, then the briefcase will
          be closed. The backend does not maintain some kind of ref count that would keep the briefcase open
          for the second connection.
    it("should not affect other users when iModel is closed TestCase:819344 #orchestrator", async () => {
      const iModelId = testContext.iModelWithChangesets.iModelId;
      const contextId = testContext.iModelWithChangesets.contextId;
      const openMode = OpenMode.Readonly;
  
      const originalAppAuth = TestRpcClientManager.configuration.applicationAuthorizationValue;
  
      try {
        // Get access token of user that does not have permission to read given iModel
        const user1accessToken = await testContext.regularUser1.getAccessToken();
        const user1accessTokenString = user1accessToken.toTokenString() || "";
  
        TestRpcClientManager.configuration.applicationAuthorizationValue = user1accessTokenString;
        const iModel1 = await IModelConnection.open(user1accessToken, contextId, iModelId, openMode);
  
        // Open the same imodel for another user
        const user2accessToken = await testContext.regularUser2.getAccessToken();
        const user2accessTokenString = user2accessToken.toTokenString() || "";
  
        TestRpcClientManager.configuration.applicationAuthorizationValue = user2accessTokenString;
        const iModel2 = await IModelConnection.open(user2accessToken, contextId, iModelId, openMode);
        const query = "SELECT ECInstanceId AS id FROM BisCore.Element";
  
        // Act: Close the iModel for the same user
        {
            TestRpcClientManager.configuration.applicationAuthorizationValue = user1accessTokenString;
            const rows = await iModel1.queryPage(query);
            expect(rows).to.exist.and.be.not.empty;
            await iModel1.close(user1accessToken);
        }
  
        // Assert: Previous session close should not affect other users
        {
            TestRpcClientManager.configuration.applicationAuthorizationValue = user2accessTokenString;
            const rows = await iModel2.queryPage(query);
            expect(rows).to.exist.and.be.not.empty;
        }
      } finally {
        TestRpcClientManager.configuration.applicationAuthorizationValue = originalAppAuth;
      }
    });
   */
});
//# sourceMappingURL=BasicScenarios.test.js.map