# All files / framework/core ConfigManager.ts

0% Statements 0/29
0% Branches 0/35
0% Functions 0/14
0% Lines 0/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198

     import { FrameworkConfig, CacheConfig, PinningConfig } from '../types/framework';

    export interface DatabaseConfig {
      userDirectoryShards?: number;
      defaultGlobalShards?: number;
      cacheSize?: number;
    }

    export interface ExtendedFrameworkConfig extends FrameworkConfig {
      database?: DatabaseConfig;
      debug?: boolean;
      logLevel?: 'error' | 'warn' | 'info' | 'debug';
    }

    export class ConfigManager {
      private config: ExtendedFrameworkConfig;
      private defaults: ExtendedFrameworkConfig = {
        cache: {
          enabled: true,
          maxSize: 1000,
          ttl: 300000, // 5 minutes
        },
        defaultPinning: {
          strategy: 'fixed' as const,
          factor: 2,
        },
        database: {
          userDirectoryShards: 4,
          defaultGlobalShards: 8,
          cacheSize: 100,
        },
        autoMigration: true,
        debug: false,
        logLevel: 'info',
      };

      constructor(config: ExtendedFrameworkConfig = {}) {
        this.config = this.mergeWithDefaults(config);
        this.validateConfig();
      }

      private mergeWithDefaults(config: ExtendedFrameworkConfig): ExtendedFrameworkConfig {
        return {
          ...this.defaults,
          ...config,
          cache: {
            ...this.defaults.cache,
            ...config.cache,
          },
          defaultPinning: {
            ...this.defaults.defaultPinning,
            ...(config.defaultPinning || {}),
          },
          database: {
            ...this.defaults.database,
            ...config.database,
          },
        };
      }

      private validateConfig(): void {
        // Validate cache configuration
        if (this.config.cache) {
          if (this.config.cache.maxSize && this.config.cache.maxSize < 1) {
            throw new Error('Cache maxSize must be at least 1');
          }
          if (this.config.cache.ttl && this.config.cache.ttl < 1000) {
            throw new Error('Cache TTL must be at least 1000ms');
          }
        }

        // Validate pinning configuration
        if (this.config.defaultPinning) {
          if (this.config.defaultPinning.factor && this.config.defaultPinning.factor < 1) {
            throw new Error('Pinning factor must be at least 1');
          }
        }

        // Validate database configuration
        if (this.config.database) {
          if (
            this.config.database.userDirectoryShards &&
            this.config.database.userDirectoryShards < 1
          ) {
            throw new Error('User directory shards must be at least 1');
          }
          if (
            this.config.database.defaultGlobalShards &&
            this.config.database.defaultGlobalShards < 1
          ) {
            throw new Error('Default global shards must be at least 1');
          }
        }
      }

      // Getters for configuration values
      get cacheConfig(): CacheConfig | undefined {
        return this.config.cache;
      }

      get defaultPinningConfig(): PinningConfig | undefined {
        return this.config.defaultPinning;
      }

      get databaseConfig(): DatabaseConfig | undefined {
        return this.config.database;
      }

      get autoMigration(): boolean {
        return this.config.autoMigration || false;
      }

      get debug(): boolean {
        return this.config.debug || false;
      }

      get logLevel(): string {
        return this.config.logLevel || 'info';
      }

      // Update configuration at runtime
      updateConfig(newConfig: Partial<ExtendedFrameworkConfig>): void {
        this.config = this.mergeWithDefaults({
          ...this.config,
          ...newConfig,
        });
        this.validateConfig();
      }

      // Get full configuration
      getConfig(): ExtendedFrameworkConfig {
        return { ...this.config };
      }

      // Configuration presets
      static developmentConfig(): ExtendedFrameworkConfig {
        return {
          debug: true,
          logLevel: 'debug',
          cache: {
            enabled: true,
            maxSize: 100,
            ttl: 60000, // 1 minute for development
          },
          database: {
            userDirectoryShards: 2,
            defaultGlobalShards: 2,
            cacheSize: 50,
          },
          defaultPinning: {
            strategy: 'fixed' as const,
            factor: 1, // Minimal pinning for development
          },
        };
      }

      static productionConfig(): ExtendedFrameworkConfig {
        return {
          debug: false,
          logLevel: 'warn',
          cache: {
            enabled: true,
            maxSize: 10000,
            ttl: 600000, // 10 minutes
          },
          database: {
            userDirectoryShards: 16,
            defaultGlobalShards: 32,
            cacheSize: 1000,
          },
          defaultPinning: {
            strategy: 'popularity' as const,
            factor: 5, // Higher redundancy for production
          },
        };
      }

      static testConfig(): ExtendedFrameworkConfig {
        return {
          debug: true,
          logLevel: 'error', // Minimal logging during tests
          cache: {
            enabled: false, // Disable caching for predictable tests
          },
          database: {
            userDirectoryShards: 1,
            defaultGlobalShards: 1,
            cacheSize: 10,
          },
          defaultPinning: {
            strategy: 'fixed',
            factor: 1,
          },
          autoMigration: false, // Manual migration control in tests
        };
      }
    }