DocumentManagerTests.coffee 18 KB

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