UrlCacheTests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.fallbackURL = 'http://filestore/bucket/project-blobs/key/ab/cd/ef'
  26. this.project_id = '60b0dd39c418bc00598a0d22'
  27. return (this.UrlCache = SandboxedModule.require(modulePath, {
  28. requires: {
  29. './UrlFetcher': (this.UrlFetcher = {
  30. promises: { pipeUrlToFileWithRetry: sinon.stub().resolves() },
  31. }),
  32. '@overleaf/settings': (this.Settings = {
  33. path: { clsiCacheDir: '/cache/dir' },
  34. }),
  35. '@overleaf/metrics': {
  36. Timer: sinon.stub().returns({ done: sinon.stub() }),
  37. },
  38. fs: (this.fs = {
  39. promises: {
  40. rm: sinon.stub().resolves(),
  41. copyFile: sinon.stub().resolves(),
  42. },
  43. }),
  44. },
  45. }))
  46. })
  47. describe('downloadUrlToFile', function () {
  48. beforeEach(function () {
  49. this.destPath = 'path/to/destination'
  50. })
  51. it('should not download on the happy path', function (done) {
  52. this.UrlCache.downloadUrlToFile(
  53. this.project_id,
  54. this.url,
  55. this.fallbackURL,
  56. this.destPath,
  57. this.lastModified,
  58. error => {
  59. expect(error).to.not.exist
  60. expect(
  61. this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
  62. ).to.equal(false)
  63. done()
  64. }
  65. )
  66. })
  67. it('should not download on the semi-happy path', function (done) {
  68. const codedError = new Error()
  69. codedError.code = 'ENOENT'
  70. this.fs.promises.copyFile.onCall(0).rejects(codedError)
  71. this.fs.promises.copyFile.onCall(1).resolves()
  72. this.UrlCache.downloadUrlToFile(
  73. this.project_id,
  74. this.url,
  75. this.fallbackURL,
  76. this.destPath,
  77. this.lastModified,
  78. error => {
  79. expect(error).to.not.exist
  80. expect(
  81. this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
  82. ).to.equal(false)
  83. done()
  84. }
  85. )
  86. })
  87. it('should download on cache miss', function (done) {
  88. const codedError = new Error()
  89. codedError.code = 'ENOENT'
  90. this.fs.promises.copyFile.onCall(0).rejects(codedError)
  91. this.fs.promises.copyFile.onCall(1).rejects(codedError)
  92. this.fs.promises.copyFile.onCall(2).resolves()
  93. this.UrlCache.downloadUrlToFile(
  94. this.project_id,
  95. this.url,
  96. this.fallbackURL,
  97. this.destPath,
  98. this.lastModified,
  99. error => {
  100. expect(error).to.not.exist
  101. expect(
  102. this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
  103. ).to.equal(true)
  104. done()
  105. }
  106. )
  107. })
  108. it('should raise non cache-miss errors', function (done) {
  109. const codedError = new Error()
  110. codedError.code = 'FOO'
  111. this.fs.promises.copyFile.rejects(codedError)
  112. this.UrlCache.downloadUrlToFile(
  113. this.project_id,
  114. this.url,
  115. this.fallbackURL,
  116. this.destPath,
  117. this.lastModified,
  118. error => {
  119. expect(error).to.equal(codedError)
  120. done()
  121. }
  122. )
  123. })
  124. })
  125. describe('clearProject', function () {
  126. beforeEach(function (done) {
  127. this.UrlCache.clearProject(this.project_id, done)
  128. })
  129. it('should clear the cache in bulk', function () {
  130. expect(
  131. this.fs.promises.rm.calledWith('/cache/dir/' + this.project_id, {
  132. force: true,
  133. recursive: true,
  134. })
  135. ).to.equal(true)
  136. })
  137. })
  138. })