77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import {
|
|
createRedisClient,
|
|
resolveRedisUrl,
|
|
runRedisHealthCheck,
|
|
} from '../src/redis-connection.js';
|
|
|
|
describe('resolveRedisUrl', () => {
|
|
it('prefers VALKEY_URL when both env vars are present', () => {
|
|
const url = resolveRedisUrl({
|
|
VALKEY_URL: 'redis://valkey.local:6379',
|
|
REDIS_URL: 'redis://redis.local:6379',
|
|
});
|
|
|
|
expect(url).toBe('redis://valkey.local:6379');
|
|
});
|
|
|
|
it('falls back to REDIS_URL when VALKEY_URL is missing', () => {
|
|
const url = resolveRedisUrl({
|
|
REDIS_URL: 'redis://redis.local:6379',
|
|
});
|
|
|
|
expect(url).toBe('redis://redis.local:6379');
|
|
});
|
|
|
|
it('throws loudly when no redis environment variable exists', () => {
|
|
expect(() => resolveRedisUrl({})).toThrowError(
|
|
/Missing required Valkey\/Redis connection URL/i,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('createRedisClient', () => {
|
|
it('uses env URL for client creation with no hardcoded defaults', () => {
|
|
class FakeRedis {
|
|
public readonly url: string;
|
|
|
|
public constructor(url: string) {
|
|
this.url = url;
|
|
}
|
|
}
|
|
|
|
const client = createRedisClient({
|
|
env: {
|
|
VALKEY_URL: 'redis://queue.local:6379',
|
|
},
|
|
redisConstructor: FakeRedis,
|
|
});
|
|
|
|
expect(client.url).toBe('redis://queue.local:6379');
|
|
});
|
|
});
|
|
|
|
describe('runRedisHealthCheck', () => {
|
|
it('returns healthy status when ping succeeds', async () => {
|
|
const health = await runRedisHealthCheck({
|
|
ping: () => Promise.resolve('PONG'),
|
|
});
|
|
|
|
expect(health.ok).toBe(true);
|
|
expect(health.response).toBe('PONG');
|
|
expect(health.latencyMs).toBeTypeOf('number');
|
|
expect(health.latencyMs).toBeGreaterThanOrEqual(0);
|
|
});
|
|
|
|
it('returns unhealthy status when ping fails', async () => {
|
|
const health = await runRedisHealthCheck({
|
|
ping: () => Promise.reject(new Error('connection refused')),
|
|
});
|
|
|
|
expect(health.ok).toBe(false);
|
|
expect(health.error).toMatch(/connection refused/i);
|
|
expect(health.latencyMs).toBeTypeOf('number');
|
|
});
|
|
});
|