README.mdβ’5.71 kB
# Universal Tool Test Helpers
This directory contains centralized helpers and utilities extracted from the oversized universal tool test files. It provides a clean, reusable infrastructure for testing universal tools while maintaining strict separation from production code.
## π Structure
```
helpers/
βββ index.ts # Main export point for all helpers
βββ test-constants.ts # Shared constants, budgets, and configuration
βββ mock-data.ts # Mock data factories following mock factory pattern
βββ mock-setup.ts # Standardized mock configurations
βββ test-helpers.ts # Reusable utilities and assertion helpers
βββ integration-helpers.ts # Real API test patterns and utilities
βββ README.md # This documentation
```
## π― Usage Patterns
### Unit Tests (Mocked)
```typescript
import {
setupUnitTestMocks,
MockRecordFactory,
assertionHelpers,
cleanupMocks
} from './helpers';
describe('My Universal Tool Tests', () => {
// Setup mocks before tests
setupUnitTestMocks();
beforeEach(async () => {
await setupMockHandlers();
});
afterEach(() => {
cleanupMocks();
});
it('should test something', async () => {
const mockData = MockRecordFactory.createCompany();
// ... test logic
assertionHelpers.assertValidCompanyRecord(result);
});
});
```
### Integration Tests (Real API)
```typescript
import {
IntegrationTestSetup,
IntegrationTestDataManager,
integrationConfig
} from './helpers';
describe('Integration Tests', () => {
if (integrationConfig.shouldRun()) {
const setup = IntegrationTestSetup.getInstance();
const dataManager = new IntegrationTestDataManager();
beforeAll(async () => {
await setup.initializeApiClient();
});
afterAll(async () => {
await dataManager.cleanupTrackedRecords(toolConfigs);
});
}
});
```
### Performance Tests
```typescript
import {
PerformanceTestRunner,
PERFORMANCE_BUDGETS,
integrationUtils
} from './helpers';
describe('Performance Tests', () => {
const runner = new PerformanceTestRunner();
it('should complete within budget', async () => {
await runner.runPerformanceTest(
'batch create operation',
async () => {
return await batchCreateHandler(params);
},
'tenRecords'
);
});
});
```
## ποΈ Architecture Principles
### Mock Factory Pattern Compliance
- **β
Test Data Isolation**: All mock data is in `/test/` directory
- **β
Production Code Separation**: No production imports in test helpers
- **β
Consistent Interfaces**: All factories follow same patterns
- **β
Type Safety**: Full TypeScript support with proper interfaces
### Clean Architecture
- **Dependency Direction**: Test helpers only depend on test utilities
- **Interface Segregation**: Separate helpers for different test types
- **Single Responsibility**: Each helper file has a focused purpose
- **Open/Closed**: Easy to extend without modifying existing code
### Performance Optimization
- **CI/Local Awareness**: Automatic timeout multipliers for different environments
- **Batch Operations**: Efficient cleanup and data management
- **Resource Management**: Proper tracking and cleanup of test resources
## π Features Extracted
### From `advanced-operations.test.ts` (1065 lines)
- β
Mock setup patterns for specialized handlers
- β
Advanced search parameter factories
- β
Date validation mock utilities
- β
Complex beforeEach/afterEach patterns
### From `core-operations.test.ts` (935 lines)
- β
ErrorService mock configurations
- β
Core operation parameter factories
- β
Standard assertion patterns
- β
Universal resource type handling
### From `integration.test.ts` (865 lines)
- β
Real API client initialization
- β
Test data creation and tracking
- β
Integration cleanup patterns
- β
Environment-specific configuration
### From `performance.test.ts` (753 lines)
- β
Performance budget management
- β
CI multiplier calculations
- β
Batch operation utilities
- β
Performance measurement tools
## π§ Customization
### Adding New Mock Types
```typescript
// In mock-data.ts
export const MockRecordFactory = {
// ... existing factories
createCustomRecord: (overrides = {}) => {
// Your custom mock logic
}
};
```
### Adding New Assertions
```typescript
// In test-helpers.ts
export const assertionHelpers = {
// ... existing assertions
assertValidCustomRecord: (record, expectedField) => {
// Your custom assertion logic
}
};
```
### Environment-Specific Configuration
```typescript
// In test-constants.ts
export const CUSTOM_BUDGETS = {
myOperation: Math.round(5000 * TEST_ENVIRONMENT.ciMultiplier),
};
```
## π§ͺ Testing the Helpers
The helpers themselves follow the same patterns they provide:
- Unit tested with mocks where appropriate
- Integration tested with real dependencies
- Performance characteristics verified
- Clean architecture principles enforced
## π Next Steps for Split Test Files
When splitting the original oversized test files, use these helpers like this:
```typescript
// advanced-operations-search.test.ts
import { setupUnitTestMocks, MockParamsFactory, assertionHelpers } from './helpers';
// integration-core-operations.test.ts
import { IntegrationTestSetup, IntegrationTestDataManager } from './helpers';
// performance-batch-operations.test.ts
import { PerformanceTestRunner, PERFORMANCE_BUDGETS } from './helpers';
```
This provides a consistent, maintainable foundation for all universal tool tests while eliminating code duplication and ensuring proper separation of concerns.