@itwin/core-backend 5.4.0-dev.5 → 5.4.0-dev.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ import { Code, IModel, QueryBinder, SubCategoryAppearance } from "@itwin/core-co
7
7
  import * as chai from "chai";
8
8
  import * as chaiAsPromised from "chai-as-promised";
9
9
  import { HubWrappers, IModelTestUtils, KnownTestLocations } from "..";
10
- import { BriefcaseManager, ChannelControl, DrawingCategory, IModelHost, SqliteChangesetReader } from "../../core-backend";
10
+ import { BriefcaseManager, ChangesetECAdaptor, ChannelControl, DrawingCategory, IModelHost, SqliteChangesetReader } from "../../core-backend";
11
11
  import { HubMock } from "../../internal/HubMock";
12
12
  import { StashManager } from "../../StashManager";
13
13
  import { existsSync, unlinkSync, writeFileSync } from "fs";
@@ -739,6 +739,41 @@ describe("rebase changes & stashing api", function () {
739
739
  chai.expect(b2.elements.tryGetElementProps(e3)).to.undefined; // add by rebase so should not exist either
740
740
  chai.expect(BriefcaseManager.containsRestorePoint(b2, BriefcaseManager.PULL_MERGE_RESTORE_POINT_NAME)).is.false;
741
741
  });
