const { test } = require('node:test')
const assert = require('node:assert/strict')
const { extractPublicationDate, withDatesFromLoadedHtml } = 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('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 c = { title: 'fixture-c', url: 'https://www.bloomberg.com/news/articles/fixture-c' }
const html = '' +
'' +
''
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('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 = '' +
''
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 })), '', 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', () => {
// 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')
})