53 lines
4.6 KiB
JavaScript
53 lines
4.6 KiB
JavaScript
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('<meta name="iso-8601-publish-date" content="2026-01-02T03:04:05Z">'), '2026-01-02T03:04:05.000Z')
|
|
assert.equal(extractPublicationDate('<time datetime="2026-01-02T04:04:05+01:00"></time>'), '2026-01-02T03:04:05.000Z')
|
|
assert.equal(extractPublicationDate('<script type="application/ld+json">{"@graph":[{"@type":"NewsArticle","datePublished":"2026-01-02T03:04:05Z"}]}</script>'), '2026-01-02T03:04:05.000Z')
|
|
})
|
|
test('invalid or missing metadata never manufactures a collection date', () => {
|
|
for (const html of ['', '<time datetime="2026-01-02"></time>', '<time datetime="2026-02-30T03:04:05Z"></time>', '<time datetime="2026-01-02T03:04:05"></time>', '<script type="application/ld+json">bad</script>', '<script type="application/ld+json">{"@type":"WebSite","datePublished":"2026-01-02T03:04:05Z"}</script>']) {
|
|
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 = '<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('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', () => {
|
|
// 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 = `<meta name="iso-8601-publish-date" content="${date}"><time datetime="${date}"></time><script type="application/ld+json">{"@type":"NewsArticle","datePublished":"${date}"}</script>`
|
|
assert.equal(extractPublicationDate(html), new Date(date).toISOString())
|
|
}
|
|
})
|
|
test('invalid primary metadata falls through time then JSON-LD publication date', () => {
|
|
const json = '<script type="application/ld+json">{"@type":"NewsArticle","datePublished":"2026-01-02T03:04:05Z"}</script>'
|
|
assert.equal(extractPublicationDate('<meta name="iso-8601-publish-date" content="invalid"><time datetime="2026-02-03T04:05:06Z"></time>' + json), '2026-02-03T04:05:06.000Z')
|
|
assert.equal(extractPublicationDate('<meta name="iso-8601-publish-date" content="invalid"><time datetime="invalid"></time>' + json), '2026-01-02T03:04:05.000Z')
|
|
})
|