Files
magpie/tests/utils/logger.test.ts
Li Liu 2b0e1ba711 refactor: comprehensive codebase improvements across 7 phases
Phase A - Quick fixes:
- Remove debug logging that leaked prompt content (qwen-code)
- Fix orchestrator session leak with try/finally cleanup
- CJK-aware token estimation for better accuracy
- Issue parser validation (line > 0, endLine >= line, non-empty fields)
- Improved similarity matching with stop words filtering and description weight

Phase B - Medium fixes:
- Add retry utility with exponential backoff for API providers
- Config validation at load time (required fields, empty API key warnings)
- GitHub PR comment deduplication (skip already-posted comments)
- Ctrl+C graceful exit for interactive comment review

Phase C - Structured logging:
- Logger class with debug/info/warn/error levels (MAGPIE_LOG_LEVEL env var)

Phase D - Type safety:
- Replace `any` types with proper types across discuss.ts, review.ts,
  issue-parser.ts, commenter.ts, repo-orchestrator.ts, history-collector.ts

Phase E - Session helper extraction:
- CliSessionHelper class shared by 4 CLI providers, reducing duplication

Phase F - Split review.ts (1991 → 6 files):
- review.ts (command + action), interactive.ts, repo-review.ts,
  session-cmds.ts, utils.ts, types.ts

Phase G - Tests:
- 6 new test files (retry, logger, session-helper, issue-parser-enhanced,
  loader-validation, orchestrator-session)
- Fix pre-existing test failures (commenter, anthropic)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:46:46 +08:00

73 lines
2.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
describe('Logger', () => {
let originalEnv: string | undefined
beforeEach(() => {
originalEnv = process.env.MAGPIE_LOG_LEVEL
vi.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
if (originalEnv !== undefined) {
process.env.MAGPIE_LOG_LEVEL = originalEnv
} else {
delete process.env.MAGPIE_LOG_LEVEL
}
vi.restoreAllMocks()
// Clear module cache so Logger re-reads env
vi.resetModules()
})
it('defaults to info level', async () => {
delete process.env.MAGPIE_LOG_LEVEL
const { logger } = await import('../../src/utils/logger.js')
expect(logger.getLevel()).toBe('info')
})
it('reads MAGPIE_LOG_LEVEL env var', async () => {
process.env.MAGPIE_LOG_LEVEL = 'debug'
const { logger } = await import('../../src/utils/logger.js')
expect(logger.getLevel()).toBe('debug')
})
it('filters debug when level is info', async () => {
delete process.env.MAGPIE_LOG_LEVEL
const { logger } = await import('../../src/utils/logger.js')
logger.setLevel('info')
logger.debug('should not appear')
expect(console.error).not.toHaveBeenCalled()
})
it('logs warn at info level', async () => {
delete process.env.MAGPIE_LOG_LEVEL
const { logger } = await import('../../src/utils/logger.js')
logger.setLevel('info')
logger.warn('warning message')
expect(console.error).toHaveBeenCalledTimes(1)
})
it('logs debug at debug level', async () => {
process.env.MAGPIE_LOG_LEVEL = 'debug'
const { logger } = await import('../../src/utils/logger.js')
logger.debug('debug message')
expect(console.error).toHaveBeenCalledTimes(1)
})
it('filters info when level is error', async () => {
delete process.env.MAGPIE_LOG_LEVEL
const { logger } = await import('../../src/utils/logger.js')
logger.setLevel('error')
logger.info('should not appear')
logger.warn('should not appear')
expect(console.error).not.toHaveBeenCalled()
})
it('setLevel updates the level', async () => {
delete process.env.MAGPIE_LOG_LEVEL
const { logger } = await import('../../src/utils/logger.js')
logger.setLevel('warn')
expect(logger.getLevel()).toBe('warn')
})
})