import assert from 'node:assert/strict';
import test from 'node:test';
import {
convertDailyNewsHtmlToWechatInlineArticle,
wechatDailyNewsInlineInternals,
} from './wechat-daily-news-inline.mjs';
const WEATHER_HTML = `
📰 每日新闻早报
🌤️ 今日天气
🌧️ 华南有雨☀️ 华北晴好
中央气象台:未来三天降水减弱。
数据来源:中央气象台
`;
test('splitSections preserves section ids after opening tag split', () => {
const sections = wechatDailyNewsInlineInternals.splitSections(WEATHER_HTML);
assert.deepEqual(sections.map((item) => item.id), ['headlines', 'weather']);
});
test('renderWeatherSection uses tourist city title and two-column city cards', () => {
const sections = wechatDailyNewsInlineInternals.splitSections(WEATHER_HTML);
const weather = sections.find((item) => item.id === 'weather');
const rendered = wechatDailyNewsInlineInternals.renderWeatherSection(weather.html);
assert.match(rendered, /热门旅游城市天气/u);
assert.match(rendered, /📍 北京/u);
assert.match(rendered, /☀️ 晴 · 温差大/u);
assert.match(rendered, /width:50%;box-sizing:border-box/u);
assert.match(rendered, /display:inline-block/u);
assert.doesNotMatch(rendered, /display:flex/u);
assert.match(rendered, /30°/u);
assert.match(rendered, /26°/u);
assert.doesNotMatch(rendered, /中央气象台/u);
assert.doesNotMatch(rendered, /今日天气/u);
});
test('renderWeatherSection skips regional fallback cards and national summary', () => {
const regionalWeatherHtml = `
🌤️ 今日天气 · 9月16日
📍 全国概况 · 江汉西南阴雨开场
🌧️ 湖北西部局地暴雨
`;
const rendered = wechatDailyNewsInlineInternals.renderWeatherSection(regionalWeatherHtml);
assert.match(rendered, /热门旅游城市天气 · 9月16日/u);
assert.match(rendered, /北京/u);
assert.match(rendered, /上海/u);
assert.doesNotMatch(rendered, /湖北西部/u);
assert.doesNotMatch(rendered, /全国概况/u);
});
test('renderWeatherSection supports news002 wc/we/wt cards', () => {
const news002Weather = `
🌤️ 今日天气 · 9月24日
全国重点城市 · 9月24日
中央气象台预报,广东广西局地大到暴雨
`;
const rendered = wechatDailyNewsInlineInternals.renderWeatherSection(news002Weather);
assert.match(rendered, /北京/u);
assert.match(rendered, /14\/26℃/u);
assert.match(rendered, /上海/u);
assert.match(rendered, /22\/28℃/u);
});
test('buildWechatInlineContent renders weather and optional quick links', () => {
const built = wechatDailyNewsInlineInternals.buildWechatInlineContent(WEATHER_HTML, {
includeFixedLayout: false,
trimProfile: { skipHistory: true, skipQuickLinks: false },
});
assert.match(built.content, /🌤️ 天气/u);
assert.match(built.content, /📍 北京/u);
assert.match(built.content, /30°/u);
});
const HISTORY_HTML = `
📰 每日新闻早报
📜 历史上的今天 · 9月24日
- 1949年中国人民政治协商会议第一届全体会议开幕。
- 1957年美国小石城中央高中发生种族融合冲突。
- 1996年《全面禁止核试验条约》在联合国开放签署。
- 2008年神舟七号载人飞船成功发射。
`;
test('renderHistorySection renders up to three compact history items', () => {
const sections = wechatDailyNewsInlineInternals.splitSections(HISTORY_HTML);
const history = sections.find((item) => item.id === 'history');
const rendered = wechatDailyNewsInlineInternals.renderHistorySection(history.html);
assert.match(rendered, /历史上的今天 · 9月24日/u);
assert.match(rendered, /1949年/u);
assert.match(rendered, /1957年/u);
assert.match(rendered, /1996年/u);
assert.doesNotMatch(rendered, /2008年/u);
assert.match(rendered, /中国人民政治协商会议/u);
});
test('buildWechatInlineContent includes history snippet by default', () => {
const built = wechatDailyNewsInlineInternals.buildWechatInlineContent(HISTORY_HTML, {
includeFixedLayout: false,
});
assert.match(built.content, /历史上的今天 · 9月24日/u);
assert.match(built.content, /1949年/u);
assert.doesNotMatch(built.content, /2008年/u);
});
test('buildWechatInlineContent can skip history when trimProfile requests it', () => {
const built = wechatDailyNewsInlineInternals.buildWechatInlineContent(HISTORY_HTML, {
includeFixedLayout: false,
trimProfile: { skipHistory: true },
});
assert.doesNotMatch(built.content, /历史上的今天/u);
});
test('convertDailyNewsHtmlToWechatInlineArticle keeps weather when trimming', () => {
const article = convertDailyNewsHtmlToWechatInlineArticle(WEATHER_HTML, {
includeFixedLayout: false,
fixedLayoutLength: 1200,
});
assert.match(article.content, /📍 北京/u);
assert.match(article.content, /30°/u);
});
test('trimWechatInlineBodyByRemovingCards drops tail cards instead of hard slicing', () => {
const html = `${WEATHER_HTML.slice(0, -24)}