|
| 1 | +import { TFile } from "obsidian"; |
1 | 2 | import { get } from "svelte/store"; |
2 | | -import { beforeEach, describe, expect, test } from "vitest"; |
| 3 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
3 | 4 |
|
4 | 5 | import { API } from "./API"; |
5 | 6 | import { |
@@ -157,3 +158,86 @@ describe("API playback rate controls", () => { |
157 | 158 | expect(api.playbackRate).toBe(1.8); |
158 | 159 | }); |
159 | 160 | }); |
| 161 | + |
| 162 | +describe("API transcript access", () => { |
| 163 | + function setTranscriptVault(files: Record<string, string>) { |
| 164 | + const createTFile = (path: string): TFile => |
| 165 | + Object.assign(new TFile(), { path }); |
| 166 | + const getAbstractFileByPath = vi.fn((path: string) => |
| 167 | + Object.prototype.hasOwnProperty.call(files, path) |
| 168 | + ? createTFile(path) |
| 169 | + : null, |
| 170 | + ); |
| 171 | + const read = vi.fn(async (file: TFile) => files[file.path] ?? ""); |
| 172 | + |
| 173 | + plugin.set({ |
| 174 | + settings: { |
| 175 | + defaultPlaybackRate: 1.8, |
| 176 | + transcript: { |
| 177 | + path: "Transcripts/{{podcast}}/{{title}}.md", |
| 178 | + }, |
| 179 | + }, |
| 180 | + app: { |
| 181 | + vault: { |
| 182 | + getAbstractFileByPath, |
| 183 | + read, |
| 184 | + }, |
| 185 | + }, |
| 186 | + } as never); |
| 187 | + |
| 188 | + return { getAbstractFileByPath, read }; |
| 189 | + } |
| 190 | + |
| 191 | + test("returns null when no episode is loaded", async () => { |
| 192 | + setTranscriptVault({}); |
| 193 | + const api = new API(); |
| 194 | + |
| 195 | + await expect(api.getTranscript()).resolves.toBeNull(); |
| 196 | + }); |
| 197 | + |
| 198 | + test("returns null when the current episode has no generated transcript file", async () => { |
| 199 | + currentEpisode.set(feedEpisode); |
| 200 | + const { getAbstractFileByPath, read } = setTranscriptVault({}); |
| 201 | + const api = new API(); |
| 202 | + |
| 203 | + await expect(api.getTranscript()).resolves.toBeNull(); |
| 204 | + |
| 205 | + expect(getAbstractFileByPath).toHaveBeenCalledWith( |
| 206 | + "Transcripts/Feed Podcast/Feed Episode.md", |
| 207 | + ); |
| 208 | + expect(read).not.toHaveBeenCalled(); |
| 209 | + }); |
| 210 | + |
| 211 | + test("reads the generated transcript note for the current episode", async () => { |
| 212 | + currentEpisode.set(feedEpisode); |
| 213 | + const transcript = "# Feed Episode\n\nTranscript body for AI macros."; |
| 214 | + const { read } = setTranscriptVault({ |
| 215 | + "Transcripts/Feed Podcast/Feed Episode.md": transcript, |
| 216 | + }); |
| 217 | + const api = new API(); |
| 218 | + |
| 219 | + await expect(api.getTranscript()).resolves.toBe(transcript); |
| 220 | + await expect(api.transcript).resolves.toBe(transcript); |
| 221 | + |
| 222 | + expect(read).toHaveBeenCalledWith( |
| 223 | + expect.objectContaining({ |
| 224 | + path: "Transcripts/Feed Podcast/Feed Episode.md", |
| 225 | + }), |
| 226 | + ); |
| 227 | + }); |
| 228 | + |
| 229 | + test("reads the generated transcript note for an explicit episode", async () => { |
| 230 | + currentEpisode.set(feedEpisode); |
| 231 | + const transcript = "Local episode transcript"; |
| 232 | + const { getAbstractFileByPath } = setTranscriptVault({ |
| 233 | + "Transcripts/local file/Local Episode.md": transcript, |
| 234 | + }); |
| 235 | + const api = new API(); |
| 236 | + |
| 237 | + await expect(api.getTranscript(localEpisode)).resolves.toBe(transcript); |
| 238 | + |
| 239 | + expect(getAbstractFileByPath).toHaveBeenCalledWith( |
| 240 | + "Transcripts/local file/Local Episode.md", |
| 241 | + ); |
| 242 | + }); |
| 243 | +}); |
0 commit comments