TrackChangesManagerTests.coffee 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = { redis: web: {} }
  10. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  11. "redis-sharelatex": createClient: () => @rclient = {}
  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 = { op: [{ i: "foo", p: 4 }] }
  38. @rclient.rpush = sinon.stub().yields(null, @length = 42)
  39. @rclient.sadd = sinon.stub().yields()
  40. @TrackChangesManager.flushDocChanges = sinon.stub().callsArg(2)
  41. describe "pushing the op", ->
  42. beforeEach ->
  43. @TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
  44. it "should push the op into redis", ->
  45. @rclient.rpush
  46. .calledWith("UncompressedHistoryOps:#{@doc_id}", JSON.stringify @op)
  47. .should.equal true
  48. @rclient.sadd
  49. .calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
  50. .should.equal true
  51. it "should call the callback", ->
  52. @callback.called.should.equal true
  53. it "should not try to flush the op", ->
  54. @TrackChangesManager.flushDocChanges.called.should.equal false
  55. describe "when there are a multiple of FLUSH_EVERY_N_OPS ops", ->
  56. beforeEach ->
  57. @rclient.rpush = sinon.stub().yields(null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
  58. @TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
  59. it "should tell the track changes api to flush", ->
  60. @TrackChangesManager.flushDocChanges
  61. .calledWith(@project_id, @doc_id)
  62. .should.equal true
  63. describe "when TrackChangesManager errors", ->
  64. beforeEach ->
  65. @rclient.rpush = sinon.stub().yields(null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
  66. @TrackChangesManager.flushDocChanges = sinon.stub().callsArgWith(2, @error = new Error("oops"))
  67. @TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
  68. it "should log out the error", ->
  69. @logger.error
  70. .calledWith(
  71. err: @error
  72. doc_id: @doc_id
  73. project_id: @project_id
  74. "error flushing doc to track changes api"
  75. )
  76. .should.equal true