DeletingAProjectTests.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. const sinon = require('sinon')
  10. const async = require('async')
  11. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  12. const MockWebApi = require('./helpers/MockWebApi')
  13. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  14. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  15. describe('Deleting a project', function () {
  16. before(function (done) {
  17. let docId0, docId1
  18. this.project_id = DocUpdaterClient.randomId()
  19. this.docs = [
  20. {
  21. id: (docId0 = DocUpdaterClient.randomId()),
  22. lines: ['one', 'two', 'three'],
  23. update: {
  24. doc: docId0,
  25. op: [
  26. {
  27. i: 'one and a half\n',
  28. p: 4,
  29. },
  30. ],
  31. v: 0,
  32. },
  33. updatedLines: ['one', 'one and a half', 'two', 'three'],
  34. },
  35. {
  36. id: (docId1 = DocUpdaterClient.randomId()),
  37. lines: ['four', 'five', 'six'],
  38. update: {
  39. doc: docId1,
  40. op: [
  41. {
  42. i: 'four and a half\n',
  43. p: 5,
  44. },
  45. ],
  46. v: 0,
  47. },
  48. updatedLines: ['four', 'four and a half', 'five', 'six'],
  49. },
  50. ]
  51. for (const doc of Array.from(this.docs)) {
  52. MockWebApi.insertDoc(this.project_id, doc.id, {
  53. lines: doc.lines,
  54. version: doc.update.v,
  55. })
  56. }
  57. DocUpdaterApp.ensureRunning(done)
  58. })
  59. describe('with documents which have been updated', function () {
  60. before(function (done) {
  61. sinon.spy(MockWebApi, 'setDocument')
  62. sinon.spy(MockProjectHistoryApi, 'flushProject')
  63. async.series(
  64. this.docs.map(doc => {
  65. return callback => {
  66. DocUpdaterClient.preloadDoc(this.project_id, doc.id, error => {
  67. if (error != null) {
  68. return callback(error)
  69. }
  70. DocUpdaterClient.sendUpdate(
  71. this.project_id,
  72. doc.id,
  73. doc.update,
  74. error => {
  75. callback(error)
  76. }
  77. )
  78. })
  79. }
  80. }),
  81. error => {
  82. if (error != null) {
  83. throw error
  84. }
  85. setTimeout(() => {
  86. DocUpdaterClient.deleteProject(
  87. this.project_id,
  88. (error, res, body) => {
  89. if (error) return done(error)
  90. this.statusCode = res.statusCode
  91. done()
  92. }
  93. )
  94. }, 200)
  95. }
  96. )
  97. })
  98. after(function () {
  99. MockWebApi.setDocument.restore()
  100. MockProjectHistoryApi.flushProject.restore()
  101. })
  102. it('should return a 204 status code', function () {
  103. this.statusCode.should.equal(204)
  104. })
  105. it('should send each document to the web api', function () {
  106. Array.from(this.docs).map(doc =>
  107. MockWebApi.setDocument
  108. .calledWith(this.project_id, doc.id, doc.updatedLines)
  109. .should.equal(true)
  110. )
  111. })
  112. it('should need to reload the docs if read again', function (done) {
  113. sinon.spy(MockWebApi, 'getDocument')
  114. async.series(
  115. this.docs.map(doc => {
  116. return callback => {
  117. MockWebApi.getDocument
  118. .calledWith(this.project_id, doc.id)
  119. .should.equal(false)
  120. DocUpdaterClient.getDoc(
  121. this.project_id,
  122. doc.id,
  123. (error, res, returnedDoc) => {
  124. if (error) return done(error)
  125. MockWebApi.getDocument
  126. .calledWith(this.project_id, doc.id)
  127. .should.equal(true)
  128. callback()
  129. }
  130. )
  131. }
  132. }),
  133. () => {
  134. MockWebApi.getDocument.restore()
  135. done()
  136. }
  137. )
  138. })
  139. it('should flush each doc in project history', function () {
  140. MockProjectHistoryApi.flushProject
  141. .calledWith(this.project_id)
  142. .should.equal(true)
  143. })
  144. })
  145. describe('with the background=true parameter from realtime and no request to flush the queue', function () {
  146. before(function (done) {
  147. sinon.spy(MockWebApi, 'setDocument')
  148. sinon.spy(MockProjectHistoryApi, 'flushProject')
  149. async.series(
  150. this.docs.map(doc => {
  151. return callback => {
  152. DocUpdaterClient.preloadDoc(this.project_id, doc.id, callback)
  153. }
  154. }),
  155. error => {
  156. if (error != null) {
  157. throw error
  158. }
  159. setTimeout(() => {
  160. DocUpdaterClient.deleteProjectOnShutdown(
  161. this.project_id,
  162. (error, res, body) => {
  163. if (error) return done(error)
  164. this.statusCode = res.statusCode
  165. done()
  166. }
  167. )
  168. }, 200)
  169. }
  170. )
  171. })
  172. after(function () {
  173. MockWebApi.setDocument.restore()
  174. MockProjectHistoryApi.flushProject.restore()
  175. })
  176. it('should return a 204 status code', function () {
  177. this.statusCode.should.equal(204)
  178. })
  179. it('should not send any documents to the web api', function () {
  180. MockWebApi.setDocument.called.should.equal(false)
  181. })
  182. it('should not flush to project history', function () {
  183. MockProjectHistoryApi.flushProject.called.should.equal(false)
  184. })
  185. })
  186. describe('with the background=true parameter from realtime and a request to flush the queue', function () {
  187. before(function (done) {
  188. sinon.spy(MockWebApi, 'setDocument')
  189. sinon.spy(MockProjectHistoryApi, 'flushProject')
  190. async.series(
  191. this.docs.map(doc => {
  192. return callback => {
  193. DocUpdaterClient.preloadDoc(this.project_id, doc.id, callback)
  194. }
  195. }),
  196. error => {
  197. if (error != null) {
  198. throw error
  199. }
  200. setTimeout(() => {
  201. DocUpdaterClient.deleteProjectOnShutdown(
  202. this.project_id,
  203. (error, res, body) => {
  204. if (error) return done(error)
  205. this.statusCode = res.statusCode
  206. // after deleting the project and putting it in the queue, flush the queue
  207. setTimeout(() => DocUpdaterClient.flushOldProjects(done), 2000)
  208. }
  209. )
  210. }, 200)
  211. }
  212. )
  213. })
  214. after(function () {
  215. MockWebApi.setDocument.restore()
  216. MockProjectHistoryApi.flushProject.restore()
  217. })
  218. it('should return a 204 status code', function () {
  219. this.statusCode.should.equal(204)
  220. })
  221. it('should send each document to the web api', function () {
  222. Array.from(this.docs).map(doc =>
  223. MockWebApi.setDocument
  224. .calledWith(this.project_id, doc.id, doc.updatedLines)
  225. .should.equal(true)
  226. )
  227. })
  228. it('should flush to project history', function () {
  229. MockProjectHistoryApi.flushProject.called.should.equal(true)
  230. })
  231. })
  232. })