mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-05-13 06:51:29 +00:00
Merge 4e808e6770 into 47ea6b5092
This commit is contained in:
commit
f3a220c173
26 changed files with 2441 additions and 304 deletions
172
test/server/MockDatabase.js
Normal file
172
test/server/MockDatabase.js
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
const Database = require('../../server/Database')
|
||||
const { Sequelize } = require('sequelize')
|
||||
const LibraryFile = require('../../server/objects/files/LibraryFile')
|
||||
const fileUtils = require('../../server/utils/fileUtils')
|
||||
const FileMetadata = require('../../server/objects/metadata/FileMetadata')
|
||||
const Path = require('path')
|
||||
const sinon = require('sinon')
|
||||
|
||||
async function loadTestDatabase(mockFileInfo) {
|
||||
let libraryItem1Id, libraryItem2Id
|
||||
|
||||
let fileInfo = mockFileInfo || getMockFileInfo()
|
||||
// mapping the keys() iterable to an explicit array so reduce() should work consistently.
|
||||
let bookLibraryFiles = [...fileInfo.keys()].reduce((acc, key) => {
|
||||
let bookfile = new LibraryFile()
|
||||
bookfile.setDataFromPath(key, key)
|
||||
acc.push(bookfile)
|
||||
return acc
|
||||
}, [])
|
||||
|
||||
global.ServerSettings = {}
|
||||
Database.sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: ':memory:',
|
||||
// Choose one of the logging options
|
||||
logging: (...msg) => console.log(msg),
|
||||
logQueryParameters: true
|
||||
})
|
||||
Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '')
|
||||
await Database.buildModels()
|
||||
|
||||
const newLibrary = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' })
|
||||
const newLibraryFolder = await Database.libraryFolderModel.create({ path: '/test', libraryId: newLibrary.id })
|
||||
const newLibraryFolder2 = await Database.libraryFolderModel.create({ path: '/mnt/drive', libraryId: newLibrary.id })
|
||||
|
||||
const newBook = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
|
||||
const newLibraryItem = await Database.libraryItemModel.create(buildBookLibraryItemParams(bookLibraryFiles[0], newBook.id, newLibrary.id, newLibraryFolder.id))
|
||||
libraryItem1Id = newLibraryItem.id
|
||||
|
||||
const newBook2 = await Database.bookModel.create({ title: 'Test Book 2', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
|
||||
const newLibraryItem2 = await Database.libraryItemModel.create(buildBookLibraryItemParams(bookLibraryFiles[1], newBook2.id, newLibrary.id, newLibraryFolder2.id))
|
||||
libraryItem2Id = newLibraryItem2.id
|
||||
|
||||
return newLibrary
|
||||
}
|
||||
exports.loadTestDatabase = loadTestDatabase
|
||||
|
||||
/** @returns {Map<string, import('fs').Stats>} */
|
||||
function getMockFileInfo() {
|
||||
// @ts-ignore
|
||||
return new Map([
|
||||
['/test/file.pdf', { path: '/test/file.pdf', isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '100' }],
|
||||
['/mnt/drive/file-same-ino-different-dev.pdf', { path: '/mnt/drive/file-same-ino-different-dev.pdf', isDirectory: () => false, size: 42, mtimeMs: Date.now(), ino: '1', dev: '200' }]
|
||||
])
|
||||
}
|
||||
|
||||
exports.getMockFileInfo = getMockFileInfo
|
||||
/** @returns {Map<string, import('fs').Stats>} */
|
||||
// this has the same data as above except one file has been renamed
|
||||
function getRenamedMockFileInfo() {
|
||||
// @ts-ignore
|
||||
return new Map([
|
||||
['/test/file-renamed.pdf', { path: '/test/file-renamed.pdf', isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '100' }],
|
||||
['/mnt/drive/file-same-ino-different-dev.pdf', { path: '/mnt/drive/file-same-ino-different-dev.pdf', isDirectory: () => false, size: 42, mtimeMs: Date.now(), ino: '1', dev: '200' }]
|
||||
])
|
||||
}
|
||||
exports.getRenamedMockFileInfo = getRenamedMockFileInfo
|
||||
|
||||
/**
|
||||
* @param {LibraryFile} libraryFile
|
||||
* @param {any} bookId
|
||||
* @param {string} libraryId
|
||||
* @param {any} libraryFolderId
|
||||
*/
|
||||
function buildBookLibraryItemParams(libraryFile, bookId, libraryId, libraryFolderId) {
|
||||
return {
|
||||
path: libraryFile.metadata?.path,
|
||||
isFile: true,
|
||||
ino: libraryFile.ino,
|
||||
deviceId: libraryFile.deviceId,
|
||||
libraryFiles: [libraryFile.toJSON()],
|
||||
mediaId: bookId,
|
||||
mediaType: 'book',
|
||||
libraryId: libraryId,
|
||||
libraryFolderId: libraryFolderId
|
||||
}
|
||||
}
|
||||
exports.buildBookLibraryItemParams = buildBookLibraryItemParams
|
||||
|
||||
function stubFileUtils(mockFileInfo = getMockFileInfo()) {
|
||||
let getInoStub, getDeviceIdStub, getFileTimestampsWithInoStub
|
||||
getInoStub = sinon.stub(fileUtils, 'getIno')
|
||||
getInoStub.callsFake((path) => {
|
||||
const normalizedPath = fileUtils.filePathToPOSIX(path).replace(/\/$/, '')
|
||||
const stats = mockFileInfo.get(normalizedPath)
|
||||
if (stats) {
|
||||
return stats.ino
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})
|
||||
|
||||
getDeviceIdStub = sinon.stub(fileUtils, 'getDeviceId')
|
||||
getDeviceIdStub.callsFake(async (path) => {
|
||||
const normalizedPath = fileUtils.filePathToPOSIX(path).replace(/\/$/, '')
|
||||
const stats = mockFileInfo.get(normalizedPath)
|
||||
if (stats) {
|
||||
return stats.dev
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})
|
||||
|
||||
getFileTimestampsWithInoStub = sinon.stub(fileUtils, 'getFileTimestampsWithIno')
|
||||
getFileTimestampsWithInoStub.callsFake(async (path) => {
|
||||
const normalizedPath = fileUtils.filePathToPOSIX(path).replace(/\/$/, '')
|
||||
const stats = mockFileInfo.get(normalizedPath)
|
||||
if (stats) {
|
||||
return stats
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})
|
||||
}
|
||||
exports.stubFileUtils = stubFileUtils
|
||||
|
||||
/** @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(path = '/tmp/foo.epub', ino = '12345', deviceId = '9876', libraryFiles = []) {
|
||||
const metadata = new FileMetadata()
|
||||
metadata.filename = Path.basename(path)
|
||||
metadata.path = path
|
||||
metadata.relPath = path
|
||||
metadata.ext = Path.extname(path)
|
||||
|
||||
return {
|
||||
ino: ino,
|
||||
deviceId: deviceId,
|
||||
metadata: metadata,
|
||||
isSupplementary: false,
|
||||
addedAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
libraryFiles: [...libraryFiles.map((lf) => lf.toJSON())]
|
||||
}
|
||||
}
|
||||
exports.buildFileProperties = buildFileProperties
|
||||
|
||||
/**
|
||||
* @returns {import('../../server/models/LibraryItem').LibraryFileObject}
|
||||
* @param {string} [path]
|
||||
* @param {string} [ino]
|
||||
* @param {string} [deviceId]
|
||||
*/
|
||||
function buildLibraryFileProperties(path, ino, deviceId) {
|
||||
return {
|
||||
ino: ino,
|
||||
deviceId: deviceId,
|
||||
isSupplementary: false,
|
||||
addedAt: 0,
|
||||
updatedAt: 0,
|
||||
metadata: {
|
||||
filename: Path.basename(path),
|
||||
ext: Path.extname(path),
|
||||
path: path,
|
||||
relPath: path,
|
||||
size: 0,
|
||||
mtimeMs: 0,
|
||||
ctimeMs: 0,
|
||||
birthtimeMs: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.buildLibraryFileProperties = buildLibraryFileProperties
|
||||
178
test/server/migrations/v2.30.0-add-deviceId.test.js
Normal file
178
test/server/migrations/v2.30.0-add-deviceId.test.js
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
const chai = require('chai')
|
||||
const sinon = require('sinon')
|
||||
const { expect } = chai
|
||||
|
||||
const { DataTypes, Sequelize } = require('sequelize')
|
||||
const Logger = require('../../../server/Logger')
|
||||
|
||||
const { up, down, migrationName } = require('../../../server/migrations/v2.30.0-add-deviceId')
|
||||
const { stubFileUtils, getMockFileInfo } = require('../MockDatabase')
|
||||
|
||||
const normalizeWhitespaceAndBackticks = (str) => str.replace(/\s+/g, ' ').trim().replace(/`/g, '')
|
||||
|
||||
describe(`Migration ${migrationName}`, () => {
|
||||
let sequelize
|
||||
let queryInterface
|
||||
let loggerInfoStub
|
||||
let mockFileInfo, file1stats, file2stats
|
||||
|
||||
beforeEach(async () => {
|
||||
sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false })
|
||||
queryInterface = sequelize.getQueryInterface()
|
||||
loggerInfoStub = sinon.stub(Logger, 'info')
|
||||
|
||||
mockFileInfo = getMockFileInfo()
|
||||
file1stats = mockFileInfo.get('/test/file.pdf')
|
||||
file2stats = mockFileInfo.get('/mnt/drive/file-same-ino-different-dev.pdf')
|
||||
|
||||
stubFileUtils(mockFileInfo)
|
||||
|
||||
await queryInterface.createTable('libraryItems', {
|
||||
id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, unique: true },
|
||||
ino: { type: DataTypes.STRING },
|
||||
path: { type: DataTypes.STRING },
|
||||
mediaId: { type: DataTypes.INTEGER, allowNull: false },
|
||||
mediaType: { type: DataTypes.STRING, allowNull: false },
|
||||
libraryId: { type: DataTypes.INTEGER, allowNull: false }
|
||||
})
|
||||
|
||||
await queryInterface.createTable('authors', {
|
||||
id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, unique: true },
|
||||
name: { type: DataTypes.STRING, allowNull: false },
|
||||
lastFirst: { type: DataTypes.STRING, allowNull: false }
|
||||
})
|
||||
|
||||
await queryInterface.createTable('bookAuthors', {
|
||||
id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, unique: true },
|
||||
bookId: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'libraryItems', key: 'id', onDelete: 'CASCADE' } },
|
||||
authorId: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'authors', key: 'id', onDelete: 'CASCADE' } },
|
||||
createdAt: { type: DataTypes.DATE, allowNull: false }
|
||||
})
|
||||
|
||||
await queryInterface.createTable('podcastEpisodes', {
|
||||
id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, unique: true },
|
||||
publishedAt: { type: DataTypes.DATE, allowNull: true }
|
||||
})
|
||||
|
||||
await queryInterface.bulkInsert('libraryItems', [
|
||||
{ id: 1, ino: file1stats.ino, mediaId: 1, path: file1stats.path, mediaType: 'book', libraryId: 1 },
|
||||
{ id: 2, ino: file2stats.ino, mediaId: 2, path: file2stats.path, mediaType: 'book', libraryId: 1 }
|
||||
])
|
||||
|
||||
await queryInterface.bulkInsert('authors', [
|
||||
{ id: 1, name: 'John Doe', lastFirst: 'Doe, John' },
|
||||
{ id: 2, name: 'Jane Smith', lastFirst: 'Smith, Jane' },
|
||||
{ id: 3, name: 'John Smith', lastFirst: 'Smith, John' }
|
||||
])
|
||||
|
||||
await queryInterface.bulkInsert('bookAuthors', [
|
||||
{ id: 1, bookId: 1, authorId: 1, createdAt: '2025-01-01 00:00:00.000 +00:00' },
|
||||
{ id: 2, bookId: 2, authorId: 2, createdAt: '2025-01-02 00:00:00.000 +00:00' },
|
||||
{ id: 3, bookId: 1, authorId: 3, createdAt: '2024-12-31 00:00:00.000 +00:00' }
|
||||
])
|
||||
|
||||
await queryInterface.bulkInsert('podcastEpisodes', [
|
||||
{ id: 1, publishedAt: '2025-01-01 00:00:00.000 +00:00' },
|
||||
{ id: 2, publishedAt: '2025-01-02 00:00:00.000 +00:00' },
|
||||
{ id: 3, publishedAt: '2025-01-03 00:00:00.000 +00:00' }
|
||||
])
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
describe('up', () => {
|
||||
it('should add the deviceId column to the libraryItems table', async () => {
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
|
||||
const libraryItems = await queryInterface.describeTable('libraryItems')
|
||||
expect(libraryItems.deviceId).to.exist
|
||||
})
|
||||
|
||||
it('should populate the deviceId columns from the filesystem for each libraryItem', async function () {
|
||||
this.timeout(0)
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
|
||||
const [libraryItems] = await queryInterface.sequelize.query('SELECT * FROM libraryItems')
|
||||
expect(libraryItems).to.deep.equal([
|
||||
{ id: 1, ino: file1stats.ino, deviceId: file1stats.dev, mediaId: 1, path: file1stats.path, mediaType: 'book', libraryId: 1 },
|
||||
{ id: 2, ino: file2stats.ino, deviceId: file2stats.dev, mediaId: 2, path: file2stats.path, mediaType: 'book', libraryId: 1 }
|
||||
])
|
||||
})
|
||||
|
||||
it('should add an index on ino and deviceId to the libraryItems table', async () => {
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
|
||||
const indexes = await queryInterface.sequelize.query(`SELECT * FROM sqlite_master WHERE type='index'`)
|
||||
const [[{ count }]] = await queryInterface.sequelize.query(`SELECT COUNT(*) as count FROM sqlite_master WHERE type='index' AND name='library_items_ino_device_id'`)
|
||||
expect(count).to.equal(1)
|
||||
|
||||
const [[{ sql }]] = await queryInterface.sequelize.query(`SELECT sql FROM sqlite_master WHERE type='index' AND name='library_items_ino_device_id'`)
|
||||
expect(normalizeWhitespaceAndBackticks(sql)).to.equal(
|
||||
normalizeWhitespaceAndBackticks(`
|
||||
CREATE INDEX library_items_ino_device_id ON libraryItems (ino, deviceId)
|
||||
`)
|
||||
)
|
||||
})
|
||||
|
||||
it('should be idempotent', async () => {
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
|
||||
const libraryItemsTable = await queryInterface.describeTable('libraryItems')
|
||||
expect(libraryItemsTable.deviceId).to.exist
|
||||
|
||||
const [[{ count: count6 }]] = await queryInterface.sequelize.query(`SELECT COUNT(*) as count FROM sqlite_master WHERE type='index' AND name='library_items_ino_device_id'`)
|
||||
expect(count6).to.equal(1)
|
||||
|
||||
const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`)
|
||||
expect(libraryItems).to.deep.equal([
|
||||
{ id: 1, ino: file1stats.ino, deviceId: file1stats.dev, path: file1stats.path, mediaId: 1, mediaType: 'book', libraryId: 1 },
|
||||
{ id: 2, ino: file2stats.ino, deviceId: file2stats.dev, path: file2stats.path, mediaId: 2, mediaType: 'book', libraryId: 1 }
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('down', () => {
|
||||
it('should remove the deviceId from the libraryItems table', async () => {
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
await down({ context: { queryInterface, logger: Logger } })
|
||||
|
||||
const libraryItemsTable = await queryInterface.describeTable('libraryItems')
|
||||
expect(libraryItemsTable.deviceId).to.not.exist
|
||||
|
||||
const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`)
|
||||
expect(libraryItems).to.deep.equal([
|
||||
{ id: 1, ino: file1stats.ino, mediaId: 1, path: file1stats.path, mediaType: 'book', libraryId: 1 },
|
||||
{ id: 2, ino: file2stats.ino, mediaId: 2, path: file2stats.path, mediaType: 'book', libraryId: 1 }
|
||||
])
|
||||
})
|
||||
|
||||
it('should remove the index on ino, deviceId from the libraryItems table', async () => {
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
await down({ context: { queryInterface, logger: Logger } })
|
||||
|
||||
const [[{ count }]] = await queryInterface.sequelize.query(`SELECT COUNT(*) as count FROM sqlite_master WHERE type='index' AND name='library_items_ino_device_id'`)
|
||||
expect(count).to.equal(0)
|
||||
})
|
||||
|
||||
it('should be idempotent', async () => {
|
||||
await up({ context: { queryInterface, logger: Logger } })
|
||||
await down({ context: { queryInterface, logger: Logger } })
|
||||
await down({ context: { queryInterface, logger: Logger } })
|
||||
|
||||
const libraryItemsTable = await queryInterface.describeTable('libraryItems')
|
||||
expect(libraryItemsTable.libraryItems).to.not.exist
|
||||
|
||||
const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`)
|
||||
expect(libraryItems).to.deep.equal([
|
||||
{ id: 1, ino: file1stats.ino, path: file1stats.path, mediaId: 1, mediaType: 'book', libraryId: 1 },
|
||||
{ id: 2, ino: file2stats.ino, path: file2stats.path, mediaId: 2, mediaType: 'book', libraryId: 1 }
|
||||
])
|
||||
|
||||
const [[{ count: count6 }]] = await queryInterface.sequelize.query(`SELECT COUNT(*) as count FROM sqlite_master WHERE type='index' AND name='library_items_ino_device_id'`)
|
||||
expect(count6).to.equal(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
183
test/server/objects/LibraryItemScanData.test.js
Normal file
183
test/server/objects/LibraryItemScanData.test.js
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
const Path = require('path')
|
||||
|
||||
const { buildFileProperties, buildLibraryFileProperties } = require('../MockDatabase')
|
||||
|
||||
const LibraryItemScanData = require('../../../server/scanner/LibraryItemScanData')
|
||||
const LibraryFile = require('../../../server/objects/files/LibraryFile')
|
||||
const LibraryScan = require('../../../server/scanner/LibraryScan')
|
||||
const ScanLogger = require('../../../server/scanner/ScanLogger')
|
||||
describe('LibraryItemScanData', () => {
|
||||
// compareUpdateLibraryFile - returns false if no changes; true if changes
|
||||
describe('compareUpdateLibraryFileWithDeviceId', () => {
|
||||
it('fileChangeDetectedWhenInodeAndDeviceIdPairDiffers', () => {
|
||||
const existing_lf = buildLibraryFileProperties('/tmp/file.pdf', '4432', '300')
|
||||
const scanned_lf = new LibraryFile({
|
||||
ino: '1',
|
||||
deviceId: '100'
|
||||
})
|
||||
|
||||
expect(existing_lf.ino).to.not.equal(scanned_lf.ino)
|
||||
expect(existing_lf.deviceId).to.not.equal(scanned_lf.deviceId)
|
||||
const changeDetected = LibraryItemScanData.compareUpdateLibraryFile('/file/path.pdf', existing_lf, scanned_lf, new LibraryScan())
|
||||
expect(changeDetected).to.be.true
|
||||
})
|
||||
|
||||
it('fileChangeNotDetectedWhenInodeSameButDeviceIdDiffers', () => {
|
||||
// Same inode on different deviceId does NOT mean these are the same file
|
||||
const existing_lf = buildLibraryFileProperties('/tmp/file.pdf', '4432', '300')
|
||||
const scanned_lf = new LibraryFile(buildLibraryFileProperties('/tmp/file.pdf', '4432', '100'))
|
||||
|
||||
expect(existing_lf.ino).to.equal(scanned_lf.ino)
|
||||
expect(existing_lf.deviceId).to.not.equal(scanned_lf.deviceId)
|
||||
const changeDetected = LibraryItemScanData.compareUpdateLibraryFile('/file/path.pdf', existing_lf, scanned_lf, new LibraryScan())
|
||||
expect(changeDetected).to.be.false
|
||||
})
|
||||
})
|
||||
|
||||
describe('findMatchingLibraryFileByPathOrInodeAndDeviceId', () => {
|
||||
it('isMatchWhenInodeAndDeviceIdPairIsSame', () => {
|
||||
const lisd = new LibraryItemScanData(buildFileProperties('/library/book/file.epub', '1', '1000', [new LibraryFile(buildLibraryFileProperties('/library/book/file.epub', '1', '1000'))]))
|
||||
|
||||
const scanned_lf_properties = buildLibraryFileProperties('/tmp/file.epub', '1', '1000')
|
||||
|
||||
const matchingFile = lisd.findMatchingLibraryFileByPathOrInodeAndDeviceId(scanned_lf_properties, new ScanLogger())
|
||||
|
||||
// don't want match based on filename
|
||||
expect(lisd.path).to.not.equal(scanned_lf_properties.metadata.path)
|
||||
expect(matchingFile).to.not.be.undefined
|
||||
expect(matchingFile?.ino).to.equal(lisd.ino)
|
||||
expect(matchingFile?.deviceId).to.equal(lisd.deviceId)
|
||||
})
|
||||
it('isNotMatchWhenInodeSameButDeviceIdDiffers', () => {
|
||||
const lisd = new LibraryItemScanData(buildFileProperties('/library/book/file.epub', '1', '1000', [new LibraryFile(buildLibraryFileProperties('/library/book/file.epub', '1', '1000'))]))
|
||||
|
||||
const scanned_lf_properties = buildLibraryFileProperties('/tmp/file.epub', '1', '500')
|
||||
|
||||
// don't want match based on filename
|
||||
expect(lisd.path).to.not.equal(scanned_lf_properties.metadata.path)
|
||||
expect(lisd.deviceId).to.not.equal(scanned_lf_properties.ino)
|
||||
|
||||
const matchingFile = lisd.findMatchingLibraryFileByPathOrInodeAndDeviceId(scanned_lf_properties, new ScanLogger())
|
||||
|
||||
expect(matchingFile).to.be.undefined
|
||||
})
|
||||
})
|
||||
|
||||
describe('checkAudioFileRemoved', function () {
|
||||
this.timeout(0)
|
||||
it('doesNotDetectFileRemovedWhenInodeIsSameButDeviceIdDiffers', () => {
|
||||
const lisd = new LibraryItemScanData(buildFileProperties('/library/book/file.mp3', '1', '1000'))
|
||||
lisd.libraryFilesRemoved.push(buildLibraryFileProperties('/library/book/file.mp3', '1', '1000'))
|
||||
const af_obj = buildAudioFileObject('/library/someotherbook/chapter1.mp3', '1', '200')
|
||||
|
||||
const fileRemoved = lisd.checkAudioFileRemoved(af_obj)
|
||||
|
||||
expect(fileRemoved).to.be.false
|
||||
})
|
||||
|
||||
it('detectsFileRemovedWhenNameDoesNotMatchButInodeAndDeviceIdMatch', () => {
|
||||
const lisd = new LibraryItemScanData(buildFileProperties('/library/book/file.mp3', '1', '1000'))
|
||||
lisd.libraryFilesRemoved.push(buildLibraryFileProperties('/library/book/file.mp3', '1', '1000'))
|
||||
const af_obj = buildAudioFileObject('/library/someotherbook/chapter1.mp3', '1', '1000')
|
||||
|
||||
expect(lisd.path).to.not.equal(af_obj.metadata.path)
|
||||
const fileRemoved = lisd.checkAudioFileRemoved(af_obj)
|
||||
|
||||
expect(fileRemoved).to.be.true
|
||||
})
|
||||
})
|
||||
|
||||
// checkEbookFileRemoved
|
||||
describe('checkEbookFileRemoved', () => {
|
||||
it('doesNotDetectFileRemovedWhenInodeIsSameButDeviceIdDiffers', () => {
|
||||
const lisd = new LibraryItemScanData(buildFileProperties('/library/book/file.epub', '1', '1000', [new LibraryFile(buildLibraryFileProperties('/library/book/file.epub', '1', '1000'))]))
|
||||
lisd.libraryFilesRemoved.push(buildLibraryFileProperties('/library/book/file.epub', '1', '1000')) // This is the file that was removed
|
||||
const ebook_obj = buildEbookFileObject('/library/someotherbook/chapter1.epub', '1', '200') // this file was NOT removed
|
||||
|
||||
expect(lisd.path).to.not.equal(ebook_obj.metadata.path)
|
||||
const fileRemoved = lisd.checkEbookFileRemoved(ebook_obj)
|
||||
|
||||
expect(fileRemoved).to.be.false
|
||||
})
|
||||
|
||||
it('detectsFileRemovedWhenInodeAndDeviceIdIsSame', () => {
|
||||
const lisd = new LibraryItemScanData(buildFileProperties('/library/book/file.epub', '1', '1000', [new LibraryFile(buildLibraryFileProperties('/library/book/file.epub', '1', '1000'))]))
|
||||
lisd.libraryFilesRemoved.push(buildLibraryFileProperties('/library/book/file.epub', '1', '1000')) // This is the file that was removed
|
||||
const ebook_obj = buildEbookFileObject('/library/someotherbook/chapter1.epub', '1', '1000') // this file was removed
|
||||
|
||||
expect(lisd.path).to.not.equal(ebook_obj.metadata.path)
|
||||
const fileRemoved = lisd.checkEbookFileRemoved(ebook_obj)
|
||||
|
||||
expect(fileRemoved).to.be.true
|
||||
})
|
||||
})
|
||||
|
||||
// libraryItemObject()
|
||||
describe('libraryItemObject', () => {
|
||||
it('setsDeviceIdOnLibraryObject', () => {
|
||||
const lisd = new LibraryItemScanData(buildFileProperties('/library/book/file.epub', '1', '1000', [new LibraryFile(buildLibraryFileProperties('/library/book/file.epub', '1', '1000'))]))
|
||||
expect(lisd.libraryItemObject.ino).to.equal(lisd.ino)
|
||||
expect(lisd.libraryItemObject.deviceId).to.equal(lisd.deviceId)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
/** @returns {import('../../../server/models/Book').AudioFileObject} */
|
||||
function buildAudioFileObject(path = '/library/somebook/file.mp3', ino = '1', deviceId = '1000') {
|
||||
return {
|
||||
index: 0,
|
||||
ino: ino,
|
||||
deviceId: deviceId,
|
||||
metadata: {
|
||||
filename: Path.basename(path),
|
||||
ext: Path.extname(path),
|
||||
path: path,
|
||||
relPath: path,
|
||||
size: 0,
|
||||
mtimeMs: 0,
|
||||
ctimeMs: 0,
|
||||
birthtimeMs: 0
|
||||
},
|
||||
addedAt: 0,
|
||||
updatedAt: 0,
|
||||
trackNumFromMeta: 0,
|
||||
discNumFromMeta: 0,
|
||||
trackNumFromFilename: 0,
|
||||
discNumFromFilename: 0,
|
||||
manuallyVerified: false,
|
||||
format: '',
|
||||
duration: 0,
|
||||
bitRate: 0,
|
||||
language: '',
|
||||
codec: '',
|
||||
timeBase: '',
|
||||
channels: 0,
|
||||
channelLayout: '',
|
||||
chapters: [],
|
||||
metaTags: undefined,
|
||||
mimeType: ''
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {import('../../../server/models/Book').EBookFileObject} */
|
||||
function buildEbookFileObject(path = '/library/somebook/file.epub', ino = '100', deviceId = '1000') {
|
||||
return {
|
||||
ino: ino,
|
||||
deviceId: deviceId,
|
||||
ebookFormat: Path.extname(path),
|
||||
addedAt: 0,
|
||||
updatedAt: 0,
|
||||
metadata: {
|
||||
filename: Path.basename(path),
|
||||
ext: Path.extname(path),
|
||||
path: path,
|
||||
relPath: path,
|
||||
size: 0,
|
||||
mtimeMs: 0,
|
||||
ctimeMs: 0,
|
||||
birthtimeMs: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
97
test/server/objects/SimilarLibraryFileObjects.test.js
Normal file
97
test/server/objects/SimilarLibraryFileObjects.test.js
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
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, buildFileProperties } = 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 LibraryItemScanData = require('../../../server/scanner/LibraryItemScanData')
|
||||
|
||||
const fileProperties = buildFileProperties()
|
||||
const lf = new LibraryFile(fileProperties)
|
||||
const ebf = new EBookFile(fileProperties)
|
||||
const af = new AudioFile(fileProperties)
|
||||
|
||||
describe('SimilarLibraryFileObjects', () => {
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
72
test/server/scanner/LibraryItemScanner.test.js
Normal file
72
test/server/scanner/LibraryItemScanner.test.js
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
const sinon = require('sinon')
|
||||
const rewire = require('rewire')
|
||||
const Path = require('path')
|
||||
|
||||
const { stubFileUtils, getMockFileInfo, loadTestDatabase } = require('../MockDatabase')
|
||||
|
||||
const LibraryFile = require('../../../server/objects/files/LibraryFile')
|
||||
const FileMetadata = require('../../../server/objects/metadata/FileMetadata')
|
||||
const LibraryFolder = require('../../../server/models/LibraryFolder')
|
||||
|
||||
describe('LibraryItemScanner', () => {
|
||||
describe('buildLibraryItemScanData', () => {
|
||||
let testLibrary = null
|
||||
beforeEach(async () => {
|
||||
stubFileUtils()
|
||||
testLibrary = await loadTestDatabase()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('setsDeviceId', async () => {
|
||||
const libraryItemScanner = rewire('../../../server/scanner/LibraryItemScanner')
|
||||
|
||||
/**
|
||||
* @param {{ path?: any; relPath?: any; mediaMetadata?: any; }} libraryItemData
|
||||
* @param {import("../../../server/models/LibraryFolder")} folder
|
||||
* @param {import("../../../server/models/Library")} library
|
||||
* @param {boolean} isSingleMediaItem
|
||||
* @param {LibraryFile[]} libraryFiles
|
||||
* @return {import('../../../server/scanner/LibraryItemScanData') | null}
|
||||
* */
|
||||
const buildLibraryItemScanData = libraryItemScanner.__get__('buildLibraryItemScanData')
|
||||
|
||||
const mockFileInfo = getMockFileInfo().get('/test/file.pdf')
|
||||
const lf = new LibraryFile()
|
||||
var fileMetadata = new FileMetadata()
|
||||
fileMetadata.setData(mockFileInfo)
|
||||
fileMetadata.filename = Path.basename(mockFileInfo?.path)
|
||||
fileMetadata.path = mockFileInfo?.path
|
||||
fileMetadata.relPath = mockFileInfo?.path
|
||||
fileMetadata.ext = Path.extname(mockFileInfo?.path)
|
||||
lf.ino = mockFileInfo?.ino
|
||||
lf.deviceId = mockFileInfo?.dev
|
||||
lf.metadata = fileMetadata
|
||||
lf.addedAt = Date.now()
|
||||
lf.updatedAt = Date.now()
|
||||
lf.metadata = fileMetadata
|
||||
|
||||
const libraryItemData = {
|
||||
path: mockFileInfo?.path, // full path
|
||||
relPath: mockFileInfo?.path, // only filename
|
||||
mediaMetadata: {
|
||||
title: Path.basename(mockFileInfo?.path, Path.extname(mockFileInfo?.path))
|
||||
}
|
||||
}
|
||||
|
||||
const scanData = await buildLibraryItemScanData(libraryItemData, buildLibraryFolder(), testLibrary, true, [lf.toJSON()])
|
||||
|
||||
expect(scanData).to.not.be.null
|
||||
expect(scanData.deviceId).to.equal(mockFileInfo?.dev)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
/** @return {import("../../../server/models/LibraryFolder")} folder */
|
||||
function buildLibraryFolder() {
|
||||
return new LibraryFolder()
|
||||
}
|
||||
328
test/server/scanner/LibraryScanner.test.js
Normal file
328
test/server/scanner/LibraryScanner.test.js
Normal file
|
|
@ -0,0 +1,328 @@
|
|||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
const sinon = require('sinon')
|
||||
const rewire = require('rewire')
|
||||
const fileUtils = require('../../../server/utils/fileUtils')
|
||||
const LibraryFile = require('../../../server/objects/files/LibraryFile')
|
||||
const LibraryItem = require('../../../server/models/LibraryItem')
|
||||
const FileMetadata = require('../../../server/objects/metadata/FileMetadata')
|
||||
const Path = require('path')
|
||||
const Database = require('../../../server/Database')
|
||||
const { stubFileUtils, loadTestDatabase, getMockFileInfo, getRenamedMockFileInfo, buildBookLibraryItemParams, buildFileProperties, buildLibraryFileProperties } = require('../MockDatabase')
|
||||
const libraryScannerInstance = require('../../../server/scanner/LibraryScanner')
|
||||
const LibraryScan = require('../../../server/scanner/LibraryScan')
|
||||
|
||||
describe('LibraryScanner', () => {
|
||||
let LibraryScanner, testLibrary
|
||||
|
||||
beforeEach(async () => {
|
||||
stubFileUtils()
|
||||
|
||||
LibraryScanner = rewire('../../../server/scanner/LibraryScanner')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('findsByInodeAndDeviceId', async function () {
|
||||
// this.timeout(50000) // Note: don't use arrow function or timeout for debugging doesn't work
|
||||
let findLibraryItemByItemToFileInoMatch = LibraryScanner.__get__('findLibraryItemByItemToFileInoMatch')
|
||||
let fullPath = '/test/file.pdf'
|
||||
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
const fileInfo = mockFileInfo.get(fullPath)
|
||||
|
||||
/** @type {Promise<import('../../../server/models/LibraryItem') | null>} */
|
||||
const result = await findLibraryItemByItemToFileInoMatch(testLibrary.id, fullPath, true)
|
||||
expect(result).to.not.be.null
|
||||
expect(result.libraryFiles[0].metadata.path).to.equal(fullPath)
|
||||
expect(result.libraryFiles[0].deviceId).to.equal(fileInfo.dev)
|
||||
})
|
||||
|
||||
it('findsTheCorrectItemByInodeAndDeviceIdWhenThereAreDuplicateInodes', async () => {
|
||||
let findLibraryItemByItemToFileInoMatch = LibraryScanner.__get__('findLibraryItemByItemToFileInoMatch')
|
||||
let fullPath = '/mnt/drive/file-same-ino-different-dev.pdf'
|
||||
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
const fileInfo = mockFileInfo.get(fullPath)
|
||||
|
||||
/** @type {Promise<import('../../../server/models/LibraryItem') | null>} */
|
||||
const result = await findLibraryItemByItemToFileInoMatch(testLibrary.id, fullPath, true)
|
||||
expect(result).to.not.be.null
|
||||
expect(result.libraryFiles[0].metadata.path).to.equal(fullPath)
|
||||
expect(result.libraryFiles[0].deviceId).to.equal(fileInfo.dev)
|
||||
})
|
||||
|
||||
it('findLibraryItemByItemToItemInoMatch', async function () {
|
||||
this.timeout(0)
|
||||
// findLibraryItemByItemToItemInoMatch(libraryId, fullPath)
|
||||
let findLibraryItemByItemToItemInoMatch = LibraryScanner.__get__('findLibraryItemByItemToItemInoMatch')
|
||||
|
||||
let fullPath = '/test/file.pdf'
|
||||
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
const fileInfo = mockFileInfo.get(fullPath)
|
||||
|
||||
/** @returns {Promise<import('../../../server/models/LibraryItem') | null>} */
|
||||
const result = await findLibraryItemByItemToItemInoMatch(testLibrary.id, fullPath)
|
||||
expect(result).to.not.be.null
|
||||
expect(result.libraryFiles[0].metadata.path).to.equal(fullPath)
|
||||
expect(result.libraryFiles[0].deviceId).to.equal(fileInfo.dev)
|
||||
})
|
||||
|
||||
it('findLibraryItemByFileToItemInoMatch-matchesRenamedFileByInoAndDeviceId', async function () {
|
||||
this.timeout(0)
|
||||
let mockFileInfo = getMockBookFileInfo()
|
||||
sinon.restore()
|
||||
stubFileUtils(mockFileInfo)
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
// findLibraryItemByFileToItemInoMatch(libraryId, fullPath, isSingleMedia, itemFiles)
|
||||
let findLibraryItemByItemToItemInoMatch = LibraryScanner.__get__('findLibraryItemByFileToItemInoMatch')
|
||||
|
||||
let bookFolderPath = '/test/bookfolder'
|
||||
|
||||
/**
|
||||
* @param {UUIDV4} libraryId
|
||||
* @param {string} fullPath
|
||||
* @param {boolean} isSingleMedia
|
||||
* @param {string[]} itemFiles
|
||||
* @returns {Promise<import('../models/LibraryItem').LibraryItemExpanded | null>} library item that matches
|
||||
*/
|
||||
const existingItem = await findLibraryItemByItemToItemInoMatch(testLibrary.id, bookFolderPath, false, ['file.epub', 'file-renamed.epub', 'file.opf'])
|
||||
|
||||
expect(existingItem).to.not.be.null
|
||||
expect(existingItem.ino).to.equal('1')
|
||||
expect(existingItem.deviceId).to.equal('100')
|
||||
})
|
||||
|
||||
it('findLibraryItemByFileToItemInoMatch-DoesNotMatchByInoAndDifferentDeviceId', async function () {
|
||||
this.timeout(0)
|
||||
testLibrary = await loadTestDatabase()
|
||||
|
||||
// findLibraryItemByFileToItemInoMatch(libraryId, fullPath, isSingleMedia, itemFiles)
|
||||
let findLibraryItemByItemToItemInoMatch = LibraryScanner.__get__('findLibraryItemByFileToItemInoMatch')
|
||||
|
||||
let bookFolderPath = '/test/bookfolder'
|
||||
|
||||
/**
|
||||
* @param {UUIDV4} libraryId
|
||||
* @param {string} fullPath
|
||||
* @param {boolean} isSingleMedia
|
||||
* @param {string[]} itemFiles
|
||||
* @returns {Promise<import('../models/LibraryItem').LibraryItemExpanded | null>} library item that matches
|
||||
*/
|
||||
const existingItem = await findLibraryItemByItemToItemInoMatch(testLibrary.id, bookFolderPath, false, ['file.epub', 'different-file.epub', 'file.opf'])
|
||||
|
||||
expect(existingItem).to.be.null
|
||||
})
|
||||
|
||||
/** @returns {Map<string, import('fs').Stats>} */
|
||||
function getMockBookFileInfo() {
|
||||
// @ts-ignore
|
||||
return new Map([
|
||||
['/test/bookfolder/file-renamed.epub', { path: '/test/bookfolder/file-renamed.epub', isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '100' }],
|
||||
['/test/bookfolder/file.epub', { path: '/test/bookfolder/file.epub', isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '100' }],
|
||||
['/test/bookfolder/different-file.epub', { path: '/test/bookfolder/different-file.epub', isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '200' }],
|
||||
['/test/bookfolder/file.opf', { path: '/test/bookfolder/file.opf', isDirectory: () => false, size: 42, mtimeMs: Date.now(), ino: '2', dev: '100' }]
|
||||
])
|
||||
}
|
||||
|
||||
// ItemToFileInoMatch
|
||||
it('ItemToFileInoMatch-ItemMatchesSelf', async function () {
|
||||
this.timeout(0)
|
||||
/**
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem1
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem2
|
||||
*/
|
||||
let ItemToFileInoMatch = LibraryScanner.__get__('ItemToFileInoMatch')
|
||||
|
||||
// this compares the inode from the first library item to the second library item's library file inode
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
const fileInfo = mockFileInfo.get('/test/file.pdf')
|
||||
|
||||
let item1 = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
// @ts-ignore
|
||||
ino: fileInfo.ino
|
||||
})
|
||||
|
||||
expect(ItemToFileInoMatch(item1, item1)).to.be.true
|
||||
})
|
||||
|
||||
it('ItemToFileInoMatch-TwoItemsWithSameInoButDifferentDeviceShouldNotMatch', async function () {
|
||||
this.timeout(0)
|
||||
/**
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem1
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem2
|
||||
*/
|
||||
let ItemToFileInoMatch = LibraryScanner.__get__('ItemToFileInoMatch')
|
||||
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
// this compares the inode from the first library item to the second library item's library file inode
|
||||
const item1 = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
path: '/test/file.pdf'
|
||||
})
|
||||
|
||||
const item2 = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
path: '/mnt/drive/file-same-ino-different-dev.pdf'
|
||||
})
|
||||
|
||||
expect(item1.path).to.not.equal(item2.path)
|
||||
|
||||
expect(ItemToFileInoMatch(item1, item2)).to.be.false
|
||||
})
|
||||
|
||||
it('ItemToFileInoMatch-RenamedFileShouldMatch', async function () {
|
||||
this.timeout(0)
|
||||
/**
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem1
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem2
|
||||
*/
|
||||
let ItemToFileInoMatch = LibraryScanner.__get__('ItemToFileInoMatch')
|
||||
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
// this compares the inode from the first library item to the second library item's library file inode
|
||||
const original = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
path: '/test/file.pdf'
|
||||
})
|
||||
|
||||
const renamedMockFileInfo = getRenamedMockFileInfo().get('/test/file-renamed.pdf')
|
||||
const renamedFile = new LibraryFile()
|
||||
var fileMetadata = new FileMetadata()
|
||||
fileMetadata.setData(renamedMockFileInfo)
|
||||
fileMetadata.filename = Path.basename(renamedMockFileInfo.path)
|
||||
fileMetadata.path = fileUtils.filePathToPOSIX(renamedMockFileInfo.path)
|
||||
fileMetadata.relPath = fileUtils.filePathToPOSIX(renamedMockFileInfo.path)
|
||||
fileMetadata.ext = Path.extname(renamedMockFileInfo.path)
|
||||
renamedFile.ino = renamedMockFileInfo.ino
|
||||
renamedFile.deviceId = renamedMockFileInfo.dev
|
||||
renamedFile.metadata = fileMetadata
|
||||
renamedFile.addedAt = Date.now()
|
||||
renamedFile.updatedAt = Date.now()
|
||||
renamedFile.metadata = fileMetadata
|
||||
|
||||
const renamedItem = new LibraryItem(buildBookLibraryItemParams(renamedFile, null, testLibrary.id, null))
|
||||
|
||||
expect(ItemToFileInoMatch(original, renamedItem)).to.be.true
|
||||
})
|
||||
|
||||
// ItemToItemInoMatch
|
||||
it('ItemToItemInoMatch-ItemMatchesSelf', async function () {
|
||||
this.timeout(0)
|
||||
/**
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem1
|
||||
* @param {import("../../../server/models/LibraryItem") | import("../../../server/scanner/LibraryItemScanData")} libraryItem2
|
||||
*/
|
||||
let ItemToItemInoMatch = LibraryScanner.__get__('ItemToItemInoMatch')
|
||||
|
||||
// this compares the inode from the first library item to the second library item's library file inode
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
const fileInfo = mockFileInfo.get('/test/file.pdf')
|
||||
|
||||
let item1 = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
// @ts-ignore
|
||||
ino: fileInfo.ino
|
||||
})
|
||||
|
||||
expect(ItemToItemInoMatch(item1, item1)).to.be.true
|
||||
})
|
||||
|
||||
it('ItemToItemInoMatch-TwoItemsWithSameInoButDifferentDeviceShouldNotMatch', async () => {
|
||||
let ItemToItemInoMatch = LibraryScanner.__get__('ItemToItemInoMatch')
|
||||
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
// this compares the inode from the first library item to the second library item's library file inode
|
||||
const item1 = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
path: '/test/file.pdf'
|
||||
})
|
||||
|
||||
const item2 = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
path: '/mnt/drive/file-same-ino-different-dev.pdf'
|
||||
})
|
||||
|
||||
expect(item1.path).to.not.equal(item2.path)
|
||||
|
||||
expect(ItemToItemInoMatch(item1, item2)).to.be.false
|
||||
})
|
||||
|
||||
it('ItemToItemInoMatch-RenamedFileShouldMatch', async () => {
|
||||
let ItemToItemInoMatch = LibraryScanner.__get__('ItemToItemInoMatch')
|
||||
|
||||
let mockFileInfo = getMockFileInfo()
|
||||
testLibrary = await loadTestDatabase(mockFileInfo)
|
||||
|
||||
// this compares the inode from the first library item to the second library item's library file inode
|
||||
const original = await Database.libraryItemModel.findOneExpanded({
|
||||
libraryId: testLibrary.id,
|
||||
path: '/test/file.pdf'
|
||||
})
|
||||
|
||||
const renamedMockFileInfo = getRenamedMockFileInfo().get('/test/file-renamed.pdf')
|
||||
const renamedFile = new LibraryFile()
|
||||
var fileMetadata = new FileMetadata()
|
||||
fileMetadata.setData(renamedMockFileInfo)
|
||||
fileMetadata.filename = Path.basename(renamedMockFileInfo.path)
|
||||
fileMetadata.path = fileUtils.filePathToPOSIX(renamedMockFileInfo.path)
|
||||
fileMetadata.relPath = fileUtils.filePathToPOSIX(renamedMockFileInfo.path)
|
||||
fileMetadata.ext = Path.extname(renamedMockFileInfo.path)
|
||||
renamedFile.ino = renamedMockFileInfo.ino
|
||||
renamedFile.deviceId = renamedMockFileInfo.dev
|
||||
renamedFile.metadata = fileMetadata
|
||||
renamedFile.addedAt = Date.now()
|
||||
renamedFile.updatedAt = Date.now()
|
||||
renamedFile.metadata = fileMetadata
|
||||
|
||||
const renamedItem = new LibraryItem(buildBookLibraryItemParams(renamedFile, null, testLibrary.id, null))
|
||||
|
||||
expect(ItemToItemInoMatch(original, renamedItem)).to.be.true
|
||||
})
|
||||
|
||||
describe('createLibraryItemScanData', () => {
|
||||
it('createLibraryItemScanDataSetsDeviceId', async () => {
|
||||
/**
|
||||
* @param {{ id: any; libraryId: any; }} folder
|
||||
* @param {{ mediaType: any; }} library
|
||||
* @param {{ ino: any; dev: any; mtimeMs: any; ctimeMs: any; birthtimeMs: any; }} libraryItemFolderStats
|
||||
* @param {{ path: any; relPath: any; mediaMetadata: any; }} libraryItemData
|
||||
* @param {any} isFile
|
||||
* @param {any} fileObjs
|
||||
* @returns {LibraryItemScanData} new object
|
||||
*/
|
||||
const createLibraryItemScanData = LibraryScanner.__get__('createLibraryItemScanData')
|
||||
|
||||
const liFolderStats = { path: '/library/book/file.epub', isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '1000' }
|
||||
const lf_properties = buildLibraryFileProperties('/library/book/file.epub', '1', '1000')
|
||||
const libraryFile = new LibraryFile(lf_properties)
|
||||
|
||||
const lisd = createLibraryItemScanData({ id: 'foo', libraryId: 'bar' }, { mediaType: 'ebook' }, liFolderStats, lf_properties, true, [libraryFile.toJSON()])
|
||||
|
||||
expect(lisd).to.not.be.null
|
||||
expect(lisd.ino).to.equal(liFolderStats.ino)
|
||||
expect(lisd.deviceId).to.equal(liFolderStats.dev)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
@ -3,8 +3,13 @@ const expect = chai.expect
|
|||
const sinon = require('sinon')
|
||||
const fileUtils = require('../../../server/utils/fileUtils')
|
||||
const fs = require('fs')
|
||||
const fsextra = require('../../../server/libs/fsExtra')
|
||||
const Logger = require('../../../server/Logger')
|
||||
|
||||
/**
|
||||
* @typedef {import('../../../server/libs/fsExtra').fsExtra} fsextra
|
||||
*/
|
||||
|
||||
describe('fileUtils', () => {
|
||||
it('shouldIgnoreFile', () => {
|
||||
global.isWin = process.platform === 'win32'
|
||||
|
|
@ -39,6 +44,46 @@ describe('fileUtils', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('fsextra', () => {
|
||||
let statStub
|
||||
|
||||
beforeEach(() => {
|
||||
// two files with same indoe but different device ID
|
||||
const mockStats = new Map([
|
||||
['/test/file1.mp3', { isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '100' }],
|
||||
['/mnt/other/file2.txt', { isDirectory: () => false, size: 512, mtimeMs: Date.now(), ino: '1', dev: '200' }]
|
||||
])
|
||||
|
||||
statStub = sinon.stub(fsextra, 'stat')
|
||||
statStub.callsFake((path) => {
|
||||
const normalizedPath = fileUtils.filePathToPOSIX(path).replace(/\/$/, '')
|
||||
const stats = mockStats.get(normalizedPath)
|
||||
if (stats) {
|
||||
return stats
|
||||
} else {
|
||||
new Error(`ENOENT: no such file or directory, stat '${normalizedPath}'`)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
after(() => {
|
||||
fsextra.stat.restore()
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('shouldGetDeviceIdForFile', async () => {
|
||||
const id = await fileUtils.getDeviceId('/test/file1.mp3')
|
||||
|
||||
expect(id).to.be.an('string')
|
||||
|
||||
const id2 = await fileUtils.getDeviceId('/mnt/other/file2.txt')
|
||||
|
||||
expect(id2).to.be.an('string')
|
||||
|
||||
expect(id).to.not.equal(id2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('recurseFiles', () => {
|
||||
let readdirStub, realpathStub, statStub
|
||||
|
||||
|
|
@ -53,7 +98,7 @@ describe('fileUtils', () => {
|
|||
])
|
||||
|
||||
const mockStats = new Map([
|
||||
['/test/file1.mp3', { isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1' }],
|
||||
['/test/file1.mp3', { isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '1', dev: '100' }],
|
||||
['/test/subfolder', { isDirectory: () => true, size: 0, mtimeMs: Date.now(), ino: '2' }],
|
||||
['/test/subfolder/file2.m4b', { isDirectory: () => false, size: 1024, mtimeMs: Date.now(), ino: '3' }],
|
||||
['/test/ignoreme', { isDirectory: () => true, size: 0, mtimeMs: Date.now(), ino: '4' }],
|
||||
|
|
@ -98,6 +143,9 @@ describe('fileUtils', () => {
|
|||
})
|
||||
|
||||
afterEach(() => {
|
||||
fs.stat.restore()
|
||||
fs.realpath.restore()
|
||||
fs.readdir.restore()
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
|
|
@ -105,6 +153,7 @@ describe('fileUtils', () => {
|
|||
const files = await fileUtils.recurseFiles('/test')
|
||||
expect(files).to.be.an('array')
|
||||
expect(files).to.have.lengthOf(3)
|
||||
expect(statStub.called).to.be.true
|
||||
|
||||
expect(files[0]).to.deep.equal({
|
||||
name: 'file1.mp3',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue