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 => ` `
test('observed basic and feature layouts extract only editorial paragraphs within the observed root', () => {
for (const html of [basic(' First synthetic paragraph. '), ` First synthetic paragraph.
`]) {
const identity = "https://www.bloomberg.com/news/articles/fixture"
const result = extractArticle(`excluded navigation ${html}`, identity)
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('excluded generic body
').content, '')
assert.equal(extractArticle('').content, '')
assert.equal(extractArticle(basic('one two') .replace('', `
.*?<\/div>/, '')
const first = extractArticle(html, identity)
assert.equal(first.content, extractArticle(html, identity).content)
assert.equal(first.content, 'é漢🙂'.repeat(MAX_CONTENT_BYTES))
assert.ok(!first.content.includes('\ufffd'))
assert.equal(first.content_truncated, false)
assert.equal(first.content_state, 'complete')
})
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') + ''
const result = extractArticle(html, 'https://www.bloomberg.com/news/articles/current')
assert.equal(result.content, '')
assert.equal(result.content_state, 'unavailable')
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 = `
`
assert.equal(extractArticle(html).content,'one\n\ntwo')
})
test('full editorial extraction preserves headings, list items and quotations', () => {
const html = basic('First paragraph.').replace('
', '