TrackChangesManagerTests.coffee 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/TrackChangesManager'
  5. describe "TrackChangesManager", ->
  6. beforeEach ->
  7. @TrackChangesManager = SandboxedModule.require modulePath, requires:
  8. "request": @request = {}
  9. "settings-sharelatex": @Settings = {}
  10. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  11. "./WebRedisManager": @WebRedisManager = {}
  12. @project_id = "mock-project-id"
  13. @doc_id = "mock-doc-id"
  14. @callback = sinon.stub()
  15. describe "flushDocChanges", ->
  16. beforeEach ->
  17. @Settings.apis =
  18. trackchanges: url: "http://trackchanges.example.com"
  19. describe "successfully", ->
  20. beforeEach ->
  21. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
  22. @TrackChangesManager.flushDocChanges @project_id, @doc_id, @callback
  23. it "should send a request to the track changes api", ->
  24. @request.post
  25. .calledWith("#{@Settings.apis.trackchanges.url}/project/#{@project_id}/doc/#{@doc_id}/flush")
  26. .should.equal true
  27. it "should return the callback", ->
  28. @callback.calledWith(null).should.equal true
  29. describe "when the track changes api returns an error", ->
  30. beforeEach ->
  31. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
  32. @TrackChangesManager.flushDocChanges @project_id, @doc_id, @callback
  33. it "should return the callback with an error", ->
  34. @callback.calledWith(new Error("track changes api return non-success code: 500")).should.equal true
  35. describe "pushUncompressedHistoryOp", ->
  36. beforeEach ->
  37. @op = "mock-op"
  38. @TrackChangesManager.flushDocChanges = sinon.stub().callsArg(2)
  39. describe "pushing the op", ->
  40. beforeEach ->
  41. @WebRedisManager.pushUncompressedHistoryOp = sinon.stub().callsArgWith(3, null, 1)
  42. @TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
  43. it "should push the op into redis", ->
  44. @WebRedisManager.pushUncompressedHistoryOp
  45. .calledWith(@project_id, @doc_id, @op)
  46. .should.equal true
  47. it "should call the callback", ->
  48. @callback.called.should.equal true
  49. it "should not try to flush the op", ->
  50. @TrackChangesManager.flushDocChanges.called.should.equal false
  51. describe "when there are a multiple of FLUSH_EVERY_N_OPS ops", ->
  52. beforeEach ->
  53. @WebRedisManager.pushUncompressedHistoryOp =
  54. sinon.stub().callsArgWith(3, null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
  55. @TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
  56. it "should tell the track changes api to flush", ->
  57. @TrackChangesManager.flushDocChanges
  58. .calledWith(@project_id, @doc_id)
  59. .should.equal true
  60. describe "when TrackChangesManager errors", ->
  61. beforeEach ->
  62. @WebRedisManager.pushUncompressedHistoryOp =
  63. sinon.stub().callsArgWith(3, null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
  64. @TrackChangesManager.flushDocChanges = sinon.stub().callsArgWith(2, @error = new Error("oops"))
  65. @TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
  66. it "should log out the error", ->
  67. @logger.error
  68. .calledWith(
  69. err: @error
  70. doc_id: @doc_id
  71. project_id: @project_id
  72. "error flushing doc to track changes api"
  73. )
  74. .should.equal true