UrlCacheTests.js 3.3 KB

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