ShareJsDBTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { expect } = require('chai')
  15. const modulePath = '../../../../app/js/ShareJsDB.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. const Errors = require('../../../../app/js/Errors')
  18. describe('ShareJsDB', function () {
  19. beforeEach(function () {
  20. this.doc_id = 'document-id'
  21. this.project_id = 'project-id'
  22. this.doc_key = `${this.project_id}:${this.doc_id}`
  23. this.callback = sinon.stub()
  24. this.ShareJsDB = SandboxedModule.require(modulePath, {
  25. requires: {
  26. './RedisManager': (this.RedisManager = {}),
  27. './Errors': Errors,
  28. '@overleaf/metrics': {
  29. inc: sinon.stub(),
  30. histogram: sinon.stub(),
  31. Timer: class Timer {
  32. done() {}
  33. },
  34. },
  35. },
  36. })
  37. this.version = 42
  38. this.lines = ['one', 'two', 'three']
  39. return (this.db = new this.ShareJsDB(
  40. this.project_id,
  41. this.doc_id,
  42. this.lines,
  43. this.version
  44. ))
  45. })
  46. describe('getSnapshot', function () {
  47. describe('successfully', function () {
  48. beforeEach(function () {
  49. return this.db.getSnapshot(this.doc_key, this.callback)
  50. })
  51. it('should return the doc lines', function () {
  52. return this.callback.args[0][1].snapshot.should.equal(
  53. this.lines.join('\n')
  54. )
  55. })
  56. it('should return the doc version', function () {
  57. return this.callback.args[0][1].v.should.equal(this.version)
  58. })
  59. return it('should return the type as text', function () {
  60. return this.callback.args[0][1].type.should.equal('text')
  61. })
  62. })
  63. return describe('when the key does not match', function () {
  64. beforeEach(function () {
  65. return this.db.getSnapshot('bad:key', this.callback)
  66. })
  67. return it('should return the callback with a NotFoundError', function () {
  68. return this.callback
  69. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  70. .should.equal(true)
  71. })
  72. })
  73. })
  74. describe('getOps', function () {
  75. describe('with start == end', function () {
  76. beforeEach(function () {
  77. this.start = this.end = 42
  78. return this.db.getOps(this.doc_key, this.start, this.end, this.callback)
  79. })
  80. return it('should return an empty array', function () {
  81. return this.callback.calledWith(null, []).should.equal(true)
  82. })
  83. })
  84. describe('with a non empty range', function () {
  85. beforeEach(function () {
  86. this.start = 35
  87. this.end = 42
  88. this.ops = new Array(this.end - this.start)
  89. this.RedisManager.getPreviousDocOps = sinon
  90. .stub()
  91. .callsArgWith(3, null, this.ops)
  92. return this.db.getOps(this.doc_key, this.start, this.end, this.callback)
  93. })
  94. it('should get the range from redis', function () {
  95. return this.RedisManager.getPreviousDocOps
  96. .calledWith(this.doc_id, this.start, this.end - 1)
  97. .should.equal(true)
  98. })
  99. return it('should return the ops', function () {
  100. return this.callback.calledWith(null, this.ops).should.equal(true)
  101. })
  102. })
  103. return describe('with no specified end', function () {
  104. beforeEach(function () {
  105. this.start = 35
  106. this.end = null
  107. this.ops = []
  108. this.RedisManager.getPreviousDocOps = sinon
  109. .stub()
  110. .callsArgWith(3, null, this.ops)
  111. return this.db.getOps(this.doc_key, this.start, this.end, this.callback)
  112. })
  113. return it('should get until the end of the list', function () {
  114. return this.RedisManager.getPreviousDocOps
  115. .calledWith(this.doc_id, this.start, -1)
  116. .should.equal(true)
  117. })
  118. })
  119. })
  120. return describe('writeOps', function () {
  121. return describe('writing an op', function () {
  122. beforeEach(function () {
  123. this.opData = {
  124. op: { p: 20, t: 'foo' },
  125. meta: { source: 'bar' },
  126. v: this.version,
  127. }
  128. return this.db.writeOp(this.doc_key, this.opData, this.callback)
  129. })
  130. it('should write into appliedOps', function () {
  131. return expect(this.db.appliedOps[this.doc_key]).to.deep.equal([
  132. this.opData,
  133. ])
  134. })
  135. return it('should call the callback without an error', function () {
  136. this.callback.called.should.equal(true)
  137. return (this.callback.args[0][0] != null).should.equal(false)
  138. })
  139. })
  140. })
  141. })