ProjectHistoryRedisManagerTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/ProjectHistoryRedisManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. tk = require "timekeeper"
  7. describe "ProjectHistoryRedisManager", ->
  8. beforeEach ->
  9. @project_id = "project-id-123"
  10. @projectHistoryId = "history-id-123"
  11. @user_id = "user-id-123"
  12. @callback = sinon.stub()
  13. @rclient = {}
  14. tk.freeze(new Date())
  15. @ProjectHistoryRedisManager = SandboxedModule.require modulePath,
  16. requires:
  17. "settings-sharelatex": @settings = {
  18. redis:
  19. project_history:
  20. key_schema:
  21. projectHistoryOps: ({project_id}) -> "ProjectHistory:Ops:#{project_id}"
  22. projectHistoryFirstOpTimestamp: ({project_id}) -> "ProjectHistory:FirstOpTimestamp:#{project_id}"
  23. }
  24. "redis-sharelatex":
  25. createClient: () => @rclient
  26. "logger-sharelatex":
  27. log:->
  28. "./Metrics": @metrics = { summary: sinon.stub()}
  29. globals:
  30. JSON: @JSON = JSON
  31. afterEach ->
  32. tk.reset()
  33. describe "queueOps", ->
  34. beforeEach ->
  35. @ops = ["mock-op-1", "mock-op-2"]
  36. @multi = exec: sinon.stub()
  37. @multi.rpush = sinon.stub()
  38. @multi.setnx = sinon.stub()
  39. @rclient.multi = () => @multi
  40. # @rclient = multi: () => @multi
  41. @ProjectHistoryRedisManager.queueOps @project_id, @ops..., @callback
  42. it "should queue an update", ->
  43. @multi.rpush
  44. .calledWithExactly(
  45. "ProjectHistory:Ops:#{@project_id}"
  46. @ops[0]
  47. @ops[1]
  48. ).should.equal true
  49. it "should set the queue timestamp if not present", ->
  50. @multi.setnx
  51. .calledWithExactly(
  52. "ProjectHistory:FirstOpTimestamp:#{@project_id}"
  53. Date.now()
  54. ).should.equal true
  55. describe "queueRenameEntity", ->
  56. beforeEach () ->
  57. @file_id = 1234
  58. @rawUpdate =
  59. pathname: @pathname = '/old'
  60. newPathname: @newPathname = '/new'
  61. version: @version = 2
  62. @ProjectHistoryRedisManager.queueOps = sinon.stub()
  63. @ProjectHistoryRedisManager.queueRenameEntity @project_id, @projectHistoryId, 'file', @file_id, @user_id, @rawUpdate, @callback
  64. it "should queue an update", ->
  65. update =
  66. pathname: @pathname
  67. new_pathname: @newPathname
  68. meta:
  69. user_id: @user_id
  70. ts: new Date()
  71. version: @version
  72. projectHistoryId: @projectHistoryId
  73. file: @file_id
  74. @ProjectHistoryRedisManager.queueOps
  75. .calledWithExactly(@project_id, @JSON.stringify(update), @callback)
  76. .should.equal true
  77. describe "queueAddEntity", ->
  78. beforeEach () ->
  79. @rclient.rpush = sinon.stub().yields()
  80. @doc_id = 1234
  81. @rawUpdate =
  82. pathname: @pathname = '/old'
  83. docLines: @docLines = 'a\nb'
  84. version: @version = 2
  85. url: @url = 'filestore.example.com'
  86. @ProjectHistoryRedisManager.queueOps = sinon.stub()
  87. @ProjectHistoryRedisManager.queueAddEntity @project_id, @projectHistoryId, 'doc', @doc_id, @user_id, @rawUpdate, @callback
  88. it "should queue an update", ->
  89. update =
  90. pathname: @pathname
  91. docLines: @docLines
  92. url: @url
  93. meta:
  94. user_id: @user_id
  95. ts: new Date()
  96. version: @version
  97. projectHistoryId: @projectHistoryId
  98. doc: @doc_id
  99. @ProjectHistoryRedisManager.queueOps
  100. .calledWithExactly(@project_id, @JSON.stringify(update), @callback)
  101. .should.equal true
  102. describe "queueResyncProjectStructure", ->
  103. it "should queue an update", ->
  104. describe "queueResyncDocContent", ->
  105. it "should queue an update", ->