jest.mock('../src/config/db', () => ({})); const { auditLogWhere } = require('../src/utils/adminAudit'); describe('auditLogWhere', () => { test('no filters → everything', () => { expect(auditLogWhere({})).toEqual({}); }); test('a "to" date includes that whole day', () => { const where = auditLogWhere({ from: '2026-10-01', to: '2026-10-28' }); expect(where.createdAt.gte).toEqual(new Date('2026-10-01T00:00:00')); expect(where.createdAt.lt).toEqual(new Date('2026-10-29T00:00:00')); expect(where.createdAt.lte).toBeUndefined(); }); test('a full timestamp is used as given', () => { const where = auditLogWhere({ to: '2026-10-28T10:00:00.000Z' }); expect(where.createdAt.lte).toEqual(new Date('2026-10-28T10:00:00.000Z')); }); test('action and role filter exactly', () => { expect(auditLogWhere({ action: 'refund_created', role: 'supervisor' })).toEqual({ action: 'refund_created', actorRole: 'supervisor' }); }); test('search looks at the person, the target and the IP', () => { const where = auditLogWhere({ search: ' #ab12 ' }); expect(where.OR).toEqual([ { actor: { name: { contains: '#ab12', mode: 'insensitive' } } }, { actor: { email: { contains: '#ab12', mode: 'insensitive' } } }, { targetType: { contains: '#ab12', mode: 'insensitive' } }, { targetId: { startsWith: 'ab12' } }, { ip: { startsWith: '#ab12' } }, ]); }); test('a blank search is ignored', () => { expect(auditLogWhere({ search: ' ' }).OR).toBeUndefined(); }); });