FlushingDocsTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const sinon = require('sinon')
  17. const chai = require('chai')
  18. chai.should()
  19. const { expect } = chai
  20. const async = require('async')
  21. const MockWebApi = require('./helpers/MockWebApi')
  22. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  23. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  24. describe('Flushing a doc to Mongo', function () {
  25. before(function (done) {
  26. this.lines = ['one', 'two', 'three']
  27. this.version = 42
  28. this.update = {
  29. doc: this.doc_id,
  30. meta: { user_id: 'last-author-fake-id' },
  31. op: [
  32. {
  33. i: 'one and a half\n',
  34. p: 4
  35. }
  36. ],
  37. v: this.version
  38. }
  39. this.result = ['one', 'one and a half', 'two', 'three']
  40. return DocUpdaterApp.ensureRunning(done)
  41. })
  42. describe('when the updated doc exists in the doc updater', function () {
  43. before(function (done) {
  44. ;[this.project_id, this.doc_id] = Array.from([
  45. DocUpdaterClient.randomId(),
  46. DocUpdaterClient.randomId()
  47. ])
  48. sinon.spy(MockWebApi, 'setDocument')
  49. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  50. lines: this.lines,
  51. version: this.version
  52. })
  53. return DocUpdaterClient.sendUpdates(
  54. this.project_id,
  55. this.doc_id,
  56. [this.update],
  57. (error) => {
  58. if (error != null) {
  59. throw error
  60. }
  61. return setTimeout(() => {
  62. return DocUpdaterClient.flushDoc(this.project_id, this.doc_id, done)
  63. }, 200)
  64. }
  65. )
  66. })
  67. after(function () {
  68. return MockWebApi.setDocument.restore()
  69. })
  70. it('should flush the updated doc lines and version to the web api', function () {
  71. return MockWebApi.setDocument
  72. .calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
  73. .should.equal(true)
  74. })
  75. return it('should flush the last update author and time to the web api', function () {
  76. const lastUpdatedAt = MockWebApi.setDocument.lastCall.args[5]
  77. parseInt(lastUpdatedAt).should.be.closeTo(new Date().getTime(), 30000)
  78. const lastUpdatedBy = MockWebApi.setDocument.lastCall.args[6]
  79. return lastUpdatedBy.should.equal('last-author-fake-id')
  80. })
  81. })
  82. describe('when the doc does not exist in the doc updater', function () {
  83. before(function (done) {
  84. ;[this.project_id, this.doc_id] = Array.from([
  85. DocUpdaterClient.randomId(),
  86. DocUpdaterClient.randomId()
  87. ])
  88. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  89. lines: this.lines
  90. })
  91. sinon.spy(MockWebApi, 'setDocument')
  92. return DocUpdaterClient.flushDoc(this.project_id, this.doc_id, done)
  93. })
  94. after(function () {
  95. return MockWebApi.setDocument.restore()
  96. })
  97. return it('should not flush the doc to the web api', function () {
  98. return MockWebApi.setDocument.called.should.equal(false)
  99. })
  100. })
  101. return describe('when the web api http request takes a long time on first request', function () {
  102. before(function (done) {
  103. ;[this.project_id, this.doc_id] = Array.from([
  104. DocUpdaterClient.randomId(),
  105. DocUpdaterClient.randomId()
  106. ])
  107. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  108. lines: this.lines,
  109. version: this.version
  110. })
  111. let t = 30000
  112. sinon
  113. .stub(MockWebApi, 'setDocument')
  114. .callsFake(
  115. (
  116. project_id,
  117. doc_id,
  118. lines,
  119. version,
  120. ranges,
  121. lastUpdatedAt,
  122. lastUpdatedBy,
  123. callback
  124. ) => {
  125. if (callback == null) {
  126. callback = function (error) {}
  127. }
  128. setTimeout(callback, t)
  129. return (t = 0)
  130. }
  131. )
  132. return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, done)
  133. })
  134. after(function () {
  135. return MockWebApi.setDocument.restore()
  136. })
  137. return it('should still work', function (done) {
  138. const start = Date.now()
  139. return DocUpdaterClient.flushDoc(
  140. this.project_id,
  141. this.doc_id,
  142. (error, res, doc) => {
  143. res.statusCode.should.equal(204)
  144. const delta = Date.now() - start
  145. expect(delta).to.be.below(20000)
  146. return done()
  147. }
  148. )
  149. })
  150. })
  151. })