DeletingAProjectTests.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. beforeEach(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('without updates', function () {
  60. beforeEach(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. callback(error)
  68. })
  69. }
  70. }),
  71. error => {
  72. if (error != null) {
  73. throw error
  74. }
  75. setTimeout(() => {
  76. DocUpdaterClient.deleteProject(
  77. this.project_id,
  78. (error, res, body) => {
  79. if (error) return done(error)
  80. this.statusCode = res.statusCode
  81. done()
  82. }
  83. )
  84. }, 200)
  85. }
  86. )
  87. })
  88. afterEach(function () {
  89. MockWebApi.setDocument.restore()
  90. MockProjectHistoryApi.flushProject.restore()
  91. })
  92. it('should return a 204 status code', function () {
  93. this.statusCode.should.equal(204)
  94. })
  95. it('should not send any document to the web api', function () {
  96. MockWebApi.setDocument.should.not.have.been.called
  97. })
  98. it('should need to reload the docs if read again', function (done) {
  99. sinon.spy(MockWebApi, 'getDocument')
  100. async.series(
  101. this.docs.map(doc => {
  102. return callback => {
  103. MockWebApi.getDocument
  104. .calledWith(this.project_id, doc.id)
  105. .should.equal(false)
  106. DocUpdaterClient.getDoc(
  107. this.project_id,
  108. doc.id,
  109. (error, res, returnedDoc) => {
  110. if (error) return done(error)
  111. MockWebApi.getDocument
  112. .calledWith(this.project_id, doc.id)
  113. .should.equal(true)
  114. callback()
  115. }
  116. )
  117. }
  118. }),
  119. () => {
  120. MockWebApi.getDocument.restore()
  121. done()
  122. }
  123. )
  124. })
  125. it('should flush each doc in project history', function () {
  126. MockProjectHistoryApi.flushProject
  127. .calledWith(this.project_id)
  128. .should.equal(true)
  129. })
  130. })
  131. describe('with documents which have been updated', function () {
  132. beforeEach(function (done) {
  133. sinon.spy(MockWebApi, 'setDocument')
  134. sinon.spy(MockProjectHistoryApi, 'flushProject')
  135. async.series(
  136. this.docs.map(doc => {
  137. return callback => {
  138. DocUpdaterClient.preloadDoc(this.project_id, doc.id, error => {
  139. if (error != null) {
  140. return callback(error)
  141. }
  142. DocUpdaterClient.sendUpdate(
  143. this.project_id,
  144. doc.id,
  145. doc.update,
  146. error => {
  147. callback(error)
  148. }
  149. )
  150. })
  151. }
  152. }),
  153. error => {
  154. if (error != null) {
  155. throw error
  156. }
  157. setTimeout(() => {
  158. DocUpdaterClient.deleteProject(
  159. this.project_id,
  160. (error, res, body) => {
  161. if (error) return done(error)
  162. this.statusCode = res.statusCode
  163. done()
  164. }
  165. )
  166. }, 200)
  167. }
  168. )
  169. })
  170. afterEach(function () {
  171. MockWebApi.setDocument.restore()
  172. MockProjectHistoryApi.flushProject.restore()
  173. })
  174. it('should return a 204 status code', function () {
  175. this.statusCode.should.equal(204)
  176. })
  177. it('should send each document to the web api', function () {
  178. Array.from(this.docs).map(doc =>
  179. MockWebApi.setDocument
  180. .calledWith(this.project_id, doc.id, doc.updatedLines)
  181. .should.equal(true)
  182. )
  183. })
  184. it('should need to reload the docs if read again', function (done) {
  185. sinon.spy(MockWebApi, 'getDocument')
  186. async.series(
  187. this.docs.map(doc => {
  188. return callback => {
  189. MockWebApi.getDocument
  190. .calledWith(this.project_id, doc.id)
  191. .should.equal(false)
  192. DocUpdaterClient.getDoc(
  193. this.project_id,
  194. doc.id,
  195. (error, res, returnedDoc) => {
  196. if (error) return done(error)
  197. MockWebApi.getDocument
  198. .calledWith(this.project_id, doc.id)
  199. .should.equal(true)
  200. callback()
  201. }
  202. )
  203. }
  204. }),
  205. () => {
  206. MockWebApi.getDocument.restore()
  207. done()
  208. }
  209. )
  210. })
  211. it('should flush each doc in project history', function () {
  212. MockProjectHistoryApi.flushProject
  213. .calledWith(this.project_id)
  214. .should.equal(true)
  215. })
  216. })
  217. describe('with the background=true parameter from realtime and no request to flush the queue', function () {
  218. beforeEach(function (done) {
  219. sinon.spy(MockWebApi, 'setDocument')
  220. sinon.spy(MockProjectHistoryApi, 'flushProject')
  221. async.series(
  222. this.docs.map(doc => {
  223. return callback => {
  224. DocUpdaterClient.preloadDoc(this.project_id, doc.id, error => {
  225. if (error != null) {
  226. return callback(error)
  227. }
  228. DocUpdaterClient.sendUpdate(
  229. this.project_id,
  230. doc.id,
  231. doc.update,
  232. error => {
  233. callback(error)
  234. }
  235. )
  236. })
  237. }
  238. }),
  239. error => {
  240. if (error != null) {
  241. throw error
  242. }
  243. setTimeout(() => {
  244. DocUpdaterClient.deleteProjectOnShutdown(
  245. this.project_id,
  246. (error, res, body) => {
  247. if (error) return done(error)
  248. this.statusCode = res.statusCode
  249. done()
  250. }
  251. )
  252. }, 200)
  253. }
  254. )
  255. })
  256. afterEach(function () {
  257. MockWebApi.setDocument.restore()
  258. MockProjectHistoryApi.flushProject.restore()
  259. })
  260. it('should return a 204 status code', function () {
  261. this.statusCode.should.equal(204)
  262. })
  263. it('should not send any documents to the web api', function () {
  264. MockWebApi.setDocument.called.should.equal(false)
  265. })
  266. it('should not flush to project history', function () {
  267. MockProjectHistoryApi.flushProject.called.should.equal(false)
  268. })
  269. })
  270. describe('with the background=true parameter from realtime and a request to flush the queue', function () {
  271. beforeEach(function (done) {
  272. sinon.spy(MockWebApi, 'setDocument')
  273. sinon.spy(MockProjectHistoryApi, 'flushProject')
  274. async.series(
  275. this.docs.map(doc => {
  276. return callback => {
  277. DocUpdaterClient.preloadDoc(this.project_id, doc.id, error => {
  278. if (error != null) {
  279. return callback(error)
  280. }
  281. DocUpdaterClient.sendUpdate(
  282. this.project_id,
  283. doc.id,
  284. doc.update,
  285. error => {
  286. callback(error)
  287. }
  288. )
  289. })
  290. }
  291. }),
  292. error => {
  293. if (error != null) {
  294. throw error
  295. }
  296. setTimeout(() => {
  297. DocUpdaterClient.deleteProjectOnShutdown(
  298. this.project_id,
  299. (error, res, body) => {
  300. if (error) return done(error)
  301. this.statusCode = res.statusCode
  302. // after deleting the project and putting it in the queue, flush the queue
  303. setTimeout(() => DocUpdaterClient.flushOldProjects(done), 2000)
  304. }
  305. )
  306. }, 200)
  307. }
  308. )
  309. })
  310. afterEach(function () {
  311. MockWebApi.setDocument.restore()
  312. MockProjectHistoryApi.flushProject.restore()
  313. })
  314. it('should return a 204 status code', function () {
  315. this.statusCode.should.equal(204)
  316. })
  317. it('should send each document to the web api', function () {
  318. Array.from(this.docs).map(doc =>
  319. MockWebApi.setDocument
  320. .calledWith(this.project_id, doc.id, doc.updatedLines)
  321. .should.equal(true)
  322. )
  323. })
  324. it('should flush to project history', function () {
  325. MockProjectHistoryApi.flushProject.called.should.equal(true)
  326. })
  327. })
  328. })