2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2021-10-06 04:11:59 -04:00
|
|
|
const { expect } = require('chai')
|
2024-11-08 05:21:56 -05:00
|
|
|
const modulePath = require('node:path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/UrlCache'
|
|
|
|
)
|
2020-02-19 06:15:37 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('UrlCache', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback = sinon.stub()
|
2021-10-06 04:11:59 -04:00
|
|
|
this.url =
|
|
|
|
'http://filestore/project/60b0dd39c418bc00598a0d22/file/60ae721ffb1d920027d3201f'
|
|
|
|
this.project_id = '60b0dd39c418bc00598a0d22'
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.UrlCache = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-10-06 04:11:59 -04:00
|
|
|
'./UrlFetcher': (this.UrlFetcher = {
|
|
|
|
promises: { pipeUrlToFileWithRetry: sinon.stub().resolves() },
|
|
|
|
}),
|
2021-07-12 12:47:21 -04:00
|
|
|
'@overleaf/settings': (this.Settings = {
|
2021-07-13 07:04:48 -04:00
|
|
|
path: { clsiCacheDir: '/cache/dir' },
|
2020-02-19 06:15:37 -05:00
|
|
|
}),
|
2024-01-17 04:22:46 -05:00
|
|
|
'@overleaf/metrics': {
|
|
|
|
Timer: sinon.stub().returns({ done: sinon.stub() }),
|
|
|
|
},
|
2021-10-06 04:11:59 -04:00
|
|
|
fs: (this.fs = {
|
|
|
|
promises: {
|
2024-01-08 05:43:41 -05:00
|
|
|
rm: sinon.stub().resolves(),
|
2021-10-06 04:11:59 -04:00
|
|
|
copyFile: sinon.stub().resolves(),
|
|
|
|
},
|
|
|
|
}),
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('downloadUrlToFile', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.destPath = 'path/to/destination'
|
2021-10-06 04:11:59 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not download on the happy path', function (done) {
|
|
|
|
this.UrlCache.downloadUrlToFile(
|
2020-02-19 06:15:37 -05:00
|
|
|
this.project_id,
|
|
|
|
this.url,
|
|
|
|
this.destPath,
|
|
|
|
this.lastModified,
|
2021-10-06 04:11:59 -04:00
|
|
|
error => {
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(
|
|
|
|
this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
|
|
|
|
).to.equal(false)
|
|
|
|
done()
|
|
|
|
}
|
2020-02-19 06:15:37 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-10-06 04:11:59 -04:00
|
|
|
it('should download on cache miss', function (done) {
|
|
|
|
const codedError = new Error()
|
|
|
|
codedError.code = 'ENOENT'
|
|
|
|
this.fs.promises.copyFile.onCall(0).rejects(codedError)
|
|
|
|
this.fs.promises.copyFile.onCall(1).resolves()
|
2020-02-19 06:15:37 -05:00
|
|
|
|
2021-10-06 04:11:59 -04:00
|
|
|
this.UrlCache.downloadUrlToFile(
|
2020-02-19 06:15:37 -05:00
|
|
|
this.project_id,
|
|
|
|
this.url,
|
2021-10-06 04:11:59 -04:00
|
|
|
this.destPath,
|
|
|
|
this.lastModified,
|
|
|
|
error => {
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(
|
|
|
|
this.UrlFetcher.promises.pipeUrlToFileWithRetry.called
|
|
|
|
).to.equal(true)
|
|
|
|
done()
|
|
|
|
}
|
2020-02-19 06:15:37 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-10-06 04:11:59 -04:00
|
|
|
it('should raise non cache-miss errors', function (done) {
|
|
|
|
const codedError = new Error()
|
|
|
|
codedError.code = 'FOO'
|
|
|
|
this.fs.promises.copyFile.rejects(codedError)
|
|
|
|
this.UrlCache.downloadUrlToFile(
|
2020-02-19 06:15:37 -05:00
|
|
|
this.project_id,
|
|
|
|
this.url,
|
2021-10-06 04:11:59 -04:00
|
|
|
this.destPath,
|
|
|
|
this.lastModified,
|
|
|
|
error => {
|
|
|
|
expect(error).to.equal(codedError)
|
|
|
|
done()
|
|
|
|
}
|
2020-02-19 06:15:37 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-06 04:11:59 -04:00
|
|
|
describe('clearProject', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.UrlCache.clearProject(this.project_id, done)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2021-10-06 04:11:59 -04:00
|
|
|
it('should clear the cache in bulk', function () {
|
|
|
|
expect(
|
2024-01-08 05:43:41 -05:00
|
|
|
this.fs.promises.rm.calledWith('/cache/dir/' + this.project_id, {
|
|
|
|
force: true,
|
|
|
|
recursive: true,
|
|
|
|
})
|
2021-10-06 04:11:59 -04:00
|
|
|
).to.equal(true)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|