mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-13 19:04:57 +02:00
Fixes + add progress to m4b and embed tools
This commit is contained in:
parent
b6875a44cf
commit
10f5f331d7
11 changed files with 530 additions and 211 deletions
95
test/server/objects/TrackProgressMonitor.test.js
Normal file
95
test/server/objects/TrackProgressMonitor.test.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
const chai = require('chai')
|
||||
const sinon = require('sinon')
|
||||
const TrackProgressMonitor = require('../../../server/objects/TrackProgressMonitor')
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
describe('TrackProgressMonitor', () => {
|
||||
let trackDurations
|
||||
let trackStartedCallback
|
||||
let progressCallback
|
||||
let trackFinishedCallback
|
||||
let monitor
|
||||
|
||||
beforeEach(() => {
|
||||
trackDurations = [10, 40, 50]
|
||||
trackStartedCallback = sinon.spy()
|
||||
progressCallback = sinon.spy()
|
||||
trackFinishedCallback = sinon.spy()
|
||||
})
|
||||
|
||||
it('should initialize correctly', () => {
|
||||
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
|
||||
|
||||
expect(monitor.trackDurations).to.deep.equal(trackDurations)
|
||||
expect(monitor.totalDuration).to.equal(100)
|
||||
expect(monitor.trackStartedCallback).to.equal(trackStartedCallback)
|
||||
expect(monitor.progressCallback).to.equal(progressCallback)
|
||||
expect(monitor.trackFinishedCallback).to.equal(trackFinishedCallback)
|
||||
expect(monitor.currentTrackIndex).to.equal(0)
|
||||
expect(monitor.cummulativeProgress).to.equal(0)
|
||||
expect(monitor.currentTrackPercentage).to.equal(10)
|
||||
expect(monitor.numTracks).to.equal(trackDurations.length)
|
||||
expect(monitor.allTracksFinished).to.be.false
|
||||
})
|
||||
|
||||
it('should update the progress', () => {
|
||||
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
|
||||
monitor.update(5)
|
||||
|
||||
expect(monitor.currentTrackIndex).to.equal(0)
|
||||
expect(monitor.cummulativeProgress).to.equal(0)
|
||||
expect(monitor.currentTrackPercentage).to.equal(10)
|
||||
expect(trackStartedCallback.calledOnceWithExactly(0)).to.be.true
|
||||
expect(progressCallback.calledOnceWithExactly(0, 50, 5)).to.be.true
|
||||
expect(trackFinishedCallback.notCalled).to.be.true
|
||||
})
|
||||
|
||||
it('should update the progress multiple times on the same track', () => {
|
||||
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
|
||||
monitor.update(5)
|
||||
monitor.update(7)
|
||||
|
||||
expect(monitor.currentTrackIndex).to.equal(0)
|
||||
expect(monitor.cummulativeProgress).to.equal(0)
|
||||
expect(monitor.currentTrackPercentage).to.equal(10)
|
||||
expect(trackStartedCallback.calledOnceWithExactly(0)).to.be.true
|
||||
expect(progressCallback.calledTwice).to.be.true
|
||||
expect(progressCallback.calledWithExactly(0, 50, 5)).to.be.true
|
||||
expect(progressCallback.calledWithExactly(0, 70, 7)).to.be.true
|
||||
expect(trackFinishedCallback.notCalled).to.be.true
|
||||
})
|
||||
|
||||
it('should update the progress multiple times on different tracks', () => {
|
||||
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
|
||||
monitor.update(5)
|
||||
monitor.update(20)
|
||||
|
||||
expect(monitor.currentTrackIndex).to.equal(1)
|
||||
expect(monitor.cummulativeProgress).to.equal(10)
|
||||
expect(monitor.currentTrackPercentage).to.equal(40)
|
||||
expect(trackStartedCallback.calledTwice).to.be.true
|
||||
expect(trackStartedCallback.calledWithExactly(0)).to.be.true
|
||||
expect(trackStartedCallback.calledWithExactly(1)).to.be.true
|
||||
expect(progressCallback.calledTwice).to.be.true
|
||||
expect(progressCallback.calledWithExactly(0, 50, 5)).to.be.true
|
||||
expect(progressCallback.calledWithExactly(1, 25, 20)).to.be.true
|
||||
expect(trackFinishedCallback.calledOnceWithExactly(0)).to.be.true
|
||||
})
|
||||
|
||||
it('should finish all tracks', () => {
|
||||
monitor = new TrackProgressMonitor(trackDurations, trackStartedCallback, progressCallback, trackFinishedCallback)
|
||||
monitor.finish()
|
||||
|
||||
expect(monitor.allTracksFinished).to.be.true
|
||||
expect(trackStartedCallback.calledThrice).to.be.true
|
||||
expect(trackFinishedCallback.calledThrice).to.be.true
|
||||
expect(progressCallback.notCalled).to.be.true
|
||||
expect(trackStartedCallback.calledWithExactly(0)).to.be.true
|
||||
expect(trackFinishedCallback.calledWithExactly(0)).to.be.true
|
||||
expect(trackStartedCallback.calledWithExactly(1)).to.be.true
|
||||
expect(trackFinishedCallback.calledWithExactly(1)).to.be.true
|
||||
expect(trackStartedCallback.calledWithExactly(2)).to.be.true
|
||||
expect(trackFinishedCallback.calledWithExactly(2)).to.be.true
|
||||
})
|
||||
})
|
|
@ -81,10 +81,9 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
ffmpegStub.run = sinon.stub().callsFake(() => {
|
||||
ffmpegStub.emit('end')
|
||||
})
|
||||
const fsCopyFileSyncStub = sinon.stub(fs, 'copyFileSync')
|
||||
const fsUnlinkSyncStub = sinon.stub(fs, 'unlinkSync')
|
||||
const fsMove = sinon.stub(fs, 'move').resolves()
|
||||
|
||||
return { audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub, fsCopyFileSyncStub, fsUnlinkSyncStub }
|
||||
return { audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub, fsMove }
|
||||
}
|
||||
|
||||
let audioFilePath = null
|
||||
|
@ -93,8 +92,7 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
let track = null
|
||||
let mimeType = null
|
||||
let ffmpegStub = null
|
||||
let fsCopyFileSyncStub = null
|
||||
let fsUnlinkSyncStub = null
|
||||
let fsMove = null
|
||||
beforeEach(() => {
|
||||
const input = createTestSetup()
|
||||
audioFilePath = input.audioFilePath
|
||||
|
@ -103,16 +101,14 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
track = input.track
|
||||
mimeType = input.mimeType
|
||||
ffmpegStub = input.ffmpegStub
|
||||
fsCopyFileSyncStub = input.fsCopyFileSyncStub
|
||||
fsUnlinkSyncStub = input.fsUnlinkSyncStub
|
||||
fsMove = input.fsMove
|
||||
})
|
||||
|
||||
it('should add cover image and metadata to audio file', async () => {
|
||||
// Act
|
||||
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
|
||||
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
|
||||
|
||||
// Assert
|
||||
expect(result).to.be.true
|
||||
expect(ffmpegStub.input.calledThrice).to.be.true
|
||||
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
|
||||
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
|
||||
|
@ -129,12 +125,10 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
|
||||
expect(ffmpegStub.run.calledOnce).to.be.true
|
||||
|
||||
expect(fsCopyFileSyncStub.calledOnce).to.be.true
|
||||
expect(fsCopyFileSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
|
||||
expect(fsCopyFileSyncStub.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
|
||||
|
||||
expect(fsUnlinkSyncStub.calledOnce).to.be.true
|
||||
expect(fsUnlinkSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
|
||||
expect(fsMove.calledOnce).to.be.true
|
||||
expect(fsMove.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
|
||||
expect(fsMove.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
|
||||
expect(fsMove.firstCall.args[2]).to.deep.equal({ overwrite: true })
|
||||
|
||||
// Restore the stub
|
||||
sinon.restore()
|
||||
|
@ -145,10 +139,9 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
coverFilePath = null
|
||||
|
||||
// Act
|
||||
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
|
||||
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
|
||||
|
||||
// Assert
|
||||
expect(result).to.be.true
|
||||
expect(ffmpegStub.input.calledTwice).to.be.true
|
||||
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
|
||||
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
|
||||
|
@ -164,12 +157,10 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
|
||||
expect(ffmpegStub.run.calledOnce).to.be.true
|
||||
|
||||
expect(fsCopyFileSyncStub.calledOnce).to.be.true
|
||||
expect(fsCopyFileSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
|
||||
expect(fsCopyFileSyncStub.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
|
||||
|
||||
expect(fsUnlinkSyncStub.calledOnce).to.be.true
|
||||
expect(fsUnlinkSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
|
||||
expect(fsMove.calledOnce).to.be.true
|
||||
expect(fsMove.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.mp3')
|
||||
expect(fsMove.firstCall.args[1]).to.equal('/path/to/audio/file.mp3')
|
||||
expect(fsMove.firstCall.args[2]).to.deep.equal({ overwrite: true })
|
||||
|
||||
// Restore the stub
|
||||
sinon.restore()
|
||||
|
@ -182,10 +173,15 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
})
|
||||
|
||||
// Act
|
||||
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
|
||||
try {
|
||||
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
|
||||
expect.fail('Expected an error to be thrown')
|
||||
} catch (error) {
|
||||
// Assert
|
||||
expect(error.message).to.equal('FFmpeg error')
|
||||
}
|
||||
|
||||
// Assert
|
||||
expect(result).to.be.false
|
||||
expect(ffmpegStub.input.calledThrice).to.be.true
|
||||
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
|
||||
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
|
||||
|
@ -202,9 +198,7 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
|
||||
expect(ffmpegStub.run.calledOnce).to.be.true
|
||||
|
||||
expect(fsCopyFileSyncStub.called).to.be.false
|
||||
|
||||
expect(fsUnlinkSyncStub.called).to.be.false
|
||||
expect(fsMove.called).to.be.false
|
||||
|
||||
// Restore the stub
|
||||
sinon.restore()
|
||||
|
@ -216,10 +210,9 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
audioFilePath = '/path/to/audio/file.m4b'
|
||||
|
||||
// Act
|
||||
const result = await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, ffmpegStub)
|
||||
await addCoverAndMetadataToFile(audioFilePath, coverFilePath, metadataFilePath, track, mimeType, null, ffmpegStub)
|
||||
|
||||
// Assert
|
||||
expect(result).to.be.true
|
||||
expect(ffmpegStub.input.calledThrice).to.be.true
|
||||
expect(ffmpegStub.input.getCall(0).args[0]).to.equal(audioFilePath)
|
||||
expect(ffmpegStub.input.getCall(1).args[0]).to.equal(metadataFilePath)
|
||||
|
@ -236,12 +229,10 @@ describe('addCoverAndMetadataToFile', () => {
|
|||
|
||||
expect(ffmpegStub.run.calledOnce).to.be.true
|
||||
|
||||
expect(fsCopyFileSyncStub.calledOnce).to.be.true
|
||||
expect(fsCopyFileSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.m4b')
|
||||
expect(fsCopyFileSyncStub.firstCall.args[1]).to.equal('/path/to/audio/file.m4b')
|
||||
|
||||
expect(fsUnlinkSyncStub.calledOnce).to.be.true
|
||||
expect(fsUnlinkSyncStub.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.m4b')
|
||||
expect(fsMove.calledOnce).to.be.true
|
||||
expect(fsMove.firstCall.args[0]).to.equal('/path/to/audio/file.tmp.m4b')
|
||||
expect(fsMove.firstCall.args[1]).to.equal('/path/to/audio/file.m4b')
|
||||
expect(fsMove.firstCall.args[2]).to.deep.equal({ overwrite: true })
|
||||
|
||||
// Restore the stub
|
||||
sinon.restore()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue