DocumentManagerTests.coffee 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DocumentManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DocumentManager", ->
  7. beforeEach ->
  8. @DocumentManager = SandboxedModule.require modulePath, requires:
  9. "./RedisManager": @RedisManager = {}
  10. "./PersistenceManager": @PersistenceManager = {}
  11. "./HistoryManager": @HistoryManager = {}
  12. "logger-sharelatex": @logger = {log: sinon.stub()}
  13. "./DocOpsManager": @DocOpsManager = {}
  14. "./Metrics": @Metrics =
  15. Timer: class Timer
  16. done: sinon.stub()
  17. "./WebRedisManager": @WebRedisManager = {}
  18. "./DiffCodec": @DiffCodec = {}
  19. "./UpdateManager": @UpdateManager = {}
  20. @project_id = "project-id-123"
  21. @doc_id = "doc-id-123"
  22. @callback = sinon.stub()
  23. @lines = ["one", "two", "three"]
  24. @version = 42
  25. @track_changes_entries = { comments: "mock", entries: "mock" }
  26. describe "flushAndDeleteDoc", ->
  27. describe "successfully", ->
  28. beforeEach ->
  29. @RedisManager.removeDocFromMemory = sinon.stub().callsArg(2)
  30. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArgWith(2)
  31. @HistoryManager.flushDocChanges = sinon.stub().callsArg(2)
  32. @DocumentManager.flushAndDeleteDoc @project_id, @doc_id, @callback
  33. it "should flush the doc", ->
  34. @DocumentManager.flushDocIfLoaded
  35. .calledWith(@project_id, @doc_id)
  36. .should.equal true
  37. it "should remove the doc from redis", ->
  38. @RedisManager.removeDocFromMemory
  39. .calledWith(@project_id, @doc_id)
  40. .should.equal true
  41. it "should call the callback without error", ->
  42. @callback.calledWith(null).should.equal true
  43. it "should time the execution", ->
  44. @Metrics.Timer::done.called.should.equal true
  45. it "should flush to track changes", ->
  46. @HistoryManager.flushDocChanges
  47. .calledWith(@project_id, @doc_id)
  48. .should.equal true
  49. describe "flushDocIfLoaded", ->
  50. describe "when the doc is in Redis", ->
  51. beforeEach ->
  52. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @track_changes_entries)
  53. @PersistenceManager.setDoc = sinon.stub().yields()
  54. @DocumentManager.flushDocIfLoaded @project_id, @doc_id, @callback
  55. it "should get the doc from redis", ->
  56. @RedisManager.getDoc
  57. .calledWith(@project_id, @doc_id)
  58. .should.equal true
  59. it "should write the doc lines to the persistence layer", ->
  60. @PersistenceManager.setDoc
  61. .calledWith(@project_id, @doc_id, @lines, @version, @track_changes_entries)
  62. .should.equal true
  63. it "should call the callback without error", ->
  64. @callback.calledWith(null).should.equal true
  65. it "should time the execution", ->
  66. @Metrics.Timer::done.called.should.equal true
  67. describe "when the document is not in Redis", ->
  68. beforeEach ->
  69. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, null, null, null)
  70. @PersistenceManager.setDoc = sinon.stub().yields()
  71. @DocOpsManager.flushDocOpsToMongo = sinon.stub().callsArgWith(2)
  72. @DocumentManager.flushDocIfLoaded @project_id, @doc_id, @callback
  73. it "should get the doc from redis", ->
  74. @RedisManager.getDoc
  75. .calledWith(@project_id, @doc_id)
  76. .should.equal true
  77. it "should not write anything to the persistence layer", ->
  78. @PersistenceManager.setDoc.called.should.equal false
  79. @DocOpsManager.flushDocOpsToMongo.called.should.equal false
  80. it "should call the callback without error", ->
  81. @callback.calledWith(null).should.equal true
  82. it "should time the execution", ->
  83. @Metrics.Timer::done.called.should.equal true
  84. describe "getDocAndRecentOps", ->
  85. describe "with a previous version specified", ->
  86. beforeEach ->
  87. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @track_changes_entries)
  88. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  89. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, @fromVersion, @callback
  90. it "should get the doc", ->
  91. @DocumentManager.getDoc
  92. .calledWith(@project_id, @doc_id)
  93. .should.equal true
  94. it "should get the doc ops", ->
  95. @RedisManager.getPreviousDocOps
  96. .calledWith(@doc_id, @fromVersion, @version)
  97. .should.equal true
  98. it "should call the callback with the doc info", ->
  99. @callback.calledWith(null, @lines, @version, @ops).should.equal true
  100. it "should time the execution", ->
  101. @Metrics.Timer::done.called.should.equal true
  102. describe "with no previous version specified", ->
  103. beforeEach ->
  104. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @track_changes_entries)
  105. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  106. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, -1, @callback
  107. it "should get the doc", ->
  108. @DocumentManager.getDoc
  109. .calledWith(@project_id, @doc_id)
  110. .should.equal true
  111. it "should not need to get the doc ops", ->
  112. @RedisManager.getPreviousDocOps.called.should.equal false
  113. it "should call the callback with the doc info", ->
  114. @callback.calledWith(null, @lines, @version, []).should.equal true
  115. it "should time the execution", ->
  116. @Metrics.Timer::done.called.should.equal true
  117. describe "getDoc", ->
  118. describe "when the doc exists in Redis", ->
  119. beforeEach ->
  120. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @track_changes_entries)
  121. @DocumentManager.getDoc @project_id, @doc_id, @callback
  122. it "should get the doc from Redis", ->
  123. @RedisManager.getDoc
  124. .calledWith(@project_id, @doc_id)
  125. .should.equal true
  126. it "should call the callback with the doc info", ->
  127. @callback.calledWith(null, @lines, @version, @track_changes_entries, true).should.equal true
  128. it "should time the execution", ->
  129. @Metrics.Timer::done.called.should.equal true
  130. describe "when the doc does not exist in Redis", ->
  131. beforeEach ->
  132. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, null, null, null, null)
  133. @PersistenceManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @track_changes_entries)
  134. @RedisManager.putDocInMemory = sinon.stub().yields()
  135. @DocumentManager.getDoc @project_id, @doc_id, @callback
  136. it "should try to get the doc from Redis", ->
  137. @RedisManager.getDoc
  138. .calledWith(@project_id, @doc_id)
  139. .should.equal true
  140. it "should get the doc from the PersistenceManager", ->
  141. @PersistenceManager.getDoc
  142. .calledWith(@project_id, @doc_id)
  143. .should.equal true
  144. it "should set the doc in Redis", ->
  145. @RedisManager.putDocInMemory
  146. .calledWith(@project_id, @doc_id, @lines, @version, @track_changes_entries)
  147. .should.equal true
  148. it "should call the callback with the doc info", ->
  149. @callback.calledWith(null, @lines, @version, @track_changes_entries, false).should.equal true
  150. it "should time the execution", ->
  151. @Metrics.Timer::done.called.should.equal true
  152. describe "setDoc", ->
  153. describe "with plain tex lines", ->
  154. beforeEach ->
  155. @beforeLines = ["before", "lines"]
  156. @afterLines = ["after", "lines"]
  157. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, @track_changes_entries, true)
  158. @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
  159. @UpdateManager.applyUpdate = sinon.stub().callsArgWith(3, null)
  160. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
  161. @DocumentManager.flushAndDeleteDoc = sinon.stub().callsArg(2)
  162. describe "when already loaded", ->
  163. beforeEach ->
  164. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback
  165. it "should get the current doc lines", ->
  166. @DocumentManager.getDoc
  167. .calledWith(@project_id, @doc_id)
  168. .should.equal true
  169. it "should return a diff of the old and new lines", ->
  170. @DiffCodec.diffAsShareJsOp
  171. .calledWith(@beforeLines, @afterLines)
  172. .should.equal true
  173. it "should apply the diff as a ShareJS op", ->
  174. @UpdateManager.applyUpdate
  175. .calledWith(
  176. @project_id,
  177. @doc_id,
  178. {
  179. doc: @doc_id,
  180. v: @version,
  181. op: @ops,
  182. meta: {
  183. type: "external"
  184. source: @source
  185. user_id: @user_id
  186. }
  187. }
  188. )
  189. .should.equal true
  190. it "should flush the doc to Mongo", ->
  191. @DocumentManager.flushDocIfLoaded
  192. .calledWith(@project_id, @doc_id)
  193. .should.equal true
  194. it "should call the callback", ->
  195. @callback.calledWith(null).should.equal true
  196. it "should time the execution", ->
  197. @Metrics.Timer::done.called.should.equal true
  198. describe "when not already loaded", ->
  199. beforeEach ->
  200. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, false)
  201. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, @callback
  202. it "should flush and delete the doc from the doc updater", ->
  203. @DocumentManager.flushAndDeleteDoc
  204. .calledWith(@project_id, @doc_id)
  205. .should.equal true
  206. describe "without new lines", ->
  207. beforeEach ->
  208. @DocumentManager.setDoc @project_id, @doc_id, null, @source, @user_id, @callback
  209. it "should return the callback with an error", ->
  210. @callback.calledWith(new Error("No lines were passed to setDoc"))
  211. it "should not try to get the doc lines", ->
  212. @DocumentManager.getDoc.called.should.equal false