DocumentManagerTests.coffee 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DocumentManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. tk = require "timekeeper"
  8. describe "DocumentManager", ->
  9. beforeEach ->
  10. @DocumentManager = SandboxedModule.require modulePath, requires:
  11. "./RedisManager": @RedisManager = {}
  12. "./PersistenceManager": @PersistenceManager = {}
  13. "./HistoryManager": @HistoryManager = {}
  14. "logger-sharelatex": @logger = {log: sinon.stub()}
  15. "./DocOpsManager": @DocOpsManager = {}
  16. "./Metrics": @Metrics =
  17. Timer: class Timer
  18. done: sinon.stub()
  19. "./RealTimeRedisManager": @RealTimeRedisManager = {}
  20. "./DiffCodec": @DiffCodec = {}
  21. "./UpdateManager": @UpdateManager = {}
  22. "./RangesManager": @RangesManager = {}
  23. @project_id = "project-id-123"
  24. @doc_id = "doc-id-123"
  25. @callback = sinon.stub()
  26. @lines = ["one", "two", "three"]
  27. @version = 42
  28. @ranges = { comments: "mock", entries: "mock" }
  29. @unflushedTime = Date.now()
  30. describe "flushAndDeleteDoc", ->
  31. describe "successfully", ->
  32. beforeEach ->
  33. @RedisManager.removeDocFromMemory = sinon.stub().callsArg(2)
  34. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArgWith(2)
  35. @HistoryManager.flushDocChanges = sinon.stub().callsArg(2)
  36. @DocumentManager.flushAndDeleteDoc @project_id, @doc_id, @callback
  37. it "should flush the doc", ->
  38. @DocumentManager.flushDocIfLoaded
  39. .calledWith(@project_id, @doc_id)
  40. .should.equal true
  41. it "should remove the doc from redis", ->
  42. @RedisManager.removeDocFromMemory
  43. .calledWith(@project_id, @doc_id)
  44. .should.equal true
  45. it "should call the callback without error", ->
  46. @callback.calledWith(null).should.equal true
  47. it "should time the execution", ->
  48. @Metrics.Timer::done.called.should.equal true
  49. it "should flush to the history api", ->
  50. @HistoryManager.flushDocChanges
  51. .calledWith(@project_id, @doc_id)
  52. .should.equal true
  53. describe "flushDocIfLoaded", ->
  54. describe "when the doc is in Redis", ->
  55. beforeEach ->
  56. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges)
  57. @RedisManager.clearUnflushedTime = sinon.stub().callsArgWith(1, null)
  58. @PersistenceManager.setDoc = sinon.stub().yields()
  59. @DocumentManager.flushDocIfLoaded @project_id, @doc_id, @callback
  60. it "should get the doc from redis", ->
  61. @RedisManager.getDoc
  62. .calledWith(@project_id, @doc_id)
  63. .should.equal true
  64. it "should write the doc lines to the persistence layer", ->
  65. @PersistenceManager.setDoc
  66. .calledWith(@project_id, @doc_id, @lines, @version, @ranges)
  67. .should.equal true
  68. it "should call the callback without error", ->
  69. @callback.calledWith(null).should.equal true
  70. it "should time the execution", ->
  71. @Metrics.Timer::done.called.should.equal true
  72. describe "when the document is not in Redis", ->
  73. beforeEach ->
  74. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, null, null, null)
  75. @PersistenceManager.setDoc = sinon.stub().yields()
  76. @DocOpsManager.flushDocOpsToMongo = sinon.stub().callsArgWith(2)
  77. @DocumentManager.flushDocIfLoaded @project_id, @doc_id, @callback
  78. it "should get the doc from redis", ->
  79. @RedisManager.getDoc
  80. .calledWith(@project_id, @doc_id)
  81. .should.equal true
  82. it "should not write anything to the persistence layer", ->
  83. @PersistenceManager.setDoc.called.should.equal false
  84. @DocOpsManager.flushDocOpsToMongo.called.should.equal false
  85. it "should call the callback without error", ->
  86. @callback.calledWith(null).should.equal true
  87. it "should time the execution", ->
  88. @Metrics.Timer::done.called.should.equal true
  89. describe "getDocAndRecentOps", ->
  90. describe "with a previous version specified", ->
  91. beforeEach ->
  92. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges)
  93. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  94. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, @fromVersion, @callback
  95. it "should get the doc", ->
  96. @DocumentManager.getDoc
  97. .calledWith(@project_id, @doc_id)
  98. .should.equal true
  99. it "should get the doc ops", ->
  100. @RedisManager.getPreviousDocOps
  101. .calledWith(@doc_id, @fromVersion, @version)
  102. .should.equal true
  103. it "should call the callback with the doc info", ->
  104. @callback.calledWith(null, @lines, @version, @ops, @ranges).should.equal true
  105. it "should time the execution", ->
  106. @Metrics.Timer::done.called.should.equal true
  107. describe "with no previous version specified", ->
  108. beforeEach ->
  109. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges)
  110. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  111. @DocumentManager.getDocAndRecentOps @project_id, @doc_id, -1, @callback
  112. it "should get the doc", ->
  113. @DocumentManager.getDoc
  114. .calledWith(@project_id, @doc_id)
  115. .should.equal true
  116. it "should not need to get the doc ops", ->
  117. @RedisManager.getPreviousDocOps.called.should.equal false
  118. it "should call the callback with the doc info", ->
  119. @callback.calledWith(null, @lines, @version, [], @ranges).should.equal true
  120. it "should time the execution", ->
  121. @Metrics.Timer::done.called.should.equal true
  122. describe "getDoc", ->
  123. describe "when the doc exists in Redis", ->
  124. beforeEach ->
  125. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, @unflushedTime)
  126. @DocumentManager.getDoc @project_id, @doc_id, @callback
  127. it "should get the doc from Redis", ->
  128. @RedisManager.getDoc
  129. .calledWith(@project_id, @doc_id)
  130. .should.equal true
  131. it "should call the callback with the doc info", ->
  132. @callback.calledWith(null, @lines, @version, @ranges, @unflushedTime, true).should.equal true
  133. it "should time the execution", ->
  134. @Metrics.Timer::done.called.should.equal true
  135. describe "when the doc does not exist in Redis", ->
  136. beforeEach ->
  137. @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, null, null, null, null)
  138. @PersistenceManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges)
  139. @RedisManager.putDocInMemory = sinon.stub().yields()
  140. @DocumentManager.getDoc @project_id, @doc_id, @callback
  141. it "should try to get the doc from Redis", ->
  142. @RedisManager.getDoc
  143. .calledWith(@project_id, @doc_id)
  144. .should.equal true
  145. it "should get the doc from the PersistenceManager", ->
  146. @PersistenceManager.getDoc
  147. .calledWith(@project_id, @doc_id)
  148. .should.equal true
  149. it "should set the doc in Redis", ->
  150. @RedisManager.putDocInMemory
  151. .calledWith(@project_id, @doc_id, @lines, @version, @ranges)
  152. .should.equal true
  153. it "should call the callback with the doc info", ->
  154. @callback.calledWith(null, @lines, @version, @ranges, null, false).should.equal true
  155. it "should time the execution", ->
  156. @Metrics.Timer::done.called.should.equal true
  157. describe "setDoc", ->
  158. describe "with plain tex lines", ->
  159. beforeEach ->
  160. @beforeLines = ["before", "lines"]
  161. @afterLines = ["after", "lines"]
  162. @ops = [{ i: "foo", p: 4 }, { d: "bar", p: 42 }]
  163. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, @ranges, @unflushedTime, true)
  164. @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
  165. @UpdateManager.applyUpdate = sinon.stub().callsArgWith(3, null)
  166. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
  167. @DocumentManager.flushAndDeleteDoc = sinon.stub().callsArg(2)
  168. describe "when already loaded", ->
  169. beforeEach ->
  170. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, false, @callback
  171. it "should get the current doc lines", ->
  172. @DocumentManager.getDoc
  173. .calledWith(@project_id, @doc_id)
  174. .should.equal true
  175. it "should return a diff of the old and new lines", ->
  176. @DiffCodec.diffAsShareJsOp
  177. .calledWith(@beforeLines, @afterLines)
  178. .should.equal true
  179. it "should apply the diff as a ShareJS op", ->
  180. @UpdateManager.applyUpdate
  181. .calledWith(
  182. @project_id,
  183. @doc_id,
  184. {
  185. doc: @doc_id,
  186. v: @version,
  187. op: @ops,
  188. meta: {
  189. type: "external"
  190. source: @source
  191. user_id: @user_id
  192. }
  193. }
  194. )
  195. .should.equal true
  196. it "should flush the doc to Mongo", ->
  197. @DocumentManager.flushDocIfLoaded
  198. .calledWith(@project_id, @doc_id)
  199. .should.equal true
  200. it "should call the callback", ->
  201. @callback.calledWith(null).should.equal true
  202. it "should time the execution", ->
  203. @Metrics.Timer::done.called.should.equal true
  204. describe "when not already loaded", ->
  205. beforeEach ->
  206. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, null, false)
  207. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, false, @callback
  208. it "should flush and delete the doc from the doc updater", ->
  209. @DocumentManager.flushAndDeleteDoc
  210. .calledWith(@project_id, @doc_id)
  211. .should.equal true
  212. describe "without new lines", ->
  213. beforeEach ->
  214. @DocumentManager.setDoc @project_id, @doc_id, null, @source, @user_id, false, @callback
  215. it "should return the callback with an error", ->
  216. @callback.calledWith(new Error("No lines were passed to setDoc"))
  217. it "should not try to get the doc lines", ->
  218. @DocumentManager.getDoc.called.should.equal false
  219. describe "with the undoing flag", ->
  220. beforeEach ->
  221. # Copy ops so we don't interfere with other tests
  222. @ops = [{ i: "foo", p: 4 }, { d: "bar", p: 42 }]
  223. @DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
  224. @DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, true, @callback
  225. it "should set the undo flag on each op", ->
  226. for op in @ops
  227. op.u.should.equal true
  228. describe "acceptChanges", ->
  229. beforeEach ->
  230. @change_id = "mock-change-id"
  231. @change_ids = [ "mock-change-id-1", "mock-change-id-2", "mock-change-id-3", "mock-change-id-4" ]
  232. @version = 34
  233. @lines = ["original", "lines"]
  234. @ranges = { entries: "mock", comments: "mock" }
  235. @updated_ranges = { entries: "updated", comments: "updated" }
  236. @DocumentManager.getDoc = sinon.stub().yields(null, @lines, @version, @ranges)
  237. @RangesManager.acceptChanges = sinon.stub().yields(null, @updated_ranges)
  238. @RedisManager.updateDocument = sinon.stub().yields()
  239. describe "successfully with a single change", ->
  240. beforeEach ->
  241. @DocumentManager.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  242. it "should get the document's current ranges", ->
  243. @DocumentManager.getDoc
  244. .calledWith(@project_id, @doc_id)
  245. .should.equal true
  246. it "should apply the accept change to the ranges", ->
  247. @RangesManager.acceptChanges
  248. .calledWith([ @change_id ], @ranges)
  249. .should.equal true
  250. it "should save the updated ranges", ->
  251. @RedisManager.updateDocument
  252. .calledWith(@doc_id, @lines, @version, [], @updated_ranges)
  253. .should.equal true
  254. it "should call the callback", ->
  255. @callback.called.should.equal true
  256. describe "successfully with multiple changes", ->
  257. beforeEach ->
  258. @DocumentManager.acceptChanges @project_id, @doc_id, @change_ids, @callback
  259. it "should apply the accept change to the ranges", ->
  260. @RangesManager.acceptChanges
  261. .calledWith(@change_ids, @ranges)
  262. .should.equal true
  263. describe "when the doc is not found", ->
  264. beforeEach ->
  265. @DocumentManager.getDoc = sinon.stub().yields(null, null, null, null)
  266. @DocumentManager.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  267. it "should not save anything", ->
  268. @RedisManager.updateDocument.called.should.equal false
  269. it "should call the callback with a not found error", ->
  270. error = new Errors.NotFoundError("document not found: #{@doc_id}")
  271. @callback.calledWith(error).should.equal true
  272. describe "deleteComment", ->
  273. beforeEach ->
  274. @comment_id = "mock-comment-id"
  275. @version = 34
  276. @lines = ["original", "lines"]
  277. @ranges = { comments: ["one", "two", "three"] }
  278. @updated_ranges = { comments: ["one", "three"] }
  279. @DocumentManager.getDoc = sinon.stub().yields(null, @lines, @version, @ranges)
  280. @RangesManager.deleteComment = sinon.stub().yields(null, @updated_ranges)
  281. @RedisManager.updateDocument = sinon.stub().yields()
  282. describe "successfully", ->
  283. beforeEach ->
  284. @DocumentManager.deleteComment @project_id, @doc_id, @comment_id, @callback
  285. it "should get the document's current ranges", ->
  286. @DocumentManager.getDoc
  287. .calledWith(@project_id, @doc_id)
  288. .should.equal true
  289. it "should delete the comment from the ranges", ->
  290. @RangesManager.deleteComment
  291. .calledWith(@comment_id, @ranges)
  292. .should.equal true
  293. it "should save the updated ranges", ->
  294. @RedisManager.updateDocument
  295. .calledWith(@doc_id, @lines, @version, [], @updated_ranges)
  296. .should.equal true
  297. it "should call the callback", ->
  298. @callback.called.should.equal true
  299. describe "when the doc is not found", ->
  300. beforeEach ->
  301. @DocumentManager.getDoc = sinon.stub().yields(null, null, null, null)
  302. @DocumentManager.acceptChanges @project_id, @doc_id, [ @comment_id ], @callback
  303. it "should not save anything", ->
  304. @RedisManager.updateDocument.called.should.equal false
  305. it "should call the callback with a not found error", ->
  306. error = new Errors.NotFoundError("document not found: #{@doc_id}")
  307. @callback.calledWith(error).should.equal true
  308. describe "getDocAndFlushIfOld", ->
  309. beforeEach ->
  310. tk.freeze(new Date())
  311. @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
  312. afterEach ->
  313. tk.reset()
  314. describe "when the doc is in Redis", ->
  315. describe "and has changes to be flushed", ->
  316. beforeEach ->
  317. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, Date.now() - 1e9, true)
  318. @DocumentManager.getDocAndFlushIfOld @project_id, @doc_id, @callback
  319. it "should get the doc", ->
  320. @DocumentManager.getDoc
  321. .calledWith(@project_id, @doc_id)
  322. .should.equal true
  323. it "should flush the doc", ->
  324. @DocumentManager.flushDocIfLoaded
  325. .calledWith(@project_id, @doc_id)
  326. .should.equal true
  327. it "should call the callback with the lines and versions", ->
  328. @callback.calledWith(null, @lines, @version).should.equal true
  329. describe "and has only changes that don't need to be flushed", ->
  330. beforeEach ->
  331. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, Date.now() - 100, true)
  332. @DocumentManager.getDocAndFlushIfOld @project_id, @doc_id, @callback
  333. it "should get the doc", ->
  334. @DocumentManager.getDoc
  335. .calledWith(@project_id, @doc_id)
  336. .should.equal true
  337. it "should not flush the doc", ->
  338. @DocumentManager.flushDocIfLoaded
  339. .called.should.equal false
  340. it "should call the callback with the lines and versions", ->
  341. @callback.calledWith(null, @lines, @version).should.equal true
  342. describe "when the doc is not in Redis", ->
  343. beforeEach ->
  344. @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, null, false)
  345. @DocumentManager.getDocAndFlushIfOld @project_id, @doc_id, @callback
  346. it "should get the doc", ->
  347. @DocumentManager.getDoc
  348. .calledWith(@project_id, @doc_id)
  349. .should.equal true
  350. it "should not flush the doc", ->
  351. @DocumentManager.flushDocIfLoaded
  352. .called.should.equal false
  353. it "should call the callback with the lines and versions", ->
  354. @callback.calledWith(null, @lines, @version).should.equal true