Files
RSSHub/lib/utils/cache.test.ts
T
DIYgod 81ff849337 feat(tests): add comprehensive unit tests for various utility functions and views (#20779)
* feat(tests): add comprehensive unit tests for various utility functions and views

- Implement tests for cache utility to ensure proper behavior with no cache and TTL keys.
- Add tests for common utilities including string manipulation and path handling.
- Introduce tests for directory import functionality to validate file imports and pattern matching.
- Create tests for git hash retrieval to handle fallback scenarios.
- Develop tests for deprecated got utility to verify response handling and retry logic.
- Enhance got utility tests to include request hooks and search parameter handling.
- Mock header generator tests to validate user agent handling.
- Expand helpers tests to cover current path retrieval and duration parsing.
- Implement tests for ofetch utility to ensure proper proxy handling and logging.
- Add OpenTelemetry metric tests to validate metric serialization.
- Create proxy tests to verify multi-proxy selection and failure handling.
- Introduce request rewriter tests to validate fetch and get wrapper functionality.
- Add timezone utility tests to handle various input types.
- Implement view tests for Atom and RSS rendering to ensure correct output.
- Create index view tests to validate debug information display based on configuration.

* refactor: remove deprecated got implementation and associated tests

* test: expand coverage for api, middleware, and utils
2025-12-29 17:34:48 +08:00

101 lines
3.3 KiB
TypeScript

import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
import wait from '@/utils/wait';
beforeAll(() => {
process.env.CACHE_EXPIRE = '1';
process.env.CACHE_CONTENT_EXPIRE = '3';
});
afterEach(() => {
delete process.env.CACHE_TYPE;
vi.resetModules();
});
afterAll(() => {
delete process.env.CACHE_EXPIRE;
});
describe('cache', () => {
it('memory', async () => {
process.env.CACHE_TYPE = 'memory';
const cache = (await import('@/utils/cache')).default;
if (!cache.clients.memoryCache || !cache.status.available) {
throw new Error('Memory cache client error');
}
await cache.set('mock', undefined);
expect(await cache.get('mock')).toBe('');
await cache.globalCache.set('mock', undefined);
expect(await cache.globalCache.get('mock')).toBe('');
await cache.globalCache.set('mock', {
mock: 1,
});
expect(await cache.globalCache.get('mock')).toBe('{"mock":1}');
}, 10000);
it('memory get returns null before init', async () => {
const memory = (await import('@/utils/cache/memory')).default;
expect(await memory.get('missing')).toBeNull();
});
it('redis', async () => {
process.env.CACHE_TYPE = 'redis';
const cache = (await import('@/utils/cache')).default;
await wait(500);
if (!cache.clients.redisClient || !cache.status.available) {
throw new Error('Redis client error');
}
await cache.set('mock1', undefined);
expect(await cache.get('mock1')).toBe('');
await cache.set('mock2', '2');
await cache.set('mock2', '2');
expect(await cache.get('mock2')).toBe('2');
await cache.clients.redisClient?.quit();
}, 10000);
it('redis with quit', async () => {
process.env.CACHE_TYPE = 'redis';
const cache = (await import('@/utils/cache')).default;
if (cache.clients.redisClient) {
await cache.clients.redisClient.quit();
} else {
throw new Error('No redis client');
}
await cache.set('mock2', '2');
expect(await cache.get('mock2')).toBe(null);
});
it('redis with error', async () => {
process.env.CACHE_TYPE = 'redis';
process.env.REDIS_URL = 'redis://wrongpath:6379';
const cache = (await import('@/utils/cache')).default;
await cache.set('mock2', '2');
expect(await cache.get('mock2')).toBe(null);
cache.clients.redisClient?.disconnect();
});
it('no cache', async () => {
process.env.CACHE_TYPE = 'NO';
const cache = (await import('@/utils/cache')).default;
await cache.init();
await cache.set('mock2', '2');
expect(await cache.get('mock2')).toBe(null);
expect(await cache.globalCache.get('mock2')).toBeNull();
expect(cache.globalCache.set('mock2', '2')).toBeNull();
});
it('throws TTL key', async () => {
process.env.CACHE_TYPE = 'redis';
const cache = (await import('@/utils/cache')).default;
try {
await cache.get('rsshub:cacheTtl:mock');
} catch (error: any) {
expect(error.message).toContain('reserved for the internal usage');
} finally {
await cache.clients.redisClient?.quit();
}
});
});