Files
lolympus-tournament-support/apps/api/src/modules/global/redis/redis.module.spec.ts
T

100 lines
3.6 KiB
TypeScript

import { Test } from '@nestjs/testing';
import { createClient } from 'redis';
import { AuctionRedisModule } from '../../auction/redis/auction-redis.module';
import { AuctionRedisService } from '../../auction/redis/auction-redis.service';
import { SecretService } from '../secret/secret.service';
import { RedisModule } from './redis.module';
import { RedisService, type RedisClient } from './redis.service';
jest.mock('redis', () => ({ createClient: jest.fn() }));
describe('shared Garnet instance', () => {
it.each([
{
name: 'only GARNET_ENDPOINT is configured',
secrets: { GARNET_ENDPOINT: 'garnet.internal:6379' },
options: { url: 'redis://garnet.internal:6379' },
},
{
name: 'existing Garnet credentials are configured',
secrets: {
GARNET_ENDPOINT: ' rediss://garnet.internal:6380/1 ',
GARNET_USERID: 'garnet-user',
GARNET_PASSWORD: 'garnet-password',
},
options: {
url: 'rediss://garnet.internal:6380/1',
username: 'garnet-user',
password: 'garnet-password',
},
},
{
name: 'obsolete Redis settings are also configured',
secrets: {
GARNET_ENDPOINT: 'garnet.internal:6379',
GARNET_USERNAME: 'garnet-user',
GARNET_PASSWORD: 'garnet-password',
GARNET_URL: 'redis://obsolete-garnet.internal:6379',
REDIS_URL: 'redis://obsolete-common.internal:6379',
REDIS_ENDPOINT: 'obsolete-common.internal:6379',
REDIS_USERNAME: 'obsolete-common-user',
REDIS_PASSWORD: 'obsolete-common-password',
AUCTION_REDIS_URL: 'redis://obsolete-common.internal:6379',
AUCTION_REDIS_ENDPOINT: 'obsolete-common.internal:6379',
AUCTION_REDIS_USERNAME: 'obsolete-auction-user',
AUCTION_REDIS_PASSWORD: 'obsolete-auction-password',
},
options: {
url: 'redis://garnet.internal:6379',
username: 'garnet-user',
password: 'garnet-password',
},
},
])('boots both Redis modules when $name', async ({ secrets, options }) => {
const values: Record<string, string | undefined> = secrets;
const secretService = {
ready: jest.fn().mockResolvedValue(undefined),
get: jest.fn((key: string, fallback: string) =>
Promise.resolve(values[key] ?? fallback),
),
};
const clients = Array.from({ length: 2 }, () => ({
connect: jest.fn().mockResolvedValue(undefined),
on: jest.fn(),
quit: jest.fn().mockResolvedValue(undefined),
destroy: jest.fn(),
}));
const createClientMock = jest.mocked(createClient);
createClientMock.mockReset();
for (const client of clients) {
createClientMock.mockReturnValueOnce(client as unknown as RedisClient);
}
const module = await Test.createTestingModule({
imports: [RedisModule, AuctionRedisModule],
})
.overrideProvider(SecretService)
.useValue(secretService)
.compile();
try {
await module.init();
expect(createClientMock).toHaveBeenCalledTimes(2);
for (let index = 0; index < clients.length; index++) {
expect(createClientMock).toHaveBeenNthCalledWith(index + 1, options);
expect(clients[index].connect).toHaveBeenCalledTimes(1);
}
expect(clients).toContain(module.get(RedisService).raw());
expect(clients).toContain(module.get(AuctionRedisService).raw());
expect(module.get(RedisService).raw()).not.toBe(
module.get(AuctionRedisService).raw(),
);
} finally {
await module.close();
}
for (const client of clients) {
expect(client.quit).toHaveBeenCalledTimes(1);
}
});
});