From cf1f162aba82a36f99b721524d29274c92cdfcf9 Mon Sep 17 00:00:00 2001 From: mikiher Date: Mon, 9 Nov 2026 11:36:53 +0200 Subject: [PATCH 1/5] Fix parallel quick match crash on duplicate bookSeries/bookAuthor links --- server/scanner/Scanner.js | 39 ++++++++---- test/server/scanner/Scanner.test.js | 97 +++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 test/server/scanner/Scanner.test.js diff --git a/server/scanner/Scanner.js b/server/scanner/Scanner.js index af4405987..f125b6283 100644 --- a/server/scanner/Scanner.js +++ b/server/scanner/Scanner.js @@ -260,16 +260,21 @@ class Scanner { // Update filter data Database.addAuthorToFilterData(libraryItem.libraryId, author.name, author.id) } - await Database.bookAuthorModel - .create({ + let bookAuthorCreated = true + try { + await Database.bookAuthorModel.create({ authorId: author.id, bookId: libraryItem.media.id }) - .then(() => { - Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added author "${author.name}" to "${libraryItem.media.title}"`) - libraryItem.media.authors.push(author) - hasAuthorUpdates = true - }) + } catch (error) { + if (error.name !== 'SequelizeUniqueConstraintError') throw error + bookAuthorCreated = false + } + libraryItem.media.authors.push(author) + hasAuthorUpdates = true + if (bookAuthorCreated) { + Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added author "${author.name}" to "${libraryItem.media.title}"`) + } } const authorsRemoved = libraryItem.media.authors.filter((a) => !matchData.author.find((ma) => ma.toLowerCase() === a.name.toLowerCase())) if (authorsRemoved.length) { @@ -315,14 +320,22 @@ class Scanner { Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id) SocketAuthority.emitter('series_added', seriesItem.toOldJSON()) } - const bookSeries = await Database.bookSeriesModel.create({ - seriesId: seriesItem.id, - bookId: libraryItem.media.id, - sequence: seriesMatchItem.sequence - }) + let bookSeries + try { + bookSeries = await Database.bookSeriesModel.create({ + seriesId: seriesItem.id, + bookId: libraryItem.media.id, + sequence: seriesMatchItem.sequence + }) + Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added series "${seriesItem.name}" to "${libraryItem.media.title}"`) + } catch (error) { + if (error.name !== 'SequelizeUniqueConstraintError') throw error + bookSeries = await Database.bookSeriesModel.findOne({ + where: { seriesId: seriesItem.id, bookId: libraryItem.media.id } + }) + } seriesItem.bookSeries = bookSeries libraryItem.media.series.push(seriesItem) - Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added series "${seriesItem.name}" to "${libraryItem.media.title}"`) hasSeriesUpdates = true } const seriesRemoved = libraryItem.media.series.filter((s) => !matchData.series.find((ms) => ms.series.toLowerCase() === s.name.toLowerCase())) diff --git a/test/server/scanner/Scanner.test.js b/test/server/scanner/Scanner.test.js new file mode 100644 index 000000000..703a7780c --- /dev/null +++ b/test/server/scanner/Scanner.test.js @@ -0,0 +1,97 @@ +const { expect } = require('chai') +const { Sequelize } = require('sequelize') +const sinon = require('sinon') + +const Database = require('../../../server/Database') +const Logger = require('../../../server/Logger') +const Scanner = require('../../../server/scanner/Scanner') + +describe('Scanner.quickMatchBookBuildUpdatePayload', () => { + /** @type {import('../../../server/models/Library')} */ + let library + /** @type {import('../../../server/models/LibraryItem')} */ + let libraryItem + /** @type {import('../../../server/models/Book')} */ + let book + + const apiRouterCtx = { + checkRemoveAuthorsWithNoBooks: sinon.stub().resolves(), + checkRemoveEmptySeries: sinon.stub().resolves() + } + + beforeEach(async () => { + global.ServerSettings = {} + Database.sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false }) + Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '') + await Database.buildModels() + + sinon.stub(Logger, 'info') + sinon.stub(Logger, 'debug') + sinon.stub(Logger, 'warn') + sinon.stub(Logger, 'error') + + library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' }) + const libraryFolder = await Database.libraryFolderModel.create({ path: '/test', libraryId: library.id }) + book = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] }) + libraryItem = await Database.libraryItemModel.create({ + libraryFiles: [], + mediaId: book.id, + mediaType: 'book', + libraryId: library.id, + libraryFolderId: libraryFolder.id + }) + libraryItem.media = book + // Stale in-memory associations: DB may already have links from a concurrent quick match + book.authors = [] + book.series = [] + }) + + afterEach(async () => { + sinon.restore() + await Database.sequelize.sync({ force: true }) + }) + + it('does not throw when bookSeries link already exists from a concurrent quick match', async () => { + const series = await Database.seriesModel.create({ name: 'Crescent City', libraryId: library.id }) + await Database.bookSeriesModel.create({ bookId: book.id, seriesId: series.id, sequence: '3' }) + + const result = await Scanner.quickMatchBookBuildUpdatePayload( + apiRouterCtx, + libraryItem, + { + series: [{ series: 'Crescent City', sequence: '3' }] + }, + {} + ) + + expect(result.hasSeriesUpdates).to.be.true + expect(libraryItem.media.series).to.have.lengthOf(1) + expect(libraryItem.media.series[0].id).to.equal(series.id) + expect(libraryItem.media.series[0].bookSeries).to.exist + expect(libraryItem.media.series[0].bookSeries.sequence).to.equal('3') + + const links = await Database.bookSeriesModel.findAll({ where: { bookId: book.id, seriesId: series.id } }) + expect(links).to.have.lengthOf(1) + }) + + it('does not throw when bookAuthor link already exists from a concurrent quick match', async () => { + const author = await Database.authorModel.create({ name: 'Sarah Maas', libraryId: library.id }) + await Database.bookAuthorModel.create({ bookId: book.id, authorId: author.id }) + + const result = await Scanner.quickMatchBookBuildUpdatePayload( + apiRouterCtx, + libraryItem, + { + author: ['Sarah Maas'] + }, + {} + ) + + expect(result.hasAuthorUpdates).to.be.true + expect(libraryItem.media.authors).to.have.lengthOf(1) + expect(libraryItem.media.authors[0].id).to.equal(author.id) + + const links = await Database.bookAuthorModel.findAll({ where: { bookId: book.id, authorId: author.id } }) + expect(links).to.have.lengthOf(1) + }) +}) From 8af3448ac0e24297b3839435cf028c8f763ed8eb Mon Sep 17 00:00:00 2001 From: mikiher Date: Mon, 9 Nov 2026 17:29:02 +0200 Subject: [PATCH 2/5] Fix quick match crash when the series row already exists --- server/scanner/Scanner.js | 28 ++++++++++++++++++++-------- test/server/scanner/Scanner.test.js | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/server/scanner/Scanner.js b/server/scanner/Scanner.js index f125b6283..151c83e3e 100644 --- a/server/scanner/Scanner.js +++ b/server/scanner/Scanner.js @@ -311,14 +311,26 @@ class Scanner { } else { let seriesItem = await Database.seriesModel.getByNameAndLibrary(seriesMatchItem.series, libraryItem.libraryId) if (!seriesItem) { - seriesItem = await Database.seriesModel.create({ - name: seriesMatchItem.series, - nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series), - libraryId: libraryItem.libraryId - }) - // Update filter data - Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id) - SocketAuthority.emitter('series_added', seriesItem.toOldJSON()) + let seriesCreated = false + try { + seriesItem = await Database.seriesModel.create({ + name: seriesMatchItem.series, + nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series), + libraryId: libraryItem.libraryId + }) + seriesCreated = true + } catch (error) { + if (error.name !== 'SequelizeUniqueConstraintError') throw error + seriesItem = await Database.seriesModel.findOne({ + where: { name: seriesMatchItem.series, libraryId: libraryItem.libraryId } + }) + if (!seriesItem) throw error + } + if (seriesCreated) { + // Update filter data + Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id) + SocketAuthority.emitter('series_added', seriesItem.toOldJSON()) + } } let bookSeries try { diff --git a/test/server/scanner/Scanner.test.js b/test/server/scanner/Scanner.test.js index 703a7780c..5d0210eba 100644 --- a/test/server/scanner/Scanner.test.js +++ b/test/server/scanner/Scanner.test.js @@ -74,6 +74,29 @@ describe('Scanner.quickMatchBookBuildUpdatePayload', () => { expect(links).to.have.lengthOf(1) }) + it('reuses an existing series when create hits the name unique constraint', async () => { + const series = await Database.seriesModel.create({ name: 'Crescent City', libraryId: library.id }) + sinon.stub(Database.seriesModel, 'getByNameAndLibrary').resolves(null) + + const result = await Scanner.quickMatchBookBuildUpdatePayload( + apiRouterCtx, + libraryItem, + { + series: [{ series: 'Crescent City', sequence: '3' }] + }, + {} + ) + + expect(result.hasSeriesUpdates).to.be.true + expect(libraryItem.media.series).to.have.lengthOf(1) + expect(libraryItem.media.series[0].id).to.equal(series.id) + + const seriesRows = await Database.seriesModel.findAll({ where: { libraryId: library.id, name: 'Crescent City' } }) + expect(seriesRows).to.have.lengthOf(1) + const links = await Database.bookSeriesModel.findAll({ where: { bookId: book.id, seriesId: series.id } }) + expect(links).to.have.lengthOf(1) + }) + it('does not throw when bookAuthor link already exists from a concurrent quick match', async () => { const author = await Database.authorModel.create({ name: 'Sarah Maas', libraryId: library.id }) await Database.bookAuthorModel.create({ bookId: book.id, authorId: author.id }) From e9ff70e522b5675826539c8250a32916dc7303e9 Mon Sep 17 00:00:00 2001 From: mikiher Date: Tue, 10 Nov 2026 08:19:33 +0200 Subject: [PATCH 3/5] Fix collection add crash when the book is already in the collection --- server/controllers/CollectionController.js | 17 +++-- .../controllers/CollectionController.test.js | 74 +++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 test/server/controllers/CollectionController.test.js diff --git a/server/controllers/CollectionController.js b/server/controllers/CollectionController.js index f01ce6664..e39e34f9f 100644 --- a/server/controllers/CollectionController.js +++ b/server/controllers/CollectionController.js @@ -247,12 +247,17 @@ class CollectionController { return res.status(400).send('Book already in collection') } - // Create collectionBook record - await Database.collectionBookModel.create({ - collectionId: req.collection.id, - bookId: libraryItem.mediaId, - order: collectionBooks.length + 1 - }) + // Create collectionBook record. A second add can pass the check above and hit the unique pair. + try { + await Database.collectionBookModel.create({ + collectionId: req.collection.id, + bookId: libraryItem.mediaId, + order: collectionBooks.length + 1 + }) + } catch (error) { + if (error.name !== 'SequelizeUniqueConstraintError') throw error + return res.status(400).send('Book already in collection') + } const jsonExpanded = await req.collection.getOldJsonExpanded(req.user) if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) res.json(jsonExpanded) diff --git a/test/server/controllers/CollectionController.test.js b/test/server/controllers/CollectionController.test.js new file mode 100644 index 000000000..dcef3856c --- /dev/null +++ b/test/server/controllers/CollectionController.test.js @@ -0,0 +1,74 @@ +const { expect } = require('chai') +const { Sequelize } = require('sequelize') +const sinon = require('sinon') + +const Database = require('../../../server/Database') +const Logger = require('../../../server/Logger') +const CollectionController = require('../../../server/controllers/CollectionController') + +describe('CollectionController.addBook', () => { + /** @type {import('../../../server/models/Collection')} */ + let collection + /** @type {import('../../../server/models/LibraryItem')} */ + let libraryItem + + beforeEach(async () => { + global.ServerSettings = {} + Database.sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false }) + Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '') + await Database.buildModels() + + sinon.stub(Logger, 'info') + sinon.stub(Logger, 'debug') + sinon.stub(Logger, 'warn') + sinon.stub(Logger, 'error') + + const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' }) + const libraryFolder = await Database.libraryFolderModel.create({ path: '/test-lib', libraryId: library.id }) + const book = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] }) + libraryItem = await Database.libraryItemModel.create({ + path: '/test-lib/book', + isFile: false, + libraryFiles: [], + mediaId: book.id, + mediaType: 'book', + libraryId: library.id, + libraryFolderId: libraryFolder.id + }) + collection = await Database.collectionModel.create({ name: 'Test Collection', libraryId: library.id }) + await Database.collectionBookModel.create({ collectionId: collection.id, bookId: book.id, order: 1 }) + }) + + afterEach(async () => { + sinon.restore() + await Database.sequelize.sync({ force: true }) + }) + + it('does not throw when the collectionBook link already exists', async () => { + sinon.stub(collection, 'getCollectionBooks').resolves([]) + + const res = { + statusCode: null, + body: null, + status(code) { + this.statusCode = code + return this + }, + send(body) { + this.body = body + return this + }, + json(body) { + this.body = body + return this + } + } + + await CollectionController.addBook({ body: { id: libraryItem.id }, collection, user: { id: 'user-id' } }, res) + + expect(res.statusCode).to.equal(400) + expect(res.body).to.equal('Book already in collection') + const links = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } }) + expect(links).to.have.lengthOf(1) + }) +}) From bf1c5688b90e873d50385b8cffbb5c1d0bf87b28 Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 18 Nov 2026 06:52:42 +0200 Subject: [PATCH 4/5] Sanitize author descriptions before they are stored or returned --- server/controllers/AuthorController.js | 26 +++- server/models/Author.js | 8 +- .../controllers/AuthorController.test.js | 139 ++++++++++++++++++ 3 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 test/server/controllers/AuthorController.test.js diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index 85af70df5..db4561904 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -11,11 +11,24 @@ const CoverManager = require('../managers/CoverManager') const AuthorFinder = require('../finders/AuthorFinder') const { reqSupportsWebp, isValidASIN, clampPositiveInt } = require('../utils/index') +const htmlSanitizer = require('../utils/htmlSanitizer') const naturalSort = createNewSortInstance({ comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare }) +/** + * @param {string} description + * @returns {string|null} + */ +function sanitizeAuthorDescription(description) { + const sanitizedDescription = htmlSanitizer.sanitize(description) + if (sanitizedDescription !== description) { + Logger.debug(`[AuthorController] Sanitized description from "${description}" to "${sanitizedDescription}"`) + } + return sanitizedDescription || null +} + /** * @typedef RequestUserObject * @property {import('../models/User')} user @@ -101,6 +114,10 @@ class AuthorController { payload[key] = req.body[key] } } + if (payload.description) { + payload.description = sanitizeAuthorDescription(payload.description) + } + if (!Object.keys(payload).length) { Logger.error(`[AuthorController] Invalid request payload. No valid keys found`, req.body) return res.status(400).send('Invalid request payload. No valid keys found') @@ -364,9 +381,12 @@ class AuthorController { } } - if (authorData.description && req.author.description !== authorData.description) { - req.author.description = authorData.description - hasUpdates = true + if (authorData.description) { + const description = sanitizeAuthorDescription(authorData.description) + if (req.author.description !== description) { + req.author.description = description + hasUpdates = true + } } if (hasUpdates) { diff --git a/server/models/Author.js b/server/models/Author.js index 65561e211..525f557a8 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -1,5 +1,6 @@ const { DataTypes, Model, where, fn, col } = require('sequelize') const parseNameString = require('../utils/parsers/parseNameString') +const htmlSanitizer = require('../utils/htmlSanitizer') class Author extends Model { constructor(values, options) { @@ -175,11 +176,16 @@ class Author extends Model { } toOldJSON() { + let description = this.description + if (description) { + description = htmlSanitizer.sanitize(description) || null + } + return { id: this.id, asin: this.asin, name: this.name, - description: this.description, + description, imagePath: this.imagePath, libraryId: this.libraryId, addedAt: this.createdAt.valueOf(), diff --git a/test/server/controllers/AuthorController.test.js b/test/server/controllers/AuthorController.test.js new file mode 100644 index 000000000..b9a46a277 --- /dev/null +++ b/test/server/controllers/AuthorController.test.js @@ -0,0 +1,139 @@ +const { expect } = require('chai') +const { Sequelize } = require('sequelize') +const sinon = require('sinon') + +const Database = require('../../../server/Database') +const AuthorController = require('../../../server/controllers/AuthorController') +const AuthorFinder = require('../../../server/finders/AuthorFinder') +const Logger = require('../../../server/Logger') + +const unsafeDescription = '

Hello

link' +const safeDescription = '

Hello

link' + +describe('AuthorController', () => { + beforeEach(async () => { + global.ServerSettings = {} + Database.sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false }) + Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '') + await Database.buildModels() + + sinon.stub(Logger, 'info') + sinon.stub(Logger, 'debug') + }) + + afterEach(async () => { + sinon.restore() + + // Clear all tables + await Database.sequelize.sync({ force: true }) + }) + + /** + * @param {string} [description] + */ + async function createAuthor(description) { + const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' }) + return Database.authorModel.create({ + name: 'Test Author', + libraryId: library.id, + description: description || null + }) + } + + describe('update', () => { + it('should store and return a sanitized description', async () => { + const author = await createAuthor() + const fakeReq = { + body: { + description: unsafeDescription + }, + author + } + const fakeRes = { + status: sinon.stub().returnsThis(), + send: sinon.spy(), + json: sinon.spy() + } + + await AuthorController.update(fakeReq, fakeRes) + + expect(fakeRes.json.calledOnce).to.be.true + const response = fakeRes.json.firstCall.args[0] + expect(response.updated).to.be.true + expect(response.author.description).to.equal(safeDescription) + expect(response.author.description).to.not.include('script') + expect(response.author.description).to.not.include('onerror') + + const stored = await Database.authorModel.findByPk(author.id) + expect(stored.description).to.equal(safeDescription) + }) + + it('should store null when sanitizing leaves an empty description', async () => { + const author = await createAuthor('

Existing

') + const fakeReq = { + body: { + description: '' + }, + author + } + const fakeRes = { + json: sinon.spy() + } + + await AuthorController.update(fakeReq, fakeRes) + + const response = fakeRes.json.firstCall.args[0] + expect(response.author.description).to.equal(null) + + const stored = await Database.authorModel.findByPk(author.id) + expect(stored.description).to.equal(null) + }) + }) + + describe('toOldJSON', () => { + it('should sanitize a stored description without rewriting the row', async () => { + const author = await createAuthor(unsafeDescription) + + expect(author.toOldJSON().description).to.equal(safeDescription) + expect(author.description).to.equal(unsafeDescription) + + const stored = await Database.authorModel.findByPk(author.id) + expect(stored.description).to.equal(unsafeDescription) + expect(stored.toOldJSON().description).to.equal(safeDescription) + }) + }) + + describe('match', () => { + it('should store the sanitized Audnexus description', async () => { + const author = await createAuthor() + sinon.stub(AuthorFinder, 'findAuthorByASIN').resolves({ + asin: 'B00TEST000', + name: 'Test Author', + description: '

Bio

', + image: null + }) + + const fakeReq = { + body: { + asin: 'B00TEST000' + }, + author + } + const fakeRes = { + status: sinon.stub().returnsThis(), + send: sinon.spy(), + json: sinon.spy() + } + + await AuthorController.match(fakeReq, fakeRes) + + expect(fakeRes.json.calledOnce).to.be.true + const response = fakeRes.json.firstCall.args[0] + expect(response.updated).to.be.true + expect(response.author.description).to.equal('

Bio

') + + const stored = await Database.authorModel.findByPk(author.id) + expect(stored.description).to.equal('

Bio

') + }) + }) +}) From 5de038dafda294d13ca48861eda7d4405bbd7368 Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 19 Nov 2026 17:39:34 -0600 Subject: [PATCH 5/5] Fix batch add collection endpoint to ignore duplicates, matching fix for #5716 --- server/controllers/CollectionController.js | 2 +- .../controllers/CollectionController.test.js | 41 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/server/controllers/CollectionController.js b/server/controllers/CollectionController.js index e0814babc..a8d912a8d 100644 --- a/server/controllers/CollectionController.js +++ b/server/controllers/CollectionController.js @@ -363,7 +363,7 @@ class CollectionController { let jsonExpanded = null if (hasUpdated) { - await Database.collectionBookModel.bulkCreate(collectionBooksToAdd) + await Database.collectionBookModel.bulkCreate(collectionBooksToAdd, { ignoreDuplicates: true }) jsonExpanded = await req.collection.getOldJsonExpanded(req.user) if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) diff --git a/test/server/controllers/CollectionController.test.js b/test/server/controllers/CollectionController.test.js index dcef3856c..f57bca673 100644 --- a/test/server/controllers/CollectionController.test.js +++ b/test/server/controllers/CollectionController.test.js @@ -6,7 +6,7 @@ const Database = require('../../../server/Database') const Logger = require('../../../server/Logger') const CollectionController = require('../../../server/controllers/CollectionController') -describe('CollectionController.addBook', () => { +describe('CollectionController', () => { /** @type {import('../../../server/models/Collection')} */ let collection /** @type {import('../../../server/models/LibraryItem')} */ @@ -71,4 +71,43 @@ describe('CollectionController.addBook', () => { const links = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } }) expect(links).to.have.lengthOf(1) }) + + it('does not throw when a batch includes a book already in the collection', async () => { + const book = await Database.bookModel.create({ title: 'Second Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] }) + const secondItem = await Database.libraryItemModel.create({ + path: '/test-lib/book-2', + isFile: false, + libraryFiles: [], + mediaId: book.id, + mediaType: 'book', + libraryId: libraryItem.libraryId, + libraryFolderId: libraryItem.libraryFolderId + }) + sinon.stub(collection, 'getCollectionBooks').resolves([]) + sinon.stub(collection, 'getOldJsonExpanded').resolves(null) + + const res = { + statusCode: null, + body: null, + status(code) { + this.statusCode = code + return this + }, + send(body) { + this.body = body + return this + }, + json(body) { + this.body = body + return this + } + } + + await CollectionController.addBatch({ body: { books: [libraryItem.id, secondItem.id] }, collection, user: { id: 'user-id' } }, res) + + const existingLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } }) + expect(existingLinks).to.have.lengthOf(1) + const newLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: secondItem.mediaId } }) + expect(newLinks).to.have.lengthOf(1) + }) })