From b7fd3c37b4783654b4ce602c4fa072bfd2662daf Mon Sep 17 00:00:00 2001 From: Jake Bromberg Date: Sat, 21 Feb 2026 08:10:43 -0800 Subject: [PATCH 1/2] fix: prevent double response in addAlbum when artistIdFromName throws Missing return after next(e) caused execution to fall through to res.send(), triggering ERR_HTTP_HEADERS_SENT. Co-authored-by: Cursor --- .../backend/controllers/library.controller.ts | 1 + .../unit/controllers/library.addAlbum.test.ts | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/unit/controllers/library.addAlbum.test.ts diff --git a/apps/backend/controllers/library.controller.ts b/apps/backend/controllers/library.controller.ts index 2210e03..7f11a10 100644 --- a/apps/backend/controllers/library.controller.ts +++ b/apps/backend/controllers/library.controller.ts @@ -46,6 +46,7 @@ export const addAlbum: RequestHandler = async (req: Request ({ + artistIdFromName: jest.fn(), +})); + +import { Request, Response, NextFunction } from 'express'; +import { addAlbum } from '../../../apps/backend/controllers/library.controller'; +import * as libraryService from '../../../apps/backend/services/library.service'; + +function buildReqResNext(body: Record) { + const req = { body } as unknown as Request; + const res = { + status: jest.fn().mockReturnThis(), + send: jest.fn().mockReturnThis(), + json: jest.fn().mockReturnThis(), + } as unknown as Response; + const next = jest.fn() as unknown as NextFunction; + return { req, res, next }; +} + +describe('addAlbum', () => { + it('should not send a response after calling next(e) when artistIdFromName throws', async () => { + const error = new Error('db connection lost'); + (libraryService.artistIdFromName as jest.Mock).mockRejectedValue(error); + + const { req, res, next } = buildReqResNext({ + album_title: 'Test Album', + label: 'Test Label', + genre_id: 1, + format_id: 1, + artist_name: 'Test Artist', + }); + + await addAlbum(req, res, next); + + expect(next).toHaveBeenCalledWith(error); + expect((res.send as jest.Mock)).not.toHaveBeenCalled(); + }); +}); From 252434e44c26868f9fffa9b0adc2331b2297cf73 Mon Sep 17 00:00:00 2001 From: Jake Bromberg Date: Fri, 27 Feb 2026 09:45:11 -0800 Subject: [PATCH 2/2] style: format files with Prettier --- tests/unit/controllers/library.addAlbum.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/controllers/library.addAlbum.test.ts b/tests/unit/controllers/library.addAlbum.test.ts index 5c246c6..34bff30 100644 --- a/tests/unit/controllers/library.addAlbum.test.ts +++ b/tests/unit/controllers/library.addAlbum.test.ts @@ -35,6 +35,6 @@ describe('addAlbum', () => { await addAlbum(req, res, next); expect(next).toHaveBeenCalledWith(error); - expect((res.send as jest.Mock)).not.toHaveBeenCalled(); + expect(res.send as jest.Mock).not.toHaveBeenCalled(); }); });