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 {
buildWeatherPushTextFromHtml,
formatWeatherPushDelivery,
normalizePushWeatherCity,
} from './weather-delivery.mjs';
const WEATHER_HTML = `
2026年9月20日
`;
test('buildWeatherPushTextFromHtml renders city card and dressing advice', () => {
const text = buildWeatherPushTextFromHtml(WEATHER_HTML, { city: '北京' });
assert.match(text, /🌤 每日天气 · 北京/u);
assert.match(text, /☀️ 晴/u);
assert.match(text, /穿衣建议/u);
assert.match(text, /上海/u);
});
test('buildWeatherPushTextFromHtml supports news002 wc/we/wt cards', () => {
const html = ``;
const text = buildWeatherPushTextFromHtml(html, { city: '北京' });
assert.match(text, /14\/26℃/u);
});
test('normalizePushWeatherCity strips suffix 市', () => {
assert.equal(normalizePushWeatherCity('北京市'), '北京');
});
test('formatWeatherPushDelivery reads weather section from today news page', async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'weather-push-'));
const userId = 'weather-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'), WEATHER_HTML);
const delivery = await formatWeatherPushDelivery({
city: '上海',
now: Date.parse('2026-09-20T07:00: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.match(delivery.text, /上海/u);
assert.match(delivery.text, /小雨/u);
});