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 { PUBLIC_ZONE_DIR, PUBLISH_ROOT_DIR } from '../../user-publish.mjs'; import { buildSectionCardsPushText, extractCardsFromSectionIds, extractKnowledgeItemsFromHtml, loadTodayNewsMorningHtml, } from './news-section-extract.mjs'; const SAMPLE_HTML = `
2026年9月20日
💰 财经
A股

沪指收涨

市场震荡走高。

AI

新模型发布

多家厂商更新。

💡

网络效应

用户越多价值越大。

1969年

人类首次登月。

`; test('extractCardsFromSectionIds reads finance and tech sections', () => { const finance = extractCardsFromSectionIds(SAMPLE_HTML, ['finance']); assert.equal(finance.length, 1); assert.match(finance[0].title, /沪指收涨/u); const tech = extractCardsFromSectionIds(SAMPLE_HTML, ['tech']); assert.equal(tech.length, 1); assert.match(tech[0].title, /新模型发布/u); }); test('extractKnowledgeItemsFromHtml reads knowledge section', () => { const items = extractKnowledgeItemsFromHtml(SAMPLE_HTML); assert.equal(items.length, 1); assert.match(items[0].title, /网络效应/u); }); test('buildSectionCardsPushText renders numbered digest', () => { const cards = extractCardsFromSectionIds(SAMPLE_HTML, ['finance']); const text = buildSectionCardsPushText({ emoji: '💰', label: '财经市场', cards, includeSummary: true, cancelId: 4, }); assert.match(text, /💰 财经市场/u); assert.match(text, /沪指收涨/u); }); test('loadTodayNewsMorningHtml loads today page from workspace', async () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'section-push-')); const userId = 'section-source'; const publicDir = path.join(root, PUBLISH_ROOT_DIR, userId, PUBLIC_ZONE_DIR); fs.mkdirSync(publicDir, { recursive: true }); fs.writeFileSync(path.join(publicDir, 'daily-news-0920.html'), SAMPLE_HTML); const loaded = await loadTodayNewsMorningHtml({ now: Date.parse('2026-09-20T08:30:00+08:00'), timezone: 'Asia/Shanghai', h5Root: root, pool: { async query() { return [[{ config_json: JSON.stringify({ sourceUserId: userId, pageSlugPattern: 'daily-news-*', timezone: 'Asia/Shanghai', }), }], []]; }, }, env: {}, }); assert.ok(loaded.html); assert.match(loaded.dateLabel, /2026年9月20日/u); });