ApplyingUpdatesToADocTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = chai.expect
  5. async = require "async"
  6. Settings = require('settings-sharelatex')
  7. rclient_history = require("redis-sharelatex").createClient(Settings.redis.history)
  8. rclient_du = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
  9. Keys = Settings.redis.documentupdater.key_schema
  10. HistoryKeys = Settings.redis.history.key_schema
  11. ProjectHistoryKeys = Settings.redis.project_history.key_schema
  12. MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
  13. MockWebApi = require "./helpers/MockWebApi"
  14. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  15. describe "Applying updates to a doc", ->
  16. before ->
  17. @lines = ["one", "two", "three"]
  18. @version = 42
  19. @update =
  20. doc: @doc_id
  21. op: [{
  22. i: "one and a half\n"
  23. p: 4
  24. }]
  25. v: @version
  26. @result = ["one", "one and a half", "two", "three"]
  27. describe "when the document is not loaded", ->
  28. before (done) ->
  29. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  30. sinon.spy MockWebApi, "getDocument"
  31. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  32. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
  33. throw error if error?
  34. setTimeout done, 200
  35. after ->
  36. MockWebApi.getDocument.restore()
  37. it "should load the document from the web API", ->
  38. MockWebApi.getDocument
  39. .calledWith(@project_id, @doc_id)
  40. .should.equal true
  41. it "should update the doc", (done) ->
  42. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  43. doc.lines.should.deep.equal @result
  44. done()
  45. it "should push the applied updates to the track changes api", (done) ->
  46. rclient_history.lrange HistoryKeys.uncompressedHistoryOps({@doc_id}), 0, -1, (error, updates) =>
  47. throw error if error?
  48. JSON.parse(updates[0]).op.should.deep.equal @update.op
  49. rclient_history.sismember HistoryKeys.docsWithHistoryOps({@project_id}), @doc_id, (error, result) =>
  50. throw error if error?
  51. result.should.equal 1
  52. done()
  53. it "should push the applied updates to the project history changes api", (done) ->
  54. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  55. throw error if error?
  56. JSON.parse(updates[0]).op.should.deep.equal @update.op
  57. done()
  58. describe "when the document is loaded", ->
  59. before (done) ->
  60. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  61. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  62. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  63. throw error if error?
  64. sinon.spy MockWebApi, "getDocument"
  65. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
  66. throw error if error?
  67. setTimeout done, 200
  68. after ->
  69. MockWebApi.getDocument.restore()
  70. it "should not need to call the web api", ->
  71. MockWebApi.getDocument.called.should.equal false
  72. it "should update the doc", (done) ->
  73. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  74. doc.lines.should.deep.equal @result
  75. done()
  76. it "should push the applied updates to the track changes api", (done) ->
  77. rclient_history.lrange HistoryKeys.uncompressedHistoryOps({@doc_id}), 0, -1, (error, updates) =>
  78. JSON.parse(updates[0]).op.should.deep.equal @update.op
  79. rclient_history.sismember HistoryKeys.docsWithHistoryOps({@project_id}), @doc_id, (error, result) =>
  80. result.should.equal 1
  81. done()
  82. it "should push the applied updates to the project history changes api", (done) ->
  83. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  84. JSON.parse(updates[0]).op.should.deep.equal @update.op
  85. done()
  86. describe "when the document has been deleted", ->
  87. describe "when the ops come in a single linear order", ->
  88. before (done) ->
  89. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  90. lines = ["", "", ""]
  91. MockWebApi.insertDoc @project_id, @doc_id, {lines: lines, version: 0}
  92. @updates = [
  93. { doc_id: @doc_id, v: 0, op: [i: "h", p: 0 ] }
  94. { doc_id: @doc_id, v: 1, op: [i: "e", p: 1 ] }
  95. { doc_id: @doc_id, v: 2, op: [i: "l", p: 2 ] }
  96. { doc_id: @doc_id, v: 3, op: [i: "l", p: 3 ] }
  97. { doc_id: @doc_id, v: 4, op: [i: "o", p: 4 ] }
  98. { doc_id: @doc_id, v: 5, op: [i: " ", p: 5 ] }
  99. { doc_id: @doc_id, v: 6, op: [i: "w", p: 6 ] }
  100. { doc_id: @doc_id, v: 7, op: [i: "o", p: 7 ] }
  101. { doc_id: @doc_id, v: 8, op: [i: "r", p: 8 ] }
  102. { doc_id: @doc_id, v: 9, op: [i: "l", p: 9 ] }
  103. { doc_id: @doc_id, v: 10, op: [i: "d", p: 10] }
  104. ]
  105. @my_result = ["hello world", "", ""]
  106. done()
  107. it "should be able to continue applying updates when the project has been deleted", (done) ->
  108. actions = []
  109. for update in @updates.slice(0,6)
  110. do (update) =>
  111. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  112. actions.push (callback) => DocUpdaterClient.deleteDoc @project_id, @doc_id, callback
  113. for update in @updates.slice(6)
  114. do (update) =>
  115. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  116. async.series actions, (error) =>
  117. throw error if error?
  118. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  119. doc.lines.should.deep.equal @my_result
  120. done()
  121. it "should push the applied updates to the track changes api", (done) ->
  122. rclient_history.lrange HistoryKeys.uncompressedHistoryOps({@doc_id}), 0, -1, (error, updates) =>
  123. updates = (JSON.parse(u) for u in updates)
  124. for appliedUpdate, i in @updates
  125. appliedUpdate.op.should.deep.equal updates[i].op
  126. rclient_history.sismember HistoryKeys.docsWithHistoryOps({@project_id}), @doc_id, (error, result) =>
  127. result.should.equal 1
  128. done()
  129. it "should store the doc ops in the correct order", (done) ->
  130. rclient_du.lrange Keys.docOps({doc_id: @doc_id}), 0, -1, (error, updates) =>
  131. updates = (JSON.parse(u) for u in updates)
  132. for appliedUpdate, i in @updates
  133. appliedUpdate.op.should.deep.equal updates[i].op
  134. done()
  135. describe "when older ops come in after the delete", ->
  136. before (done) ->
  137. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  138. lines = ["", "", ""]
  139. MockWebApi.insertDoc @project_id, @doc_id, {lines: lines, version: 0}
  140. @updates = [
  141. { doc_id: @doc_id, v: 0, op: [i: "h", p: 0 ] }
  142. { doc_id: @doc_id, v: 1, op: [i: "e", p: 1 ] }
  143. { doc_id: @doc_id, v: 2, op: [i: "l", p: 2 ] }
  144. { doc_id: @doc_id, v: 3, op: [i: "l", p: 3 ] }
  145. { doc_id: @doc_id, v: 4, op: [i: "o", p: 4 ] }
  146. { doc_id: @doc_id, v: 0, op: [i: "world", p: 1 ] }
  147. ]
  148. @my_result = ["hello", "world", ""]
  149. done()
  150. it "should be able to continue applying updates when the project has been deleted", (done) ->
  151. actions = []
  152. for update in @updates.slice(0,5)
  153. do (update) =>
  154. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  155. actions.push (callback) => DocUpdaterClient.deleteDoc @project_id, @doc_id, callback
  156. for update in @updates.slice(5)
  157. do (update) =>
  158. actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
  159. async.series actions, (error) =>
  160. throw error if error?
  161. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  162. doc.lines.should.deep.equal @my_result
  163. done()
  164. describe "with a broken update", ->
  165. before (done) ->
  166. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  167. @broken_update = { doc_id: @doc_id, v: @version, op: [d: "not the correct content", p: 0 ] }
  168. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  169. DocUpdaterClient.subscribeToAppliedOps @messageCallback = sinon.stub()
  170. DocUpdaterClient.sendUpdate @project_id, @doc_id, @broken_update, (error) ->
  171. throw error if error?
  172. setTimeout done, 200
  173. it "should not update the doc", (done) ->
  174. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  175. doc.lines.should.deep.equal @lines
  176. done()
  177. it "should send a message with an error", ->
  178. @messageCallback.called.should.equal true
  179. [channel, message] = @messageCallback.args[0]
  180. channel.should.equal "applied-ops"
  181. JSON.parse(message).should.deep.equal {
  182. project_id: @project_id,
  183. doc_id: @doc_id,
  184. error:'Delete component \'not the correct content\' does not match deleted text \'one\ntwo\nthree\''
  185. }
  186. describe "with enough updates to flush to the track changes api", ->
  187. before (done) ->
  188. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  189. updates = []
  190. for v in [0..199] # Should flush after 100 ops
  191. updates.push
  192. doc_id: @doc_id,
  193. op: [i: v.toString(), p: 0]
  194. v: v
  195. sinon.spy MockTrackChangesApi, "flushDoc"
  196. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: 0}
  197. # Send updates in chunks to causes multiple flushes
  198. actions = []
  199. for i in [0..19]
  200. do (i) =>
  201. actions.push (cb) =>
  202. DocUpdaterClient.sendUpdates @project_id, @doc_id, updates.slice(i*10, (i+1)*10), cb
  203. async.series actions, (error) =>
  204. throw error if error?
  205. setTimeout done, 2000
  206. after ->
  207. MockTrackChangesApi.flushDoc.restore()
  208. it "should flush the doc twice", ->
  209. MockTrackChangesApi.flushDoc.calledTwice.should.equal true
  210. describe "when there is no version in Mongo", ->
  211. before (done) ->
  212. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  213. MockWebApi.insertDoc @project_id, @doc_id, {
  214. lines: @lines
  215. }
  216. update =
  217. doc: @doc_id
  218. op: @update.op
  219. v: 0
  220. DocUpdaterClient.sendUpdate @project_id, @doc_id, update, (error) ->
  221. throw error if error?
  222. setTimeout done, 200
  223. it "should update the doc (using version = 0)", (done) ->
  224. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  225. doc.lines.should.deep.equal @result
  226. done()
  227. describe "when the sending duplicate ops", ->
  228. before (done) ->
  229. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  230. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  231. DocUpdaterClient.subscribeToAppliedOps @messageCallback = sinon.stub()
  232. # One user delete 'one', the next turns it into 'once'. The second becomes a NOP.
  233. DocUpdaterClient.sendUpdate @project_id, @doc_id, {
  234. doc: @doc_id
  235. op: [{
  236. i: "one and a half\n"
  237. p: 4
  238. }]
  239. v: @version
  240. meta:
  241. source: "ikHceq3yfAdQYzBo4-xZ"
  242. }, (error) =>
  243. throw error if error?
  244. setTimeout () =>
  245. DocUpdaterClient.sendUpdate @project_id, @doc_id, {
  246. doc: @doc_id
  247. op: [{
  248. i: "one and a half\n"
  249. p: 4
  250. }]
  251. v: @version
  252. dupIfSource: ["ikHceq3yfAdQYzBo4-xZ"]
  253. meta:
  254. source: "ikHceq3yfAdQYzBo4-xZ"
  255. }, (error) =>
  256. throw error if error?
  257. setTimeout done, 200
  258. , 200
  259. it "should update the doc", (done) ->
  260. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  261. doc.lines.should.deep.equal @result
  262. done()
  263. it "should return a message about duplicate ops", ->
  264. @messageCallback.calledTwice.should.equal true
  265. @messageCallback.args[0][0].should.equal "applied-ops"
  266. expect(JSON.parse(@messageCallback.args[0][1]).op.dup).to.be.undefined
  267. @messageCallback.args[1][0].should.equal "applied-ops"
  268. expect(JSON.parse(@messageCallback.args[1][1]).op.dup).to.equal true