UrlCacheTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath = require('path').join(__dirname, '../../../app/js/UrlCache')
  17. describe('UrlCache', function () {
  18. beforeEach(function () {
  19. this.callback = sinon.stub()
  20. this.url =
  21. 'http://filestore/project/60b0dd39c418bc00598a0d22/file/60ae721ffb1d920027d3201f'
  22. this.project_id = '60b0dd39c418bc00598a0d22'
  23. return (this.UrlCache = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './UrlFetcher': (this.UrlFetcher = {
  26. promises: { pipeUrlToFileWithRetry: sinon.stub().resolves() },
  27. }),
  28. '@overleaf/settings': (this.Settings = {
  29. path: { clsiCacheDir: '/cache/dir' },
  30. }),
  31. '@overleaf/metrics': {
  32. Timer: sinon.stub().returns({ done: sinon.stub() }),
  33. },
  34. fs: (this.fs = {
  35. promises: {
  36. rm: sinon.stub().resolves(),
  37. copyFile: sinon.stub().resolves(),
  38. },
  39. }),
  40. },
  41. }))
  42. })
  43. describe('downloadUrlToFile', function () {
  44. beforeEach(function () {
  45. this.destPath = 'path/to/destination'
  46. })
  47. it('should not download on the happy path', function (done) {
  48. this.UrlCache.downloadUrlToFile(
  49. this.project_id,
  50. this.url,
  51. this.destPath,
  52. this.lastModified,
  53. error => {
  54. expect(error).to.not.exist
  55. expect(
  56. this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
  57. ).to.equal(false)
  58. done()
  59. }
  60. )
  61. })
  62. it('should download on cache miss', function (done) {
  63. const codedError = new Error()
  64. codedError.code = 'ENOENT'
  65. this.fs.promises.copyFile.onCall(0).rejects(codedError)
  66. this.fs.promises.copyFile.onCall(1).resolves()
  67. this.UrlCache.downloadUrlToFile(
  68. this.project_id,
  69. this.url,
  70. this.destPath,
  71. this.lastModified,
  72. error => {
  73. expect(error).to.not.exist
  74. expect(
  75. this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
  76. ).to.equal(true)
  77. done()
  78. }
  79. )
  80. })
  81. it('should raise non cache-miss errors', function (done) {
  82. const codedError = new Error()
  83. codedError.code = 'FOO'
  84. this.fs.promises.copyFile.rejects(codedError)
  85. this.UrlCache.downloadUrlToFile(
  86. this.project_id,
  87. this.url,
  88. this.destPath,
  89. this.lastModified,
  90. error => {
  91. expect(error).to.equal(codedError)
  92. done()
  93. }
  94. )
  95. })
  96. })
  97. describe('clearProject', function () {
  98. beforeEach(function (done) {
  99. this.UrlCache.clearProject(this.project_id, done)
  100. })
  101. it('should clear the cache in bulk', function () {
  102. expect(
  103. this.fs.promises.rm.calledWith('/cache/dir/' + this.project_id, {
  104. force: true,
  105. recursive: true,
  106. })
  107. ).to.equal(true)
  108. })
  109. })
  110. })