ProjectZipStreamManagerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. one-var,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS201: Simplify complex destructure assignments
  15. * DS205: Consider reworking code to avoid use of IIFEs
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. const sinon = require('sinon')
  19. const { expect } = require('chai')
  20. const modulePath =
  21. '../../../../app/src/Features/Downloads/ProjectZipStreamManager.js'
  22. const SandboxedModule = require('sandboxed-module')
  23. const { EventEmitter } = require('events')
  24. describe('ProjectZipStreamManager', function () {
  25. beforeEach(function () {
  26. this.project_id = 'project-id-123'
  27. this.callback = sinon.stub()
  28. this.archive = {
  29. on() {},
  30. append: sinon.stub(),
  31. }
  32. return (this.ProjectZipStreamManager = SandboxedModule.require(modulePath, {
  33. requires: {
  34. archiver: (this.archiver = sinon.stub().returns(this.archive)),
  35. '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {}),
  36. '../FileStore/FileStoreHandler': (this.FileStoreHandler = {}),
  37. '../Project/ProjectGetter': (this.ProjectGetter = {}),
  38. },
  39. }))
  40. })
  41. describe('createZipStreamForMultipleProjects', function () {
  42. describe('successfully', function () {
  43. beforeEach(function (done) {
  44. this.project_ids = ['project-1', 'project-2']
  45. this.zip_streams = {
  46. 'project-1': new EventEmitter(),
  47. 'project-2': new EventEmitter(),
  48. }
  49. this.project_names = {
  50. 'project-1': 'Project One Name',
  51. 'project-2': 'Project Two Name',
  52. }
  53. this.ProjectZipStreamManager.createZipStreamForProject = (
  54. project_id,
  55. callback
  56. ) => {
  57. callback(null, this.zip_streams[project_id])
  58. setTimeout(() => {
  59. return this.zip_streams[project_id].emit('end')
  60. })
  61. return 0
  62. }
  63. sinon.spy(this.ProjectZipStreamManager, 'createZipStreamForProject')
  64. this.ProjectGetter.getProject = (project_id, fields, callback) => {
  65. return callback(null, { name: this.project_names[project_id] })
  66. }
  67. sinon.spy(this.ProjectGetter, 'getProject')
  68. this.ProjectZipStreamManager.createZipStreamForMultipleProjects(
  69. this.project_ids,
  70. (...args) => {
  71. return this.callback(...Array.from(args || []))
  72. }
  73. )
  74. return (this.archive.finalize = () => done())
  75. })
  76. it('should create a zip archive', function () {
  77. return this.archiver.calledWith('zip').should.equal(true)
  78. })
  79. it('should return a stream before any processing is done', function () {
  80. this.callback
  81. .calledWith(sinon.match.falsy, this.archive)
  82. .should.equal(true)
  83. return this.callback
  84. .calledBefore(this.ProjectZipStreamManager.createZipStreamForProject)
  85. .should.equal(true)
  86. })
  87. it('should get a zip stream for all of the projects', function () {
  88. return Array.from(this.project_ids).map(project_id =>
  89. this.ProjectZipStreamManager.createZipStreamForProject
  90. .calledWith(project_id)
  91. .should.equal(true)
  92. )
  93. })
  94. it('should get the names of each project', function () {
  95. return Array.from(this.project_ids).map(project_id =>
  96. this.ProjectGetter.getProject
  97. .calledWith(project_id, { name: true })
  98. .should.equal(true)
  99. )
  100. })
  101. it('should add all of the projects to the zip', function () {
  102. return Array.from(this.project_ids).map(project_id =>
  103. this.archive.append
  104. .calledWith(this.zip_streams[project_id], {
  105. name: this.project_names[project_id] + '.zip',
  106. })
  107. .should.equal(true)
  108. )
  109. })
  110. })
  111. })
  112. describe('createZipStreamForProject', function () {
  113. describe('successfully', function () {
  114. beforeEach(function () {
  115. this.ProjectZipStreamManager.addAllDocsToArchive = sinon
  116. .stub()
  117. .callsArg(2)
  118. this.ProjectZipStreamManager.addAllFilesToArchive = sinon
  119. .stub()
  120. .callsArg(2)
  121. this.archive.finalize = sinon.stub()
  122. return this.ProjectZipStreamManager.createZipStreamForProject(
  123. this.project_id,
  124. this.callback
  125. )
  126. })
  127. it('should create a zip archive', function () {
  128. return this.archiver.calledWith('zip').should.equal(true)
  129. })
  130. it('should return a stream before any processing is done', function () {
  131. this.callback
  132. .calledWith(sinon.match.falsy, this.archive)
  133. .should.equal(true)
  134. this.callback
  135. .calledBefore(this.ProjectZipStreamManager.addAllDocsToArchive)
  136. .should.equal(true)
  137. return this.callback
  138. .calledBefore(this.ProjectZipStreamManager.addAllFilesToArchive)
  139. .should.equal(true)
  140. })
  141. it('should add all of the project docs to the zip', function () {
  142. return this.ProjectZipStreamManager.addAllDocsToArchive
  143. .calledWith(this.project_id, this.archive)
  144. .should.equal(true)
  145. })
  146. it('should add all of the project files to the zip', function () {
  147. return this.ProjectZipStreamManager.addAllFilesToArchive
  148. .calledWith(this.project_id, this.archive)
  149. .should.equal(true)
  150. })
  151. it('should finalise the stream', function () {
  152. return this.archive.finalize.called.should.equal(true)
  153. })
  154. })
  155. describe('with an error adding docs', function () {
  156. beforeEach(function () {
  157. this.ProjectZipStreamManager.addAllDocsToArchive = sinon
  158. .stub()
  159. .callsArgWith(2, new Error('something went wrong'))
  160. this.ProjectZipStreamManager.addAllFilesToArchive = sinon
  161. .stub()
  162. .callsArg(2)
  163. this.archive.finalize = sinon.stub()
  164. return this.ProjectZipStreamManager.createZipStreamForProject(
  165. this.project_id,
  166. this.callback
  167. )
  168. })
  169. it('should log out an error', function () {
  170. return this.logger.error
  171. .calledWith(sinon.match.any, 'error adding docs to zip stream')
  172. .should.equal(true)
  173. })
  174. it('should continue with the process', function () {
  175. this.ProjectZipStreamManager.addAllDocsToArchive.called.should.equal(
  176. true
  177. )
  178. this.ProjectZipStreamManager.addAllFilesToArchive.called.should.equal(
  179. true
  180. )
  181. return this.archive.finalize.called.should.equal(true)
  182. })
  183. })
  184. describe('with an error adding files', function () {
  185. beforeEach(function () {
  186. this.ProjectZipStreamManager.addAllDocsToArchive = sinon
  187. .stub()
  188. .callsArg(2)
  189. this.ProjectZipStreamManager.addAllFilesToArchive = sinon
  190. .stub()
  191. .callsArgWith(2, new Error('something went wrong'))
  192. this.archive.finalize = sinon.stub()
  193. return this.ProjectZipStreamManager.createZipStreamForProject(
  194. this.project_id,
  195. this.callback
  196. )
  197. })
  198. it('should log out an error', function () {
  199. return this.logger.error
  200. .calledWith(sinon.match.any, 'error adding files to zip stream')
  201. .should.equal(true)
  202. })
  203. it('should continue with the process', function () {
  204. this.ProjectZipStreamManager.addAllDocsToArchive.called.should.equal(
  205. true
  206. )
  207. this.ProjectZipStreamManager.addAllFilesToArchive.called.should.equal(
  208. true
  209. )
  210. return this.archive.finalize.called.should.equal(true)
  211. })
  212. })
  213. })
  214. describe('addAllDocsToArchive', function () {
  215. beforeEach(function (done) {
  216. this.docs = {
  217. '/main.tex': {
  218. lines: [
  219. '\\documentclass{article}',
  220. '\\begin{document}',
  221. 'Hello world',
  222. '\\end{document}',
  223. ],
  224. },
  225. '/chapters/chapter1.tex': {
  226. lines: ['chapter1', 'content'],
  227. },
  228. }
  229. this.ProjectEntityHandler.getAllDocs = sinon
  230. .stub()
  231. .callsArgWith(1, null, this.docs)
  232. return this.ProjectZipStreamManager.addAllDocsToArchive(
  233. this.project_id,
  234. this.archive,
  235. error => {
  236. this.callback(error)
  237. return done()
  238. }
  239. )
  240. })
  241. it('should get the docs for the project', function () {
  242. return this.ProjectEntityHandler.getAllDocs
  243. .calledWith(this.project_id)
  244. .should.equal(true)
  245. })
  246. it('should add each doc to the archive', function () {
  247. return (() => {
  248. const result = []
  249. for (let path in this.docs) {
  250. const doc = this.docs[path]
  251. path = path.slice(1) // remove "/"
  252. result.push(
  253. this.archive.append
  254. .calledWith(doc.lines.join('\n'), { name: path })
  255. .should.equal(true)
  256. )
  257. }
  258. return result
  259. })()
  260. })
  261. })
  262. describe('addAllFilesToArchive', function () {
  263. beforeEach(function () {
  264. this.files = {
  265. '/image.png': {
  266. _id: 'file-id-1',
  267. },
  268. '/folder/picture.png': {
  269. _id: 'file-id-2',
  270. },
  271. }
  272. this.streams = {
  273. 'file-id-1': new EventEmitter(),
  274. 'file-id-2': new EventEmitter(),
  275. }
  276. this.ProjectEntityHandler.getAllFiles = sinon
  277. .stub()
  278. .callsArgWith(1, null, this.files)
  279. this.FileStoreHandler.getFileStream = (project_id, file_id, ...rest) => {
  280. const obj = rest[0],
  281. callback = rest[1]
  282. return callback(null, this.streams[file_id])
  283. }
  284. sinon.spy(this.FileStoreHandler, 'getFileStream')
  285. this.ProjectZipStreamManager.addAllFilesToArchive(
  286. this.project_id,
  287. this.archive,
  288. this.callback
  289. )
  290. return (() => {
  291. const result = []
  292. for (const path in this.streams) {
  293. const stream = this.streams[path]
  294. result.push(stream.emit('end'))
  295. }
  296. return result
  297. })()
  298. })
  299. it('should get the files for the project', function () {
  300. return this.ProjectEntityHandler.getAllFiles
  301. .calledWith(this.project_id)
  302. .should.equal(true)
  303. })
  304. it('should get a stream for each file', function () {
  305. return (() => {
  306. const result = []
  307. for (const path in this.files) {
  308. const file = this.files[path]
  309. result.push(
  310. this.FileStoreHandler.getFileStream
  311. .calledWith(this.project_id, file._id)
  312. .should.equal(true)
  313. )
  314. }
  315. return result
  316. })()
  317. })
  318. it('should add each file to the archive', function () {
  319. return (() => {
  320. const result = []
  321. for (let path in this.files) {
  322. const file = this.files[path]
  323. path = path.slice(1) // remove "/"
  324. result.push(
  325. this.archive.append
  326. .calledWith(this.streams[file._id], { name: path })
  327. .should.equal(true)
  328. )
  329. }
  330. return result
  331. })()
  332. })
  333. })
  334. })