FlushingDocsTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const { setTimeout } = require('node:timers/promises')
  4. const MockWebApi = require('./helpers/MockWebApi')
  5. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  6. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  7. describe('Flushing a doc to Mongo', function () {
  8. before(async function () {
  9. this.lines = ['one', 'two', 'three']
  10. this.version = 42
  11. this.update = {
  12. doc: this.doc_id,
  13. meta: { user_id: 'last-author-fake-id' },
  14. op: [
  15. {
  16. i: 'one and a half\n',
  17. p: 4,
  18. },
  19. ],
  20. v: this.version,
  21. }
  22. this.result = ['one', 'one and a half', 'two', 'three']
  23. await DocUpdaterApp.ensureRunning()
  24. })
  25. describe('when the updated doc exists in the doc updater', function () {
  26. before(async function () {
  27. this.project_id = DocUpdaterClient.randomId()
  28. this.doc_id = DocUpdaterClient.randomId()
  29. sinon.spy(MockWebApi, 'setDocument')
  30. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  31. lines: this.lines,
  32. version: this.version,
  33. })
  34. await DocUpdaterClient.sendUpdates(this.project_id, this.doc_id, [
  35. this.update,
  36. ])
  37. await setTimeout(200)
  38. await DocUpdaterClient.flushDoc(this.project_id, this.doc_id)
  39. })
  40. after(function () {
  41. MockWebApi.setDocument.restore()
  42. })
  43. it('should flush the updated doc lines and version to the web api', function () {
  44. MockWebApi.setDocument
  45. .calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
  46. .should.equal(true)
  47. })
  48. it('should flush the last update author and time to the web api', function () {
  49. const lastUpdatedAt = MockWebApi.setDocument.lastCall.args[5]
  50. parseInt(lastUpdatedAt).should.be.closeTo(new Date().getTime(), 30000)
  51. const lastUpdatedBy = MockWebApi.setDocument.lastCall.args[6]
  52. lastUpdatedBy.should.equal('last-author-fake-id')
  53. })
  54. })
  55. describe('when the doc does not exist in the doc updater', function () {
  56. before(async function () {
  57. this.project_id = DocUpdaterClient.randomId()
  58. this.doc_id = DocUpdaterClient.randomId()
  59. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  60. lines: this.lines,
  61. })
  62. sinon.spy(MockWebApi, 'setDocument')
  63. await DocUpdaterClient.flushDoc(this.project_id, this.doc_id)
  64. })
  65. after(function () {
  66. MockWebApi.setDocument.restore()
  67. })
  68. it('should not flush the doc to the web api', function () {
  69. MockWebApi.setDocument.called.should.equal(false)
  70. })
  71. })
  72. describe('when the web api http request takes a long time on first request', function () {
  73. before(async function () {
  74. this.project_id = DocUpdaterClient.randomId()
  75. this.doc_id = DocUpdaterClient.randomId()
  76. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  77. lines: this.lines,
  78. version: this.version,
  79. })
  80. let t = 30000
  81. sinon
  82. .stub(MockWebApi, 'setDocument')
  83. .callsFake(
  84. (
  85. projectId,
  86. docId,
  87. lines,
  88. version,
  89. ranges,
  90. lastUpdatedAt,
  91. lastUpdatedBy,
  92. callback
  93. ) => {
  94. if (!callback) {
  95. callback = function () {}
  96. }
  97. setTimeout(callback, t)
  98. t = 0
  99. }
  100. )
  101. await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
  102. })
  103. after(function () {
  104. MockWebApi.setDocument.restore()
  105. })
  106. it('should still work', async function () {
  107. const start = Date.now()
  108. const res = await DocUpdaterClient.flushDoc(this.project_id, this.doc_id)
  109. res.status.should.equal(204)
  110. const delta = Date.now() - start
  111. expect(delta).to.be.below(20000)
  112. })
  113. })
  114. })