HistoryManagerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/HistoryManager'
  5. describe "HistoryManager", ->
  6. beforeEach ->
  7. @HistoryManager = 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. @HistoryManager.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. @HistoryManager.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 "pushUncompressedHistoryOps", ->
  36. beforeEach ->
  37. @ops = ["mock-ops"]
  38. @HistoryManager.flushDocChanges = sinon.stub().callsArg(2)
  39. describe "pushing the op", ->
  40. beforeEach ->
  41. @WebRedisManager.pushUncompressedHistoryOps = sinon.stub().callsArgWith(3, null, 1)
  42. @HistoryManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
  43. it "should push the ops into redis", ->
  44. @WebRedisManager.pushUncompressedHistoryOps
  45. .calledWith(@project_id, @doc_id, @ops)
  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. @HistoryManager.flushDocChanges.called.should.equal false
  51. describe "when we hit a multiple of FLUSH_EVERY_N_OPS ops", ->
  52. beforeEach ->
  53. @WebRedisManager.pushUncompressedHistoryOps =
  54. sinon.stub().callsArgWith(3, null, 2 * @HistoryManager.FLUSH_EVERY_N_OPS)
  55. @HistoryManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
  56. it "should tell the track changes api to flush", ->
  57. @HistoryManager.flushDocChanges
  58. .calledWith(@project_id, @doc_id)
  59. .should.equal true
  60. describe "when we go over a multiple of FLUSH_EVERY_N_OPS ops", ->
  61. beforeEach ->
  62. @ops = ["op1", "op2", "op3"]
  63. @WebRedisManager.pushUncompressedHistoryOps =
  64. sinon.stub().callsArgWith(3, null, 2 * @HistoryManager.FLUSH_EVERY_N_OPS + 1)
  65. @HistoryManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
  66. it "should tell the track changes api to flush", ->
  67. @HistoryManager.flushDocChanges
  68. .calledWith(@project_id, @doc_id)
  69. .should.equal true
  70. describe "when HistoryManager errors", ->
  71. beforeEach ->
  72. @WebRedisManager.pushUncompressedHistoryOps =
  73. sinon.stub().callsArgWith(3, null, 2 * @HistoryManager.FLUSH_EVERY_N_OPS)
  74. @HistoryManager.flushDocChanges = sinon.stub().callsArgWith(2, @error = new Error("oops"))
  75. @HistoryManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
  76. it "should log out the error", ->
  77. @logger.error
  78. .calledWith(
  79. err: @error
  80. doc_id: @doc_id
  81. project_id: @project_id
  82. "error flushing doc to track changes api"
  83. )
  84. .should.equal true
  85. describe "with no ops", ->
  86. beforeEach ->
  87. @WebRedisManager.pushUncompressedHistoryOps = sinon.stub().callsArgWith(3, null, 1)
  88. @HistoryManager.pushUncompressedHistoryOps @project_id, @doc_id, [], @callback
  89. it "should not call WebRedisManager.pushUncompressedHistoryOps", ->
  90. @WebRedisManager.pushUncompressedHistoryOps.called.should.equal false