const { test } = require('node:test')
const assert = require('node:assert/strict')
const { extractPublicationDate, withPublicationDates } = require('../publication-date')
test('minimal metadata fixtures preserve genuine dates with explicit zones', () => {
assert.equal(extractPublicationDate(''), '2026-01-02T03:04:05.000Z')
assert.equal(extractPublicationDate(''), '2026-01-02T03:04:05.000Z')
assert.equal(extractPublicationDate(''), '2026-01-02T03:04:05.000Z')
})
test('invalid or missing metadata never manufactures a collection date', () => {
for (const html of ['', '', '', '', '', '']) {
assert.equal(extractPublicationDate(html), null)
}
})
test('metadata priority and failed hydration retain order and nullable dates', async () => {
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 ''
})
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)
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('the three observed page shapes expose all publication metadata keys', () => {
// Attribute-only synthetic snippets: no source titles, URLs or page content.
for (const date of ['2026-01-02T03:04:05Z', '2026-02-03T04:05:06Z', '2026-03-04T05:06:07Z']) {
const html = ``
assert.equal(extractPublicationDate(html), new Date(date).toISOString())
}
})
test('invalid primary metadata falls through time then JSON-LD publication date', () => {
const json = ''
assert.equal(extractPublicationDate('' + json), '2026-02-03T04:05:06.000Z')
assert.equal(extractPublicationDate('' + json), '2026-01-02T03:04:05.000Z')
})