Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | import { Logger } from '../../src/utils/logger'; |
| 3 | |
| 4 | describe('Logger credential redaction', () => { |
| 5 | afterEach(() => vi.restoreAllMocks()); |
| 6 | |
| 7 | it('redacts secret-shaped fields at the output boundary', () => { |
| 8 | const debug = vi.spyOn(console, 'debug').mockImplementation(() => undefined); |
| 9 | const logger = new Logger({ level: 'debug', includeTimestamp: false }); |
| 10 | |
| 11 | logger.debug('credentials', { |
| 12 | apiKey: 'uak_test_secret', |
| 13 | auth: 'app_key:pusher_signature', |
| 14 | password: 'correct horse battery staple', |
| 15 | nested: { access_token: 'oauth_test_secret' }, |
| 16 | safe: 'visible', |
| 17 | }); |
| 18 | |
| 19 | const output = String(debug.mock.calls[0]?.[0]); |
| 20 | expect(output).not.toContain('uak_test_secret'); |
| 21 | expect(output).not.toContain('pusher_signature'); |
| 22 | expect(output).not.toContain('correct horse battery staple'); |
| 23 | expect(output).not.toContain('oauth_test_secret'); |
| 24 | expect(output).toContain('[REDACTED]'); |
| 25 | expect(output).toContain('visible'); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe('Logger sink and configure', () => { |
| 30 | afterEach(() => vi.restoreAllMocks()); |
| 31 | |
| 32 | const createSink = () => ({ |
| 33 | error: vi.fn(), |
| 34 | warn: vi.fn(), |
| 35 | info: vi.fn(), |
| 36 | debug: vi.fn(), |
| 37 | }); |
| 38 | |
| 39 | it('writes formatted, redacted output to a custom sink instead of console', () => { |
| 40 | const sink = createSink(); |
| 41 | const consoleWarn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); |
| 42 | const logger = new Logger({ level: 'warn', includeTimestamp: false, sink }); |
| 43 | |
| 44 | logger.warn('token', { apiKey: 'uak_test_secret' }); |
| 45 | |
| 46 | expect(consoleWarn).not.toHaveBeenCalled(); |
| 47 | expect(sink.warn).toHaveBeenCalledTimes(1); |
| 48 | const output = String(sink.warn.mock.calls[0]?.[0]); |
| 49 | expect(output).not.toContain('uak_test_secret'); |
| 50 | expect(output).toContain('[REDACTED]'); |
| 51 | }); |
| 52 | |
| 53 | it('configure updates the level in place', () => { |
| 54 | const sink = createSink(); |
| 55 | const logger = new Logger({ level: 'error', includeTimestamp: false, sink }); |
| 56 | |
| 57 | logger.warn('dropped'); |
| 58 | expect(sink.warn).not.toHaveBeenCalled(); |
| 59 | |
| 60 | logger.configure({ level: 'warn' }); |
| 61 | expect(logger.getLevel()).toBe('warn'); |
| 62 | logger.warn('emitted'); |
| 63 | expect(sink.warn).toHaveBeenCalledWith('emitted'); |
| 64 | }); |
| 65 | |
| 66 | it('configure swaps the sink and keeps the level when level is omitted', () => { |
| 67 | const first = createSink(); |
| 68 | const second = createSink(); |
| 69 | const logger = new Logger({ level: 'debug', includeTimestamp: false, sink: first }); |
| 70 | |
| 71 | logger.configure({ sink: second }); |
| 72 | logger.debug('hello'); |
| 73 | |
| 74 | expect(logger.getLevel()).toBe('debug'); |
| 75 | expect(first.debug).not.toHaveBeenCalled(); |
| 76 | expect(second.debug).toHaveBeenCalledWith('hello'); |
| 77 | }); |
| 78 | |
| 79 | it("'silent' suppresses every level", () => { |
| 80 | const sink = createSink(); |
| 81 | const logger = new Logger({ level: 'debug', includeTimestamp: false, sink }); |
| 82 | |
| 83 | logger.configure({ level: 'silent' }); |
| 84 | logger.error('e'); |
| 85 | logger.warn('w'); |
| 86 | logger.info('i'); |
| 87 | logger.debug('d'); |
| 88 | |
| 89 | expect(sink.error).not.toHaveBeenCalled(); |
| 90 | expect(sink.warn).not.toHaveBeenCalled(); |
| 91 | expect(sink.info).not.toHaveBeenCalled(); |
| 92 | expect(sink.debug).not.toHaveBeenCalled(); |
| 93 | }); |
| 94 | }); |