Files
RSSHub/lib/pkg.test.ts
T
Tony bb12119123 refactor: enforce typecheck (#22917)
* 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
2026-08-04 09:00:41 +08:00

140 lines
4.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Route } from '@/types';
describe('pkg', () => {
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
delete process.env.IS_PACKAGE;
delete process.env.UA;
});
it('requires init before request', async () => {
const { request } = await import('./pkg');
await expect(request('/test/1')).rejects.toThrow('RSSHub not initialized. Please call init() first.');
});
it('config', async () => {
const { init } = await import('./pkg');
await init({
UA: 'mock',
});
const { config } = await import('./config');
expect(config.ua).toBe('mock');
});
it('request', async () => {
const { init, request } = await import('./pkg');
await init();
const data = await request('/test/1');
expect(data).toMatchObject({
atomlink: 'http://localhost/test/1',
title: 'Test 1',
itunes_author: null,
link: 'https://github.com/DIYgod/RSSHub',
item: [
{
title: 'Title1',
description: 'Description1',
pubDate: 'Mon, 31 Dec 2018 15:59:50 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/1',
author: 'DIYgod1',
},
{
title: 'Title2',
description: 'Description2',
pubDate: 'Mon, 31 Dec 2018 15:59:40 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/2',
author: 'DIYgod2',
},
{
title: 'Title3',
description: 'Description3',
pubDate: 'Mon, 31 Dec 2018 15:59:30 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/3',
author: 'DIYgod3',
},
{
title: 'Title4',
description: 'Description4',
pubDate: 'Mon, 31 Dec 2018 15:59:20 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/4',
author: 'DIYgod4',
},
{
title: 'Title5',
description: 'Description5',
pubDate: 'Mon, 31 Dec 2018 15:59:10 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/5',
author: 'DIYgod5',
},
],
allowEmpty: false,
});
});
it('error', async () => {
try {
const { init, request } = await import('./pkg');
await init();
await request('/test/error');
} catch (error) {
expect(error).toBe('Error test');
}
});
it('registerRoute adds custom routes and namespaces', async () => {
const { init, registerRoute, request } = await import('./pkg');
await init();
await registerRoute(
'custom',
{
path: '/hello',
name: 'Custom Hello',
handler: () => ({
title: 'Custom',
link: 'https://example.com',
item: [
{
title: 'Entry',
link: 'https://example.com/entry',
},
],
allowEmpty: true,
}),
} as unknown as Route,
{
name: 'Custom Namespace',
url: 'https://example.com',
lang: 'en',
}
);
const data = await request('/custom/hello');
expect(data.title).toBe('Custom');
const { namespaces } = await import('./registry');
expect(namespaces.custom?.name).toBe('Custom Namespace');
expect(namespaces.custom?.routes['/hello']).toBeDefined();
});
it('registerRoute supports handlers that return Response', async () => {
const { init, registerRoute } = await import('./pkg');
await init();
await registerRoute('custom-response', {
path: '/hello',
name: 'Custom Response',
handler: () => new Response('ok'),
} as unknown as Route);
const app = (await import('@/app')).default;
const response = await app.request('/custom-response/hello');
expect(await response.text()).toBe('ok');
});
});