DeletingAProjectTests.js 8.6 KB

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