Files
magpie/tests/config/loader.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

72 lines
2.0 KiB
TypeScript

// tests/config/loader.test.ts
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { loadConfig, expandEnvVars, getConfigPath } from '../../src/config/loader.js'
import { writeFileSync, mkdirSync, rmSync } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
describe('Config Loader', () => {
const testDir = join(tmpdir(), 'magpie-test-' + Date.now())
beforeEach(() => {
mkdirSync(testDir, { recursive: true })
})
afterEach(() => {
rmSync(testDir, { recursive: true, force: true })
})
describe('expandEnvVars', () => {
it('should expand environment variables', () => {
process.env.TEST_API_KEY = 'secret123'
const result = expandEnvVars('${TEST_API_KEY}')
expect(result).toBe('secret123')
delete process.env.TEST_API_KEY
})
it('should leave non-env strings unchanged', () => {
const result = expandEnvVars('plain-string')
expect(result).toBe('plain-string')
})
})
describe('loadConfig', () => {
it('should load and parse yaml config', () => {
const configPath = join(testDir, 'config.yaml')
writeFileSync(configPath, `
providers:
anthropic:
api_key: test-key
defaults:
max_rounds: 3
output_format: markdown
reviewers:
test-reviewer:
model: claude-sonnet-4-20250514
prompt: Test prompt
summarizer:
model: claude-sonnet-4-20250514
prompt: Summarizer prompt
analyzer:
model: claude-sonnet-4-20250514
prompt: Analyzer prompt
`)
const config = loadConfig(configPath)
expect(config.defaults.max_rounds).toBe(3)
expect(config.reviewers['test-reviewer'].model).toBe('claude-sonnet-4-20250514')
})
})
describe('getConfigPath', () => {
it('should return custom path if provided', () => {
const result = getConfigPath('/custom/path.yaml')
expect(result).toBe('/custom/path.yaml')
})
it('should return default path if not provided', () => {
const result = getConfigPath()
expect(result).toContain('.magpie/config.yaml')
})
})
})