742
+ it("calling discardChanges() from inside indirect scope is not allowed", async () => {
743
+ const b1 = await testIModel.openBriefcase();
744
+ await testIModel.insertElement(b1);
745
+ b1.saveChanges();
746
+ const p1 = b1.txns.withIndirectTxnModeAsync(async () => {
747
+ await b1.discardChanges();
748
+ });
749
+ await chai.expect(p1).to.be.rejectedWith("Cannot discard changes when there are indirect changes");
750
+ b1.saveChanges();
751
+ });
752
+ it("calling discardChanges() during rebasing is not allowed", async () => {
753
+ const b1 = await testIModel.openBriefcase();
754
+ const b2 = await testIModel.openBriefcase();
755
+ await testIModel.insertElement(b1);
756
+ await testIModel.insertElement(b1);
757
+ b1.saveChanges();
758
+ await b1.pushChanges({ description: "inserted element" });
759
+ await testIModel.insertElement(b2);
760
+ await testIModel.insertElement(b2);
761
+ b2.saveChanges();
762
+ b2.txns.rebaser.setCustomHandler({
763
+ shouldReinstate: (_txnProps) => {
764
+ return true;
765
+ },
766
+ recompute: async (_txnProps) => {
767
+ chai.expect(b2.txns.rebaser.isAborting).is.false;
768
+ await b2.discardChanges();
769
+ },
770
+ });
771
+ chai.expect(b2.txns.rebaser.isAborting).is.false;
772
+ const p1 = b2.pullChanges();
773
+ await chai.expect(p1).to.be.rejectedWith("Cannot discard changes while a rebase is in progress");
774
+ chai.expect(b2.txns.rebaser.canAbort()).is.true;
775
+ await b2.txns.rebaser.abort();
776
+ });
742
777
  it("getStash() should throw exception", async () => {
743
778
  const b1 = await testIModel.openBriefcase();
744
779
  chai.expect(() => StashManager.getStash({ db: b1, stash: "invalid_stash" })).to.throw("Invalid stash");
@@ -803,5 +838,154 @@ describe("rebase changes & stashing api", function () {
803
838
  const e3Props = await findElement(e3);
804
839
  chai.expect(e3Props).to.exist;
805
840
  });
841
+ it("enum txn changes in recompute", async () => {
842
+ const b1 = await testIModel.openBriefcase();
843
+ const b2 = await testIModel.openBriefcase();
844
+ const e1 = await testIModel.insertElement(b1);
845
+ const e2 = await testIModel.insertElement(b1, true);
846
+ b1.saveChanges();
847
+ await b1.pushChanges({ description: "insert element 1 direct and 1 indirect" });
848
+ await b2.pullChanges();
849
+ await testIModel.updateElement(b1, e1);
850
+ await testIModel.updateElement(b1, e2, true);
851
+ b1.saveChanges();
852
+ await b1.pushChanges({ description: "update element 1 direct and 1 indirect" });
853
+ await testIModel.insertElement(b2);
854
+ await testIModel.insertElement(b2, true);
855
+ b2.saveChanges("first change");
856
+ await testIModel.insertElement(b2);
857
+ await testIModel.insertElement(b2, true);
858
+ b2.saveChanges("second change");
859
+ await testIModel.insertElement(b2);
860
+ await testIModel.insertElement(b2, true);
861
+ b2.saveChanges("third change");
862
+ let txnVerified = 0;
863
+ b2.txns.rebaser.setCustomHandler({
864
+ shouldReinstate: (_txn) => {
865
+ return true;
866
+ },
867
+ recompute: async (txn) => {
868
+ const reader = SqliteChangesetReader.openTxn({ txnId: txn.id, db: b2, disableSchemaCheck: true });
869
+ const adaptor = new ChangesetECAdaptor(reader);
870
+ adaptor.acceptClass("TestDomain:a1");
871
+ const ids = new Set();
872
+ while (adaptor.step()) {
873
+ if (!adaptor.reader.isIndirect)
874
+ ids.add(adaptor.inserted?.ECInstanceId || adaptor.deleted?.ECInstanceId);
875
+ }
876
+ adaptor.close();
877
+ if (txn.props.description === "first change") {
878
+ chai.expect(Array.from(ids.keys())).deep.equal(["0x40000000001"]);
879
+ txnVerified++;
880
+ }
881
+ else if (txn.props.description === "second change") {
882
+ chai.expect(Array.from(ids.keys())).deep.equal(["0x40000000003"]);
883
+ txnVerified++;
884
+ }
885
+ else if (txn.props.description === "third change") {
886
+ chai.expect(Array.from(ids.keys())).deep.equal(["0x40000000005"]);
887
+ txnVerified++;
888
+ }
889
+ else {
890
+ txnVerified++;
891
+ }
892
+ },
893
+ });
894
+ await b2.pullChanges();
895
+ chai.expect(txnVerified).to.equal(3);
896
+ });
897
+ it("before and after rebase events", async () => {
898
+ const b1 = await testIModel.openBriefcase();
899
+ const b2 = await testIModel.openBriefcase();
900
+ const e1 = await testIModel.insertElement(b1);
901
+ const e2 = await testIModel.insertElement(b1, true);
902
+ b1.saveChanges();
903
+ await b1.pushChanges({ description: "insert element 1 direct and 1 indirect" });
904
+ await b2.pullChanges();
905
+ await testIModel.updateElement(b1, e1);
906
+ await testIModel.updateElement(b1, e2, true);
907
+ b1.saveChanges();
908
+ await b1.pushChanges({ description: "update element 1 direct and 1 indirect" });
909
+ await testIModel.insertElement(b2);
910
+ await testIModel.insertElement(b2, true);
911
+ b2.saveChanges("first change");
912
+ await testIModel.insertElement(b2);
913
+ await testIModel.insertElement(b2, true);
914
+ b2.saveChanges("second change");
915
+ await testIModel.insertElement(b2);
916
+ await testIModel.insertElement(b2, true);
917
+ b2.saveChanges("third change");
918
+ const events = {
919
+ onRebase: {
920
+ beginCount: 0,
921
+ endCount: 0,
922
+ beginIds: [],
923
+ },
924
+ onRebaseTxn: {
925
+ beginTxns: [],
926
+ endTxns: [],
927
+ },
928
+ rebaseHandler: {
929
+ shouldReinstate: [],
930
+ recompute: [],
931
+ }
932
+ };
933
+ const resetEvent = () => {
934
+ events.onRebase.beginCount = 0;
935
+ events.onRebase.endCount = 0;
936
+ events.onRebase.beginIds = [];
937
+ events.onRebaseTxn.beginTxns = [];
938
+ events.onRebaseTxn.endTxns = [];
939
+ events.rebaseHandler.shouldReinstate = [];
940
+ events.rebaseHandler.recompute = [];
941
+ };
942
+ b2.txns.onRebaseBegin.addListener((ids) => {
943
+ events.onRebase.beginCount++;
944
+ events.onRebase.beginIds.push(...ids);
945
+ });
946
+ b2.txns.onRebaseEnd.addListener(() => {
947
+ events.onRebase.endCount++;
948
+ });
949
+ b2.txns.onRebaseTxnBegin.addListener((txn) => {
950
+ events.onRebaseTxn.beginTxns.push(txn);
951
+ });
952
+ b2.txns.onRebaseTxnEnd.addListener((txn) => {
953
+ events.onRebaseTxn.endTxns.push(txn);
954
+ });
955
+ b2.txns.rebaser.setCustomHandler({
956
+ shouldReinstate: (_txn) => {
957
+ events.rebaseHandler.shouldReinstate.push(_txn);
958
+ return true;
959
+ },
960
+ recompute: async (_txn) => {
961
+ events.rebaseHandler.recompute.push(_txn);
962
+ },
963
+ });
964
+ resetEvent();
965
+ await b2.pullChanges();
966
+ chai.expect(events.onRebase.beginCount).to.equal(1);
967
+ chai.expect(events.onRebase.endCount).to.equal(1);
968
+ chai.expect(events.onRebase.beginIds).to.deep.equal(["0x100000000", "0x100000001", "0x100000002"]);
969
+ chai.expect(events.onRebaseTxn.beginTxns.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002"]);
970
+ chai.expect(events.onRebaseTxn.endTxns.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002"]);
971
+ chai.expect(events.rebaseHandler.shouldReinstate.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002"]);
972
+ chai.expect(events.rebaseHandler.recompute.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002"]);
973
+ await testIModel.updateElement(b1, e1);
974
+ await testIModel.updateElement(b1, e2, true);
975
+ b1.saveChanges();
976
+ await b1.pushChanges({ description: "update element 1 direct and 1 indirect" });
977
+ await testIModel.insertElement(b2);
978
+ await testIModel.insertElement(b2, true);
979
+ b2.saveChanges("forth change");
980
+ resetEvent();
981
+ await b2.pullChanges();
982
+ chai.expect(events.onRebase.beginCount).to.equal(1);
983
+ chai.expect(events.onRebase.endCount).to.equal(1);
984
+ chai.expect(events.onRebase.beginIds).to.deep.equal(["0x100000000", "0x100000001", "0x100000002", "0x100000003"]);
985
+ chai.expect(events.onRebaseTxn.beginTxns.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002", "0x100000003"]);
986
+ chai.expect(events.onRebaseTxn.endTxns.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002", "0x100000003"]);
987
+ chai.expect(events.rebaseHandler.shouldReinstate.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002", "0x100000003"]);
988
+ chai.expect(events.rebaseHandler.recompute.map((txn) => txn.id)).to.deep.equal(["0x100000000", "0x100000001", "0x100000002", "0x100000003"]);
989
+ });
806
990
  });
807
991
  //# sourceMappingURL=Rebase.test.js.map