${text}
excluded placeholder
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 => ` ${text} excluded placeholder First synthetic paragraph. excluded generic body
three\n four
`)).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') + '' 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 = `one
excluded
excluded
two