Skip to content

move dynamic module composition behind typed environment config - #340

Open
Emoji-dot wants to merge 1 commit into
BlockDash-Studios:mainfrom
Emoji-dot:feat/RUSTACADEMY
Open

move dynamic module composition behind typed environment config#340
Emoji-dot wants to merge 1 commit into
BlockDash-Studios:mainfrom
Emoji-dot:feat/RUSTACADEMY

Conversation

@Emoji-dot

Copy link
Copy Markdown
Contributor

close #336
What Was Changed

  1. Created EnvironmentModuleLoader Class
    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

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.

move dynamic module composition behind typed environment config

1 participant