2024-02-04 14:58:44 -05:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { describe, it } from 'node:test';
|
|
|
|
|
2024-01-24 00:37:06 -05:00
|
|
|
import { z } from 'astro/zod';
|
2023-12-04 11:56:32 -05:00
|
|
|
import rss, { getRssString } from '../dist/index.js';
|
|
|
|
import { rssSchema } from '../dist/schema.js';
|
2023-01-19 11:24:55 -05:00
|
|
|
import {
|
|
|
|
description,
|
2024-02-21 09:08:19 -05:00
|
|
|
parseXmlString,
|
2023-01-19 11:24:55 -05:00
|
|
|
phpFeedItem,
|
|
|
|
phpFeedItemWithContent,
|
|
|
|
phpFeedItemWithCustomData,
|
2023-12-04 11:56:32 -05:00
|
|
|
site,
|
|
|
|
title,
|
2023-01-19 11:24:55 -05:00
|
|
|
web1FeedItem,
|
2023-04-26 07:58:53 -05:00
|
|
|
web1FeedItemWithAllData,
|
2023-12-04 11:56:32 -05:00
|
|
|
web1FeedItemWithContent,
|
2023-01-19 11:24:55 -05:00
|
|
|
} from './test-utils.js';
|
2022-05-03 17:26:13 -05:00
|
|
|
|
|
|
|
// note: I spent 30 minutes looking for a nice node-based snapshot tool
|
|
|
|
// ...and I gave up. Enjoy big strings!
|
2022-12-06 18:19:55 -05:00
|
|
|
// prettier-ignore
|
2023-04-26 07:58:53 -05:00
|
|
|
const validXmlResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link><item><title><![CDATA[${phpFeedItem.title}]]></title><link>${site}${phpFeedItem.link}/</link><guid isPermaLink="true">${site}${phpFeedItem.link}/</guid><description><![CDATA[${phpFeedItem.description}]]></description><pubDate>${new Date(phpFeedItem.pubDate).toUTCString()}</pubDate></item><item><title><![CDATA[${web1FeedItem.title}]]></title><link>${site}${web1FeedItem.link}/</link><guid isPermaLink="true">${site}${web1FeedItem.link}/</guid><description><![CDATA[${web1FeedItem.description}]]></description><pubDate>${new Date(web1FeedItem.pubDate).toUTCString()}</pubDate></item></channel></rss>`;
|
2022-12-06 18:19:55 -05:00
|
|
|
// prettier-ignore
|
2023-04-26 07:58:53 -05:00
|
|
|
const validXmlWithContentResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link><item><title><![CDATA[${phpFeedItemWithContent.title}]]></title><link>${site}${phpFeedItemWithContent.link}/</link><guid isPermaLink="true">${site}${phpFeedItemWithContent.link}/</guid><description><![CDATA[${phpFeedItemWithContent.description}]]></description><pubDate>${new Date(phpFeedItemWithContent.pubDate).toUTCString()}</pubDate><content:encoded><![CDATA[${phpFeedItemWithContent.content}]]></content:encoded></item><item><title><![CDATA[${web1FeedItemWithContent.title}]]></title><link>${site}${web1FeedItemWithContent.link}/</link><guid isPermaLink="true">${site}${web1FeedItemWithContent.link}/</guid><description><![CDATA[${web1FeedItemWithContent.description}]]></description><pubDate>${new Date(web1FeedItemWithContent.pubDate).toUTCString()}</pubDate><content:encoded><![CDATA[${web1FeedItemWithContent.content}]]></content:encoded></item></channel></rss>`;
|
2022-12-13 08:45:35 -05:00
|
|
|
// prettier-ignore
|
2023-04-26 07:58:53 -05:00
|
|
|
const validXmlResultWithAllData = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link><item><title><![CDATA[${phpFeedItem.title}]]></title><link>${site}${phpFeedItem.link}/</link><guid isPermaLink="true">${site}${phpFeedItem.link}/</guid><description><![CDATA[${phpFeedItem.description}]]></description><pubDate>${new Date(phpFeedItem.pubDate).toUTCString()}</pubDate></item><item><title><![CDATA[${web1FeedItemWithAllData.title}]]></title><link>${site}${web1FeedItemWithAllData.link}/</link><guid isPermaLink="true">${site}${web1FeedItemWithAllData.link}/</guid><description><![CDATA[${web1FeedItemWithAllData.description}]]></description><pubDate>${new Date(web1FeedItemWithAllData.pubDate).toUTCString()}</pubDate><category>${web1FeedItemWithAllData.categories[0]}</category><category>${web1FeedItemWithAllData.categories[1]}</category><author>${web1FeedItemWithAllData.author}</author><comments>${web1FeedItemWithAllData.commentsUrl}</comments><source url="${web1FeedItemWithAllData.source.url}">${web1FeedItemWithAllData.source.title}</source><enclosure url="${site}${web1FeedItemWithAllData.enclosure.url}" length="${web1FeedItemWithAllData.enclosure.length}" type="${web1FeedItemWithAllData.enclosure.type}"/></item></channel></rss>`;
|
|
|
|
// prettier-ignore
|
|
|
|
const validXmlWithCustomDataResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link><item><title><![CDATA[${phpFeedItemWithCustomData.title}]]></title><link>${site}${phpFeedItemWithCustomData.link}/</link><guid isPermaLink="true">${site}${phpFeedItemWithCustomData.link}/</guid><description><![CDATA[${phpFeedItemWithCustomData.description}]]></description><pubDate>${new Date(phpFeedItemWithCustomData.pubDate).toUTCString()}</pubDate>${phpFeedItemWithCustomData.customData}</item><item><title><![CDATA[${web1FeedItemWithContent.title}]]></title><link>${site}${web1FeedItemWithContent.link}/</link><guid isPermaLink="true">${site}${web1FeedItemWithContent.link}/</guid><description><![CDATA[${web1FeedItemWithContent.description}]]></description><pubDate>${new Date(web1FeedItemWithContent.pubDate).toUTCString()}</pubDate><content:encoded><![CDATA[${web1FeedItemWithContent.content}]]></content:encoded></item></channel></rss>`;
|
2022-12-14 08:39:48 -05:00
|
|
|
// prettier-ignore
|
|
|
|
const validXmlWithStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feedstylesheet.css"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link></channel></rss>`;
|
|
|
|
// prettier-ignore
|
|
|
|
const validXmlWithXSLStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feedstylesheet.xsl" type="text/xsl"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link></channel></rss>`;
|
2022-05-03 17:26:13 -05:00
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
function assertXmlDeepEqual(a, b) {
|
|
|
|
const parsedA = parseXmlString(a);
|
|
|
|
const parsedB = parseXmlString(b);
|
|
|
|
|
2024-02-16 06:41:16 -05:00
|
|
|
assert.equal(parsedA.err, null);
|
|
|
|
assert.equal(parsedB.err, null);
|
|
|
|
assert.deepEqual(parsedA.result, parsedB.result);
|
2024-02-04 14:58:44 -05:00
|
|
|
}
|
|
|
|
|
2022-05-03 17:26:13 -05:00
|
|
|
describe('rss', () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
it('should return a response', async () => {
|
|
|
|
const response = await rss({
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [phpFeedItem, web1FeedItem],
|
|
|
|
site,
|
|
|
|
});
|
|
|
|
|
|
|
|
const str = await response.text();
|
2024-02-04 14:58:44 -05:00
|
|
|
|
|
|
|
// NOTE: Chai used the below parser to perform the tests, but I have omitted it for now.
|
|
|
|
// parser = new xml2js.Parser({ trim: flag(this, 'deep') });
|
|
|
|
|
|
|
|
assertXmlDeepEqual(str, validXmlResult);
|
2023-08-23 08:09:11 -05:00
|
|
|
|
|
|
|
const contentType = response.headers.get('Content-Type');
|
2024-02-16 06:41:16 -05:00
|
|
|
assert.equal(contentType, 'application/xml');
|
2023-08-23 08:09:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be the same string as getRssString', async () => {
|
|
|
|
const options = {
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [phpFeedItem, web1FeedItem],
|
|
|
|
site,
|
|
|
|
};
|
|
|
|
|
|
|
|
const response = await rss(options);
|
|
|
|
const str1 = await response.text();
|
|
|
|
const str2 = await getRssString(options);
|
|
|
|
|
2024-02-16 06:41:16 -05:00
|
|
|
assert.equal(str1, str2);
|
2023-08-23 08:09:11 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getRssString', () => {
|
2022-05-03 17:26:13 -05:00
|
|
|
it('should generate on valid RSSFeedItem array', async () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2022-05-03 17:26:13 -05:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [phpFeedItem, web1FeedItem],
|
2022-05-05 17:03:25 -05:00
|
|
|
site,
|
2022-05-03 17:26:13 -05:00
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assertXmlDeepEqual(str, validXmlResult);
|
2022-05-03 17:27:01 -05:00
|
|
|
});
|
2022-05-03 17:26:13 -05:00
|
|
|
|
2022-12-06 18:19:55 -05:00
|
|
|
it('should generate on valid RSSFeedItem array with HTML content included', async () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2022-12-06 18:19:55 -05:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [phpFeedItemWithContent, web1FeedItemWithContent],
|
|
|
|
site,
|
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assertXmlDeepEqual(str, validXmlWithContentResult);
|
2022-12-06 18:19:55 -05:00
|
|
|
});
|
|
|
|
|
2023-04-26 07:58:53 -05:00
|
|
|
it('should generate on valid RSSFeedItem array with all RSS content included', async () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2023-04-26 07:58:53 -05:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [phpFeedItem, web1FeedItemWithAllData],
|
|
|
|
site,
|
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assertXmlDeepEqual(str, validXmlResultWithAllData);
|
2023-04-26 07:58:53 -05:00
|
|
|
});
|
|
|
|
|
2022-12-13 08:45:35 -05:00
|
|
|
it('should generate on valid RSSFeedItem array with custom data included', async () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2022-12-13 08:45:35 -05:00
|
|
|
xmlns: {
|
|
|
|
dc: 'http://purl.org/dc/elements/1.1/',
|
|
|
|
},
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [phpFeedItemWithCustomData, web1FeedItemWithContent],
|
|
|
|
site,
|
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assertXmlDeepEqual(str, validXmlWithCustomDataResult);
|
2022-12-13 08:45:35 -05:00
|
|
|
});
|
|
|
|
|
2022-12-14 08:39:48 -05:00
|
|
|
it('should include xml-stylesheet instruction when stylesheet is defined', async () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2022-12-14 08:39:48 -05:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [],
|
|
|
|
site,
|
|
|
|
stylesheet: '/feedstylesheet.css',
|
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assertXmlDeepEqual(str, validXmlWithStylesheet);
|
2022-12-14 08:39:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should include xml-stylesheet instruction with xsl type when stylesheet is set to xsl file', async () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2022-12-14 08:39:48 -05:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [],
|
|
|
|
site,
|
|
|
|
stylesheet: '/feedstylesheet.xsl',
|
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assertXmlDeepEqual(str, validXmlWithXSLStylesheet);
|
2022-12-14 08:39:48 -05:00
|
|
|
});
|
|
|
|
|
2023-03-13 16:34:23 -05:00
|
|
|
it('should preserve self-closing tags on `customData`', async () => {
|
|
|
|
const customData =
|
|
|
|
'<atom:link href="https://example.com/feed.xml" rel="self" type="application/rss+xml"/>';
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2023-03-13 16:34:23 -05:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [],
|
|
|
|
site,
|
|
|
|
xmlns: {
|
|
|
|
atom: 'http://www.w3.org/2005/Atom',
|
|
|
|
},
|
|
|
|
customData,
|
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assert.ok(str.includes(customData));
|
2023-03-13 16:34:23 -05:00
|
|
|
});
|
|
|
|
|
2023-03-09 02:57:03 -05:00
|
|
|
it('should not append trailing slash to URLs with the given option', async () => {
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2023-03-09 02:57:03 -05:00
|
|
|
title,
|
|
|
|
description,
|
2023-11-27 11:44:20 -05:00
|
|
|
items: [phpFeedItem],
|
2023-03-09 02:57:03 -05:00
|
|
|
site,
|
|
|
|
trailingSlash: false,
|
|
|
|
});
|
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assert.ok(str.includes('https://example.com/<'));
|
|
|
|
assert.ok(str.includes('https://example.com/php<'));
|
2023-03-09 02:57:03 -05:00
|
|
|
});
|
|
|
|
|
2023-01-19 11:24:55 -05:00
|
|
|
it('Deprecated import.meta.glob mapping still works', async () => {
|
|
|
|
const globResult = {
|
|
|
|
'./posts/php.md': () =>
|
|
|
|
new Promise((resolve) =>
|
|
|
|
resolve({
|
|
|
|
url: phpFeedItem.link,
|
|
|
|
frontmatter: {
|
|
|
|
title: phpFeedItem.title,
|
|
|
|
pubDate: phpFeedItem.pubDate,
|
|
|
|
description: phpFeedItem.description,
|
2022-07-13 15:39:29 -05:00
|
|
|
},
|
2023-01-19 11:24:55 -05:00
|
|
|
})
|
|
|
|
),
|
|
|
|
'./posts/nested/web1.md': () =>
|
|
|
|
new Promise((resolve) =>
|
|
|
|
resolve({
|
|
|
|
url: web1FeedItem.link,
|
|
|
|
frontmatter: {
|
|
|
|
title: web1FeedItem.title,
|
|
|
|
pubDate: web1FeedItem.pubDate,
|
|
|
|
description: web1FeedItem.description,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
),
|
|
|
|
};
|
2022-09-12 14:05:57 -05:00
|
|
|
|
2023-08-23 08:09:11 -05:00
|
|
|
const str = await getRssString({
|
2023-01-19 11:24:55 -05:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: globResult,
|
|
|
|
site,
|
2022-09-12 14:05:57 -05:00
|
|
|
});
|
2023-01-19 11:24:55 -05:00
|
|
|
|
2024-02-04 14:58:44 -05:00
|
|
|
assertXmlDeepEqual(str, validXmlResult);
|
2022-07-13 15:39:29 -05:00
|
|
|
});
|
2023-05-15 01:10:32 -05:00
|
|
|
|
2023-05-15 01:12:35 -05:00
|
|
|
it('should fail when an invalid date string is provided', async () => {
|
|
|
|
const res = rssSchema.safeParse({
|
|
|
|
title: phpFeedItem.title,
|
|
|
|
pubDate: 'invalid date',
|
|
|
|
description: phpFeedItem.description,
|
|
|
|
link: phpFeedItem.link,
|
|
|
|
});
|
|
|
|
|
2024-02-16 06:41:16 -05:00
|
|
|
assert.equal(res.success, false);
|
|
|
|
assert.equal(res.error.issues[0].path[0], 'pubDate');
|
2023-05-15 01:12:35 -05:00
|
|
|
});
|
2024-01-24 00:37:06 -05:00
|
|
|
|
|
|
|
it('should be extendable', () => {
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
rssSchema.extend({
|
|
|
|
category: z.string().optional(),
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
error = e.message;
|
|
|
|
}
|
2024-02-16 06:41:16 -05:00
|
|
|
assert.equal(error, null);
|
2024-01-24 00:37:06 -05:00
|
|
|
});
|
2024-02-04 14:42:26 -05:00
|
|
|
|
|
|
|
it('should not fail when an enclosure has a length of 0', async () => {
|
2024-02-05 04:13:54 -05:00
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await getRssString({
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
title: 'Title',
|
|
|
|
pubDate: new Date().toISOString(),
|
|
|
|
description: 'Description',
|
|
|
|
link: '/link',
|
|
|
|
enclosure: {
|
|
|
|
url: '/enclosure',
|
|
|
|
length: 0,
|
|
|
|
type: 'audio/mpeg',
|
|
|
|
},
|
2024-02-04 14:42:26 -05:00
|
|
|
},
|
2024-02-05 04:13:54 -05:00
|
|
|
],
|
|
|
|
site,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
error = e.message;
|
|
|
|
}
|
2024-02-04 14:42:26 -05:00
|
|
|
|
2024-02-16 06:41:16 -05:00
|
|
|
assert.equal(error, null);
|
2024-02-04 14:42:26 -05:00
|
|
|
});
|
2022-05-03 17:27:01 -05:00
|
|
|
});
|