|
5 | 5 | detectAudioFileExtension, |
6 | 6 | downloadEpisode, |
7 | 7 | getEpisodeAudioBuffer, |
| 8 | + safeDownloadBasename, |
8 | 9 | } from "./downloadEpisode"; |
9 | 10 | import { downloadedEpisodes } from "./store"; |
10 | 11 | import type { Episode } from "./types/Episode"; |
@@ -128,6 +129,52 @@ describe("downloadEpisode (API path)", () => { |
128 | 129 | expect(requestUrlMock).not.toHaveBeenCalled(); |
129 | 130 | expect(createBinary).not.toHaveBeenCalled(); |
130 | 131 | }); |
| 132 | + |
| 133 | + it("never writes a '.<ext>' dotfile when the path template is empty (#183)", async () => { |
| 134 | + const { createBinary } = setupVault(); |
| 135 | + const buffer = bytes(0x49, 0x44, 0x33, 0x01); |
| 136 | + requestUrlMock.mockResolvedValue({ |
| 137 | + status: 200, |
| 138 | + headers: { "content-type": "audio/mpeg" }, |
| 139 | + arrayBuffer: buffer, |
| 140 | + } as unknown as Awaited<ReturnType<typeof requestUrl>>); |
| 141 | + |
| 142 | + const path = await downloadEpisode(makeEpisode(), ""); |
| 143 | + |
| 144 | + // Falls back to a per-episode name instead of the un-indexable ".mp3". |
| 145 | + expect(path).toBe("My Title.mp3"); |
| 146 | + expect(createBinary).toHaveBeenCalledWith("My Title.mp3", buffer); |
| 147 | + const [writtenPath] = createBinary.mock.calls[0]; |
| 148 | + expect(writtenPath).not.toBe(".mp3"); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +describe("safeDownloadBasename (#183)", () => { |
| 153 | + it("falls back to the title when the template resolves to an empty name", () => { |
| 154 | + expect(safeDownloadBasename("", makeEpisode())).toBe("My Title"); |
| 155 | + }); |
| 156 | + |
| 157 | + it("replaces an empty trailing segment but keeps the folder", () => { |
| 158 | + expect(safeDownloadBasename("Downloads/", makeEpisode())).toBe( |
| 159 | + "Downloads/My Title", |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + it("drops a stray leading slash instead of writing an absolute path", () => { |
| 164 | + expect(safeDownloadBasename("/{{title}}", makeEpisode())).toBe("My Title"); |
| 165 | + }); |
| 166 | + |
| 167 | + it("falls back to 'episode' when the title is empty/all-illegal", () => { |
| 168 | + expect(safeDownloadBasename("", makeEpisode({ title: "??" }))).toBe( |
| 169 | + "episode", |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it("leaves a valid per-episode template untouched", () => { |
| 174 | + expect( |
| 175 | + safeDownloadBasename("podcast/{{podcast}}/{{title}}", makeEpisode()), |
| 176 | + ).toBe("podcast/Pod/My Title"); |
| 177 | + }); |
131 | 178 | }); |
132 | 179 |
|
133 | 180 | describe("getEpisodeAudioBuffer (issue #107)", () => { |
|
0 commit comments