|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { DownloadPathTemplateEngine } from "./TemplateEngine"; |
| 2 | +import { |
| 3 | + DownloadPathTemplateEngine, |
| 4 | + FeedFilePathTemplateEngine, |
| 5 | + FeedNoteTemplateEngine, |
| 6 | + NoteTemplateEngine, |
| 7 | + getFeedNoteWikilink, |
| 8 | +} from "./TemplateEngine"; |
3 | 9 | import type { Episode } from "./types/Episode"; |
| 10 | +import type { PodcastFeed } from "./types/PodcastFeed"; |
| 11 | +import { plugin } from "./store"; |
4 | 12 |
|
5 | 13 | // The illegal-character sanitizer is private; exercise it through |
6 | 14 | // DownloadPathTemplateEngine, which applies it to {{title}} and {{podcast}}. |
@@ -49,3 +57,168 @@ describe("replaceIllegalFileNameCharactersInString (via DownloadPathTemplateEngi |
49 | 57 | ); |
50 | 58 | }); |
51 | 59 | }); |
| 60 | + |
| 61 | +const demoEpisode: Episode = { |
| 62 | + title: "Episode 1", |
| 63 | + streamUrl: "https://example.com/ep1.mp3", |
| 64 | + url: "https://example.com/ep1", |
| 65 | + description: "", |
| 66 | + content: "", |
| 67 | + podcastName: "My Show", |
| 68 | + feedUrl: "https://example.com/feed.xml", |
| 69 | + artworkUrl: "https://example.com/ep1.png", |
| 70 | + episodeDate: new Date("2024-01-01"), |
| 71 | +}; |
| 72 | + |
| 73 | +describe("NoteTemplateEngine feed-scoped tags (#163)", () => { |
| 74 | + it("keeps {{url}} and {{artwork}} pointing at the episode itself", () => { |
| 75 | + plugin.set({ settings: { feedNote: { path: "" }, savedFeeds: {} } } as never); |
| 76 | + expect(NoteTemplateEngine("{{url}}|{{artwork}}", demoEpisode)).toBe( |
| 77 | + "https://example.com/ep1|https://example.com/ep1.png", |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("adds episode aliases and {{feedurl}}", () => { |
| 82 | + plugin.set({ settings: { feedNote: { path: "" }, savedFeeds: {} } } as never); |
| 83 | + expect(NoteTemplateEngine("{{episodeurl}}", demoEpisode)).toBe( |
| 84 | + "https://example.com/ep1", |
| 85 | + ); |
| 86 | + expect(NoteTemplateEngine("{{episodeartwork}}", demoEpisode)).toBe( |
| 87 | + "https://example.com/ep1.png", |
| 88 | + ); |
| 89 | + expect(NoteTemplateEngine("{{feedurl}}", demoEpisode)).toBe( |
| 90 | + "https://example.com/feed.xml", |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it("resolves {{feedartwork}} from the saved feed, else the episode art", () => { |
| 95 | + plugin.set({ |
| 96 | + settings: { |
| 97 | + feedNote: { path: "" }, |
| 98 | + savedFeeds: { |
| 99 | + "My Show": { |
| 100 | + title: "My Show", |
| 101 | + url: "x", |
| 102 | + artworkUrl: "https://example.com/feed.png", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + } as never); |
| 107 | + expect(NoteTemplateEngine("{{feedartwork}}", demoEpisode)).toBe( |
| 108 | + "https://example.com/feed.png", |
| 109 | + ); |
| 110 | + |
| 111 | + plugin.set({ settings: { feedNote: { path: "" }, savedFeeds: {} } } as never); |
| 112 | + expect(NoteTemplateEngine("{{feedartwork}}", demoEpisode)).toBe( |
| 113 | + "https://example.com/ep1.png", |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it("emits {{podcastlink}} as a path-qualified wikilink to the feed note", () => { |
| 118 | + plugin.set({ |
| 119 | + settings: { |
| 120 | + feedNote: { path: "PodNotes/Podcasts/{{podcast}}.md" }, |
| 121 | + savedFeeds: {}, |
| 122 | + }, |
| 123 | + } as never); |
| 124 | + expect(NoteTemplateEngine("{{podcastlink}}", demoEpisode)).toBe( |
| 125 | + "[[PodNotes/Podcasts/My Show|My Show]]", |
| 126 | + ); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +describe("FeedNoteTemplateEngine (#163)", () => { |
| 131 | + const feed: PodcastFeed = { |
| 132 | + title: "My Show: A Podcast", |
| 133 | + url: "https://example.com/feed.xml", |
| 134 | + artworkUrl: "https://example.com/art.png", |
| 135 | + link: "https://example.com", |
| 136 | + description: "<p>Great show</p>", |
| 137 | + author: "Jane Doe", |
| 138 | + }; |
| 139 | + |
| 140 | + it("maps {{url}}/{{artwork}} to the feed and exposes feed metadata", () => { |
| 141 | + expect(FeedNoteTemplateEngine("{{url}}", feed)).toBe("https://example.com"); |
| 142 | + expect(FeedNoteTemplateEngine("{{feedurl}}", feed)).toBe( |
| 143 | + "https://example.com/feed.xml", |
| 144 | + ); |
| 145 | + expect(FeedNoteTemplateEngine("{{artwork}}", feed)).toBe( |
| 146 | + "https://example.com/art.png", |
| 147 | + ); |
| 148 | + expect(FeedNoteTemplateEngine("{{feedartwork}}", feed)).toBe( |
| 149 | + "https://example.com/art.png", |
| 150 | + ); |
| 151 | + expect(FeedNoteTemplateEngine("{{author}}", feed)).toBe("Jane Doe"); |
| 152 | + // htmlToMarkdown is a passthrough in the test mock. |
| 153 | + expect(FeedNoteTemplateEngine("{{description}}", feed)).toBe("<p>Great show</p>"); |
| 154 | + }); |
| 155 | + |
| 156 | + it("exposes raw {{title}} and sanitized {{podcast}}/{{safetitle}}", () => { |
| 157 | + expect(FeedNoteTemplateEngine("{{title}}", feed)).toBe("My Show: A Podcast"); |
| 158 | + expect(FeedNoteTemplateEngine("{{podcast}}", feed)).toBe("My Show A Podcast"); |
| 159 | + expect(FeedNoteTemplateEngine("{{safetitle}}", feed)).toBe("My Show A Podcast"); |
| 160 | + }); |
| 161 | + |
| 162 | + it("leaves {{url}} empty when the feed has no website link", () => { |
| 163 | + expect(FeedNoteTemplateEngine("{{url}}", { ...feed, link: undefined })).toBe(""); |
| 164 | + }); |
| 165 | + |
| 166 | + it("strips quote/backslash from URL tags so quoted YAML frontmatter stays valid", () => { |
| 167 | + const malformed: PodcastFeed = { |
| 168 | + ...feed, |
| 169 | + link: 'https://example.com/a?x="b"\\c', |
| 170 | + artworkUrl: 'https://example.com/art".png', |
| 171 | + }; |
| 172 | + expect(FeedNoteTemplateEngine("{{url}}", malformed)).toBe( |
| 173 | + "https://example.com/a?x=bc", |
| 174 | + ); |
| 175 | + expect(FeedNoteTemplateEngine("{{artwork}}", malformed)).toBe( |
| 176 | + "https://example.com/art.png", |
| 177 | + ); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +describe("FeedFilePathTemplateEngine (#163)", () => { |
| 182 | + const feed: PodcastFeed = { |
| 183 | + title: "My Show: A Podcast", |
| 184 | + url: "u", |
| 185 | + artworkUrl: "", |
| 186 | + }; |
| 187 | + |
| 188 | + it("sanitizes the feed title for {{podcast}} and {{title}}", () => { |
| 189 | + expect( |
| 190 | + FeedFilePathTemplateEngine("PodNotes/Podcasts/{{podcast}}.md", feed), |
| 191 | + ).toBe("PodNotes/Podcasts/My Show A Podcast.md"); |
| 192 | + }); |
| 193 | + |
| 194 | + it("supports a whitespace-replacement argument", () => { |
| 195 | + expect(FeedFilePathTemplateEngine("{{podcast:-}}", feed)).toBe( |
| 196 | + "My-Show-A-Podcast", |
| 197 | + ); |
| 198 | + }); |
| 199 | +}); |
| 200 | + |
| 201 | +describe("getFeedNoteWikilink (#163)", () => { |
| 202 | + it("path-qualifies the link when the feed-note path has a folder", () => { |
| 203 | + plugin.set({ |
| 204 | + settings: { feedNote: { path: "PodNotes/Podcasts/{{podcast}}.md" } }, |
| 205 | + } as never); |
| 206 | + expect(getFeedNoteWikilink("My Show: A Podcast")).toBe( |
| 207 | + "[[PodNotes/Podcasts/My Show A Podcast|My Show A Podcast]]", |
| 208 | + ); |
| 209 | + }); |
| 210 | + |
| 211 | + it("uses a plain wikilink when the feed-note path has no folder", () => { |
| 212 | + plugin.set({ |
| 213 | + settings: { feedNote: { path: "{{podcast}}.md" } }, |
| 214 | + } as never); |
| 215 | + expect(getFeedNoteWikilink("My Show: A Podcast")).toBe( |
| 216 | + "[[My Show A Podcast]]", |
| 217 | + ); |
| 218 | + }); |
| 219 | + |
| 220 | + it("falls back to a plain sanitized link when no path is configured", () => { |
| 221 | + plugin.set({ settings: { feedNote: { path: "" } } } as never); |
| 222 | + expect(getFeedNoteWikilink("My Show: A Podcast")).toBe("[[My Show A Podcast]]"); |
| 223 | + }); |
| 224 | +}); |
0 commit comments