Skip to content

Add metadata extraction and migrate to Vitest#6

Merged
masonfox merged 11 commits intomainfrom
feature/metadata-extraction
Jan 24, 2026
Merged

Add metadata extraction and migrate to Vitest#6
masonfox merged 11 commits intomainfrom
feature/metadata-extraction

Conversation

@masonfox
Copy link
Copy Markdown
Owner

@masonfox masonfox commented Jan 24, 2026

Summary

This PR adds embedded metadata extraction from audiobook files and migrates the test suite from Bun's test runner to Vitest, resolving test isolation issues.

Changes

Metadata Extraction (NEW)

  • ✨ Extract embedded metadata (author, title, year, genre) from audiobook files using ffprobe
  • 📚 Pass metadata to Claude AI for enhanced normalization
  • 🔄 Graceful fallback when metadata unavailable
  • 📊 Comprehensive test coverage for metadata extraction

Test Infrastructure Migration

  • 🔧 Migrate from Bun test runner to Vitest
  • Fix 11 failing tests caused by module mock pollution
  • 🎯 Better test isolation between files
  • 📈 Coverage reporting with v8
  • ⏭️ Gracefully skip Claude API tests when ANTHROPIC_API_KEY unset (CI-friendly)

CI/CD Updates

  • 🚀 Update PR checks workflow to run tests with coverage
  • 📦 Update publish workflow to use Vitest
  • 🐳 Docker build validation (only runs if tests pass)
  • 📊 Automatic coverage upload to Codecov

Test Results

With API key (local dev):

  • ✅ 158/158 tests passing (100%)
  • 📊 ~80% code coverage

Without API key (CI):

  • ✅ 141 tests passing
  • ⏭️ 17 tests skipped (Claude API integration tests)
  • ❌ 0 tests failing

Breaking Changes

None - this is backward compatible. If no embedded metadata exists, the system falls back to filename heuristics as before.

Related Issues

Resolves the module mock pollution issue that caused 11 tests to fail when run together.

Closes #3 and #5

- Create src/metadata.js with extractAudioMetadata() and extractDirectoryMetadata()
  - Extracts title, author, year, and genre from MP3/M4B files using ffprobe
  - Prefers .m4b files over .mp3 for directories (better metadata quality)
  - Gracefully degrades when ffprobe unavailable or metadata missing
  - Shows one-time warning if ffprobe not installed

- Update src/claude.js to accept fileMetadata parameter
  - Enhanced prompt includes embedded metadata when available
  - Claude uses metadata as authoritative source for normalization
  - Year and genre provide disambiguation context

- Update src/migrate.js to extract and pass metadata
  - migrateJobFile() extracts metadata from single files
  - migrateJobDirectory() extracts from directory (prefers .m4b)
  - Logs extracted metadata at info level for visibility

- Update Dockerfile to install ffmpeg package
  - Adds ffprobe capability to Docker containers

- Add comprehensive tests in __tests__/metadata.test.js
  - Tests extraction from real audiobook files
  - Validates metadata structure and field handling
  - Tests directory metadata extraction with file preference

This enhancement improves accuracy by providing Claude with authoritative
embedded metadata from audio files, reducing reliance on filename parsing
while maintaining backward compatibility when metadata is unavailable.
- Expand __tests__/metadata.test.js from 5 to 29 tests
  - Unit tests for extractAudioMetadata() with real audiobook files
  - Tests for extractDirectoryMetadata() with file preferences
  - Edge case handling (missing files, permissions, empty metadata)
  - Logging behavior verification
  - Performance validation (< 2s per file)

- Add __tests__/metadata-integration.test.js with 13 integration tests
  - Claude normalization with embedded metadata
  - Year and genre for disambiguation context
  - Author name variation handling
  - Backward compatibility without metadata
  - End-to-end pipeline validation
  - Quality comparison: with vs without metadata

- Add __tests__/METADATA_TESTS.md documentation
  - Comprehensive test suite overview
  - Test coverage breakdown by feature
  - Running instructions and debugging guide
  - Mock object documentation
  - Future enhancement suggestions

Test Results:
- Total: 42 tests across 2 files
- Assertions: 151 expect() calls
- Success Rate: 100%
- Execution Time: ~18 seconds

Test Coverage:
- ✅ Real audiobook files (5 different sources)
- ✅ M4B and MP3 format support
- ✅ Metadata field extraction (title, author, year, genre)
- ✅ Directory metadata with file preference logic
- ✅ Claude integration with metadata enrichment
- ✅ Error handling and graceful degradation
- ✅ Performance and consistency validation
- Install vitest and @vitest/coverage-v8 dependencies
- Create vitest.config.js with Node environment and coverage setup
- Update all test imports from 'bun:test' to 'vitest'
- Convert Bun mock functions to Vitest equivalents:
  - mock() → vi.fn()
  - mock.module() → vi.mock()
  - Remove optional chaining from mock methods
- Rename zz-jobQueue.test.js → jobQueue.test.js (no longer need alphabetical ordering)
- Update package.json scripts: test, test:watch, test:coverage
- Update CLAUDE.md with new test commands
- Add GitHub Actions PR checks workflow for CI/CD

Benefits:
- Fixes module mock pollution issue (11 previously failing tests now pass)
- Better test isolation between files
- Built-in coverage reporting
- Watch mode for better DX
- CI/CD integration

Test results: 145/158 passing (9 failures are Claude API integration tests requiring ANTHROPIC_API_KEY)
…s unset

- Load .env file in vitest.config.js using vite's loadEnv
- Add hasApiKey() check to metadata-integration tests
- Use test.skip pattern (testIf) for all Claude API integration tests
- Fix test expectation for sanitized filename (underscores → spaces)

All 158 tests now pass with API key set.
Without API key, Claude tests are skipped gracefully (suitable for CI).
- Change 'npm test' to 'npm run test:coverage' to generate coverage report
- Update step name to reflect coverage generation
- Coverage files will now be available for Codecov upload
- Replace Bun test runner commands with 'npm run test:coverage'
- Simplify to single test command (runs all tests)
- Update coverage file path from lcov.info to coverage-final.json
- Remove redundant integration test step (now included in main test suite)
- API key still loaded from GitHub secrets for integration tests
The 'node --version' check doesn't validate anything meaningful.
The Docker build itself is sufficient validation - if the Dockerfile
is broken, the build will fail. Full runtime testing happens in the
publish workflow when merging to main.
- Mock node:child_process.execFile to simulate ffprobe responses
- Add mock metadata responses for test audio files
- Update directory metadata tests to use temporary files instead of copying
- Add file existence checks in metadata-integration tests
- Skip tests requiring real audio files when not available
- All tests now pass in CI without requiring actual audio files
…utput

The 'should handle migration failures gracefully' test intentionally
triggers a permission error. Use a silent logger for this test to avoid
confusing error messages in CI logs while still validating the error
handling behavior.
@masonfox masonfox merged commit 17031c9 into main Jan 24, 2026
2 checks passed
@masonfox masonfox deleted the feature/metadata-extraction branch January 24, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract file metadata pre-normalization

1 participant