FlushingDocsTests.js 4.7 KB

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