diff --git a/packages/core/src/caches/index.test.ts b/packages/core/src/caches/index.test.ts index e3511b851..720c246df 100644 --- a/packages/core/src/caches/index.test.ts +++ b/packages/core/src/caches/index.test.ts @@ -1,4 +1,7 @@ import { createMockUtils } from '@logto/shared/esm'; +import Sinon from 'sinon'; + +import { EnvSet } from '#src/env-set/index.js'; const { jest } = import.meta; const { mockEsm } = createMockUtils(jest); @@ -26,7 +29,7 @@ mockEsm('redis', () => ({ }), })); -const { RedisCache, RedisClusterCache } = await import('./index.js'); +const { RedisCache, RedisClusterCache, redisCacheFactory } = await import('./index.js'); describe('RedisCache', () => { it('should successfully construct with no REDIS_URL', async () => { @@ -46,8 +49,13 @@ describe('RedisCache', () => { it('should successfully construct with a Redis client', async () => { for (const url of ['1', 'redis://url']) { jest.clearAllMocks(); - const cache = new RedisCache(url); + const stub = Sinon.stub(EnvSet, 'values').value({ + ...EnvSet.values, + redisUrl: url, + }); + const cache = redisCacheFactory(); + expect(cache instanceof RedisCache).toBeTruthy(); expect(cache.client).toBeTruthy(); // eslint-disable-next-line no-await-in-loop @@ -61,14 +69,20 @@ describe('RedisCache', () => { // Do sanity check only expect(mockFunction).toBeCalledTimes(6); + stub.restore(); } }); it('should successfully construct with a Redis Cluster client', async () => { - for (const url of ['redis://url', 'redis:?host=h1&host=h2&host=h3']) { + for (const url of ['redis://url?cluster=1', 'redis:?host=h1&host=h2&host=h3&cluster=true']) { jest.clearAllMocks(); - const cache = new RedisClusterCache(new URL(url)); + const stub = Sinon.stub(EnvSet, 'values').value({ + ...EnvSet.values, + redisUrl: url, + }); + const cache = redisCacheFactory(); + expect(cache instanceof RedisClusterCache).toBeTruthy(); expect(cache.client).toBeTruthy(); // eslint-disable-next-line no-await-in-loop @@ -82,6 +96,7 @@ describe('RedisCache', () => { // Do sanity check only expect(mockFunction).toBeCalledTimes(6); + stub.restore(); } }); }); diff --git a/packages/core/src/caches/index.ts b/packages/core/src/caches/index.ts index 20666babe..9e9e6bc5e 100644 --- a/packages/core/src/caches/index.ts +++ b/packages/core/src/caches/index.ts @@ -135,12 +135,12 @@ export class RedisClusterCache extends RedisCacheBase { } } -const redisCacheFactory = (): RedisCacheBase => { +export const redisCacheFactory = (): RedisCacheBase => { const { redisUrl } = EnvSet.values; if (redisUrl) { - const url = new URL(redisUrl); - if (yes(url.searchParams.get('redis_cluster'))) { + const url = trySafe(() => new URL(redisUrl)); + if (url && yes(url.searchParams.get('cluster'))) { return new RedisClusterCache(url); } }