Files
homeclaw/tests/top-headlines.test.ts
T

161 lines
7.0 KiB
TypeScript

/**
* top-headlines — 1면 RSS 파싱
*
* 순수 함수만 테스트한다. 피드 수신은 네트워크에 의존하므로 다루지 않는다.
*
* 이 파서가 조용히 깨지면 "뉴스가 없는 날"처럼 보인다 — 남의 문서 구조에 기대는 코드라
* 언젠가 바뀐다. 저장된 조각으로 회귀를 잡는 게 이 테스트의 목적이다.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import { parseFeedItems, hasTopHeadlineFeeds, formatHeadlines, extractMetaDescription } from '../src/tools/top-headlines';
const RSS = `<?xml version="1.0"?><rss><channel>
<title>NYT &gt; Top Stories</title>
<item><title>Lisa Demuth Wins Republican Primary</title><link>https://ex.invalid/a</link></item>
<item><title><![CDATA[Rescuers scramble: 180 dead & counting]]></title><link>https://ex.invalid/b</link></item>
</channel></rss>`;
const ATOM = `<?xml version="1.0"?><feed>
<entry><title>Front page story</title><link rel="alternate" href="https://ex.invalid/c"/></entry>
</feed>`;
describe('parseFeedItems', () => {
test('RSS item에서 제목과 링크를 뽑는다', () => {
const items = parseFeedItems(RSS, 'nytimes.com');
assert.equal(items.length, 2);
assert.equal(items[0].title, 'Lisa Demuth Wins Republican Primary');
assert.equal(items[0].link, 'https://ex.invalid/a');
assert.equal(items[0].source, 'nytimes.com');
});
test('채널 제목은 기사로 세지 않는다', () => {
assert.ok(!parseFeedItems(RSS, 'x').some(i => i.title.includes('Top Stories')));
});
test('CDATA와 엔티티를 푼다', () => {
assert.equal(parseFeedItems(RSS, 'x')[1].title, 'Rescuers scramble: 180 dead & counting');
});
test('Atom entry도 읽는다 — link는 href 속성에 있다', () => {
const items = parseFeedItems(ATOM, 'x');
assert.equal(items[0].title, 'Front page story');
assert.equal(items[0].link, 'https://ex.invalid/c');
});
test('limit을 지킨다', () => {
assert.equal(parseFeedItems(RSS, 'x', 1).length, 1);
});
test('구조가 바뀌면 0건 — 호출부가 NewsData로 폴백할 신호', () => {
assert.deepEqual(parseFeedItems('<html>완전히 다른 문서</html>', 'x'), []);
assert.deepEqual(parseFeedItems('', 'x'), []);
});
});
describe('hasTopHeadlineFeeds', () => {
test('피드가 있는 나라', () => {
for (const c of ['us', 'gb', 'de', 'fr', 'it', 'es', 'jp']) assert.equal(hasTopHeadlineFeeds(c), true, c);
});
test('다국가 목록은 하나라도 있으면 참', () => {
assert.equal(hasTopHeadlineFeeds('gb,de,fr,it,es'), true);
assert.equal(hasTopHeadlineFeeds('jp,de'), true);
});
test('피드가 없는 나라 — NewsData 경로로 간다', () => {
assert.equal(hasTopHeadlineFeeds('kr'), false);
assert.equal(hasTopHeadlineFeeds(''), false);
});
});
describe('formatHeadlines', () => {
test('번호·링크·출처를 담는다', () => {
const out = formatHeadlines(parseFeedItems(RSS, 'nytimes.com'));
assert.match(out, /^\[1\] Lisa Demuth/);
assert.match(out, /Source: nytimes\.com/);
});
});
/**
* 요약 추출 (2026-08-15 추가)
*
* 제목만 넘기면 모델이 기사를 하나씩 web_fetch 하러 간다 — 실측: "오늘 미국 주요 뉴스" 한 턴에
* web_fetch 6번, 매번 22 tok/s 모델의 생성 라운드 하나씩. 정작 피드엔 description이 이미
* 실려 있었는데(NYT 138자, NPR 293자) 파서가 지나치고 버리고 있었다.
*/
describe('요약 추출', () => {
test('description을 뽑는다', () => {
const xml = `<rss><channel><item>
<title>제목</title><link>https://e.com/1</link>
<description>디에고 가르시아가 물류 허브가 되었다.</description>
</item></channel></rss>`;
assert.equal(parseFeedItems(xml, 'e.com')[0].summary, '디에고 가르시아가 물류 허브가 되었다.');
});
test('Atom의 summary도 뽑는다', () => {
const xml = `<feed><entry>
<title>T</title><link href="https://e.com/2"/><summary>요약본이다.</summary>
</entry></feed>`;
assert.equal(parseFeedItems(xml, 'e.com')[0].summary, '요약본이다.');
});
test('이중 인코딩된 마크업을 걷어낸다 — NPR이 &lt;em&gt;로 보내온다', () => {
const xml = `<rss><channel><item><title>T</title><link>u</link>
<description>Clint Smith&apos;s &lt;em&gt;How the Word Is Passed&lt;/em&gt; explores it.</description>
</item></channel></rss>`;
const s = parseFeedItems(xml, 'x')[0].summary;
assert.ok(!s.includes('<em>') && !s.includes('&lt;'), `태그가 남았다: ${s}`);
assert.ok(s.includes("Clint Smith's") && s.includes('How the Word Is Passed'));
});
test('CDATA 안의 요약도 뽑는다', () => {
const xml = `<rss><channel><item><title>T</title><link>u</link>
<description><![CDATA[<p>본문 요약</p>]]></description></item></channel></rss>`;
assert.equal(parseFeedItems(xml, 'x')[0].summary, '본문 요약');
});
test('요약이 없어도 기사 자체는 살린다', () => {
const xml = `<rss><channel><item><title>제목만</title><link>u</link></item></channel></rss>`;
const items = parseFeedItems(xml, 'x');
assert.equal(items.length, 1);
assert.equal(items[0].summary, '');
});
test('지나치게 긴 요약은 자른다', () => {
const xml = `<rss><channel><item><title>T</title><link>u</link>
<description>${'가'.repeat(2000)}</description></item></channel></rss>`;
assert.ok(parseFeedItems(xml, 'x')[0].summary.length <= 400);
});
test('formatHeadlines가 요약을 실어 보낸다', () => {
const xml = `<rss><channel><item><title>제목</title><link>https://e.com/1</link>
<description>핵심 요약</description></item></channel></rss>`;
const out = formatHeadlines(parseFeedItems(xml, 'e.com'));
assert.ok(out.includes('핵심 요약'), out);
assert.ok(out.includes('https://e.com/1'));
});
test('요약이 없으면 빈 줄을 넣지 않는다', () => {
const xml = `<rss><channel><item><title>제목만</title><link>u</link></item></channel></rss>`;
assert.ok(!/\n\s*\n/.test(formatHeadlines(parseFeedItems(xml, 'x'))));
});
});
describe('extractMetaDescription', () => {
test('기사 페이지의 og:description/description을 꺼낸다', () => {
assert.equal(extractMetaDescription('<meta name="description" content="佐川急便は…発表しました。"/>'), '佐川急便は…発表しました。');
assert.equal(extractMetaDescription('<meta property="og:description" content="A &amp; B"/>'), 'A &amp; B');
assert.equal(extractMetaDescription('<title>x</title>'), '');
});
});
describe('formatHeadlines variant spellings', () => {
test('[2026-10-24] 華火 → 花火 so the model reads it as fireworks', () => {
const out = formatHeadlines([{ title: '東京湾大華火祭 11年ぶり', link: 'https://x', source: 'yahoo.co.jp', summary: '大華火祭が24日開催' }]);
assert.ok(out.includes('東京湾大花火祭') && out.includes('大花火祭が'));
assert.ok(!out.includes('華火'));
});
});