|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + DEBUG_MODE_DURATION_MS, |
| 4 | + MAX_DEBUG_ENTRIES, |
| 5 | + MAX_STRING_LENGTH, |
| 6 | + clearDebugLogs, |
| 7 | + getDebugLogEntries, |
| 8 | + getDebugLogStatus, |
| 9 | + isDebugModeEnabled, |
| 10 | + recordDebugLog, |
| 11 | + sanitizeForDebug, |
| 12 | + setDebugMode, |
| 13 | +} from '@/lib/debug-log'; |
| 14 | +import { createLogger } from '@/lib/logger'; |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + setDebugMode(false); |
| 18 | + clearDebugLogs(); |
| 19 | +}); |
| 20 | + |
| 21 | +describe('调试日志', () => { |
| 22 | + it('调试模式关闭时不记录', () => { |
| 23 | + recordDebugLog('info', {}, 'ignored'); |
| 24 | + expect(getDebugLogEntries()).toHaveLength(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it('开启后记录所有级别,开启时清空上一轮日志,关闭后保留日志', () => { |
| 28 | + setDebugMode(true); |
| 29 | + recordDebugLog('debug', { module: 'x' }, 'first'); |
| 30 | + expect(getDebugLogEntries()).toHaveLength(1); |
| 31 | + |
| 32 | + setDebugMode(true); |
| 33 | + recordDebugLog('trace', {}, 'second'); |
| 34 | + expect(getDebugLogEntries().map((e) => e.msg)).toEqual(['second']); |
| 35 | + |
| 36 | + setDebugMode(false); |
| 37 | + recordDebugLog('info', {}, 'after off'); |
| 38 | + expect(getDebugLogStatus()).toMatchObject({ enabled: false, entryCount: 1 }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('超过时限后自动关闭', () => { |
| 42 | + const start = Date.UTC(2026, 0, 1); |
| 43 | + setDebugMode(true, start); |
| 44 | + expect(isDebugModeEnabled(start + DEBUG_MODE_DURATION_MS - 1)).toBe(true); |
| 45 | + expect(isDebugModeEnabled(start + DEBUG_MODE_DURATION_MS)).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('超过条数上限时丢弃最早的记录', () => { |
| 49 | + setDebugMode(true); |
| 50 | + for (let i = 0; i < MAX_DEBUG_ENTRIES + 3; i++) recordDebugLog('info', {}, `m${i}`); |
| 51 | + const entries = getDebugLogEntries(); |
| 52 | + expect(entries).toHaveLength(MAX_DEBUG_ENTRIES); |
| 53 | + expect(entries[0].msg).toBe('m3'); |
| 54 | + expect(getDebugLogStatus().droppedCount).toBe(3); |
| 55 | + }); |
| 56 | + |
| 57 | + it('logger 在调试模式下记录低于 LOG_LEVEL 的日志', () => { |
| 58 | + setDebugMode(true); |
| 59 | + createLogger('debug-test').trace({ step: 1 }, 'trace message'); |
| 60 | + expect(getDebugLogEntries()).toEqual([ |
| 61 | + expect.objectContaining({ level: 'trace', module: 'debug-test', step: 1, msg: 'trace message' }), |
| 62 | + ]); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('调试日志脱敏', () => { |
| 67 | + it('去掉密钥类字段,保留 token 用量等普通字段', () => { |
| 68 | + const result = sanitizeForDebug({ |
| 69 | + apiKey: 'AIza-secret', |
| 70 | + nested: { password: 'p', accessToken: 't', Authorization: 'Bearer x', maxTokens: 100, totalTokens: 5 }, |
| 71 | + emptyKey: { apiKey: '' }, |
| 72 | + }); |
| 73 | + expect(result).toEqual({ |
| 74 | + apiKey: '[REDACTED]', |
| 75 | + nested: { password: '[REDACTED]', accessToken: '[REDACTED]', Authorization: '[REDACTED]', maxTokens: 100, totalTokens: 5 }, |
| 76 | + emptyKey: { apiKey: '' }, |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('字符串中的 Bearer 令牌、sk- Key、URL key 参数和图片 data URL 被替换', () => { |
| 81 | + const text = sanitizeForDebug('Bearer abc.def sk-1234567890abcdef https://x/v1?key=AIzaSecret&a=1 data:image/png;base64,AAAABBBB') as string; |
| 82 | + expect(text).not.toMatch(/abc\.def|1234567890abcdef|AIzaSecret|AAAABBBB/); |
| 83 | + expect(text).toContain('[data:image/png,'); |
| 84 | + expect(text).toContain('&a=1'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('超长字符串截断,循环引用和 Error 可序列化', () => { |
| 88 | + const circular: Record<string, unknown> = { name: 'a' }; |
| 89 | + circular.self = circular; |
| 90 | + const result = sanitizeForDebug({ |
| 91 | + long: 'x'.repeat(MAX_STRING_LENGTH + 10), |
| 92 | + circular, |
| 93 | + error: new TypeError('boom'), |
| 94 | + }) as Record<string, Record<string, unknown> | string>; |
| 95 | + expect((result.long as string).length).toBeLessThan(MAX_STRING_LENGTH + 50); |
| 96 | + expect((result.circular as Record<string, unknown>).self).toBe('[Circular]'); |
| 97 | + expect(result.error).toMatchObject({ name: 'TypeError', message: 'boom' }); |
| 98 | + expect(() => JSON.stringify(result)).not.toThrow(); |
| 99 | + }); |
| 100 | +}); |
0 commit comments