DeletingAProjectTests.js 8.4 KB

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