move dynamic module composition behind typed environment config - #340
Open
Emoji-dot wants to merge 1 commit into
Open
move dynamic module composition behind typed environment config#340Emoji-dot wants to merge 1 commit into
Emoji-dot wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
close #336
What Was Changed
File: app/backend/src/module-factory.ts
Class-based encapsulation replacing the standalone function approach
Three public methods:
getModules() - Returns dynamic modules based on feature flags (ReconciliationModule, NotificationsModule, DeveloperModule)
getProviders() - Returns conditional providers (IngestionBootstrapService when INGESTION_ENABLED)
getConfig() - Exposes the validated environment configuration
Private validation method:
validateConfiguration() - Runs production safety checks (blocks DeveloperModule in production)
Backward compatibility: Kept getDynamicModules() function as a deprecated wrapper
2. Refactored app.module.ts
File: app/backend/src/app.module.ts
Before:
typescript
const validatedEnv = envSchema.validate(...).value as EnvConfig;
@module({
imports: [
...getDynamicModules(validatedEnv),
],
providers: [
...(validatedEnv.INGESTION_ENABLED ? [IngestionBootstrapService] : []),
],
})
After:
typescript
const validatedEnv = envSchema.validate(...).value as EnvConfig;
const moduleLoader = new EnvironmentModuleLoader(validatedEnv);
@module({
imports: [
...moduleLoader.getModules(),
],
providers: [
...moduleLoader.getProviders(),
],
})
Changes:
Replaced getDynamicModules(validatedEnv) with moduleLoader.getModules()
Replaced inline ternary INGESTION_ENABLED check with moduleLoader.getProviders()
Removed unused IngestionBootstrapService import (now handled internally by loader)
3. Comprehensive Test Coverage
File: app/backend/src/module-factory.unit.spec.ts (19 tests)
Tests for EnvironmentModuleLoader class:
✅ getModules() with various feature flag combinations (6 tests)
✅ getProviders() for ingestion service registration (3 tests)
✅ getConfig() returns validated config (1 test)
✅ Constructor validation for production safety (3 tests)
✅ Backward compatibility for deprecated getDynamicModules() (6 tests)
File: app/backend/src/app.module.unit.spec.ts (10 tests)
Integration tests for AppModule:
✅ Module compilation with different feature flag configurations (6 tests)
✅ Production safety checks (2 tests)
✅ Environment configuration integration (2 tests)
4. Test Execution Results
Cleaned up 318 stray .js files from src/ directory that were causing test failures
All 29 tests passing:
module-factory.unit.spec.ts: 19/19 ✅
app.module.unit.spec.ts: 10/10 ✅
Benefits of the Refactoring
Better Encapsulation - Logic is grouped in a cohesive class instead of scattered functions
Type Safety - All methods have explicit return types and typed parameters
Improved Testability - Easier to test module composition in isolation
Maintainability - Clear separation of concerns (validation, module loading, provider registration)
Runtime Safety - Production checks happen at instantiation time, failing fast
Extensibility - Easy to add new conditional modules or providers in the future
Backward Compatibility - Existing code using getDynamicModules() still works
Files Modified
✅ app/backend/src/module-factory.ts - Created EnvironmentModuleLoader class
✅ app/backend/src/app.module.ts - Updated to use the new loader
✅ app/backend/src/module-factory.unit.spec.ts - Comprehensive unit tests
✅ app/backend/src/app.module.unit.spec.ts - Integration tests