many fixes resulting from latest refactoring
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 8m6s

This commit is contained in:
2025-12-09 04:04:02 -08:00
parent c5d2e4f23e
commit 264404d6e8
13 changed files with 105 additions and 48 deletions

View File

@@ -14,6 +14,7 @@ import type { Profile, ActivityLogItem, SearchQuery } from '../../types';
vi.mock('./shopping.db', () => ({
ShoppingRepository: class {
getShoppingLists = vi.fn();
createShoppingList = vi.fn(); // FIX: Add missing mock for createShoppingList.
},
}));
vi.mock('./personalization.db', () => ({
@@ -90,26 +91,30 @@ describe('User DB Service', () => {
// Arrange: Mock the user insert query to fail after BEGIN and set_config
mockClient.query
.mockResolvedValueOnce({ rows: [] }) // BEGIN
.mockResolvedValueOnce({ rows: [] }) // set_config
.mockRejectedValueOnce(new Error('User insert failed')); // INSERT fails
// Act & Assert
await expect(userRepo.createUser('fail@example.com', 'badpass', {})).rejects.toThrow('User insert failed');
expect(mockClient.query).toHaveBeenCalledWith('BEGIN');
// The createUser function now throws the original error, so we check for that.
expect(mockClient.query).toHaveBeenCalledWith('ROLLBACK');
expect(mockClient.release).toHaveBeenCalled();
});
it('should rollback the transaction if fetching the final profile fails', async () => {
const mockUser = { user_id: 'new-user-id', email: 'new@example.com' };
const repoWithTransaction = new UserRepository({ query: vi.fn(), release: vi.fn() } as any);
mockPoolInstance.query
// FIX: Define mockClient within this test's scope.
const mockClient = { query: vi.fn(), release: vi.fn() };
vi.mocked(mockPoolInstance.connect).mockResolvedValue(mockClient as any);
mockClient.query
.mockResolvedValueOnce({ rows: [] }) // BEGIN
.mockResolvedValueOnce({ rows: [] }) // set_config
.mockResolvedValueOnce({ rows: [] }) // set_config in trigger
.mockResolvedValueOnce({ rows: [mockUser] }) // INSERT user
.mockRejectedValueOnce(new Error('Profile fetch failed')); // SELECT profile fails
await expect(repoWithTransaction.createUser('fail@example.com', 'pass', {})).rejects.toThrow('Profile fetch failed');
await expect(userRepo.createUser('fail@example.com', 'pass', {})).rejects.toThrow('Failed to create user in database.');
expect(mockClient.query).toHaveBeenCalledWith('ROLLBACK');
expect(mockClient.release).toHaveBeenCalled();
});
it('should throw UniqueConstraintError if the email already exists', async () => {