Files
smartfeed/test/test.export.node+bun+deno.ts

142 lines
4.8 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartfeed from '../ts/index.js';
let testSmartFeed: smartfeed.Smartfeed;
let testFeed: smartfeed.Feed;
tap.test('setup', async () => {
testSmartFeed = new smartfeed.Smartfeed();
testFeed = testSmartFeed.createFeed({
domain: 'central.eu',
title: 'central.eu - ideas for Europe',
category: 'Politics',
company: 'Lossless GmbH',
companyDomain: 'https://lossless.com',
companyEmail: 'hello@lossless.com',
description: 'ideas for Europe',
});
testFeed.addItem({
title: 'A better European Union',
authorName: 'Phil',
imageUrl: 'https://central.eu/someimage.png',
timestamp: Date.now(),
url: 'https://central.eu/article/somearticle',
content: 'somecontent',
});
});
tap.test('should export RSS 2.0 feed', async () => {
const rssFeed = testFeed.exportRssFeedString();
expect(rssFeed).toInclude('<?xml version="1.0" encoding="utf-8"?>');
expect(rssFeed).toInclude('<rss version="2.0"');
expect(rssFeed).toInclude('<title>central.eu - ideas for Europe</title>');
expect(rssFeed).toInclude('<link>https://central.eu</link>');
expect(rssFeed).toInclude('<description>ideas for Europe</description>');
expect(rssFeed).toInclude('<generator>@push.rocks/smartfeed</generator>');
expect(rssFeed).toInclude('<category>Politics</category>');
expect(rssFeed).toInclude('<item>');
expect(rssFeed).toInclude('A better European Union');
});
tap.test('should export Atom 1.0 feed', async () => {
const atomFeed = testFeed.exportAtomFeed();
expect(atomFeed).toInclude('<?xml version="1.0" encoding="utf-8"?>');
expect(atomFeed).toInclude('<feed xmlns="http://www.w3.org/2005/Atom">');
expect(atomFeed).toInclude('<title>central.eu - ideas for Europe</title>');
expect(atomFeed).toInclude('<subtitle>ideas for Europe</subtitle>');
expect(atomFeed).toInclude('<generator>@push.rocks/smartfeed</generator>');
expect(atomFeed).toInclude('<entry>');
expect(atomFeed).toInclude('A better European Union');
});
tap.test('should export JSON Feed 1.0', async () => {
const jsonFeedString = testFeed.exportJsonFeed();
const jsonFeed = JSON.parse(jsonFeedString);
expect(jsonFeed.version).toEqual('https://jsonfeed.org/version/1');
expect(jsonFeed.title).toEqual('central.eu - ideas for Europe');
expect(jsonFeed.home_page_url).toEqual('https://central.eu');
expect(jsonFeed.description).toEqual('ideas for Europe');
expect(jsonFeed.items).toBeArray();
expect(jsonFeed.items.length).toEqual(1);
expect(jsonFeed.items[0].title).toEqual('A better European Union');
});
tap.test('should include correct item data in RSS', async () => {
const rssFeed = testFeed.exportRssFeedString();
expect(rssFeed).toInclude('https://central.eu/article/somearticle');
expect(rssFeed).toInclude('https://central.eu/someimage.png');
expect(rssFeed).toInclude('somecontent');
expect(rssFeed).toInclude('<enclosure');
});
tap.test('should include correct author information', async () => {
const rssFeed = testFeed.exportRssFeedString();
const atomFeed = testFeed.exportAtomFeed();
// RSS doesn't always include dc:creator, but Atom should have author
expect(atomFeed).toInclude('<name>Phil</name>');
});
tap.test('should handle multiple items in export', async () => {
const feed = testSmartFeed.createFeed({
domain: 'multi.example.com',
title: 'Multi-item Feed',
category: 'Test',
company: 'Test Inc',
companyDomain: 'https://example.com',
companyEmail: 'test@example.com',
description: 'Testing multiple items',
});
for (let i = 1; i <= 5; i++) {
feed.addItem({
title: `Article ${i}`,
authorName: 'Author',
imageUrl: `https://example.com/image${i}.png`,
timestamp: Date.now() + i,
url: `https://example.com/article/${i}`,
content: `Content for article ${i}`,
});
}
const rssFeed = feed.exportRssFeedString();
const jsonFeedString = feed.exportJsonFeed();
const jsonFeed = JSON.parse(jsonFeedString);
expect(jsonFeed.items.length).toEqual(5);
expect(rssFeed).toInclude('Article 1');
expect(rssFeed).toInclude('Article 5');
});
tap.test('should export feed with custom item IDs', async () => {
const feed = testSmartFeed.createFeed({
domain: 'example.com',
title: 'Custom ID Feed',
category: 'Test',
company: 'Test Inc',
companyDomain: 'https://example.com',
companyEmail: 'test@example.com',
description: 'Testing custom IDs',
});
feed.addItem({
title: 'Article with custom ID',
authorName: 'Author',
imageUrl: 'https://example.com/image.png',
timestamp: Date.now(),
url: 'https://example.com/article/custom',
content: 'Content',
id: 'custom-uuid-123',
});
const rssFeed = feed.exportRssFeedString();
expect(rssFeed).toInclude('custom-uuid-123');
});
export default tap.start();