Files
TradarrV2/scraper-service/tests/publication-date.test.js

50 lines
3.5 KiB
JavaScript

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('<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('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 '<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)
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 = `<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')
})