feat(bloomberg): enrich article content for summaries (#36)
This commit is contained in:
92
scraper-service/tests/article-content.test.js
Normal file
92
scraper-service/tests/article-content.test.js
Normal file
@ -0,0 +1,92 @@
|
||||
const { test } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const { extractArticle, enrichArticles, MAX_CONTENT_BYTES } = require('../article-content')
|
||||
const paragraph = 'ArticleBodyText_articleBodyContent__17wqE typography_articleBody___5jDr'
|
||||
const basic = text => `<article class="articleLayout_basic__gcpQk"><div class="body-content"><p class="${paragraph}">${text}</p><div class="placeholder"><p class="${paragraph}">excluded placeholder</p></div></div></article>`
|
||||
test('observed basic and feature layouts extract only editorial paragraphs within the observed root', () => {
|
||||
for (const html of [basic(' First <em>synthetic</em> paragraph. '), `<article><div class="FeatureContent_body__NTf8M"><p class="${paragraph}"> First <em>synthetic</em> paragraph. </p></div></article>`]) {
|
||||
const result = extractArticle(`<nav>excluded navigation</nav><meta name="iso-8601-publish-date" content="2026-01-02T03:04:05Z">${html}`)
|
||||
assert.equal(result.content, 'First synthetic paragraph.')
|
||||
assert.equal(result.published_at, '2026-01-02T03:04:05.000Z')
|
||||
}
|
||||
})
|
||||
test('no broad article/main or JSON-LD content fallback; paragraph normalization', () => {
|
||||
assert.equal(extractArticle('<main><article><p>excluded generic body</p></article></main><script type="application/ld+json">bad</script>').content, '')
|
||||
assert.equal(extractArticle('<script type="application/ld+json">{"@type":"NewsArticle","articleBody":"excluded"}</script>').content, '')
|
||||
assert.equal(extractArticle(basic('one two<script>excluded</script>') .replace('</div></article>', `<p class="${paragraph}"> three\n four </p></div></article>`)).content, 'one two\n\nthree four')
|
||||
})
|
||||
test('oversized non-ASCII content is deterministically bounded on UTF-8 boundaries', () => {
|
||||
const html = basic('é漢🙂'.repeat(MAX_CONTENT_BYTES))
|
||||
const first = extractArticle(html)
|
||||
assert.equal(first.content, extractArticle(html).content)
|
||||
assert.ok(Buffer.byteLength(first.content) <= MAX_CONTENT_BYTES)
|
||||
assert.ok(!first.content.includes('\ufffd'))
|
||||
assert.equal(first.content_truncated, true)
|
||||
})
|
||||
test('enrichment deduplicates loads, bounds concurrency and preserves input order', async () => {
|
||||
const urls = ['a', 'b', 'a', 'c'].map(id => `https://www.bloomberg.com/news/articles/${id}`)
|
||||
const calls = []; let active = 0; let peak = 0
|
||||
const result = await enrichArticles(urls, async url => {
|
||||
calls.push(url); active++; peak = Math.max(peak, active)
|
||||
await new Promise(resolve => setTimeout(resolve, url.endsWith('/a') ? 10 : 1))
|
||||
active--; return basic('synthetic ' + url.slice(-1))
|
||||
}, { concurrency: 2, articleTimeoutMs: 100, totalTimeoutMs: 200 })
|
||||
assert.equal(calls.length, 3); assert.equal(peak, 2)
|
||||
assert.deepEqual(result.map(v => v.url), urls)
|
||||
assert.deepEqual(result.map(v => v.content), ['synthetic a', 'synthetic b', 'synthetic a', 'synthetic c'])
|
||||
})
|
||||
test('failure and ignored cancellation stay bounded without reusing occupied slots', async () => {
|
||||
const urls = ['a', 'b', 'c'].map(id => `https://www.bloomberg.com/news/articles/${id}`)
|
||||
let calls = 0
|
||||
const result = await enrichArticles(urls, async () => { calls++; return new Promise(() => {}) }, { concurrency: 2, articleTimeoutMs: 5, totalTimeoutMs: 20 })
|
||||
assert.equal(calls, 2); assert.ok(result.every(v => v.content === '' && v.published_at === null))
|
||||
const failed = await enrichArticles(urls, async () => { throw new Error('private') }, { articleTimeoutMs: 10, totalTimeoutMs: 50 })
|
||||
assert.ok(failed.every(v => v.content === '' && v.published_at === null))
|
||||
})
|
||||
|
||||
test('JSON-LD belonging to another article cannot supply this article date', () => {
|
||||
const html = basic('synthetic paragraph') + '<script type="application/ld+json">{"@type":"NewsArticle","url":"https://www.bloomberg.com/news/articles/other","datePublished":"2026-01-02T03:04:05Z"}</script>'
|
||||
const result = extractArticle(html, 'https://www.bloomberg.com/news/articles/current')
|
||||
assert.equal(result.content, 'synthetic paragraph')
|
||||
assert.equal(result.published_at, null)
|
||||
})
|
||||
|
||||
test('sanitized structural fixtures exclude placeholders, recommendations and disclaimers', () => {
|
||||
const fs = require('node:fs'); const path = require('node:path')
|
||||
const load = name => fs.readFileSync(path.join(__dirname, 'fixtures', name), 'utf8')
|
||||
assert.equal(extractArticle(load('content-basic.html')).content, 'Synthetic first paragraph.\n\nSynthetic second paragraph.')
|
||||
assert.equal(extractArticle(load('content-feature.html')).content, 'Synthetic feature paragraph.')
|
||||
})
|
||||
test('aborted enrichment and invalid URLs never invoke a loader', async () => {
|
||||
let calls = 0; const controller = new AbortController(); controller.abort()
|
||||
const urls = ['https://www.bloomberg.com/news/articles/a']
|
||||
const result = await enrichArticles(urls, async () => { calls++; return basic('unused') }, { signal: controller.signal })
|
||||
assert.equal(calls, 0); assert.equal(result[0].content, '')
|
||||
await enrichArticles(['http://127.0.0.1/news/articles/a', 'https://other.invalid/news/articles/a'], async () => { calls++; return '' })
|
||||
assert.equal(calls, 0)
|
||||
})
|
||||
|
||||
test('remaining pool budget gives every candidate a bounded attempt with an offline clock', async t => {
|
||||
t.mock.timers.enable({ apis: ['setTimeout', 'Date'] })
|
||||
const urls = Array.from({ length: 30 }, (_, i) => `https://www.bloomberg.com/news/articles/pool-${i}`)
|
||||
let active = 0; let peak = 0; let calls = 0; const slices = []
|
||||
let done = false
|
||||
const operation = enrichArticles(urls, async (_url, options) => {
|
||||
calls++; active++; peak = Math.max(peak, active); slices.push(options.timeoutMs)
|
||||
try { await new Promise((resolve, reject) => options.signal.addEventListener('abort', () => reject(new Error('private')), { once: true })) } finally { active-- }
|
||||
}, { concurrency: 2, articleTimeoutMs: 100, totalTimeoutMs: 1000 }).then(result => { done = true; return result })
|
||||
for (let i = 0; i < 20 && !done; i++) {
|
||||
await new Promise(resolve => setImmediate(resolve))
|
||||
t.mock.timers.tick(62)
|
||||
}
|
||||
const result = await operation
|
||||
assert.equal(calls, 30); assert.equal(peak, 2); assert.equal(active, 0)
|
||||
assert.ok(slices.every(ms => ms <= 62))
|
||||
assert.deepEqual(result.map(a => a.url), urls)
|
||||
assert.ok(result.every(a => a.content === '' && a.published_at === null))
|
||||
})
|
||||
|
||||
test('editorial descendants preserve document order and exclude non-editorial ancestors', () => {
|
||||
const html = `<article><div class="FeatureContent_body__NTf8M"><div><section><p class="${paragraph}">one</p></section></div><aside><div><p class="${paragraph}">excluded</p></div></aside><div hidden><p class="${paragraph}">excluded</p></div><div data-component="related-stories"><p class="${paragraph}">excluded</p></div><div><div><p class="${paragraph}">two</p></div></div></div></article>`
|
||||
assert.equal(extractArticle(html).content,'one\n\ntwo')
|
||||
})
|
||||
Reference in New Issue
Block a user