Guard audit writes when advisory lock is unavailable

This commit is contained in:
Codex 2026-09-29 08:52:05 +02:00
parent 4976fc529e
commit 51089b2249
2 changed files with 33 additions and 1 deletions

View file

@ -0,0 +1,31 @@
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();
});
});