Added deviceId sequelize migration and completed unit tests

This commit is contained in:
Jason Axley 2025-08-24 15:56:43 -07:00
parent 423f2d311e
commit 41a288bcdf
10 changed files with 451 additions and 29 deletions

View file

@ -86,12 +86,12 @@ function buildBookLibraryItemParams(libraryFile, bookId, libraryId, libraryFolde
}
exports.buildBookLibraryItemParams = buildBookLibraryItemParams
function stubFileUtils() {
function stubFileUtils(mockFileInfo = getMockFileInfo()) {
let getInoStub, getDeviceIdStub, getFileTimestampsWithInoStub
getInoStub = sinon.stub(fileUtils, 'getIno')
getInoStub.callsFake((path) => {
const normalizedPath = fileUtils.filePathToPOSIX(path).replace(/\/$/, '')
const stats = getMockFileInfo().get(normalizedPath)
const stats = mockFileInfo.get(normalizedPath)
if (stats) {
return stats.ino
} else {
@ -102,7 +102,7 @@ function stubFileUtils() {
getDeviceIdStub = sinon.stub(fileUtils, 'getDeviceId')
getDeviceIdStub.callsFake(async (path) => {
const normalizedPath = fileUtils.filePathToPOSIX(path).replace(/\/$/, '')
const stats = getMockFileInfo().get(normalizedPath)
const stats = mockFileInfo.get(normalizedPath)
if (stats) {
return stats.dev
} else {
@ -113,7 +113,7 @@ function stubFileUtils() {
getFileTimestampsWithInoStub = sinon.stub(fileUtils, 'getFileTimestampsWithIno')
getFileTimestampsWithInoStub.callsFake(async (path) => {
const normalizedPath = fileUtils.filePathToPOSIX(path).replace(/\/$/, '')
const stats = getMockFileInfo().get(normalizedPath)
const stats = mockFileInfo.get(normalizedPath)
if (stats) {
return stats
} else {

View file

@ -0,0 +1,169 @@
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.29.0-add-deviceId')
const normalizeWhitespaceAndBackticks = (str) => str.replace(/\s+/g, ' ').trim().replace(/`/g, '')
describe(`Migration ${migrationName}`, () => {
let sequelize
let queryInterface
let loggerInfoStub
beforeEach(async () => {
sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false })
queryInterface = sequelize.getQueryInterface()
loggerInfoStub = sinon.stub(Logger, 'info')
await queryInterface.createTable('libraryItems', {
id: { type: DataTypes.INTEGER, allowNull: false, primaryKey: true, unique: true },
ino: { 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, mediaId: 1, mediaType: 'book', libraryId: 1, ino: '1' },
{ id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, ino: '2' }
])
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
})
/* TODO
it('should populate the deviceId columns from the filesystem for each libraryItem', async () => {
await up({ context: { queryInterface, logger: Logger } })
const [libraryItems] = await queryInterface.sequelize.query('SELECT * FROM libraryItems')
expect(libraryItems).to.deep.equal([
{ id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Smith, John Doe', authorNamesLastFirst: 'Smith, John, Doe, John' },
{ id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'Jane Smith', authorNamesLastFirst: 'Smith, Jane' }
])
})
*/
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: '1', deviceId: null, mediaId: 1, mediaType: 'book', libraryId: 1 },
{ id: 2, ino: '2', deviceId: null, 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, mediaId: 1, mediaType: 'book', libraryId: 1, ino: '1' },
{ id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, ino: '2' }
])
})
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: '1', mediaId: 1, mediaType: 'book', libraryId: 1 },
{ id: 2, ino: '2', 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)
})
})
})

View file

@ -61,7 +61,6 @@ describe('LibraryScanner', () => {
it('findLibraryItemByItemToItemInoMatch', async function () {
this.timeout(0)
// findLibraryItemByItemToItemInoMatch(libraryId, fullPath)
// findLibraryItemByFileToItemInoMatch(libraryId, fullPath, isSingleMedia, itemFiles)
let findLibraryItemByItemToItemInoMatch = LibraryScanner.__get__('findLibraryItemByItemToItemInoMatch')
let fullPath = '/test/file.pdf'
@ -71,13 +70,71 @@ describe('LibraryScanner', () => {
const fileInfo = mockFileInfo.get(fullPath)
/** @type {Promise<import('../../../server/models/LibraryItem') | null>} */
/** @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)