DeletingAProjectTests.js 8.6 KB

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