DocumentManagerTests.js 20 KB

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