ProjectHistoryRedisManagerTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "./RedisMigrationManager":
  27. createClient: () => @rclient
  28. "logger-sharelatex":
  29. log:->
  30. globals:
  31. JSON: @JSON = JSON
  32. afterEach ->
  33. tk.reset()
  34. describe "queueOps", ->
  35. beforeEach ->
  36. @ops = ["mock-op-1", "mock-op-2"]
  37. @multi = exec: sinon.stub()
  38. @multi.rpush = sinon.stub()
  39. @multi.setnx = sinon.stub()
  40. @rclient.multi = () => @multi
  41. # @rclient = multi: () => @multi
  42. @ProjectHistoryRedisManager.queueOps @project_id, @ops..., @callback
  43. it "should queue an update", ->
  44. @multi.rpush
  45. .calledWithExactly(
  46. "ProjectHistory:Ops:#{@project_id}"
  47. @ops[0]
  48. @ops[1]
  49. ).should.equal true
  50. it "should set the queue timestamp if not present", ->
  51. @multi.setnx
  52. .calledWithExactly(
  53. "ProjectHistory:FirstOpTimestamp:#{@project_id}"
  54. Date.now()
  55. ).should.equal true
  56. describe "queueRenameEntity", ->
  57. beforeEach () ->
  58. @file_id = 1234
  59. @rawUpdate =
  60. pathname: @pathname = '/old'
  61. newPathname: @newPathname = '/new'
  62. version: @version = 2
  63. @ProjectHistoryRedisManager.queueOps = sinon.stub()
  64. @ProjectHistoryRedisManager.queueRenameEntity @project_id, @projectHistoryId, 'file', @file_id, @user_id, @rawUpdate, @callback
  65. it "should queue an update", ->
  66. update =
  67. pathname: @pathname
  68. new_pathname: @newPathname
  69. meta:
  70. user_id: @user_id
  71. ts: new Date()
  72. version: @version
  73. projectHistoryId: @projectHistoryId
  74. file: @file_id
  75. @ProjectHistoryRedisManager.queueOps
  76. .calledWithExactly(@project_id, @JSON.stringify(update), @callback)
  77. .should.equal true
  78. describe "queueAddEntity", ->
  79. beforeEach () ->
  80. @rclient.rpush = sinon.stub().yields()
  81. @doc_id = 1234
  82. @rawUpdate =
  83. pathname: @pathname = '/old'
  84. docLines: @docLines = 'a\nb'
  85. version: @version = 2
  86. url: @url = 'filestore.example.com'
  87. @ProjectHistoryRedisManager.queueOps = sinon.stub()
  88. @ProjectHistoryRedisManager.queueAddEntity @project_id, @projectHistoryId, 'doc', @doc_id, @user_id, @rawUpdate, @callback
  89. it "should queue an update", ->
  90. update =
  91. pathname: @pathname
  92. docLines: @docLines
  93. url: @url
  94. meta:
  95. user_id: @user_id
  96. ts: new Date()
  97. version: @version
  98. projectHistoryId: @projectHistoryId
  99. doc: @doc_id
  100. @ProjectHistoryRedisManager.queueOps
  101. .calledWithExactly(@project_id, @JSON.stringify(update), @callback)
  102. .should.equal true
  103. describe "queueResyncProjectStructure", ->
  104. it "should queue an update", ->
  105. describe "queueResyncDocContent", ->
  106. it "should queue an update", ->