@@ -5,9 +5,10 @@ import { readFileSync } from 'fs'
55
66import nock from 'nock'
77
8- import { hasProperty } from 'bellajs'
8+ import { hasProperty , isString } from 'bellajs'
99
1010import { read } from './main.js'
11+ import { isValid as isValidUrl } from './utils/linker.js'
1112
1213const feedAttrs = 'title link description generator language published entries' . split ( ' ' )
1314const entryAttrs = 'title link description published id' . split ( ' ' )
@@ -20,6 +21,19 @@ const parseUrl = (url) => {
2021 }
2122}
2223
24+ const isValidDate = ( d ) => {
25+ return ( new Date ( d ) ) . toString ( ) !== 'Invalid Date'
26+ }
27+
28+ const validateProps = ( entry ) => {
29+ const { id, link, title, published, description } = entry
30+ return isString ( description ) &&
31+ isString ( id ) && id !== '' &&
32+ isString ( title ) && title !== '' &&
33+ isString ( link ) && isValidUrl ( link ) &&
34+ isString ( published ) && isValidDate ( published )
35+ }
36+
2337describe ( 'test read() function with common issues' , ( ) => {
2438 test ( 'read feed from a non-string link' , ( ) => {
2539 expect ( read ( [ ] ) ) . rejects . toThrow ( new Error ( 'Input param must be a valid URL' ) )
@@ -76,6 +90,7 @@ describe('test read() standard feed', (done) => {
7690 entryAttrs . forEach ( ( k ) => {
7791 expect ( hasProperty ( result . entries [ 0 ] , k ) ) . toBe ( true )
7892 } )
93+ expect ( validateProps ( result . entries [ 0 ] ) ) . toBe ( true )
7994 } )
8095
8196 test ( 'read atom feed from Google' , async ( ) => {
@@ -92,6 +107,7 @@ describe('test read() standard feed', (done) => {
92107 entryAttrs . forEach ( ( k ) => {
93108 expect ( hasProperty ( result . entries [ 0 ] , k ) ) . toBe ( true )
94109 } )
110+ expect ( validateProps ( result . entries [ 0 ] ) ) . toBe ( true )
95111 } )
96112
97113 test ( 'read atom feed from Google with extraFields' , async ( ) => {
@@ -115,6 +131,7 @@ describe('test read() standard feed', (done) => {
115131 } )
116132 expect ( hasProperty ( result , 'author' ) ) . toBe ( true )
117133 expect ( hasProperty ( result . entries [ 0 ] , 'id' ) ) . toBe ( true )
134+ expect ( validateProps ( result . entries [ 0 ] ) ) . toBe ( true )
118135 } )
119136
120137 test ( 'read atom feed which contains multi links' , async ( ) => {
@@ -131,6 +148,7 @@ describe('test read() standard feed', (done) => {
131148 entryAttrs . forEach ( ( k ) => {
132149 expect ( hasProperty ( result . entries [ 0 ] , k ) ) . toBe ( true )
133150 } )
151+ expect ( validateProps ( result . entries [ 0 ] ) ) . toBe ( true )
134152 } )
135153
136154 test ( 'read json feed from Micro.blog' , async ( ) => {
@@ -147,6 +165,7 @@ describe('test read() standard feed', (done) => {
147165 entryAttrs . forEach ( ( k ) => {
148166 expect ( hasProperty ( result . entries [ 0 ] , k ) ) . toBe ( true )
149167 } )
168+ expect ( validateProps ( result . entries [ 0 ] ) ) . toBe ( true )
150169 } )
151170
152171 test ( 'read json feed from Micro.blog with extra fields' , async ( ) => {
@@ -170,6 +189,24 @@ describe('test read() standard feed', (done) => {
170189 } )
171190 expect ( hasProperty ( result , 'icon' ) ) . toBe ( true )
172191 expect ( hasProperty ( result . entries [ 0 ] , 'id' ) ) . toBe ( true )
192+ expect ( validateProps ( result . entries [ 0 ] ) ) . toBe ( true )
193+ } )
194+
195+ test ( 'read rss feed from huggingface.co (no link)' , async ( ) => {
196+ const url = 'https://huggingface.co/no-link/rss'
197+ const xml = readFileSync ( 'test-data/rss-feed-miss-link.xml' , 'utf8' )
198+ const { baseUrl, path } = parseUrl ( url )
199+ nock ( baseUrl ) . get ( path ) . reply ( 200 , xml , {
200+ 'Content-Type' : 'application/xml'
201+ } )
202+ const result = await read ( url )
203+ feedAttrs . forEach ( ( k ) => {
204+ expect ( hasProperty ( result , k ) ) . toBe ( true )
205+ } )
206+ entryAttrs . forEach ( ( k ) => {
207+ expect ( hasProperty ( result . entries [ 0 ] , k ) ) . toBe ( true )
208+ } )
209+ expect ( validateProps ( result . entries [ 0 ] ) ) . toBe ( true )
173210 } )
174211} )
175212
0 commit comments