DocumentManagerTests.coffee 19 KB

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