fix(bloomberg): avoid collection timeouts
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
const { test } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const { extractPublicationDate, withPublicationDates } = require('../publication-date')
|
||||
const { extractPublicationDate, withDatesFromLoadedHtml } = require('../publication-date')
|
||||
|
||||
test('minimal metadata fixtures preserve genuine dates with explicit zones', () => {
|
||||
assert.equal(extractPublicationDate('<meta name="iso-8601-publish-date" content="2026-01-02T03:04:05Z">'), '2026-01-02T03:04:05.000Z')
|
||||
@ -12,27 +12,30 @@ test('invalid or missing metadata never manufactures a collection date', () => {
|
||||
assert.equal(extractPublicationDate(html), null)
|
||||
}
|
||||
})
|
||||
test('metadata priority and failed hydration retain order and nullable dates', async () => {
|
||||
test('already-loaded section metadata is associated per article, never by page date', () => {
|
||||
const a = { title: 'fixture-a', url: 'https://www.bloomberg.com/news/articles/fixture-a' }
|
||||
const b = { title: 'fixture-b', url: 'https://www.bloomberg.com/news/articles/fixture-b' }
|
||||
const seen = []
|
||||
const result = await withPublicationDates([a, b], async url => {
|
||||
seen.push(url)
|
||||
if (url === b.url) throw new Error('fixture only')
|
||||
return '<meta name="iso-8601-publish-date" content="2026-01-02T03:04:05Z"><time datetime="2026-01-03T00:00:00Z"></time>'
|
||||
})
|
||||
assert.deepEqual(seen, [a.url, b.url])
|
||||
assert.equal(result[0].published_at, '2026-01-02T03:04:05.000Z')
|
||||
assert.equal(result[1].published_at, null)
|
||||
const c = { title: 'fixture-c', url: 'https://www.bloomberg.com/news/articles/fixture-c' }
|
||||
const html = '<meta name="iso-8601-publish-date" content="2026-12-31T00:00:00Z">' +
|
||||
'<article><a data-component="story-link" href="/news/articles/fixture-a"></a><time datetime="2026-01-02T03:04:05Z"></time></article>' +
|
||||
'<script type="application/ld+json">{"@type":"NewsArticle","url":"https://www.bloomberg.com/news/articles/fixture-b","datePublished":"2026-02-03T04:05:06Z"}</script>'
|
||||
const result = withDatesFromLoadedHtml([a, b, c], html, 'https://www.bloomberg.com/markets/stocks')
|
||||
assert.deepEqual(result.map(article => article.url), [a.url, b.url, c.url])
|
||||
assert.deepEqual(result.map(article => article.published_at), ['2026-01-02T03:04:05.000Z', '2026-02-03T04:05:06.000Z', null])
|
||||
assert.equal(a.published_at, undefined)
|
||||
})
|
||||
|
||||
test('date hydration never fetches unrelated hosts or non-article URLs', async () => {
|
||||
let calls = 0
|
||||
const urls = ['http://127.0.0.1/news/articles/x', 'https://other.invalid/news/articles/x', 'https://www.bloomberg.com@other.invalid/news/articles/x', 'https://www.bloomberg.com:8443/news/articles/x', 'https://www.bloomberg.com/markets']
|
||||
const result = await withPublicationDates(urls.map(url => ({ url })), async () => { calls++; return '' })
|
||||
assert.equal(calls, 0)
|
||||
assert.ok(result.every(item => item.published_at === null))
|
||||
test('ambiguous cards, invalid metadata and unrelated URLs remain date-null', () => {
|
||||
const urls = ['https://www.bloomberg.com/news/articles/a', 'https://www.bloomberg.com/news/articles/b', 'http://127.0.0.1/news/articles/x', 'https://other.invalid/news/articles/x']
|
||||
const html = '<article><a data-component="story-link" href="/news/articles/a"></a><a data-component="story-link" href="/news/articles/b"></a><time datetime="2026-01-02T03:04:05Z"></time></article>' +
|
||||
'<script type="application/ld+json">{"@type":"NewsArticle","url":"http://127.0.0.1/news/articles/x","datePublished":"2026-01-02T03:04:05Z"}</script>'
|
||||
const result = withDatesFromLoadedHtml(urls.map(url => ({ url })), html, 'https://www.bloomberg.com/markets')
|
||||
assert.ok(result.every(article => article.published_at === null))
|
||||
})
|
||||
test('page-level publication metadata applies only to the already-loaded article', () => {
|
||||
const urls = ['https://www.bloomberg.com/news/articles/a', 'https://www.bloomberg.com/news/articles/b']
|
||||
const result = withDatesFromLoadedHtml(urls.map(url => ({ url })), '<meta name="iso-8601-publish-date" content="2026-01-02T03:04:05Z">', urls[0])
|
||||
assert.equal(result[0].published_at, '2026-01-02T03:04:05.000Z')
|
||||
assert.equal(result[1].published_at, null)
|
||||
})
|
||||
|
||||
test('the three observed page shapes expose all publication metadata keys', () => {
|
||||
|
||||
96
scraper-service/tests/reliability.test.js
Normal file
96
scraper-service/tests/reliability.test.js
Normal file
@ -0,0 +1,96 @@
|
||||
const { test } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const fs = require('node:fs')
|
||||
const vm = require('node:vm')
|
||||
const { createRequire } = require('node:module')
|
||||
const path = require('node:path')
|
||||
|
||||
const dated = '<article><a data-component="story-link" href="/news/articles/fixture-a"><div data-testid="headline"><span>Synthetic fixture headline</span></div></a><time datetime="2026-01-02T03:04:05Z"></time></article>'
|
||||
const undated = dated.replace('<time datetime="2026-01-02T03:04:05Z"></time>', '')
|
||||
|
||||
// Exercise actual collection/route code with fake transports, browser and timers.
|
||||
// No server, browser process or source network request is started.
|
||||
async function runFixture ({ byparr = true, httpError = false, launchError = false } = {}) {
|
||||
const route = {}
|
||||
const calls = []
|
||||
const navigations = []
|
||||
const logs = []
|
||||
let bodyReads = 0
|
||||
let contents = 0
|
||||
let result
|
||||
let status = 200
|
||||
let closed = false
|
||||
const app = { use () {}, get () {}, post (name, handler) { route[name] = handler }, listen () {} }
|
||||
const express = () => app
|
||||
express.json = () => () => {}
|
||||
const index = path.resolve(__dirname, '../index.js')
|
||||
const localRequire = createRequire(index)
|
||||
const page = {
|
||||
async setViewport () {}, async evaluateOnNewDocument () {},
|
||||
async goto (url) { navigations.push(url) },
|
||||
async evaluate () { return true }, async waitForSelector () {}, async type () {},
|
||||
async waitForFunction () {}, async waitForNavigation () {}, async $ () { return null },
|
||||
keyboard: { async press () {} },
|
||||
url () { return 'https://www.bloomberg.com/markets' },
|
||||
async content () { return contents++ === 0 ? undated : dated }
|
||||
}
|
||||
const browser = { async newPage () { return page }, async close () { closed = true } }
|
||||
vm.runInNewContext(fs.readFileSync(index, 'utf8'), {
|
||||
require (name) {
|
||||
if (name === 'express') return express
|
||||
if (name === 'puppeteer-extra') return {
|
||||
use () {},
|
||||
async launch () {
|
||||
if (launchError) throw new Error('PRIVATE_BODY https://PRIVATE_URL.invalid')
|
||||
return browser
|
||||
}
|
||||
}
|
||||
if (name === 'puppeteer-extra-plugin-stealth') return () => ({})
|
||||
return localRequire(name)
|
||||
},
|
||||
process: { env: { BYPARR_URL: byparr ? 'http://fixture.invalid' : '' } },
|
||||
setTimeout (callback) { queueMicrotask(callback) },
|
||||
console: { log (...args) { logs.push(args.join(' ')) }, error (...args) { logs.push(args.join(' ')) } },
|
||||
async fetch (_url, options) {
|
||||
const requested = JSON.parse(options.body).url
|
||||
calls.push(requested)
|
||||
return {
|
||||
ok: !httpError, status: httpError ? 503 : 200,
|
||||
async text () { bodyReads++; return 'PRIVATE_BODY https://PRIVATE_URL.invalid' },
|
||||
async json () { return { solution: { response: calls.length === 1 ? undated : dated } } }
|
||||
}
|
||||
}
|
||||
})
|
||||
await route['/bloomberg/scrape']({ body: { username: 'PRIVATE_USER', password: 'PRIVATE_SECRET' } }, {
|
||||
json (body) { result = body }, status (code) { status = code; return this }
|
||||
})
|
||||
return { result, status, calls, navigations, logs, bodyReads, closed }
|
||||
}
|
||||
|
||||
test('Byparr loads only seven existing sections and enriches a repeated link in place', async () => {
|
||||
const { result, calls, logs } = await runFixture()
|
||||
assert.equal(calls.length, 7)
|
||||
assert.ok(calls.every(url => !url.includes('/news/articles/')))
|
||||
assert.equal(result.articles.length, 1)
|
||||
assert.equal(result.articles[0].published_at, '2026-01-02T03:04:05.000Z')
|
||||
assert.ok(logs.every(line => !line.includes('http') && !line.includes('Synthetic fixture headline')))
|
||||
})
|
||||
test('Puppeteer loads only login and three existing sections, no article revisits', async () => {
|
||||
const { result, navigations, calls, closed, logs } = await runFixture({ byparr: false })
|
||||
assert.equal(navigations.length, 4)
|
||||
assert.ok(navigations.every(url => !url.includes('/news/articles/')))
|
||||
assert.equal(calls.length, 0)
|
||||
assert.equal(result.articles.length, 1)
|
||||
assert.equal(result.articles[0].published_at, '2026-01-02T03:04:05.000Z')
|
||||
assert.equal(closed, true)
|
||||
assert.ok(logs.every(line => !line.includes('http') && !line.includes('PRIVATE')))
|
||||
})
|
||||
test('scraper failures never expose response bodies, URLs or credentials', async () => {
|
||||
const http = await runFixture({ httpError: true })
|
||||
assert.equal(http.bodyReads, 0)
|
||||
assert.ok(http.logs.every(line => !line.includes('http') && !line.includes('PRIVATE')))
|
||||
const browser = await runFixture({ byparr: false, launchError: true })
|
||||
assert.equal(browser.status, 500)
|
||||
assert.equal(browser.result.error, 'scrape unavailable')
|
||||
assert.ok(browser.logs.every(line => !line.includes('http') && !line.includes('PRIVATE')))
|
||||
})
|
||||
Reference in New Issue
Block a user