From e8170a09a7277a326f1ff0e303311ab9be59d5ad Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@rwth-aachen.de>
Date: Thu, 26 Oct 2017 14:40:21 +0200
Subject: [PATCH] fixed all insert tests

---
 src/Struct/Item.js          | 34 ++++++++++++++++++----------------
 test/y-array.tests.js       | 17 ++++++++++-------
 tests-lib/helper.js         |  2 --
 tests-lib/test-connector.js | 11 +++++++++--
 4 files changed, 37 insertions(+), 27 deletions(-)

diff --git a/src/Struct/Item.js b/src/Struct/Item.js
index 6dceb10c..26390612 100644
--- a/src/Struct/Item.js
+++ b/src/Struct/Item.js
@@ -231,9 +231,12 @@ export default class Item {
     if (info & 0b1) {
       encoder.writeID(this._origin._lastId)
     }
+    // TODO: remove
+    /* see above
     if (info & 0b10) {
       encoder.writeID(this._left._lastId)
     }
+    */
     if (info & 0b100) {
       encoder.writeID(this._right_origin._id)
     }
@@ -254,34 +257,33 @@ export default class Item {
     if (info & 0b1) {
       // origin != null
       const originID = decoder.readID()
-      if (this._origin === null) {
-        const origin = y.os.getItemCleanEnd(originID)
-        if (origin === null) {
-          missing.push(originID)
-        } else {
-          this._origin = origin
-          this._left = this._origin
-        }
+      // we have to query for left again because it might have been split/merged..
+      const origin = y.os.getItemCleanEnd(originID)
+      if (origin === null) {
+        missing.push(originID)
+      } else {
+        this._origin = origin
+        this._left = this._origin
       }
     }
     // read right
     if (info & 0b100) {
       // right != null
       const rightID = decoder.readID()
-      if (this._right_origin === null) {
-        const right = y.os.getItemCleanStart(rightID)
-        if (right === null) {
-          missing.push(rightID)
-        } else {
-          this._right = right
-          this._right_origin = right
-        }
+      // we have to query for right again because it might have been split/merged..
+      const right = y.os.getItemCleanStart(rightID)
+      if (right === null) {
+        missing.push(rightID)
+      } else {
+        this._right = right
+        this._right_origin = right
       }
     }
     // read parent
     if (~info & 0b101) {
       // neither origin nor right is defined
       const parentID = decoder.readID()
+      // parent does not change, so we don't have to search for it again
       if (this._parent === null) {
         const parent = y.os.get(parentID)
         if (parent === null) {
diff --git a/test/y-array.tests.js b/test/y-array.tests.js
index bf76e4f9..cc169105 100644
--- a/test/y-array.tests.js
+++ b/test/y-array.tests.js
@@ -2,7 +2,6 @@ import { wait, initArrays, compareUsers, Y, flushAll, applyRandomTests } from '.
 import { test, proxyConsole } from 'cutest'
 
 proxyConsole()
-
 test('basic spec', async function array0 (t) {
   let { users, array0 } = await initArrays(t, { users: 2 })
 
@@ -229,7 +228,6 @@ var arrayTransactions = [
     var pos = chance.integer({ min: 0, max: yarray.length })
     yarray.insert(pos, content)
   },
-  /*
   function insertTypeArray (t, user, chance) {
     const yarray = user.get('array', Y.Array)
     var pos = chance.integer({ min: 0, max: yarray.length })
@@ -268,11 +266,10 @@ var arrayTransactions = [
       }
     }
   }
-  */
 ]
 
-test('y-array: Random tests (5)', async function randomArray5 (t) {
-  await applyRandomTests(t, arrayTransactions, 30)
+test('y-array: Random tests (20)', async function randomArray20 (t) {
+  await applyRandomTests(t, arrayTransactions, 20)
 })
 
 test('y-array: Random tests (42)', async function randomArray42 (t) {
@@ -299,7 +296,6 @@ test('y-array: Random tests (47)', async function randomArray47 (t) {
   await applyRandomTests(t, arrayTransactions, 47)
 })
 
-/*
 test('y-array: Random tests (200)', async function randomArray200 (t) {
   await applyRandomTests(t, arrayTransactions, 200)
 })
@@ -315,4 +311,11 @@ test('y-array: Random tests (400)', async function randomArray400 (t) {
 test('y-array: Random tests (500)', async function randomArray500 (t) {
   await applyRandomTests(t, arrayTransactions, 500)
 })
-*/
+
+test('y-array: Random tests (1000)', async function randomArray1000 (t) {
+  await applyRandomTests(t, arrayTransactions, 1000)
+})
+
+test('y-array: Random tests (2000)', async function randomArray2000 (t) {
+  await applyRandomTests(t, arrayTransactions, 2000)
+})
diff --git a/tests-lib/helper.js b/tests-lib/helper.js
index 15f536e8..ebf7fe9a 100644
--- a/tests-lib/helper.js
+++ b/tests-lib/helper.js
@@ -248,8 +248,6 @@ export function wait (t) {
 
 export async function applyRandomTests (t, mods, iterations) {
   const chance = new Chance(t.getSeed() * 1000000000)
-    // TODO: remove
-    console.info('seed: ' + t._seed)
   var initInformation = await initArrays(t, { users: 5, chance: chance })
   let { users } = initInformation
   for (var i = 0; i < iterations; i++) {
diff --git a/tests-lib/test-connector.js b/tests-lib/test-connector.js
index 8461b1b6..ec24f71d 100644
--- a/tests-lib/test-connector.js
+++ b/tests-lib/test-connector.js
@@ -14,8 +14,15 @@ export class TestRoom {
     this.users.set(userID, connector)
     for (let [uid, user] of this.users) {
       if (uid !== userID && (user.role === 'master' || connector.role === 'master')) {
-        connector.userJoined(uid, user.role)
-        user.userJoined(userID, connector.role)
+        // The order is important because there is no timeout in send/receiveMessage
+        // (the user that receives a sync step must already now about the sender)
+        if (user.role === 'master') {
+          connector.userJoined(uid, user.role)
+          user.userJoined(userID, connector.role)
+        } else if (connector.role === 'master') {
+          user.userJoined(userID, connector.role)
+          connector.userJoined(uid, user.role)
+        }
       }
     }
   }