|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { buildSubstackNoteItem, buildSubstackPostItem, getSubstackPostSlug, renderSubstackNote } from './substack'; |
| 4 | + |
| 5 | +describe('Substack utilities', () => { |
| 6 | + it('extracts a post slug from a canonical URL', () => { |
| 7 | + expect(getSubstackPostSlug('https://example.substack.com/p/a-paid-post')).toBe('a-paid-post'); |
| 8 | + expect(getSubstackPostSlug('https://example.substack.com/archive')).toBeUndefined(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('uses the post API body and metadata instead of the feed preview', () => { |
| 12 | + const item = buildSubstackPostItem( |
| 13 | + { |
| 14 | + title: 'Feed title', |
| 15 | + link: 'https://example.substack.com/p/full-post', |
| 16 | + 'content:encoded': '<p>Feed preview</p>', |
| 17 | + creator: 'Feed author', |
| 18 | + }, |
| 19 | + { |
| 20 | + id: 42, |
| 21 | + title: 'Full post', |
| 22 | + slug: 'full-post', |
| 23 | + canonical_url: 'https://example.substack.com/p/full-post', |
| 24 | + body_html: '<p>Subscriber-visible body</p>', |
| 25 | + post_date: '2026-07-17T12:00:00.000Z', |
| 26 | + publishedBylines: [{ name: 'Author' }], |
| 27 | + postTags: [{ name: 'Markets' }], |
| 28 | + cover_image: 'https://example.com/cover.jpg', |
| 29 | + }, |
| 30 | + 'example' |
| 31 | + ); |
| 32 | + |
| 33 | + expect(item).toMatchObject({ |
| 34 | + title: 'Full post', |
| 35 | + description: '<p>Subscriber-visible body</p>', |
| 36 | + link: 'https://example.substack.com/p/full-post', |
| 37 | + guid: 'https://example.substack.com/p/full-post', |
| 38 | + author: 'Author', |
| 39 | + category: ['Markets'], |
| 40 | + image: 'https://example.com/cover.jpg', |
| 41 | + }); |
| 42 | + expect(item.pubDate).toEqual(new Date('2026-07-17T12:00:00.000Z')); |
| 43 | + }); |
| 44 | + |
| 45 | + it('keeps the feed body when post detail is unavailable', () => { |
| 46 | + const item = buildSubstackPostItem( |
| 47 | + { |
| 48 | + title: 'Feed title', |
| 49 | + link: 'https://example.substack.com/p/fallback', |
| 50 | + 'content:encoded': '<p>Feed body</p>', |
| 51 | + guid: 'post-42', |
| 52 | + }, |
| 53 | + undefined, |
| 54 | + 'example' |
| 55 | + ); |
| 56 | + |
| 57 | + expect(item).toMatchObject({ |
| 58 | + title: 'Feed title', |
| 59 | + description: '<p>Feed body</p>', |
| 60 | + link: 'https://example.substack.com/p/fallback', |
| 61 | + guid: 'https://example.substack.com/p/fallback', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('renders rich Notes content and attachments safely', () => { |
| 66 | + const note = { |
| 67 | + id: 123, |
| 68 | + body: 'A complete note', |
| 69 | + date: '2026-07-16T19:41:28.774Z', |
| 70 | + name: 'Writer', |
| 71 | + handle: 'writer', |
| 72 | + body_json: { |
| 73 | + type: 'doc', |
| 74 | + content: [ |
| 75 | + { |
| 76 | + type: 'paragraph', |
| 77 | + content: [ |
| 78 | + { type: 'text', text: 'Bold & ', marks: [{ type: 'bold' }] }, |
| 79 | + { type: 'text', text: 'linked', marks: [{ type: 'link', attrs: { href: 'https://example.com/read' } }] }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + attachments: [ |
| 85 | + { type: 'image' as const, imageUrl: 'https://example.com/chart.png', imageWidth: 1200, imageHeight: 800 }, |
| 86 | + { |
| 87 | + type: 'link' as const, |
| 88 | + linkMetadata: { |
| 89 | + url: 'https://example.com/source', |
| 90 | + title: 'Source', |
| 91 | + description: 'Details & context', |
| 92 | + }, |
| 93 | + }, |
| 94 | + ], |
| 95 | + }; |
| 96 | + |
| 97 | + const description = renderSubstackNote(note); |
| 98 | + expect(description).toContain('<strong>Bold & </strong>'); |
| 99 | + expect(description).toContain('<a href="https://example.com/read">linked</a>'); |
| 100 | + expect(description).toContain('<img src="https://example.com/chart.png" width="1200" height="800">'); |
| 101 | + expect(description).toContain('<figcaption>Details & context</figcaption>'); |
| 102 | + |
| 103 | + const item = buildSubstackNoteItem(note, { name: 'Writer', handle: 'writer' }); |
| 104 | + expect(item).toMatchObject({ |
| 105 | + title: 'A complete note', |
| 106 | + link: 'https://substack.com/@writer/note/c-123', |
| 107 | + guid: 'https://substack.com/@writer/note/c-123', |
| 108 | + author: [ |
| 109 | + { |
| 110 | + name: 'Writer', |
| 111 | + url: 'https://substack.com/@writer', |
| 112 | + }, |
| 113 | + ], |
| 114 | + }); |
| 115 | + expect(item.pubDate).toEqual(new Date('2026-07-16T19:41:28.774Z')); |
| 116 | + }); |
| 117 | + |
| 118 | + it('escapes a plain-text Note fallback', () => { |
| 119 | + expect(renderSubstackNote({ body: '<script>alert(1)</script>\nsecond line' })).toBe('<p><script>alert(1)</script><br>second line</p>'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('handles attachment-only Notes and rejects unsafe attachment URLs', () => { |
| 123 | + const note = { |
| 124 | + id: 456, |
| 125 | + attachments: [ |
| 126 | + { type: 'image' as const, imageUrl: 'https://example.com/chart.heic' }, |
| 127 | + { type: 'link' as const, linkMetadata: { url: 'javascript:alert(1)', title: 'Unsafe' } }, |
| 128 | + ], |
| 129 | + }; |
| 130 | + |
| 131 | + const item = buildSubstackNoteItem(note, { name: 'Writer', handle: 'writer' }); |
| 132 | + expect(item.title).toBe('Note by Writer'); |
| 133 | + expect(item.description).toBe('<p><img src="https://example.com/chart.heic"></p>'); |
| 134 | + }); |
| 135 | +}); |
0 commit comments