diff --git a/test/utils/wechat-mp.js b/lib/utils/wechat-mp.test.ts
similarity index 90%
rename from test/utils/wechat-mp.js
rename to lib/utils/wechat-mp.test.ts
index 2dd7e865b..1661b1b40 100644
--- a/test/utils/wechat-mp.js
+++ b/lib/utils/wechat-mp.test.ts
@@ -1,17 +1,7 @@
-process.env.REQUEST_TIMEOUT = '500';
-const cheerio = require('cheerio');
-const {
- _internal: { normalizeUrl },
- fetchArticle,
- finishArticleItem,
- fixArticleContent,
-} = require('../../lib/utils/wechat-mp');
-const nock = require('nock');
-const ctx = require('../../lib/app').context;
-
-afterAll(() => {
- delete process.env.REQUEST_TIMEOUT;
-});
+import { describe, expect, it } from '@jest/globals';
+import { load } from 'cheerio';
+import nock from 'nock';
+import { fixArticleContent, fetchArticle, finishArticleItem, normalizeUrl } from '@/utils/wechat-mp';
// date from the cache will be an ISO8601 string, so we need to use this function
const compareDate = (date1, date2) => {
@@ -56,20 +46,19 @@ describe('wechat-mp', () => {
'';
const expectedHtmlSection =
expectedCodeSection + '
test
' + '' + '' + '' + '' + 'test
' + '';
- let $ = cheerio.load(divHeader + htmlSection + divFooter);
+ let $ = load(divHeader + htmlSection + divFooter);
expect(fixArticleContent(htmlSection)).toBe(expectedHtmlSection);
expect(fixArticleContent($('div#js_content.rich_media_content'))).toBe(expectedHtmlSection);
const htmlImg = '
' + '
' + '
';
const expectedHtmlImg = Array.from({ length: 3 + 1 }).join('
');
- $ = cheerio.load(divHeader + htmlImg + divFooter);
+ $ = load(divHeader + htmlImg + divFooter);
expect(fixArticleContent(htmlImg)).toBe(expectedHtmlImg);
expect(fixArticleContent($('div#js_content.rich_media_content'))).toBe(expectedHtmlImg);
expect(fixArticleContent(htmlImg, true)).toBe(htmlImg);
expect(fixArticleContent($('div#js_content.rich_media_content'), true)).toBe(htmlImg);
expect(fixArticleContent('')).toBe('');
- expect(fixArticleContent(null)).toBe('');
expect(fixArticleContent()).toBe('');
expect(fixArticleContent($('div#something_not_in.the_document_tree'))).toBe('');
});
@@ -121,7 +110,14 @@ describe('wechat-mp', () => {
const httpsUrl = 'https://mp.weixin.qq.com/rsshub_test/wechatMp_fetchArticle';
const httpUrl = httpsUrl.replace(/^https:\/\//, 'http://');
- const expectedItem = {
+ const expectedItem: {
+ title: string;
+ summary: string;
+ author: string;
+ description: string;
+ mpName?: string;
+ link: string;
+ } = {
title: 'title',
summary: 'summary',
author: 'author',
@@ -131,13 +127,13 @@ describe('wechat-mp', () => {
};
const expectedDate = new Date(ct * 1000);
- const fetchArticleItem = await fetchArticle(ctx, httpUrl);
+ const fetchArticleItem = await fetchArticle(httpUrl);
expect(compareDate(fetchArticleItem.pubDate, expectedDate)).toBe(true);
delete fetchArticleItem.pubDate;
expect(fetchArticleItem).toEqual(expectedItem);
delete expectedItem.mpName;
- const finishedArticleItem = await finishArticleItem(ctx, { link: httpUrl });
+ const finishedArticleItem = await finishArticleItem({ link: httpUrl });
expect(compareDate(finishedArticleItem.pubDate, expectedDate)).toBe(true);
delete finishedArticleItem.pubDate;
expect(finishedArticleItem).toEqual(expectedItem);
diff --git a/lib/utils/wechat-mp.ts b/lib/utils/wechat-mp.ts
index 4ea1c811e..2f847df79 100644
--- a/lib/utils/wechat-mp.ts
+++ b/lib/utils/wechat-mp.ts
@@ -26,8 +26,9 @@
*/
import got from '@/utils/got';
-import { load } from 'cheerio';
+import { load, type Cheerio, type Element } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
+import cache from '@/utils/cache';
const replaceTag = ($, oldTag, newTagName) => {
oldTag = $(oldTag);
@@ -76,12 +77,17 @@ const detectSourceUrl = ($) => {
* @param {boolean} skipImg - Whether to skip fixing images.
* @return {string} - The fixed html, a string.
*/
-const fixArticleContent = (html, skipImg = false) => {
- html = html && html.html ? html.html() : html; // do not disturb the original tree
- if (!html) {
+const fixArticleContent = (html?: string | Cheerio, skipImg = false) => {
+ let htmlResult = '';
+ if (typeof html === 'string') {
+ htmlResult = html;
+ } else if (html?.html) {
+ htmlResult = html.html() || '';
+ }
+ if (!htmlResult) {
return '';
}
- const $ = load(html, undefined, false);
+ const $ = load(htmlResult, undefined, false);
if (!skipImg) {
// fix img lazy loading
$('img[data-src]').each((_, img) => {
@@ -187,9 +193,9 @@ const normalizeUrl = (url, bypassHostCheck = false) => {
* @param {boolean} bypassHostCheck - Whether to bypass host check.
* @return {Promise