ApplyingUpdatesToProjectStructureTests.coffee 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. Settings = require('settings-sharelatex')
  5. rclient_history = require("redis-sharelatex").createClient(Settings.redis.history)
  6. ProjectHistoryKeys = Settings.redis.project_history.key_schema
  7. MockProjectHistoryApi = require "./helpers/MockProjectHistoryApi"
  8. MockWebApi = require "./helpers/MockWebApi"
  9. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  10. describe "Applying updates to a project's structure", ->
  11. before ->
  12. @user_id = 'user-id-123'
  13. describe "renaming a file", ->
  14. before (done) ->
  15. @project_id = DocUpdaterClient.randomId()
  16. @fileUpdate =
  17. id: DocUpdaterClient.randomId()
  18. pathname: '/file-path'
  19. newPathname: '/new-file-path'
  20. @fileUpdates = [ @fileUpdate ]
  21. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, [], @fileUpdates, (error) ->
  22. throw error if error?
  23. setTimeout done, 200
  24. it "should push the applied file renames to the project history api", (done) ->
  25. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  26. throw error if error?
  27. update = JSON.parse(updates[0])
  28. update.file.should.equal @fileUpdate.id
  29. update.pathname.should.equal '/file-path'
  30. update.new_pathname.should.equal '/new-file-path'
  31. update.meta.user_id.should.equal @user_id
  32. update.meta.ts.should.be.a('string')
  33. done()
  34. describe "renaming a document", ->
  35. before ->
  36. @docUpdate =
  37. id: DocUpdaterClient.randomId()
  38. pathname: '/doc-path'
  39. newPathname: '/new-doc-path'
  40. @docUpdates = [ @docUpdate ]
  41. describe "when the document is not loaded", ->
  42. before (done) ->
  43. @project_id = DocUpdaterClient.randomId()
  44. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  45. throw error if error?
  46. setTimeout done, 200
  47. it "should push the applied doc renames to the project history api", (done) ->
  48. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  49. throw error if error?
  50. update = JSON.parse(updates[0])
  51. update.doc.should.equal @docUpdate.id
  52. update.pathname.should.equal '/doc-path'
  53. update.new_pathname.should.equal '/new-doc-path'
  54. update.meta.user_id.should.equal @user_id
  55. update.meta.ts.should.be.a('string')
  56. done()
  57. describe "when the document is loaded", ->
  58. before (done) ->
  59. @project_id = DocUpdaterClient.randomId()
  60. MockWebApi.insertDoc @project_id, @docUpdate.id, {}
  61. DocUpdaterClient.preloadDoc @project_id, @docUpdate.id, (error) =>
  62. throw error if error?
  63. sinon.spy MockWebApi, "getDocument"
  64. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  65. throw error if error?
  66. setTimeout done, 200
  67. after ->
  68. MockWebApi.getDocument.restore()
  69. it "should update the doc", (done) ->
  70. DocUpdaterClient.getDoc @project_id, @docUpdate.id, (error, res, doc) =>
  71. doc.pathname.should.equal @docUpdate.newPathname
  72. done()
  73. it "should push the applied doc renames to the project history api", (done) ->
  74. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  75. throw error if error?
  76. update = JSON.parse(updates[0])
  77. update.doc.should.equal @docUpdate.id
  78. update.pathname.should.equal '/doc-path'
  79. update.new_pathname.should.equal '/new-doc-path'
  80. update.meta.user_id.should.equal @user_id
  81. update.meta.ts.should.be.a('string')
  82. done()
  83. describe "adding a file", ->
  84. before (done) ->
  85. @project_id = DocUpdaterClient.randomId()
  86. @fileUpdate =
  87. id: DocUpdaterClient.randomId()
  88. pathname: '/file-path'
  89. url: 'filestore.example.com'
  90. @fileUpdates = [ @fileUpdate ]
  91. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, [], @fileUpdates, (error) ->
  92. throw error if error?
  93. setTimeout done, 200
  94. it "should push the file addition to the project history api", (done) ->
  95. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  96. throw error if error?
  97. update = JSON.parse(updates[0])
  98. update.file.should.equal @fileUpdate.id
  99. update.pathname.should.equal '/file-path'
  100. update.url.should.equal 'filestore.example.com'
  101. update.meta.user_id.should.equal @user_id
  102. update.meta.ts.should.be.a('string')
  103. done()
  104. describe "adding a doc", ->
  105. before (done) ->
  106. @project_id = DocUpdaterClient.randomId()
  107. @docUpdate =
  108. id: DocUpdaterClient.randomId()
  109. pathname: '/file-path'
  110. docLines: 'a\nb'
  111. @docUpdates = [ @docUpdate ]
  112. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  113. throw error if error?
  114. setTimeout done, 200
  115. it "should push the doc addition to the project history api", (done) ->
  116. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  117. throw error if error?
  118. update = JSON.parse(updates[0])
  119. update.doc.should.equal @docUpdate.id
  120. update.pathname.should.equal '/file-path'
  121. update.docLines.should.equal 'a\nb'
  122. update.meta.user_id.should.equal @user_id
  123. update.meta.ts.should.be.a('string')
  124. done()
  125. describe "with enough updates to flush to the history service", ->
  126. before (done) ->
  127. @project_id = DocUpdaterClient.randomId()
  128. @user_id = DocUpdaterClient.randomId()
  129. updates = []
  130. for v in [0..599] # Should flush after 500 ops
  131. updates.push
  132. id: DocUpdaterClient.randomId(),
  133. pathname: '/file-' + v
  134. docLines: 'a\nb'
  135. sinon.spy MockProjectHistoryApi, "flushProject"
  136. # Send updates in chunks to causes multiple flushes
  137. projectId = @project_id
  138. userId = @project_id
  139. DocUpdaterClient.sendProjectUpdate projectId, userId, updates.slice(0, 250), [], (error) ->
  140. throw error if error?
  141. DocUpdaterClient.sendProjectUpdate projectId, userId, updates.slice(250), [], (error) ->
  142. throw error if error?
  143. setTimeout done, 2000
  144. after ->
  145. MockProjectHistoryApi.flushProject.restore()
  146. it "should flush project history", ->
  147. MockProjectHistoryApi.flushProject.calledWith(@project_id).should.equal true