Files
RSSHub/lib/middleware/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

191 lines
6.8 KiB
TypeScript

import Parser from 'rss-parser';
import { afterEach, describe, expect, it, vi } from 'vitest';
import wait from '@/utils/wait';
process.env.CACHE_EXPIRE = '1';
process.env.CACHE_CONTENT_EXPIRE = '2';
const parser = new Parser();
afterEach(() => {
vi.resetModules();
});
const noCacheTestFunc = async () => {
const app = (await import('@/app')).default;
const response1 = await app.request('/test/cache');
const response2 = await app.request('/test/cache');
const parsed1 = await parser.parseString(await response1.text());
const parsed2 = await parser.parseString(await response2.text());
expect(response2.status).toBe(200);
expect(response2.headers).not.toHaveProperty('rsshub-cache-status');
expect(parsed1.items[0].content).toBe('Cache1');
expect(parsed2.items[0].content).toBe('Cache2');
expect(parsed1.ttl).toEqual('1');
};
describe('cache', () => {
it('memory', async () => {
process.env.CACHE_TYPE = 'memory';
const app = (await import('@/app')).default;
const response1 = await app.request('/test/cache');
const response2 = await app.request('/test/cache');
const parsed1 = await parser.parseString(await response1.text());
const parsed2 = await parser.parseString(await response2.text());
delete parsed1.lastBuildDate;
delete parsed2.lastBuildDate;
delete parsed1.feedUrl;
delete parsed2.feedUrl;
delete parsed1.paginationLinks;
delete parsed2.paginationLinks;
expect(parsed2).toMatchObject(parsed1);
expect(response2.status).toBe(200);
expect(response2.headers.get('rsshub-cache-status')).toBe('HIT');
expect(parsed1.ttl).toEqual('1');
await wait(1 * 1000 + 100);
const response3 = await app.request('/test/cache');
expect(response3.headers).not.toHaveProperty('rsshub-cache-status');
const parsed3 = await parser.parseString(await response3.text());
await wait(2 * 1000 + 100);
const response4 = await app.request('/test/cache');
const parsed4 = await parser.parseString(await response4.text());
expect(parsed1.items[0].content).toBe('Cache1');
expect(parsed2.items[0].content).toBe('Cache1');
expect(parsed3.items[0].content).toBe('Cache1');
expect(parsed4.items[0].content).toBe('Cache2');
await app.request('/test/refreshCache');
await wait(1 * 1000 + 100);
const response5 = await app.request('/test/refreshCache');
const parsed5 = await parser.parseString(await response5.text());
await wait(1 * 1000 + 100);
const response6 = await app.request('/test/refreshCache');
const parsed6 = await parser.parseString(await response6.text());
expect(parsed5.items[0].content).toBe('1 1');
expect(parsed6.items[0].content).toBe('1 0');
}, 10000);
it('redis', async () => {
process.env.CACHE_TYPE = 'redis';
const app = (await import('@/app')).default;
await wait(500);
const response1 = await app.request('/test/cache');
const response2 = await app.request('/test/cache');
const parsed1 = await parser.parseString(await response1.text());
const parsed2 = await parser.parseString(await response2.text());
delete parsed1.lastBuildDate;
delete parsed2.lastBuildDate;
delete parsed1.feedUrl;
delete parsed2.feedUrl;
delete parsed1.paginationLinks;
delete parsed2.paginationLinks;
expect(parsed2).toMatchObject(parsed1);
expect(response2.status).toBe(200);
expect(response2.headers.get('rsshub-cache-status')).toBe('HIT');
expect(parsed1.ttl).toEqual('1');
await wait(1 * 1000 + 100);
const response3 = await app.request('/test/cache');
expect(response3.headers).not.toHaveProperty('rsshub-cache-status');
const parsed3 = await parser.parseString(await response3.text());
await wait(2 * 1000 + 100);
const response4 = await app.request('/test/cache');
const parsed4 = await parser.parseString(await response4.text());
expect(parsed1.items[0].content).toBe('Cache1');
expect(parsed2.items[0].content).toBe('Cache1');
expect(parsed3.items[0].content).toBe('Cache1');
expect(parsed4.items[0].content).toBe('Cache2');
await app.request('/test/refreshCache');
await wait(1 * 1000 + 100);
const response5 = await app.request('/test/refreshCache');
const parsed5 = await parser.parseString(await response5.text());
await wait(1 * 1000 + 100);
const response6 = await app.request('/test/refreshCache');
const parsed6 = await parser.parseString(await response6.text());
expect(parsed5.items[0].content).toBe('1 1');
expect(parsed6.items[0].content).toBe('1 0');
const cache = (await import('@/utils/cache')).default;
await cache.clients.redisClient!.quit();
}, 10000);
it('redis with quit', async () => {
process.env.CACHE_TYPE = 'redis';
const cache = (await import('@/utils/cache')).default;
await cache.clients.redisClient!.quit();
await noCacheTestFunc();
});
it('redis with error', async () => {
process.env.CACHE_TYPE = 'redis';
process.env.REDIS_URL = 'redis://wrongpath:6379';
await noCacheTestFunc();
const cache = (await import('@/utils/cache')).default;
cache.clients.redisClient?.disconnect();
}, 20000);
it('no cache', async () => {
process.env.CACHE_TYPE = 'NO';
await noCacheTestFunc();
});
it('no cache (empty string)', async () => {
process.env.CACHE_TYPE = '';
await noCacheTestFunc();
});
it('throws URL key', async () => {
process.env.CACHE_TYPE = 'memory';
const app = (await import('@/app')).default;
try {
const response = await app.request('/test/cacheUrlKey');
expect(response).toThrow(Error);
} catch (error: any) {
expect(error.message).toContain('Cache key must be a string');
}
});
it('RSS TTL (no cache)', async () => {
process.env.CACHE_TYPE = '';
process.env.CACHE_EXPIRE = '600';
const app = (await import('@/app')).default;
const response = await app.request('/test/cache');
const parsed = await parser.parseString(await response.text());
expect(parsed.ttl).toEqual('1');
});
it('RSS TTL (w/ cache)', async () => {
process.env.CACHE_TYPE = 'memory';
process.env.CACHE_EXPIRE = '600';
const app = (await import('@/app')).default;
const response = await app.request('/test/cache');
const parsed = await parser.parseString(await response.text());
expect(parsed.ttl).toEqual('10');
});
});