import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import {
convertNewsPageHtmlToWechatArticle,
createWechatNewsMorningDraftService,
findLatestNewsMorningPage,
NEWS_MORNING_TEMPLATE_VERSION,
wechatNewsMorningDraftInternals,
} from './wechat-news-morning-draft.mjs';
import { PUBLIC_ZONE_DIR, PUBLISH_ROOT_DIR } from './user-publish.mjs';
const SAMPLE_HTML = `
每日新闻早报 · 2026年9月10日
📰 每日新闻早报
2026年9月10日 · 星期三
综合多家权威媒体梳理全网核心热点。
`;
function createPool(seedRow = null) {
const state = { row: seedRow, runs: [] };
return {
async query(sql, params) {
if (sql.includes('CREATE TABLE')) return [[], []];
if (sql.includes('SELECT config_json')) return [state.row ? [state.row] : [], []];
if (sql.includes('INSERT INTO h5_wechat_admin_config')) {
state.row = {
config_json: params[1],
updated_by: params[2],
updated_at: params[3],
};
return [[], []];
}
if (sql.includes('INSERT INTO h5_wechat_news_draft_runs')) {
state.runs.unshift({
id: params[0],
status: params[1],
pageSlug: params[2],
pageUrl: params[3],
draftMediaId: params[4],
errorMessage: params[5],
triggeredBy: params[6],
createdAt: params[7],
});
return [[], []];
}
if (sql.includes('FROM h5_wechat_news_draft_runs')) {
return [state.runs, []];
}
throw new Error(`Unexpected query: ${sql}`);
},
};
}
test('convertNewsPageHtmlToWechatArticle converts daily-news to inline html', () => {
const article = convertNewsPageHtmlToWechatArticle(SAMPLE_HTML, {
publicUrl: 'https://m.tkmind.cn/MindSpace/demo/public/daily-news-0910.html',
});
assert.match(article.title, /2026年9月10日/u);
assert.equal(article.contentMode, 'inline_html');
assert.match(article.content, /每日新闻早报/u);
assert.match(article.content, /示例热点/u);
assert.match(article.content, /点击阅读原文/u);
assert.match(article.content, /一起创作/u);
assert.match(article.content, /点我/u);
assert.doesNotMatch(article.content, /近7天新闻早报/u);
});
test('buildDailyNewsWechatHtmlContent preserves style and body markup', () => {
const content = wechatNewsMorningDraftInternals.buildDailyNewsWechatHtmlContent(SAMPLE_HTML, {
publicUrl: 'https://m.tkmind.cn/MindSpace/demo/public/daily-news-0910.html',
publicBaseUrl: 'https://m.tkmind.cn',
userId: 'demo-user',
});
assert.match(content, /