Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/metrics-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function isTruthyCacheBypass(value: string | null): boolean {
return ["1", "true", "yes", "on"].includes(value.trim().toLowerCase());
}

function getRedisClient(): Redis | null {
export function getRedisClient(): Redis | null {
if (redisClient !== undefined) {
return redisClient;
}
Expand Down
50 changes: 50 additions & 0 deletions test/getRedisClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('@upstash/redis', () => {
const MockRedis = vi.fn(function() { return {}; });
return { Redis: MockRedis };
});

describe('getRedisClient lazy initialization', () => {
beforeEach(() => {
vi.resetModules();
});

it('returns null when UPSTASH_REDIS_REST_URL is not set', async () => {
delete process.env.UPSTASH_REDIS_REST_URL;
delete process.env.UPSTASH_REDIS_REST_TOKEN;

const { getRedisClient } = await import('../src/lib/metrics-cache');
const client = getRedisClient();
expect(client).toBeNull();
});

it('returns null when UPSTASH_REDIS_REST_TOKEN is not set', async () => {
process.env.UPSTASH_REDIS_REST_URL = 'https://example.upstash.io';
delete process.env.UPSTASH_REDIS_REST_TOKEN;

const { getRedisClient } = await import('../src/lib/metrics-cache');
const client = getRedisClient();
expect(client).toBeNull();
});

it('returns Redis instance when both env vars are set', async () => {
process.env.UPSTASH_REDIS_REST_URL = 'https://example.upstash.io';
process.env.UPSTASH_REDIS_REST_TOKEN = 'test-token-123';

const { getRedisClient } = await import('../src/lib/metrics-cache');
const client = getRedisClient();
expect(client).not.toBeNull();
expect(client).toBeDefined();
});

it('returns same singleton instance on repeated calls', async () => {
process.env.UPSTASH_REDIS_REST_URL = 'https://example.upstash.io';
process.env.UPSTASH_REDIS_REST_TOKEN = 'test-token-123';

const { getRedisClient } = await import('../src/lib/metrics-cache');
const client1 = getRedisClient();
const client2 = getRedisClient();
expect(client1).toBe(client2);
});
});
Loading