Files
memind/wechat-draft-publication-standard.test.mjs
T
john b8bcbcb764 feat(wechat): 早报草稿改用内联模板并去掉正文大封面
让定时任务按新版式推送草稿:热门旅游城市天气、无 Hero logo,并约束生成页必须写城市天气卡。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-16 15:20:44 +08:00

116 lines
4.9 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import test from 'node:test';
import {
applyWechatDraftArticleLayout,
clipWechatDraftBodyToFitFooter,
resolveMpFollowQrcodePublicUrl,
} from './wechat-draft-article-layout.mjs';
import {
buildMindSpacePageWechatDraftArticleForPush,
resolveWechatDraftThumbPath,
} from './mindspace-wechat-page-draft.mjs';
import {
verifyWechatDraftCoverResolution,
verifyWechatDraftPublicationContent,
WECHAT_DRAFT_PUBLICATION_STANDARD,
WECHAT_DRAFT_PUBLICATION_STANDARD_VERSION,
} from './wechat-draft-publication-standard.mjs';
const userId = '1c99b83b-0454-474f-a5d2-129d34506a32';
const AUGUST_HTML = fs.readFileSync(
`MindSpace/${userId}/public/august-afternoon.html`,
'utf8',
);
const PUBLIC_URL = `http://127.0.0.1:8081/MindSpace/${userId}/public/august-afternoon.html`;
test('standard version is pinned', () => {
assert.equal(WECHAT_DRAFT_PUBLICATION_STANDARD.version, WECHAT_DRAFT_PUBLICATION_STANDARD_VERSION);
assert.equal(WECHAT_DRAFT_PUBLICATION_STANDARD.cover.priority[0], 'hero-raster');
});
test('applyWechatDraftArticleLayout renders guided read-original section and smaller qrcode', () => {
const content = applyWechatDraftArticleLayout('<p>正文</p>', {
publicUrl: PUBLIC_URL,
qrcodeImageUrl: 'https://m.tkmind.cn/assets/mp-follow-qrcode.png',
});
assert.match(content, /完整网页版更精彩/);
assert.match(content, /⬇ ⬇ ⬇/);
assert.match(content, /点击上方按钮,查看完整早报/);
assert.match(content, /width:120px;height:120px/);
assert.match(content, /📖 阅读原文/);
const check = verifyWechatDraftPublicationContent(content, { publicUrl: PUBLIC_URL });
assert.equal(check.ok, true, check.issues.join('; '));
});
test('resolveMpFollowQrcodePublicUrl points to platform asset path', () => {
assert.equal(
resolveMpFollowQrcodePublicUrl({ H5_PUBLIC_BASE_URL: 'https://m.tkmind.cn' }),
'https://m.tkmind.cn/assets/mp-follow-qrcode.png',
);
});
test('verifyWechatDraftPublicationContent rejects legacy large read button', () => {
const bad = applyWechatDraftArticleLayout('<p>正文</p>', {
publicUrl: PUBLIC_URL,
qrcodeImageUrl: 'https://mmbiz.qpic.cn/qrcode.png',
}).replace('阅读原文', '点击阅读原文');
const result = verifyWechatDraftPublicationContent(bad, { publicUrl: PUBLIC_URL });
assert.equal(result.ok, false);
assert.ok(result.issues.some((item) => /大按钮/.test(item)));
});
test('august-afternoon cover resolves to hero raster', () => {
const resolved = resolveWechatDraftThumbPath(AUGUST_HTML, {
h5Root: process.cwd(),
userId,
workspaceRelativePath: 'public/august-afternoon.html',
publishDir: path.join(process.cwd(), 'MindSpace', userId),
});
assert.equal(resolved.source, 'hero');
assert.match(resolved.path ?? '', /\.webp$/);
const coverCheck = verifyWechatDraftCoverResolution(resolved);
assert.equal(coverCheck.ok, true);
});
test('applyWechatDraftArticleLayout keeps required footer when body exceeds 20000 chars', () => {
const longBody = `<p>${'一年级语文'.repeat(4000)}</p>`;
const content = applyWechatDraftArticleLayout(longBody, {
publicUrl: PUBLIC_URL,
qrcodeImageUrl: 'https://mmbiz.qpic.cn/qrcode.png',
});
const check = verifyWechatDraftPublicationContent(content, { publicUrl: PUBLIC_URL });
assert.equal(check.ok, true, check.issues.join('; '));
assert.ok(content.length <= WECHAT_DRAFT_PUBLICATION_STANDARD.limits.maxContentChars);
assert.ok(content.indexOf('一年级语文') >= 0);
assert.ok(content.indexOf('阅读原文') < content.indexOf('欢迎关注 TKMind 智趣'));
assert.equal(
clipWechatDraftBodyToFitFooter(longBody, 'FOOTER', 20).length + 'FOOTER'.length + 1,
20,
);
});
test('august-afternoon article layout matches publication standard', async () => {
const article = await buildMindSpacePageWechatDraftArticleForPush({
html: AUGUST_HTML,
publicUrl: PUBLIC_URL,
pageTitle: '八月午后 · 一首短诗',
pageSummary: '一首关于八月午后的短诗',
publishDir: path.join(process.cwd(), 'MindSpace', userId),
htmlRelativePath: 'public/august-afternoon.html',
memindLibRoot: process.cwd(),
});
const check = verifyWechatDraftPublicationContent(article.content, { publicUrl: PUBLIC_URL });
assert.equal(check.ok, true, check.issues.join('; '));
assert.match(article.content, /https:\/\/m\.tkmind\.cn\/assets\/mp-follow-qrcode\.png/);
const readIdx = article.content.indexOf('阅读原文');
const followIdx = article.content.indexOf('欢迎关注 TKMind 智趣');
assert.ok(readIdx >= 0, '缺少阅读原文');
assert.ok(readIdx < followIdx, '阅读原文应在关注区之前');
if (AUGUST_HTML.includes('蝉声把正午拉得很长')) {
assert.match(article.content, /蝉声把正午拉得很长/);
assert.doesNotMatch(article.content.slice(0, readIdx), /mp-follow-qrcode/);
}
});