mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 22:04:56 +00:00
Add tests for error logging functionality
Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const mockCreate = vi.fn()
|
||||
const mockAdapter = { create: mockCreate }
|
||||
|
||||
vi.mock('../../core/dbal-client', () => ({
|
||||
getAdapter: () => mockAdapter,
|
||||
}))
|
||||
|
||||
import { addErrorLog } from '../crud/add-error-log'
|
||||
|
||||
describe('addErrorLog', () => {
|
||||
beforeEach(() => {
|
||||
mockCreate.mockReset()
|
||||
})
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: 'minimal error log',
|
||||
log: {
|
||||
timestamp: Date.now(),
|
||||
level: 'error' as const,
|
||||
message: 'Test error',
|
||||
resolved: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'complete error log',
|
||||
log: {
|
||||
timestamp: Date.now(),
|
||||
level: 'error' as const,
|
||||
message: 'Test error',
|
||||
stack: 'Error: Test error\n at test.ts:10',
|
||||
context: '{"key":"value"}',
|
||||
userId: 'user_1',
|
||||
username: 'testuser',
|
||||
tenantId: 'tenant_1',
|
||||
source: 'test.ts',
|
||||
resolved: false,
|
||||
},
|
||||
},
|
||||
])('should add $name', async ({ log }) => {
|
||||
mockCreate.mockResolvedValue(undefined)
|
||||
|
||||
const id = await addErrorLog(log)
|
||||
|
||||
expect(mockCreate).toHaveBeenCalledWith('ErrorLog', expect.objectContaining({
|
||||
id: expect.stringContaining('error_'),
|
||||
timestamp: expect.any(BigInt),
|
||||
level: log.level,
|
||||
message: log.message,
|
||||
resolved: false,
|
||||
}))
|
||||
expect(id).toMatch(/^error_/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,91 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const mockList = vi.fn()
|
||||
const mockAdapter = { list: mockList }
|
||||
|
||||
vi.mock('../../core/dbal-client', () => ({
|
||||
getAdapter: () => mockAdapter,
|
||||
}))
|
||||
|
||||
import { getErrorLogs } from '../crud/get-error-logs'
|
||||
|
||||
describe('getErrorLogs', () => {
|
||||
beforeEach(() => {
|
||||
mockList.mockReset()
|
||||
})
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: 'empty array when no logs',
|
||||
dbData: [],
|
||||
options: undefined,
|
||||
expectedLength: 0,
|
||||
},
|
||||
{
|
||||
name: 'all error logs',
|
||||
dbData: [
|
||||
{
|
||||
id: 'error_1',
|
||||
timestamp: BigInt(Date.now()),
|
||||
level: 'error',
|
||||
message: 'Test error',
|
||||
stack: 'Error: Test error',
|
||||
context: null,
|
||||
userId: null,
|
||||
username: null,
|
||||
tenantId: null,
|
||||
source: 'test.ts',
|
||||
resolved: false,
|
||||
resolvedAt: null,
|
||||
resolvedBy: null,
|
||||
},
|
||||
],
|
||||
options: undefined,
|
||||
expectedLength: 1,
|
||||
},
|
||||
{
|
||||
name: 'filtered by level',
|
||||
dbData: [
|
||||
{
|
||||
id: 'error_1',
|
||||
timestamp: BigInt(Date.now()),
|
||||
level: 'error',
|
||||
message: 'Test error',
|
||||
stack: null,
|
||||
context: null,
|
||||
userId: null,
|
||||
username: null,
|
||||
tenantId: null,
|
||||
source: null,
|
||||
resolved: false,
|
||||
resolvedAt: null,
|
||||
resolvedBy: null,
|
||||
},
|
||||
{
|
||||
id: 'warning_1',
|
||||
timestamp: BigInt(Date.now()),
|
||||
level: 'warning',
|
||||
message: 'Test warning',
|
||||
stack: null,
|
||||
context: null,
|
||||
userId: null,
|
||||
username: null,
|
||||
tenantId: null,
|
||||
source: null,
|
||||
resolved: false,
|
||||
resolvedAt: null,
|
||||
resolvedBy: null,
|
||||
},
|
||||
],
|
||||
options: { level: 'error' },
|
||||
expectedLength: 1,
|
||||
},
|
||||
])('should return $name', async ({ dbData, options, expectedLength }) => {
|
||||
mockList.mockResolvedValue({ data: dbData })
|
||||
|
||||
const result = await getErrorLogs(options)
|
||||
|
||||
expect(mockList).toHaveBeenCalledWith('ErrorLog')
|
||||
expect(result).toHaveLength(expectedLength)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user