FlushingAProjectTests.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const sinon = require('sinon')
  2. const { setTimeout } = require('node:timers/promises')
  3. const MockWebApi = require('./helpers/MockWebApi')
  4. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  5. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  6. describe('Flushing a project', function () {
  7. before(async function () {
  8. this.project_id = DocUpdaterClient.randomId()
  9. const docId0 = DocUpdaterClient.randomId()
  10. const docId1 = DocUpdaterClient.randomId()
  11. this.docs = [
  12. {
  13. id: docId0,
  14. lines: ['one', 'two', 'three'],
  15. update: {
  16. doc: docId0,
  17. op: [
  18. {
  19. i: 'one and a half\n',
  20. p: 4,
  21. },
  22. ],
  23. v: 0,
  24. },
  25. updatedLines: ['one', 'one and a half', 'two', 'three'],
  26. },
  27. {
  28. id: docId1,
  29. lines: ['four', 'five', 'six'],
  30. update: {
  31. doc: docId1,
  32. op: [
  33. {
  34. i: 'four and a half\n',
  35. p: 5,
  36. },
  37. ],
  38. v: 0,
  39. },
  40. updatedLines: ['four', 'four and a half', 'five', 'six'],
  41. },
  42. ]
  43. for (const doc of this.docs) {
  44. MockWebApi.insertDoc(this.project_id, doc.id, {
  45. lines: doc.lines,
  46. version: doc.update.v,
  47. })
  48. }
  49. await DocUpdaterApp.ensureRunning()
  50. })
  51. describe('with documents which have been updated', function () {
  52. before(async function () {
  53. sinon.spy(MockWebApi, 'setDocument')
  54. for (const doc of this.docs) {
  55. await DocUpdaterClient.preloadDoc(this.project_id, doc.id)
  56. await DocUpdaterClient.sendUpdate(this.project_id, doc.id, doc.update)
  57. }
  58. await setTimeout(200)
  59. const res = await DocUpdaterClient.flushProject(this.project_id)
  60. this.statusCode = res.status
  61. })
  62. after(function () {
  63. MockWebApi.setDocument.restore()
  64. })
  65. it('should return a 204 status code', function () {
  66. this.statusCode.should.equal(204)
  67. })
  68. it('should send each document to the web api', function () {
  69. for (const doc of this.docs) {
  70. MockWebApi.setDocument
  71. .calledWith(this.project_id, doc.id, doc.updatedLines)
  72. .should.equal(true)
  73. }
  74. })
  75. it('should update the lines in the doc updater', async function () {
  76. for (const doc of this.docs) {
  77. const returnedDoc = await DocUpdaterClient.getDoc(
  78. this.project_id,
  79. doc.id
  80. )
  81. returnedDoc.lines.should.deep.equal(doc.updatedLines)
  82. }
  83. })
  84. })
  85. })