FlushingDocsTests.js 4.7 KB

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