audiobookshelf/test/server/managers/BinaryManager.test.js

264 lines
8.7 KiB
JavaScript
Raw Normal View History

2023-12-05 21:18:37 +01:00
const chai = require('chai')
const sinon = require('sinon')
const fs = require('../../../server/libs/fsExtra')
const fileUtils = require('../../../server/utils/fileUtils')
2023-12-05 21:18:37 +01:00
const which = require('../../../server/libs/which')
const ffbinaries = require('../../../server/libs/ffbinaries')
2023-12-05 21:18:37 +01:00
const path = require('path')
const BinaryManager = require('../../../server/managers/BinaryManager')
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const expect = chai.expect
2023-12-05 20:19:17 +01:00
describe('BinaryManager', () => {
2023-12-05 21:18:37 +01:00
let binaryManager
2023-12-05 20:19:17 +01:00
describe('init', () => {
2023-12-05 21:18:37 +01:00
let findStub
let installStub
let errorStub
let exitStub
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
binaryManager = new BinaryManager()
findStub = sinon.stub(binaryManager, 'findRequiredBinaries')
installStub = sinon.stub(binaryManager, 'install')
errorStub = sinon.stub(console, 'error')
exitStub = sinon.stub(process, 'exit')
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
2023-12-05 21:18:37 +01:00
findStub.restore()
installStub.restore()
errorStub.restore()
exitStub.restore()
})
2023-12-05 20:19:17 +01:00
it('should not install binaries if they are already found', async () => {
2023-12-05 21:18:37 +01:00
findStub.resolves([])
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.init()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(installStub.called).to.be.false
expect(findStub.calledOnce).to.be.true
expect(errorStub.called).to.be.false
expect(exitStub.called).to.be.false
})
2023-12-05 20:19:17 +01:00
it('should install missing binaries', async () => {
2023-12-05 21:18:37 +01:00
const missingBinaries = ['ffmpeg', 'ffprobe']
const missingBinariesAfterInstall = []
findStub.onFirstCall().resolves(missingBinaries)
findStub.onSecondCall().resolves(missingBinariesAfterInstall)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.init()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(findStub.calledTwice).to.be.true
expect(installStub.calledOnce).to.be.true
expect(errorStub.called).to.be.false
expect(exitStub.called).to.be.false
})
2023-12-05 20:19:17 +01:00
it('exit if binaries are not found after installation', async () => {
2023-12-05 21:18:37 +01:00
const missingBinaries = ['ffmpeg', 'ffprobe']
const missingBinariesAfterInstall = ['ffmpeg', 'ffprobe']
findStub.onFirstCall().resolves(missingBinaries)
findStub.onSecondCall().resolves(missingBinariesAfterInstall)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.init()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(findStub.calledTwice).to.be.true
expect(installStub.calledOnce).to.be.true
expect(errorStub.calledOnce).to.be.true
expect(exitStub.calledOnce).to.be.true
expect(exitStub.calledWith(1)).to.be.true
})
})
2023-12-05 20:19:17 +01:00
describe('findRequiredBinaries', () => {
2023-12-05 21:18:37 +01:00
let findBinaryStub
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
const requiredBinaries = [{ name: 'ffmpeg', envVariable: 'FFMPEG_PATH' }]
binaryManager = new BinaryManager(requiredBinaries)
findBinaryStub = sinon.stub(binaryManager, 'findBinary')
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
2023-12-05 21:18:37 +01:00
findBinaryStub.restore()
})
2023-12-05 20:19:17 +01:00
it('should put found paths in the correct environment variables', async () => {
2023-12-05 21:18:37 +01:00
const pathToFFmpeg = '/path/to/ffmpeg'
const missingBinaries = []
delete process.env.FFMPEG_PATH
findBinaryStub.resolves(pathToFFmpeg)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findRequiredBinaries()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.deep.equal(missingBinaries)
expect(findBinaryStub.calledOnce).to.be.true
expect(process.env.FFMPEG_PATH).to.equal(pathToFFmpeg)
})
2023-12-05 20:19:17 +01:00
it('should add missing binaries to result', async () => {
2023-12-05 21:18:37 +01:00
const missingBinaries = ['ffmpeg']
delete process.env.FFMPEG_PATH
findBinaryStub.resolves(null)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findRequiredBinaries()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.deep.equal(missingBinaries)
expect(findBinaryStub.calledOnce).to.be.true
expect(process.env.FFMPEG_PATH).to.be.undefined
})
})
2023-12-05 20:19:17 +01:00
describe('install', () => {
let isWritableStub
2023-12-05 21:18:37 +01:00
let downloadBinariesStub
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
binaryManager = new BinaryManager()
isWritableStub = sinon.stub(fileUtils, 'isWritable')
downloadBinariesStub = sinon.stub(ffbinaries, 'downloadBinaries')
2023-12-05 20:19:17 +01:00
binaryManager.mainInstallPath = '/path/to/main/install'
binaryManager.altInstallPath = '/path/to/alt/install'
2023-12-05 21:18:37 +01:00
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
isWritableStub.restore()
2023-12-05 21:18:37 +01:00
downloadBinariesStub.restore()
})
2023-12-05 20:19:17 +01:00
it('should not install binaries if no binaries are passed', async () => {
2023-12-05 21:18:37 +01:00
const binaries = []
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.install(binaries)
2023-12-05 20:19:17 +01:00
expect(isWritableStub.called).to.be.false
2023-12-05 21:18:37 +01:00
expect(downloadBinariesStub.called).to.be.false
})
2023-12-05 20:19:17 +01:00
it('should install binaries in main install path if has access', async () => {
2023-12-05 21:18:37 +01:00
const binaries = ['ffmpeg']
const destination = binaryManager.mainInstallPath
isWritableStub.withArgs(destination).resolves(true)
2023-12-05 21:18:37 +01:00
downloadBinariesStub.resolves()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.install(binaries)
2023-12-05 20:19:17 +01:00
expect(isWritableStub.calledOnce).to.be.true
2023-12-05 21:18:37 +01:00
expect(downloadBinariesStub.calledOnce).to.be.true
expect(downloadBinariesStub.calledWith(binaries, sinon.match({ destination: destination }))).to.be.true
})
2023-12-05 20:19:17 +01:00
it('should install binaries in alt install path if has no access to main', async () => {
2023-12-05 21:18:37 +01:00
const binaries = ['ffmpeg']
const mainDestination = binaryManager.mainInstallPath
const destination = binaryManager.altInstallPath
isWritableStub.withArgs(mainDestination).resolves(false)
2023-12-05 21:18:37 +01:00
downloadBinariesStub.resolves()
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
await binaryManager.install(binaries)
2023-12-05 20:19:17 +01:00
expect(isWritableStub.calledOnce).to.be.true
2023-12-05 21:18:37 +01:00
expect(downloadBinariesStub.calledOnce).to.be.true
expect(downloadBinariesStub.calledWith(binaries, sinon.match({ destination: destination }))).to.be.true
})
})
})
2023-12-05 20:19:17 +01:00
describe('findBinary', () => {
2023-12-05 21:18:37 +01:00
let binaryManager
let fsPathExistsStub
let whichSyncStub
let mainInstallPath
let altInstallPath
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const name = 'ffmpeg'
const envVariable = 'FFMPEG_PATH'
const defaultPath = '/path/to/ffmpeg'
const executable = name + (process.platform == 'win32' ? '.exe' : '')
const whichPath = '/usr/bin/ffmpeg'
2023-12-05 20:19:17 +01:00
beforeEach(() => {
2023-12-05 21:18:37 +01:00
binaryManager = new BinaryManager()
fsPathExistsStub = sinon.stub(fs, 'pathExists')
whichSyncStub = sinon.stub(which, 'sync')
2023-12-05 20:19:17 +01:00
binaryManager.mainInstallPath = '/path/to/main/install'
2023-12-05 21:18:37 +01:00
mainInstallPath = path.join(binaryManager.mainInstallPath, executable)
2023-12-05 20:19:17 +01:00
binaryManager.altInstallPath = '/path/to/alt/install'
2023-12-05 21:18:37 +01:00
altInstallPath = path.join(binaryManager.altInstallPath, executable)
})
2023-12-05 20:19:17 +01:00
afterEach(() => {
2023-12-05 21:18:37 +01:00
fsPathExistsStub.restore()
whichSyncStub.restore()
})
2023-12-05 20:19:17 +01:00
it('should return defaultPath if it exists', async () => {
2023-12-05 21:18:37 +01:00
process.env[envVariable] = defaultPath
fsPathExistsStub.withArgs(defaultPath).resolves(true)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.equal(defaultPath)
expect(fsPathExistsStub.calledOnceWith(defaultPath)).to.be.true
expect(whichSyncStub.notCalled).to.be.true
})
2023-12-05 20:19:17 +01:00
it('should return whichPath if it exists', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
whichSyncStub.returns(whichPath)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.equal(whichPath)
expect(fsPathExistsStub.notCalled).to.be.true
expect(whichSyncStub.calledOnce).to.be.true
})
2023-12-05 20:19:17 +01:00
it('should return mainInstallPath if it exists', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
whichSyncStub.returns(null)
fsPathExistsStub.withArgs(mainInstallPath).resolves(true)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.equal(mainInstallPath)
expect(whichSyncStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledOnceWith(mainInstallPath)).to.be.true
})
2023-12-05 20:19:17 +01:00
it('should return altInstallPath if it exists', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
whichSyncStub.returns(null)
fsPathExistsStub.withArgs(mainInstallPath).resolves(false)
fsPathExistsStub.withArgs(altInstallPath).resolves(true)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
const result = await binaryManager.findBinary(name, envVariable)
2023-12-05 20:19:17 +01:00
2023-12-05 21:18:37 +01:00
expect(result).to.equal(altInstallPath)
expect(whichSyncStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledTwice).to.be.true
expect(fsPathExistsStub.calledWith(mainInstallPath)).to.be.true
expect(fsPathExistsStub.calledWith(altInstallPath)).to.be.true
})
2023-12-05 20:19:17 +01:00
it('should return null if binary is not found', async () => {
2023-12-05 21:18:37 +01:00
delete process.env[envVariable]
whichSyncStub.returns(null)
fsPathExistsStub.withArgs(mainInstallPath).resolves(false)
fsPathExistsStub.withArgs(altInstallPath).resolves(false)
const result = await binaryManager.findBinary(name, envVariable)
expect(result).to.be.null
expect(whichSyncStub.calledOnce).to.be.true
expect(fsPathExistsStub.calledTwice).to.be.true
expect(fsPathExistsStub.calledWith(mainInstallPath)).to.be.true
expect(fsPathExistsStub.calledWith(altInstallPath)).to.be.true
})
})