Testing Infrastructure Summary
What Was Implemented
This document summarizes the complete testing infrastructure rebuild for the Gofannon project.
Key Achievements
✅ Full test coverage infrastructure for frontend and backend ✅ 95% minimum coverage requirement enforced via CI/CD ✅ Automated PR checks run unit tests on every pull request ✅ Nightly integration tests run comprehensive tests daily ✅ Comprehensive documentation for writing and maintaining tests ✅ Example tests demonstrating best practices ✅ Test data factories for generating consistent test fixtures
Testing Strategy
Test Types
| Type | Location | Run When | Purpose |
|---|---|---|---|
| Unit Tests (Frontend) | packages/webui/src/**/*.test.jsx | Every PR | Test React components in isolation |
| Unit Tests (Backend) | packages/api/user-service/tests/unit/ | Every PR | Test Python functions/classes in isolation |
| Integration Tests | packages/api/user-service/tests/integration/ | Nightly | Test with real services (DB, S3, etc.) |
| E2E Tests | webapp/tests/e2e/ | Nightly | Test complete user workflows |
Coverage Requirements
- Minimum: 95% for lines, functions, branches, statements
- Enforced: CI/CD blocks PRs below threshold
- Reported: Codecov integration for tracking over time
Infrastructure Components
Frontend Testing (Vitest)
Files Created:
vitest.config.ts- Vitest configurationsrc/test/setup.ts- Test environment setupsrc/components/ActionCard.test.jsx- Example component testsrc/test/utils.test.js- Example utility test
Dependencies Added:
vitest- Test runner@testing-library/react- React component testing utilities@testing-library/user-event- User interaction simulation@testing-library/jest-dom- DOM matchers@vitest/ui- Interactive test UI@vitest/coverage-v8- Coverage reportingjsdom- DOM environment for Node
Commands:
pnpm test # Run tests
pnpm test:ui # Interactive UI
pnpm test:coverage # With coverage report
Backend Testing (pytest)
Files Created:
pytest.ini- pytest configuration.coveragerc- Coverage settingstests/conftest.py- Shared fixturestests/factories/- Test data factoriesagent_factory.pyuser_factory.pychat_factory.py
tests/unit/test_user_service.py- Example service teststests/unit/test_user_model.py- Example model teststests/integration/test_health_endpoint.py- Example integration test
Dependencies Added:
pytest>=8.0.0- Test frameworkpytest-asyncio>=0.23.0- Async test supportpytest-cov>=4.1.0- Coverage pluginpytest-mock>=3.12.0- Mocking utilitiesfactory-boy>=3.3.0- Test data factoriesfaker>=22.0.0- Fake data generation
Commands:
python -m pytest tests/unit # Unit tests
python -m pytest tests/integration # Integration tests
python -m pytest --cov=. --cov-report=html # With coverage
CI/CD Workflows
Files Created:
-
pr-unit-tests.yml- Runs on every PR- Frontend unit tests with coverage
- Backend unit tests with coverage
- Lint checks
- Coverage threshold validation
- Codecov upload
-
nightly-integration-tests.yml- Runs daily at 2 AM UTC- Backend integration tests with full Docker stack
- E2E tests with Playwright
- Uses GitHub Secrets for API keys (OPENAI_API_KEY, AWS credentials)
- Collects logs on failure
- Sends notifications on failure
Secrets Required:
OPENAI_API_KEY- For AI model testingGEMINI_API_KEY- For alternative AI modelAWS_ACCESS_KEY_ID- For S3 integration testsAWS_SECRET_ACCESS_KEY- For S3 integration testsAWS_REGION- AWS region (optional, defaults to us-east-1)
NPM Scripts
Updated package.json:
{
"test": "pnpm run test:unit && pnpm run test:integration",
"test:unit": "pnpm run test:unit:frontend && pnpm run test:unit:backend",
"test:unit:frontend": "pnpm --filter webui test",
"test:unit:backend": "cd packages/api/user-service && python -m pytest tests/unit -v",
"test:integration": "pnpm run test:integration:backend && pnpm run test:e2e",
"test:integration:backend": "cd packages/api/user-service && python -m pytest tests/integration -v",
"test:e2e": "playwright test",
"test:coverage": "pnpm run test:coverage:frontend && pnpm run test:coverage:backend",
"test:coverage:frontend": "pnpm --filter webui test:coverage",
"test:coverage:backend": "cd packages/api/user-service && python -m pytest tests/unit --cov=. --cov-report=html --cov-report=term-missing"
}
Documentation
Created in docs/testing/:
README.md- Testing overview and quick startunit-testing.md- Comprehensive unit testing guidecontributing.md- PR requirements and guidelinesSUMMARY.md- This document
Topics Covered:
- Testing philosophy and strategy
- Running tests locally
- Writing unit tests (frontend & backend)
- Using test factories
- Mocking dependencies
- Testing async code
- Coverage requirements
- PR requirements
- Best practices
- Common patterns
- Troubleshooting
Test Examples
Backend Unit Test Example
class TestUserService:
@pytest.fixture
def mock_db(self):
db = Mock()
db.get = Mock(return_value={"id": "test-123"})
return db
def test_get_user_existing(self, user_service, mock_db):
user = user_service.get_user("test-123")
mock_db.get.assert_called_once_with("users", "test-123")
assert user.id == "test-123"
Frontend Unit Test Example
describe('ActionCard', () => {
it('calls onClick when clicked', async () => {
const onClick = vi.fn();
render(<ActionCard {...props} onClick={onClick} />);
await userEvent.click(screen.getByRole('button'));
expect(onClick).toHaveBeenCalled();
});
});
Project Structure
webapp/
├── packages/
│ ├── webui/ # Frontend
│ │ ├── src/
│ │ │ ├── components/*.test.jsx # Component tests
│ │ │ └── test/
│ │ │ ├── setup.ts # Test setup
│ │ │ └── *.test.js # Utility tests
│ │ └── vitest.config.ts # Vitest config
│ │
│ └── api/user-service/ # Backend
│ ├── tests/
│ │ ├── conftest.py # Pytest fixtures
│ │ ├── unit/ # Unit tests
│ │ ├── integration/ # Integration tests
│ │ └── factories/ # Test data factories
│ ├── pytest.ini # Pytest config
│ └── .coveragerc # Coverage config
│
└── package.json # NPM scripts
.github/workflows/
├── pr-unit-tests.yml # PR checks
└── nightly-integration-tests.yml # Nightly tests
docs/testing/
├── README.md # Overview
├── unit-testing.md # Unit test guide
├── contributing.md # PR requirements
└── SUMMARY.md # This file
Usage Guide
For Developers
When writing new code:
- Write tests first (TDD)
- Ensure 95% coverage on new files
- Run
pnpm test:unitbefore committing - PR checks will run automatically
When reviewing PRs:
- Verify tests exist for new code
- Check coverage reports
- Run tests locally if needed
- Ensure CI checks pass
For Contributors
Required for every PR:
- Unit tests for new/modified code
- 95% minimum coverage
- All tests passing
- No lint errors
See contributing.md for detailed guidelines.
Maintenance
Adding New Tests
Backend:
# Create test file
touch tests/unit/test_new_feature.py
# Write tests following examples
# Run to verify
python -m pytest tests/unit/test_new_feature.py -v
Frontend:
# Create test file next to component
touch src/components/NewComponent.test.jsx
# Write tests following examples
# Run to verify
pnpm test NewComponent.test.jsx
Updating Coverage Thresholds
Frontend: Edit vitest.config.ts
coverage: {
thresholds: {
lines: 95, // Adjust here
functions: 95,
branches: 95,
statements: 95,
}
}
Backend: Edit pytest.ini
addopts =
--cov-fail-under=95 # Adjust here
Adding GitHub Secrets
- Go to repository Settings → Secrets and variables → Actions
- Click "New repository secret"
- Add required secrets:
OPENAI_API_KEYAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_REGION(optional)
Benefits
For Development
- ✅ Catch bugs early
- ✅ Faster debugging
- ✅ Safe refactoring
- ✅ Living documentation
- ✅ Better code design
For Collaboration
- ✅ Consistent quality bar
- ✅ Easier code reviews
- ✅ Onboarding documentation
- ✅ Prevents regressions
- ✅ CI/CD confidence
For Maintenance
- ✅ Easy to add features
- ✅ Safe to modify code
- ✅ Coverage tracking
- ✅ Automated validation
- ✅ Long-term stability
Next Steps
-
Install dependencies:
cd webapp
pnpm install
cd packages/api/user-service
pip install -r requirements.txt -
Run tests:
cd webapp
pnpm test:unit -
Check coverage:
pnpm test:coverage -
Read the guides:
- Start with
README.md - Review
unit-testing.md - Read
contributing.mdbefore your first PR
- Start with
-
Write your first test:
- Pick a file without tests
- Follow the examples
- Aim for 100% coverage
- Run and verify
Support
- Documentation: Check
docs/testing/ - Examples: Review existing test files
- Questions: Ask in team chat or PR comments
- Issues: Open a GitHub issue for testing infrastructure problems
Testing infrastructure implemented: January 2025 Documentation version: 1.0