DeletingAProjectTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. const sinon = require('sinon')
  2. const { setTimeout } = require('node:timers/promises')
  3. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  4. const MockWebApi = require('./helpers/MockWebApi')
  5. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  6. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  7. describe('Deleting a project', function () {
  8. beforeEach(async function () {
  9. let docId0, docId1
  10. this.project_id = DocUpdaterClient.randomId()
  11. this.docs = [
  12. {
  13. id: (docId0 = DocUpdaterClient.randomId()),
  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 = DocUpdaterClient.randomId()),
  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('without updates', function () {
  52. beforeEach(async function () {
  53. sinon.spy(MockWebApi, 'setDocument')
  54. sinon.spy(MockProjectHistoryApi, 'flushProject')
  55. for (const doc of this.docs) {
  56. await DocUpdaterClient.preloadDoc(this.project_id, doc.id)
  57. }
  58. await setTimeout(200)
  59. const res = await DocUpdaterClient.deleteProject(this.project_id)
  60. this.statusCode = res.status
  61. })
  62. afterEach(function () {
  63. MockWebApi.setDocument.restore()
  64. MockProjectHistoryApi.flushProject.restore()
  65. })
  66. it('should return a 204 status code', function () {
  67. this.statusCode.should.equal(204)
  68. })
  69. it('should not send any document to the web api', function () {
  70. MockWebApi.setDocument.should.not.have.been.called
  71. })
  72. it('should need to reload the docs if read again', async function () {
  73. sinon.spy(MockWebApi, 'getDocument')
  74. for (const doc of this.docs) {
  75. MockWebApi.getDocument
  76. .calledWith(this.project_id, doc.id)
  77. .should.equal(false)
  78. await DocUpdaterClient.getDoc(this.project_id, doc.id)
  79. MockWebApi.getDocument
  80. .calledWith(this.project_id, doc.id)
  81. .should.equal(true)
  82. }
  83. MockWebApi.getDocument.restore()
  84. })
  85. it('should flush each doc in project history', function () {
  86. MockProjectHistoryApi.flushProject
  87. .calledWith(this.project_id)
  88. .should.equal(true)
  89. })
  90. })
  91. describe('with documents which have been updated', function () {
  92. beforeEach(async function () {
  93. sinon.spy(MockWebApi, 'setDocument')
  94. sinon.spy(MockProjectHistoryApi, 'flushProject')
  95. for (const doc of this.docs) {
  96. await DocUpdaterClient.preloadDoc(this.project_id, doc.id)
  97. await DocUpdaterClient.sendUpdate(this.project_id, doc.id, doc.update)
  98. }
  99. await setTimeout(200)
  100. const res = await DocUpdaterClient.deleteProject(this.project_id)
  101. this.statusCode = res.status
  102. })
  103. afterEach(function () {
  104. MockWebApi.setDocument.restore()
  105. MockProjectHistoryApi.flushProject.restore()
  106. })
  107. it('should return a 204 status code', function () {
  108. this.statusCode.should.equal(204)
  109. })
  110. it('should send each document to the web api', function () {
  111. for (const doc of this.docs) {
  112. MockWebApi.setDocument
  113. .calledWith(this.project_id, doc.id, doc.updatedLines)
  114. .should.equal(true)
  115. }
  116. })
  117. it('should need to reload the docs if read again', async function () {
  118. sinon.spy(MockWebApi, 'getDocument')
  119. for (const doc of this.docs) {
  120. MockWebApi.getDocument
  121. .calledWith(this.project_id, doc.id)
  122. .should.equal(false)
  123. await DocUpdaterClient.getDoc(this.project_id, doc.id)
  124. MockWebApi.getDocument
  125. .calledWith(this.project_id, doc.id)
  126. .should.equal(true)
  127. }
  128. MockWebApi.getDocument.restore()
  129. })
  130. it('should flush each doc in project history', function () {
  131. MockProjectHistoryApi.flushProject
  132. .calledWith(this.project_id)
  133. .should.equal(true)
  134. })
  135. })
  136. describe('with the background=true parameter from realtime and no request to flush the queue', function () {
  137. beforeEach(async function () {
  138. sinon.spy(MockWebApi, 'setDocument')
  139. sinon.spy(MockProjectHistoryApi, 'flushProject')
  140. for (const doc of this.docs) {
  141. await DocUpdaterClient.preloadDoc(this.project_id, doc.id)
  142. await DocUpdaterClient.sendUpdate(this.project_id, doc.id, doc.update)
  143. }
  144. await setTimeout(200)
  145. const res = await DocUpdaterClient.deleteProjectOnShutdown(
  146. this.project_id
  147. )
  148. this.statusCode = res.status
  149. })
  150. afterEach(function () {
  151. MockWebApi.setDocument.restore()
  152. MockProjectHistoryApi.flushProject.restore()
  153. })
  154. it('should return a 204 status code', function () {
  155. this.statusCode.should.equal(204)
  156. })
  157. it('should not send any documents to the web api', function () {
  158. MockWebApi.setDocument.called.should.equal(false)
  159. })
  160. it('should not flush to project history', function () {
  161. MockProjectHistoryApi.flushProject.called.should.equal(false)
  162. })
  163. })
  164. describe('with the background=true parameter from realtime and a request to flush the queue', function () {
  165. beforeEach(async function () {
  166. sinon.spy(MockWebApi, 'setDocument')
  167. sinon.spy(MockProjectHistoryApi, 'flushProject')
  168. for (const doc of this.docs) {
  169. await DocUpdaterClient.preloadDoc(this.project_id, doc.id)
  170. await DocUpdaterClient.sendUpdate(this.project_id, doc.id, doc.update)
  171. }
  172. await setTimeout(200)
  173. const res = await DocUpdaterClient.deleteProjectOnShutdown(
  174. this.project_id
  175. )
  176. this.statusCode = res.status
  177. // after deleting the project and putting it in the queue, flush the queue
  178. await setTimeout(2000)
  179. await DocUpdaterClient.flushOldProjects()
  180. })
  181. afterEach(function () {
  182. MockWebApi.setDocument.restore()
  183. MockProjectHistoryApi.flushProject.restore()
  184. })
  185. it('should return a 204 status code', function () {
  186. this.statusCode.should.equal(204)
  187. })
  188. it('should send each document to the web api', function () {
  189. for (const doc of this.docs) {
  190. MockWebApi.setDocument
  191. .calledWith(this.project_id, doc.id, doc.updatedLines)
  192. .should.equal(true)
  193. }
  194. })
  195. it('should flush to project history', function () {
  196. MockProjectHistoryApi.flushProject.called.should.equal(true)
  197. })
  198. })
  199. })