import { test } from 'node:test';
import assert from 'node:assert/strict';
import { extractMarkdown } from './markdown-extract.js';
const ARTICLE_HTML = `
How Widgets Work
WidgetCo
How Widgets Work
${'Widgets are small mechanical devices used in countless everyday applications. '.repeat(10)}
${'This second paragraph explains the history of widget manufacturing in great detail. '.repeat(10)}
Manufacturing Process
${'The manufacturing process involves several precise steps performed by skilled technicians. '.repeat(10)}
`;
const APP_DASHBOARD_HTML = `Dashboard
Dashboard
Revenue$12,400
Users842
`;
const EMPTY_HTML = ``;
test('extractMarkdown: article page uses Readability and strips nav/footer chrome', () => {
const result = extractMarkdown(ARTICLE_HTML, 'https://example.com/blog/widgets');
assert.ok(result);
assert.equal(result!.method, 'readability');
assert.match(result!.markdown, /How Widgets Work/);
assert.match(result!.markdown, /Manufacturing Process/);
assert.doesNotMatch(result!.markdown, /Copyright 2026/);
assert.doesNotMatch(result!.markdown, /tracking pixel/);
assert.ok(result!.wordCount > 50);
});
test('extractMarkdown: app dashboard (no article) falls back to full-page conversion', () => {
const result = extractMarkdown(APP_DASHBOARD_HTML, 'https://app.example.com/dashboard');
assert.ok(result);
assert.equal(result!.method, 'full-page');
assert.match(result!.markdown, /Dashboard/);
assert.match(result!.markdown, /Revenue/);
assert.match(result!.markdown, /Export CSV/);
assert.doesNotMatch(result!.markdown, /__STATE__/);
});
test('extractMarkdown: empty page returns null instead of an empty markdown string', () => {
const result = extractMarkdown(EMPTY_HTML, 'https://example.com/empty');
assert.equal(result, null);
});
test('extractMarkdown: malformed HTML never throws', () => {
assert.doesNotThrow(() => extractMarkdown('
unclosed', 'https://example.com/x'));
});
test('extractMarkdown: script/style content is never leaked into markdown output', () => {
const html = `Page
Real Content Heading For Testing
${'Some real paragraph text that is long enough to be meaningful content here. '.repeat(6)}