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('

正文

', { 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('

正文

', { 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 = `

${'一年级语文'.repeat(4000)}

`; 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/); } });