ProjectHistoryRedisManagerTests.coffee 3.3 KB

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