File size: 5,540 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import { describe, expect, it } from 'vitest';
process.env.OPENAI_API_KEY = 'sk-1234567890';
process.env.OPENAI_API_ENDPOINT = 'https://api.openai.mock/v1';
const { config } = await import('@/config');
const { default: parameter } = await import('@/middleware/parameter');
const runMiddleware = async (data: any, query: Record<string, string | undefined>) => {
const store = new Map<string, unknown>([['data', data]]);
const ctx = {
req: {
query: (key: string) => query[key],
},
get: (key: string) => store.get(key),
set: (key: string, value: unknown) => store.set(key, value),
};
await parameter(ctx as any, async () => {});
return store.get('data') as any;
};
describe('parameter middleware branches', () => {
it('normalizes base urls and updates quote links', async () => {
const data = {
link: 'example.com/base',
item: [
{
title: 'Item 1',
link: '/relative',
description: '<div class="rsshub-quote">Quote</div><a href="/foo">Foo</a>',
_extra: {
links: [{ href: 'https://example.com' }],
},
},
{
title: 'Item 2',
description: '<img src="/img.png" />',
},
],
allowEmpty: true,
};
const result = await runMiddleware(data, {});
expect(result.item[0].link).toBe('http://example.com/relative');
expect(result.item[1].description).toContain('http://example.com/img.png');
expect(result.item[0]._extra.links[0].content_html).toContain('rsshub-quote');
});
it('filters with RegExp engine', async () => {
const originalEngine = config.feature.filter_regex_engine;
config.feature.filter_regex_engine = 'regexp';
const data = {
link: 'https://example.com',
item: [
{ title: 'Keep', description: 'A' },
{ title: 'Drop', description: 'B' },
],
};
const result = await runMiddleware(data, { filter: 'Keep' });
expect(result.item).toHaveLength(1);
expect(result.item[0].title).toBe('Keep');
config.feature.filter_regex_engine = originalEngine;
});
it('filters by category when title and description do not match', async () => {
const originalEngine = config.feature.filter_regex_engine;
config.feature.filter_regex_engine = 'regexp';
const data = {
link: 'https://example.com',
item: [
{
title: 'Nope',
description: 'Also nope',
author: 'Still nope',
category: ['Match'],
},
],
};
const result = await runMiddleware(data, { filter: 'Match' });
expect(result.item).toHaveLength(1);
expect(result.item[0].category).toContain('Match');
config.feature.filter_regex_engine = originalEngine;
});
it('keeps items without link in tgiv mode', async () => {
const data = {
link: 'https://example.com',
item: [{ title: 'NoLink' }],
};
const result = await runMiddleware(data, { tgiv: 'hash' });
expect(result.item[0].link).toBeUndefined();
});
it('rewrites links for scihub', async () => {
const data = {
link: 'https://example.com',
item: [
{ title: 'With DOI', doi: '10.1000/xyz' },
{ title: 'With link', link: 'https://example.com/paper' },
],
};
const result = await runMiddleware(data, { scihub: '1' });
expect(result.item[0].link).toBe(`${config.scihub.host}10.1000/xyz`);
expect(result.item[1].link).toBe(`${config.scihub.host}https://example.com/paper`);
});
it('throws on invalid brief value', async () => {
const data = {
link: 'https://example.com',
item: [{ title: 'Item', description: 'Desc' }],
};
await expect(runMiddleware(data, { brief: '10' })).rejects.toThrow('Invalid parameter brief');
});
it('processes openai description and title', async () => {
const originalInput = config.openai.inputOption;
const descriptionData = {
link: 'https://example.com',
item: [
{
title: 'Title',
description: 'Description',
link: `https://example.com/${Date.now()}/desc`,
},
],
};
config.openai.inputOption = 'description';
const descriptionResult = await runMiddleware(descriptionData, { chatgpt: 'true' });
expect(descriptionResult.item[0].description).toContain('AI processed content.');
const titleData = {
link: 'https://example.com',
item: [
{
title: 'Title',
description: 'Description',
link: `https://example.com/${Date.now()}/title`,
},
],
};
config.openai.inputOption = 'title';
const titleResult = await runMiddleware(titleData, { chatgpt: 'true' });
expect(titleResult.item[0].title).toContain('AI processed content.');
config.openai.inputOption = originalInput;
});
});
|