mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-16 16:44:53 +08:00
* feat(types): add upvotes, downvotes, and comments to DataItem * refactor: fix TS2339 * refactor: fix TS2322 * refactor: fix TS2322 * refactor: fix TS2345 * refactor: fix TS2339 * refactor: fix TS2322 * refactor: fix TS2345 * refactor: fix TS2531/2532/18048/18047 * refactor: fix TS18046/TS2571 * refactor: fix TS2538 * refactor: fix TS2554/2769/2558 * refactor: fix TS2362/2363 * refactor: fix TS2353/TS2561 * refactor: fix TS2741/2739/2740 * refactor: fix TS2304 TS2307 TS2305 TS2614 TS2552 TS2551 * refactor: fix TS2352 * refactor: fix TS2604 TS17001 * refactor: fix TS2367 TS2366 * refactor: fix TS2493 * refactor: fix TS2454 * refactor: fix TS2790 * refactor: fix TS2488 * refactor: fix TS2677 * refactor: fix TS18048/18049/2532 * refactor: fix TS2322 * refactor: fix TS2344 * refactor: fix TS2339 * refactor: fix TS2339 * refactor: fix TS2365 * refactor: fix TS1117 * refactor: fix TS2561 * refactor: fix TS2555 * refactor: fix TS2503 * refactor: fix TS2339 mobile api no longer work * fix: clean slop * fix: use Context instead of any * fix: fix more typing after merge * fix: add void return for ctx.redirect in handler * chore: add type check to lint and husky * fix: remove ts ignore * chore: add oxlint rule * fix: suppress build artifact * fix: update getArticles to use cache * fix: codeql * fix(route/typeless): use async gunzip * fix: address review * fix: address review * fix: double quotes escape
97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const serve = vi.fn<(...args: any[]) => any>(() => ({ 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,
|
|
});
|
|
});
|
|
});
|