import assert from 'node:assert/strict' import test from 'node:test' import { htmlSitemapGroupPage, htmlSitemapOverview, htmlSitemapPage } from '../server/lib/htmlSitemap.ts' test('htmlSitemapOverview: empty groups and malformed entries are left out', () => { const out = htmlSitemapOverview({ groups: [ { key: 'posts', slug: 'posts', title: 'Wpisy', total: 1234, more: true, entries: [{ title: 'A', url: 'https://x.test/a', extra: 1 }, { url: '/no-title' }] }, { key: 'tags', slug: 'tagi', title: 'Tagi', total: 0, entries: [] }, { key: 'bad', slug: '../x', title: 'Bad', entries: [{ title: 'B', url: '/b' }] }, { key: 'cats', slug: 'kategorie', title: 'Kategorie', entries: [{ title: 'C', url: '/c', children: [{ title: 'D', url: '/d' }] }] }, ], }) assert.deepEqual(out.groups.map((g) => g.slug), ['posts', 'kategorie']) assert.deepEqual(out.groups[0], { key: 'posts', slug: 'posts', title: 'Wpisy', total: 1234, entries: [{ title: 'A', url: 'https://x.test/a' }], more: true }) assert.deepEqual(out.groups[1]!.entries, [{ title: 'C', url: '/c', children: [{ title: 'D', url: '/d' }] }]) assert.equal(out.groups[1]!.total, 1, 'a missing total counts the entries') assert.deepEqual(htmlSitemapOverview(null), { groups: [] }) }) test('htmlSitemapGroupPage: page and pages', () => { assert.deepEqual(htmlSitemapGroupPage({ title: 'Wpisy', page: 2, pages: 3, entries: [{ title: 'A', url: '/a' }] }, 'posts'), { group: 'posts', title: 'Wpisy', page: 2, pages: 3, entries: [{ title: 'A', url: '/a' }], }) assert.equal(htmlSitemapGroupPage({ page: 4 }, 'posts').pages, 4) }) test('htmlSitemapPage: 1 by default, null for anything but a page number', () => { assert.equal(htmlSitemapPage(undefined), 1) assert.equal(htmlSitemapPage('3'), 3) assert.equal(htmlSitemapPage('0'), null) assert.equal(htmlSitemapPage('abc'), null) assert.equal(htmlSitemapPage(['1', '2']), null) assert.equal(htmlSitemapPage('1234567'), null) })