Yuki Takei il y a 11 mois
Parent
commit
291576b951

+ 59 - 1
apps/app/src/server/service/config-manager/config-manager.spec.ts

@@ -1,9 +1,14 @@
+import type { RawConfigData } from '@growi/core/dist/interfaces';
 import { mock } from 'vitest-mock-extended';
 
 import type { S2sMessagingService } from '../s2s-messaging/base';
 
+import type { ConfigKey, ConfigValues } from './config-definition';
 import { configManager } from './config-manager';
 
+// Test helper type for setting configs
+type TestConfigData = RawConfigData<ConfigKey, ConfigValues>;
+
 const mocks = vi.hoisted(() => ({
   ConfigMock: {
     updateOne: vi.fn(),
@@ -104,7 +109,6 @@ describe('ConfigManager test', () => {
   });
 
   describe('getManagedEnvVars()', () => {
-
     beforeAll(async() => {
       process.env.AUTO_INSTALL_ADMIN_USERNAME = 'admin';
       process.env.AUTO_INSTALL_ADMIN_PASSWORD = 'password';
@@ -129,7 +133,61 @@ describe('ConfigManager test', () => {
       expect(result.AUTO_INSTALL_ADMIN_USERNAME).toEqual('admin');
       expect(result.AUTO_INSTALL_ADMIN_PASSWORD).toEqual('***');
     });
+  });
+
+  describe('getConfig()', () => {
+    // Helper function to set configs with proper typing
+    const setTestConfigs = (dbConfig: Partial<TestConfigData>, envConfig: Partial<TestConfigData>): void => {
+      Object.defineProperties(configManager, {
+        dbConfig: { value: dbConfig, configurable: true },
+        envConfig: { value: envConfig, configurable: true },
+      });
+    };
+
+    beforeEach(async() => {
+      // Reset configs before each test using properly typed empty objects
+      setTestConfigs({}, {});
+    });
 
+    test('should fallback to env value when dbConfig[key] exists but its value is undefined', async() => {
+      // Prepare test data that simulates the issue with proper typing
+      const dbConfig: Partial<TestConfigData> = {
+        'app:title': { value: undefined },
+      };
+      const envConfig: Partial<TestConfigData> = {
+        'app:title': { value: 'GROWI' },
+      };
+      setTestConfigs(dbConfig, envConfig);
+
+      // Act
+      const result = configManager.getConfig('app:title');
+
+      // Assert - Should return env value since db value is undefined
+      expect(result).toBe('GROWI');
+    });
+
+    test('should handle various edge case scenarios correctly', async() => {
+      // Setup multiple test scenarios with proper typing
+      const dbConfig: Partial<TestConfigData> = {
+        'app:title': { value: undefined }, // db value is explicitly undefined
+        'app:siteUrl': { value: undefined }, // another undefined value
+        'app:fileUpload': undefined, // db config entry itself is undefined
+        'app:fileUploadType': { value: 'gridfs' }, // db has valid value
+      };
+      const envConfig: Partial<TestConfigData> = {
+        'app:title': { value: 'GROWI' },
+        'app:siteUrl': { value: 'https://example.com' },
+        'app:fileUpload': { value: true },
+        'app:fileUploadType': { value: 'aws' },
+      };
+      setTestConfigs(dbConfig, envConfig);
+
+      // Test each scenario
+      expect(configManager.getConfig('app:title')).toBe('GROWI'); // Should fallback to env when db value is undefined
+      expect(configManager.getConfig('app:siteUrl')).toBe('https://example.com'); // Should fallback to env when db value is undefined
+      expect(configManager.getConfig('app:fileUpload')).toBe(true); // Should fallback to env when db config is undefined
+      expect(configManager.getConfig('app:fileUploadType')).toBe('gridfs'); // Should use db value when valid
+    });
   });
 
 });