File size: 4,587 Bytes
bf48b89 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
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,
}),
},
{
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'),
});
const app = (await import('@/app')).default;
const response = await app.request('/custom-response/hello');
expect(await response.text()).toBe('ok');
});
});
|