2025-07-15 16:24:31 -06:00
|
|
|
describe('cacheConfig', () => {
|
2025-10-02 07:33:58 -06:00
|
|
|
let originalEnv: NodeJS.ProcessEnv;
|
2025-07-15 16:24:31 -06:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
originalEnv = { ...process.env };
|
|
|
|
|
|
|
|
|
|
// Clear all related env vars first
|
|
|
|
|
delete process.env.REDIS_URI;
|
|
|
|
|
delete process.env.REDIS_CA;
|
|
|
|
|
delete process.env.REDIS_KEY_PREFIX_VAR;
|
|
|
|
|
delete process.env.REDIS_KEY_PREFIX;
|
|
|
|
|
delete process.env.USE_REDIS;
|
2025-08-16 19:41:53 +01:00
|
|
|
delete process.env.USE_REDIS_CLUSTER;
|
2025-07-25 09:00:02 -06:00
|
|
|
delete process.env.REDIS_PING_INTERVAL;
|
2025-07-25 08:23:36 -06:00
|
|
|
delete process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES;
|
2025-07-15 16:24:31 -06:00
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
// Clear module cache
|
2025-07-15 16:24:31 -06:00
|
|
|
jest.resetModules();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
process.env = originalEnv;
|
|
|
|
|
jest.resetModules();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('REDIS_KEY_PREFIX validation and resolution', () => {
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should throw error when both REDIS_KEY_PREFIX_VAR and REDIS_KEY_PREFIX are set', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.REDIS_KEY_PREFIX_VAR = 'DEPLOYMENT_ID';
|
|
|
|
|
process.env.REDIS_KEY_PREFIX = 'manual-prefix';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
await expect(async () => {
|
|
|
|
|
await import('../cacheConfig');
|
|
|
|
|
}).rejects.toThrow('Only either REDIS_KEY_PREFIX_VAR or REDIS_KEY_PREFIX can be set.');
|
2025-07-15 16:24:31 -06:00
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should resolve REDIS_KEY_PREFIX from variable reference', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.REDIS_KEY_PREFIX_VAR = 'DEPLOYMENT_ID';
|
|
|
|
|
process.env.DEPLOYMENT_ID = 'test-deployment-123';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-15 16:24:31 -06:00
|
|
|
expect(cacheConfig.REDIS_KEY_PREFIX).toBe('test-deployment-123');
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should use direct REDIS_KEY_PREFIX value', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.REDIS_KEY_PREFIX = 'direct-prefix';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-15 16:24:31 -06:00
|
|
|
expect(cacheConfig.REDIS_KEY_PREFIX).toBe('direct-prefix');
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should default to empty string when no prefix is configured', async () => {
|
|
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-15 16:24:31 -06:00
|
|
|
expect(cacheConfig.REDIS_KEY_PREFIX).toBe('');
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should handle empty variable reference', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.REDIS_KEY_PREFIX_VAR = 'EMPTY_VAR';
|
|
|
|
|
process.env.EMPTY_VAR = '';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-15 16:24:31 -06:00
|
|
|
expect(cacheConfig.REDIS_KEY_PREFIX).toBe('');
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should handle undefined variable reference', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.REDIS_KEY_PREFIX_VAR = 'UNDEFINED_VAR';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-15 16:24:31 -06:00
|
|
|
expect(cacheConfig.REDIS_KEY_PREFIX).toBe('');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('USE_REDIS and REDIS_URI validation', () => {
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should throw error when USE_REDIS is enabled but REDIS_URI is not set', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.USE_REDIS = 'true';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
await expect(async () => {
|
|
|
|
|
await import('../cacheConfig');
|
|
|
|
|
}).rejects.toThrow('USE_REDIS is enabled but REDIS_URI is not set.');
|
2025-07-15 16:24:31 -06:00
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should not throw error when USE_REDIS is enabled and REDIS_URI is set', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.USE_REDIS = 'true';
|
|
|
|
|
process.env.REDIS_URI = 'redis://localhost:6379';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const importModule = async () => {
|
|
|
|
|
await import('../cacheConfig');
|
|
|
|
|
};
|
|
|
|
|
await expect(importModule()).resolves.not.toThrow();
|
2025-07-15 16:24:31 -06:00
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should handle empty REDIS_URI when USE_REDIS is enabled', async () => {
|
2025-07-15 16:24:31 -06:00
|
|
|
process.env.USE_REDIS = 'true';
|
|
|
|
|
process.env.REDIS_URI = '';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
await expect(async () => {
|
|
|
|
|
await import('../cacheConfig');
|
|
|
|
|
}).rejects.toThrow('USE_REDIS is enabled but REDIS_URI is not set.');
|
2025-07-15 16:24:31 -06:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-16 19:41:53 +01:00
|
|
|
describe('USE_REDIS_CLUSTER configuration', () => {
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should default to false when USE_REDIS_CLUSTER is not set', async () => {
|
|
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-08-16 19:41:53 +01:00
|
|
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should be false when USE_REDIS_CLUSTER is set to false', async () => {
|
2025-08-16 19:41:53 +01:00
|
|
|
process.env.USE_REDIS_CLUSTER = 'false';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-08-16 19:41:53 +01:00
|
|
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should be true when USE_REDIS_CLUSTER is set to true', async () => {
|
2025-08-16 19:41:53 +01:00
|
|
|
process.env.USE_REDIS_CLUSTER = 'true';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-08-16 19:41:53 +01:00
|
|
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should work with USE_REDIS enabled and REDIS_URI set', async () => {
|
2025-08-16 19:41:53 +01:00
|
|
|
process.env.USE_REDIS_CLUSTER = 'true';
|
|
|
|
|
process.env.USE_REDIS = 'true';
|
|
|
|
|
process.env.REDIS_URI = 'redis://localhost:6379';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-08-16 19:41:53 +01:00
|
|
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(true);
|
|
|
|
|
expect(cacheConfig.USE_REDIS).toBe(true);
|
|
|
|
|
expect(cacheConfig.REDIS_URI).toBe('redis://localhost:6379');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-15 16:24:31 -06:00
|
|
|
describe('REDIS_CA file reading', () => {
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should be null when REDIS_CA is not set', async () => {
|
|
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-15 16:24:31 -06:00
|
|
|
expect(cacheConfig.REDIS_CA).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-07-25 08:23:36 -06:00
|
|
|
|
2025-07-25 09:00:02 -06:00
|
|
|
describe('REDIS_PING_INTERVAL configuration', () => {
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should default to 0 when REDIS_PING_INTERVAL is not set', async () => {
|
|
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-25 09:00:02 -06:00
|
|
|
expect(cacheConfig.REDIS_PING_INTERVAL).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should use provided REDIS_PING_INTERVAL value', async () => {
|
2025-07-25 09:00:02 -06:00
|
|
|
process.env.REDIS_PING_INTERVAL = '300';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-25 09:00:02 -06:00
|
|
|
expect(cacheConfig.REDIS_PING_INTERVAL).toBe(300);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-25 08:23:36 -06:00
|
|
|
describe('FORCED_IN_MEMORY_CACHE_NAMESPACES validation', () => {
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should parse comma-separated cache keys correctly', async () => {
|
2025-09-10 17:01:44 -06:00
|
|
|
process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES = ' ROLES, MESSAGES ';
|
2025-07-25 08:23:36 -06:00
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
|
|
|
|
expect(cacheConfig.FORCED_IN_MEMORY_CACHE_NAMESPACES).toEqual(['ROLES', 'MESSAGES']);
|
2025-07-25 08:23:36 -06:00
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should throw error for invalid cache keys', async () => {
|
2025-07-25 08:23:36 -06:00
|
|
|
process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES = 'INVALID_KEY,ROLES';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
await expect(async () => {
|
|
|
|
|
await import('../cacheConfig');
|
|
|
|
|
}).rejects.toThrow('Invalid cache keys in FORCED_IN_MEMORY_CACHE_NAMESPACES: INVALID_KEY');
|
2025-07-25 08:23:36 -06:00
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should handle empty string gracefully', async () => {
|
2025-07-25 08:23:36 -06:00
|
|
|
process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES = '';
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-25 08:23:36 -06:00
|
|
|
expect(cacheConfig.FORCED_IN_MEMORY_CACHE_NAMESPACES).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 07:33:58 -06:00
|
|
|
test('should handle undefined env var gracefully', async () => {
|
|
|
|
|
const { cacheConfig } = await import('../cacheConfig');
|
2025-07-25 08:23:36 -06:00
|
|
|
expect(cacheConfig.FORCED_IN_MEMORY_CACHE_NAMESPACES).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-07-15 16:24:31 -06:00
|
|
|
});
|