Golden Test Implementation
Golden Test Implementation Summary
Overview
This document tracks the implementation of a comprehensive golden test framework for DocBuilder’s integration testing. The framework verifies end-to-end functionality including Git cloning, document discovery, Hugo generation, and HTML rendering.
Implementation Status
✅ Phase 1: Foundation (Complete)
- Infrastructure: Test helpers, golden config loading, build execution
- Files Created:
test/integration/helpers.go(~609 lines) - Core test utilitiestest/integration/golden_test.go(~1,107 lines) - Test implementationstest/testdata/golden/- Golden file storage
- Key Functions:
loadGoldenConfig()- Load and validate test configsrunBuild()- Execute build with BuildServiceverifyDirectoryStructure()- Check output file treeverifyFileContent()- Compare files against golden snapshotsupdateGoldenFiles()- Regenerate golden snapshots
✅ Phase 2: Core Test Coverage (Complete - 13 tests)
Theme Tests
- TestGolden_HextraBasic - Basic Hextra theme generation
- TestGolden_HextraMath - Math rendering support
- TestGolden_DocsyBasic - Basic Docsy theme generation
- TestGolden_HextraSearch - Search index generation
- TestGolden_DocsyAPI - Docsy API documentation style
Content Tests
- TestGolden_FrontmatterInjection - Front matter generation
- TestGolden_ImagePaths - Image path transformation
- TestGolden_SectionIndexes - Section index page generation
- TestGolden_MenuGeneration - Automatic menu generation
- TestGolden_HextraMultilang - Multi-language support
Repository Tests
- TestGolden_TwoRepos - Multi-repository aggregation
- TestGolden_ConflictingPaths - Path conflict resolution
- TestGolden_CrossRepoLinks - Cross-repository linking
✅ Phase 3: Edge Cases & Enhanced Verification (Complete - 9 tests)
Edge Case Tests
- TestGolden_EmptyDocs - Repository with no markdown files
- TestGolden_OnlyReadme - Only README.md (should be skipped)
- TestGolden_MalformedFrontmatter - Invalid YAML front matter
- TestGolden_DeepNesting - Deep directory nesting (10+ levels)
- TestGolden_UnicodeNames - Unicode filenames and paths
- TestGolden_SpecialChars - Special characters in filenames
HTML Verification
- Framework: DOM parsing with
golang.org/x/net/html - Capabilities:
- CSS selector matching (tag, .class, #id)
- Element counting
- Text content verification
- Attribute checking
- Implementation:
verifyRenderedSamples()- Parse and verify HTML structurefindElements()- Recursive DOM tree traversalparseSimpleSelector()- CSS selector parsingrenderHugoSite()- Execute Hugo build
- Golden Files:
rendered-samples.golden.json- HTML verification samples
Error Handling Tests
- TestGolden_Error_InvalidRepository - Invalid repository URL handling
- TestGolden_Error_InvalidConfig - Empty/minimal configuration
- TestGolden_Warning_NoGitCommit - Repository without commits
Test Results
Golden File Structure
Key Technical Decisions
1. HTML Verification Approach
- Choice: Parse HTML DOM with
golang.org/x/net/html - Rationale: More reliable than string matching, verifies actual rendered structure
- Trade-off: Requires Hugo execution during tests (slower but more accurate)
2. Error Handling Philosophy
- Choice: Verify graceful degradation, not hard failures
- Rationale: Build service logs errors but continues when possible
- Implementation: Check
RepositoriesSkippedcounter rather than expecting errors
3. Golden File Format
- Structure Files: JSON array of file paths
- Content Files: JSON map of path→content
- HTML Samples: JSON array of selector-based verifications
- Rationale: JSON is structured, diffable, and language-agnostic
4. Test Organization
- Pattern: One test per scenario, subtests for verification steps
- Naming:
TestGolden_{Feature}for discoverability - Isolation: Each test uses
t.TempDir()for complete isolation
Usage
Running Tests
Adding New Tests
- Create test configuration in
test/testdata/configs/{name}.yaml - Create golden directory in
test/testdata/golden/{name}/ - Add test function to
test/integration/golden_test.go - Run with
-update-goldento generate initial snapshots - Review and commit golden files
Future Enhancements
Potential Additions
- Performance Benchmarks: Track build time trends
- Incremental Build Testing: Verify cache behavior
- Theme Variations: Test theme-specific features
- Plugin System Testing: Verify transform plugins
- Build Report Validation: Parse and verify build-report.json
CI Integration
- Run golden tests on every PR
- Flag golden file changes for review
- Generate test coverage reports
- Performance regression detection
Related Documentation
Maintenance Notes
- Golden files should be reviewed when updated (not blindly regenerated)
- HTML verification samples should check structure, not exact content
- Error tests should verify logging and graceful handling, not hard failures
- Test execution should remain fast (< 1s for full suite without Hugo)