mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-03-13 10:30:18 +08:00
* 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
97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const serve = vi.fn(() => ({ close: vi.fn() }));
|
|
const logger = {
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
debug: vi.fn(),
|
|
http: vi.fn(),
|
|
};
|
|
const fork = vi.fn();
|
|
const clusterState = { isPrimary: true };
|
|
const clusterMock = {
|
|
get isPrimary() {
|
|
return clusterState.isPrimary;
|
|
},
|
|
fork,
|
|
};
|
|
const availableParallelism = vi.fn(() => 2);
|
|
|
|
vi.mock('@hono/node-server', () => ({
|
|
serve,
|
|
}));
|
|
vi.mock('@/utils/logger', () => ({
|
|
default: logger,
|
|
}));
|
|
vi.mock('@/utils/common-utils', () => ({
|
|
getLocalhostAddress: () => ['192.0.2.1'],
|
|
}));
|
|
vi.mock('@/app', () => ({
|
|
default: { fetch: vi.fn() },
|
|
}));
|
|
vi.mock('node:cluster', () => ({
|
|
__esModule: true,
|
|
default: clusterMock,
|
|
}));
|
|
vi.mock('node:os', () => ({
|
|
__esModule: true,
|
|
default: {
|
|
availableParallelism,
|
|
},
|
|
}));
|
|
|
|
describe('index', () => {
|
|
afterEach(() => {
|
|
vi.resetModules();
|
|
vi.unstubAllEnvs();
|
|
serve.mockClear();
|
|
fork.mockClear();
|
|
availableParallelism.mockClear();
|
|
logger.info.mockClear();
|
|
clusterState.isPrimary = true;
|
|
});
|
|
|
|
it('starts a server when cluster is disabled', async () => {
|
|
vi.stubEnv('ENABLE_CLUSTER', '');
|
|
vi.stubEnv('LISTEN_INADDR_ANY', '');
|
|
vi.stubEnv('PORT', '12345');
|
|
|
|
const module = await import('@/index');
|
|
expect(module.default).toBeDefined();
|
|
expect(serve).toHaveBeenCalledTimes(1);
|
|
expect(serve.mock.calls[0][0]).toMatchObject({
|
|
hostname: '127.0.0.1',
|
|
port: 12345,
|
|
});
|
|
});
|
|
|
|
it('forks workers when cluster is enabled and primary', async () => {
|
|
clusterState.isPrimary = true;
|
|
vi.stubEnv('ENABLE_CLUSTER', 'true');
|
|
vi.stubEnv('LISTEN_INADDR_ANY', 'true');
|
|
vi.stubEnv('PORT', '12346');
|
|
availableParallelism.mockReturnValue(2);
|
|
|
|
await import('@/index');
|
|
|
|
expect(fork).toHaveBeenCalledTimes(2);
|
|
expect(serve).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('starts a worker server when cluster is enabled and not primary', async () => {
|
|
clusterState.isPrimary = false;
|
|
vi.stubEnv('ENABLE_CLUSTER', 'true');
|
|
vi.stubEnv('LISTEN_INADDR_ANY', '');
|
|
vi.stubEnv('PORT', '12347');
|
|
|
|
await import('@/index');
|
|
|
|
expect(serve).toHaveBeenCalledTimes(1);
|
|
expect(serve.mock.calls[0][0]).toMatchObject({
|
|
hostname: '127.0.0.1',
|
|
port: 12347,
|
|
});
|
|
});
|
|
});
|