import { describe, expect, it, vi } from 'vitest'; const connection = vi.hoisted(() => ({ beginTransaction: vi.fn(), query: vi.fn(), execute: vi.fn(), commit: vi.fn(), rollback: vi.fn(), release: vi.fn(), })); vi.mock('../src/db.js', () => ({ pool: { getConnection: vi.fn(async () => connection) }, one: vi.fn(), query: vi.fn(), })); import { audit } from '../src/audit.js'; describe('audit advisory lock', () => { it.each([0, null])('does not append an event when GET_LOCK returns %s', async (acquired) => { vi.clearAllMocks(); connection.query.mockResolvedValueOnce([[{ acquired }]]).mockResolvedValueOnce([[{ released: null }]]); await expect(audit({ actorType: 'system', action: 'test.lock' })).rejects.toThrow('Audit lock unavailable'); expect(connection.execute).not.toHaveBeenCalled(); expect(connection.commit).not.toHaveBeenCalled(); expect(connection.rollback).toHaveBeenCalledOnce(); expect(connection.release).toHaveBeenCalledOnce(); }); });