overleaf/libraries/object-persistor/test/unit/PersistorFactoryTests.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

102 lines
2.9 KiB
JavaScript

const chai = require('chai')
const { expect } = chai
const SandboxedModule = require('sandboxed-module')
const StreamPromises = require('node:stream/promises')
const MODULE_PATH = '../../src/PersistorFactory.js'
describe('PersistorManager', function () {
let PersistorFactory, FSPersistor, S3Persistor, Settings, GcsPersistor
beforeEach(function () {
FSPersistor = class {
constructor(settings) {
this.settings = settings
}
wrappedMethod() {
return 'FSPersistor'
}
}
S3Persistor = class {
wrappedMethod() {
return 'S3Persistor'
}
}
GcsPersistor = class {
wrappedMethod() {
return 'GcsPersistor'
}
}
Settings = {}
const requires = {
'./GcsPersistor': GcsPersistor,
'./S3Persistor': { S3Persistor },
'./FSPersistor': FSPersistor,
'@overleaf/logger': {
info() {},
err() {},
},
'stream/promises': StreamPromises,
}
PersistorFactory = SandboxedModule.require(MODULE_PATH, { requires })
})
it('should implement the S3 wrapped method when S3 is configured', function () {
Settings.backend = 's3'
expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
})
it("should implement the S3 wrapped method when 'aws-sdk' is configured", function () {
Settings.backend = 'aws-sdk'
expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
})
it('should implement the FS wrapped method when FS is configured', function () {
Settings.backend = 'fs'
expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
expect(PersistorFactory(Settings).wrappedMethod()).to.equal('FSPersistor')
})
it('should forward useSubdirectories=true to FSPersistor', function () {
Settings.backend = 'fs'
Settings.useSubdirectories = true
expect(PersistorFactory(Settings).settings.useSubdirectories).to.be.true
})
it('should forward useSubdirectories=false to FSPersistor', function () {
Settings.backend = 'fs'
Settings.useSubdirectories = false
expect(PersistorFactory(Settings).settings.useSubdirectories).to.be.false
})
it('should throw an error when the backend is not configured', function () {
try {
PersistorFactory(Settings)
} catch (err) {
expect(err.message).to.equal('no backend specified - config incomplete')
return
}
expect('should have caught an error').not.to.exist
})
it('should throw an error when the backend is unknown', function () {
Settings.backend = 'magic'
try {
PersistorFactory(Settings)
} catch (err) {
expect(err.message).to.equal('unknown backend')
expect(err.info.backend).to.equal('magic')
return
}
expect('should have caught an error').not.to.exist
})
})