38 lines
2.0 KiB
TypeScript
38 lines
2.0 KiB
TypeScript
/**
|
||
* web-extract — 검색 결과 페이지 본문 추출
|
||
* 표본: 2026-10-17 실측(GitHub/당근/연합뉴스 404 페이지에서 메뉴가 본문보다 앞서거나 본문으로 뽑힘).
|
||
*/
|
||
import { test, describe } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { extractMainText } from '../src/tools/web-extract';
|
||
|
||
const ARTICLE = `<html><head><title>HQ 카메라 리뷰 | 블로그</title></head><body>
|
||
<header><nav><a>홈</a><a>로그인</a><a>회원가입</a></nav></header>
|
||
<main><article><h1>HQ 카메라 리뷰</h1>
|
||
<p>${'IMX477 센서는 4056×3040 해상도를 제공하며 C/CS 마운트 렌즈를 쓸 수 있다. '.repeat(8)}</p>
|
||
<p>${'초점은 렌즈 경통으로 조절하고 조리개 잠금 나사로 고정한다. '.repeat(6)}</p></article></main>
|
||
<footer><p>Copyright 2026 All rights reserved</p></footer></body></html>`;
|
||
|
||
describe('extractMainText', () => {
|
||
test('메뉴·푸터를 빼고 본문만 남긴다', () => {
|
||
const r = extractMainText(ARTICLE);
|
||
assert.ok(r.text.includes('IMX477'));
|
||
assert.ok(!r.text.includes('회원가입'));
|
||
assert.ok(!/all rights reserved/i.test(r.text));
|
||
assert.ok(r.text.length >= 400);
|
||
});
|
||
test('메뉴 목록뿐인 페이지는 본문으로 인정하지 않는다', () => {
|
||
const menu = '<html><body><main>' + Array.from({ length: 60 }, (_, i) => `<div>메뉴${i}</div>`).join('') + '</main></body></html>';
|
||
assert.equal(extractMainText(menu).text, '');
|
||
});
|
||
test('표 셀이 한 줄로 뭉치지 않는다(줄바꿈 보존)', () => {
|
||
const html = `<html><body><article><p>${'설명 문장입니다. '.repeat(40)}</p><table><tr><td>가격</td><td>35달러</td></tr><tr><td>해상도</td><td>12MP</td></tr></table></article></body></html>`;
|
||
const t = extractMainText(html).text;
|
||
assert.ok(/가격[\s|]+35달러/.test(t));
|
||
assert.ok(t.split('\n').some(l => l.includes('35달러') && !l.includes('12MP')));
|
||
});
|
||
test('깨진 HTML도 예외 없이 빈 문자열', () => {
|
||
assert.equal(extractMainText('<<<>>>').text, '');
|
||
});
|
||
});
|