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 = '
Synthetic fixture headline
' const undated = dated.replace('', '') // 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'))) })