fix(logger): address code review findings

- Remove misleading await from audit-logger tests (functions are sync)
- Add cleanup() to LoggerAPI type definition in index.d.ts
- Fix circular reference fallback to preserve null values

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-04-03 21:37:59 +08:00
parent 6a9d144bbc
commit 6413eef5b8
3 changed files with 21 additions and 17 deletions

View File

@@ -61,10 +61,13 @@ const consoleFormat = winston.format.combine(
try { try {
metaStr = ` ${JSON.stringify(meta, null, 2)}` metaStr = ` ${JSON.stringify(meta, null, 2)}`
} catch { } catch {
// Fallback for circular references: stringify primitives, replace objects with placeholder // Fallback for circular references: stringify primitives, replace complex objects with placeholder
metaStr = ` ${JSON.stringify( metaStr = ` ${JSON.stringify(
Object.fromEntries( Object.fromEntries(
Object.entries(meta).map(([k, v]) => [k, typeof v === 'object' ? '[Object]' : v]) Object.entries(meta).map(([k, v]) => [
k,
v !== null && typeof v === 'object' ? `[Object]` : v
])
), ),
null, null,
2 2

View File

@@ -130,6 +130,7 @@ export interface ConfigAPI {
export interface LoggerAPI { export interface LoggerAPI {
log: (level: LogLevel, message: string, context?: Record<string, unknown>) => void log: (level: LogLevel, message: string, context?: Record<string, unknown>) => void
fetchLevel: () => Promise<void> fetchLevel: () => Promise<void>
cleanup: () => void
} }
export interface UpdateAPI { export interface UpdateAPI {

View File

@@ -92,7 +92,7 @@ describe('Audit Logger - Real File Integration', () => {
const entry = createTestEntry() const entry = createTestEntry()
await logAudit(entry.action as string, entry.userId as string, { logAudit(entry.action as string, entry.userId as string, {
username: entry.username as string, username: entry.username as string,
computerName: entry.computerName as string, computerName: entry.computerName as string,
resource: entry.resource as string, resource: entry.resource as string,
@@ -101,7 +101,7 @@ describe('Audit Logger - Real File Integration', () => {
}) })
// Close logger to flush writes // Close logger to flush writes
await closeAuditLogger() closeAuditLogger()
// Find the audit log file (should be today's file) // Find the audit log file (should be today's file)
const today = new Date().toISOString().split('T')[0] const today = new Date().toISOString().split('T')[0]
@@ -122,7 +122,7 @@ describe('Audit Logger - Real File Integration', () => {
await import('../../src/main/services/logger/audit-logger') await import('../../src/main/services/logger/audit-logger')
// Test success status // Test success status
await logAudit('EXTRACT', 'user1', { logAudit('EXTRACT', 'user1', {
username: 'extractor', username: 'extractor',
computerName: 'PC-001', computerName: 'PC-001',
resource: 'materials', resource: 'materials',
@@ -130,7 +130,7 @@ describe('Audit Logger - Real File Integration', () => {
}) })
// Test failure status // Test failure status
await logAudit('DELETE', 'user2', { logAudit('DELETE', 'user2', {
username: 'cleaner', username: 'cleaner',
computerName: 'PC-002', computerName: 'PC-002',
resource: 'temp_files', resource: 'temp_files',
@@ -139,7 +139,7 @@ describe('Audit Logger - Real File Integration', () => {
}) })
// Test partial status // Test partial status
await logAudit('UPDATE', 'user3', { logAudit('UPDATE', 'user3', {
username: 'updater', username: 'updater',
computerName: 'PC-003', computerName: 'PC-003',
resource: 'config', resource: 'config',
@@ -147,7 +147,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: { updated: 5, failed: 2 } metadata: { updated: 5, failed: 2 }
}) })
await closeAuditLogger() closeAuditLogger()
// Verify all entries were processed // Verify all entries were processed
expect(true).toBe(true) // Logger accepted all status types without error expect(true).toBe(true) // Logger accepted all status types without error
@@ -158,7 +158,7 @@ describe('Audit Logger - Real File Integration', () => {
await import('../../src/main/services/logger/audit-logger') await import('../../src/main/services/logger/audit-logger')
// Without metadata // Without metadata
await logAudit('LOGIN', 'user-no-meta', { logAudit('LOGIN', 'user-no-meta', {
username: 'no.meta', username: 'no.meta',
computerName: 'PC-001', computerName: 'PC-001',
resource: 'ERP', resource: 'ERP',
@@ -166,7 +166,7 @@ describe('Audit Logger - Real File Integration', () => {
}) })
// With metadata // With metadata
await logAudit('LOGOUT', 'user-with-meta', { logAudit('LOGOUT', 'user-with-meta', {
username: 'with.meta', username: 'with.meta',
computerName: 'PC-002', computerName: 'PC-002',
resource: 'ERP', resource: 'ERP',
@@ -174,7 +174,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: { sessionDuration: 3600, actionsPerformed: 15 } metadata: { sessionDuration: 3600, actionsPerformed: 15 }
}) })
await closeAuditLogger() closeAuditLogger()
// Both entries should be processed successfully // Both entries should be processed successfully
expect(true).toBe(true) expect(true).toBe(true)
@@ -186,14 +186,14 @@ describe('Audit Logger - Real File Integration', () => {
const beforeLog = Date.now() const beforeLog = Date.now()
await logAudit('TEST', 'timestamp-user', { logAudit('TEST', 'timestamp-user', {
username: 'timestamp.test', username: 'timestamp.test',
computerName: 'PC-TS', computerName: 'PC-TS',
resource: 'test_resource', resource: 'test_resource',
status: 'success' status: 'success'
}) })
await closeAuditLogger() closeAuditLogger()
const afterLog = Date.now() const afterLog = Date.now()
@@ -212,7 +212,7 @@ describe('Audit Logger - Real File Integration', () => {
const { logAudit, closeAuditLogger } = const { logAudit, closeAuditLogger } =
await import('../../src/main/services/logger/audit-logger') await import('../../src/main/services/logger/audit-logger')
await logAudit('LOGIN_ATTEMPT', 'user-special', { logAudit('LOGIN_ATTEMPT', 'user-special', {
username: 'user.name+test@example.com', username: 'user.name+test@example.com',
computerName: 'DESKTOP-特殊字符-001', computerName: 'DESKTOP-特殊字符-001',
resource: 'ERP/子系统', resource: 'ERP/子系统',
@@ -220,7 +220,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: { reason: '密码错误', attempt: 3 } metadata: { reason: '密码错误', attempt: 3 }
}) })
await closeAuditLogger() closeAuditLogger()
// Should handle without errors // Should handle without errors
expect(true).toBe(true) expect(true).toBe(true)
@@ -230,7 +230,7 @@ describe('Audit Logger - Real File Integration', () => {
const { logAudit, closeAuditLogger } = const { logAudit, closeAuditLogger } =
await import('../../src/main/services/logger/audit-logger') await import('../../src/main/services/logger/audit-logger')
await logAudit('PING', 'ping-user', { logAudit('PING', 'ping-user', {
username: 'pinger', username: 'pinger',
computerName: 'PC-PING', computerName: 'PC-PING',
resource: 'health_check', resource: 'health_check',
@@ -238,7 +238,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: {} metadata: {}
}) })
await closeAuditLogger() closeAuditLogger()
// Should handle empty metadata // Should handle empty metadata
expect(true).toBe(true) expect(true).toBe(true)