mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-21 11:19:37 +00:00
Added additional unit tests for construction of objects containing deviceId property
This commit is contained in:
parent
3a4aacb7bf
commit
974e17ee3e
9 changed files with 333 additions and 124 deletions
|
|
@ -1,9 +1,115 @@
|
|||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
const sinon = require('sinon')
|
||||
|
||||
const Path = require('path')
|
||||
const Database = require('../../../server/Database')
|
||||
const { loadTestDatabase, stubFileUtils, getMockFileInfo } = require('../MockDatabase')
|
||||
|
||||
// TODO: all of these classes duplicate each other.
|
||||
const LibraryFile = require('../../../server/objects/files/LibraryFile')
|
||||
const EBookFile = require('../../../server/objects/files/EBookFile')
|
||||
const AudioFile = require('../../../server/objects/files/AudioFile')
|
||||
const LibraryItem = require('../../../server/models/LibraryItem')
|
||||
const LibraryItemScanData = require('../../../server/scanner/LibraryItemScanData')
|
||||
const FileMetadata = require('../../../server/objects/metadata/FileMetadata')
|
||||
|
||||
// TODO: all of these duplicate each other. Need to verify that deviceId is set on each when constructing. And that deviceId is populated when using toJSON()
|
||||
const fileProperties = buildFileProperties()
|
||||
const lf = new LibraryFile(fileProperties)
|
||||
const ebf = new EBookFile(fileProperties)
|
||||
const af = new AudioFile(fileProperties)
|
||||
|
||||
// TODO: check that any libraryFiles properties set to JSON contain a LibraryFile which has a deviceId property
|
||||
describe('ObjectSetsDeviceIdWhenConstructed', function () {
|
||||
this.timeout(0)
|
||||
beforeEach(async () => {
|
||||
stubFileUtils()
|
||||
await loadTestDatabase()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
const lisd = new LibraryItemScanData(fileProperties)
|
||||
|
||||
const objects = [lf, ebf, af, lisd]
|
||||
|
||||
objects.forEach((obj) => {
|
||||
it(`${obj.constructor.name}SetsDeviceIdWhenConstructed`, () => {
|
||||
expect(obj.ino).to.equal(fileProperties.ino)
|
||||
expect(obj.deviceId).to.equal(fileProperties.deviceId)
|
||||
})
|
||||
})
|
||||
|
||||
it('LibraryItemSetsDeviceIdWhenConstructed', async () => {
|
||||
const mockFileInfo = getMockFileInfo().get('/test/file.pdf')
|
||||
|
||||
/** @type {import('../../../server/models/LibraryItem') | null} */
|
||||
const li = await Database.libraryItemModel.findOneExpanded({
|
||||
path: '/test/file.pdf'
|
||||
})
|
||||
|
||||
expect(li?.ino).to.equal(mockFileInfo?.ino)
|
||||
expect(li?.deviceId).to.equal(mockFileInfo?.dev)
|
||||
})
|
||||
|
||||
it('LibraryFileJSONHasDeviceId', async () => {
|
||||
const mockFileInfo = getMockFileInfo().get('/test/file.pdf')
|
||||
|
||||
/** @type {import('../../../server/models/LibraryItem') | null} */
|
||||
const li = await Database.libraryItemModel.findOneExpanded({
|
||||
path: '/test/file.pdf'
|
||||
})
|
||||
|
||||
const lf_json = li?.libraryFiles[0]
|
||||
expect(lf_json).to.not.be.null
|
||||
expect(lf_json?.deviceId).to.equal(mockFileInfo?.dev)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ObjectSetsDeviceIdWhenSerialized', () => {
|
||||
const objects = [lf, ebf, af]
|
||||
objects.forEach((obj) => {
|
||||
it(`${obj.constructor.name}SetsDeviceIdWhenSerialized`, () => {
|
||||
const obj_json = obj.toJSON()
|
||||
expect(obj_json.ino).to.equal(fileProperties.ino)
|
||||
expect(obj_json.deviceId).to.equal(fileProperties.deviceId)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
/** @returns {{ libraryFolderId: any; libraryId: any; mediaType: any; ino: any; deviceId: any; mtimeMs: any; ctimeMs: any; birthtimeMs: any; path: any; relPath: any; isFile: any; mediaMetadata: any; libraryFiles: any; }} */
|
||||
function buildFileProperties() {
|
||||
const path = '/tmp/foo.epub'
|
||||
const metadata = new FileMetadata()
|
||||
metadata.filename = Path.basename(path)
|
||||
metadata.path = path
|
||||
metadata.relPath = path
|
||||
metadata.ext = Path.extname(path)
|
||||
|
||||
return {
|
||||
ino: '12345',
|
||||
deviceId: '9876',
|
||||
metadata: metadata,
|
||||
isSupplementary: false,
|
||||
addedAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
function buildLibraryItemProperties(fileProperties) {
|
||||
return {
|
||||
id: '7792E90F-D526-4636-8A38-EA8342E71FEE',
|
||||
path: fileProperties.path,
|
||||
relPath: fileProperties.path,
|
||||
isFile: true,
|
||||
ino: fileProperties.ino,
|
||||
deviceId: fileProperties.dev,
|
||||
libraryFiles: [],
|
||||
mediaId: '7195803A-9974-46E4-A7D1-7A6E1AD7FD4B',
|
||||
mediaType: 'book',
|
||||
libraryId: '907DA361-67E4-47CF-9C67-C8E2E5CA1B15',
|
||||
libraryFolderId: 'E2216F60-8ABF-4E55-BA83-AD077EB907F3',
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue