hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; |
| 2 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | import { PodcastPanel } from "@/components/notes/podcast-panel"; |
| 4 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 5 | |
| 6 | const podcastApi = vi.hoisted(() => ({ |
| 7 | postPodcastJob: vi.fn(), |
| 8 | getPodcastJobById: vi.fn(), |
| 9 | getPodcastJobResultById: vi.fn(), |
| 10 | postPodcastJobCancel: vi.fn(), |
| 11 | listPodcastJobs: vi.fn(async () => ({ items: [], total: 0, limit: 10, offset: 0 })), |
| 12 | podcastAudioUrl: vi.fn((jobId: string) => `/api/podcast/jobs/${encodeURIComponent(jobId)}/audio`), |
| 13 | })); |
| 14 | |
| 15 | vi.mock("@/api/podcast", () => podcastApi); |
| 16 | |
| 17 | // The panel asks what the AI tier allows as soon as it mounts; without this the |
| 18 | // real client reaches for a relative URL node's fetch cannot parse. |
| 19 | const aiApi = vi.hoisted(() => ({ |
| 20 | getAiCapabilities: vi.fn(async () => ({ |
| 21 | enabled: true, |
| 22 | protocol: "hezarfen.ai.v1", |
| 23 | capabilities: [{ capability: "podcast.submit", workers: 1, inflight: 0 }], |
| 24 | })), |
| 25 | })); |
| 26 | |
| 27 | vi.mock("@/api/ai", () => aiApi); |
| 28 | |
| 29 | describe("PodcastPanel", () => { |
| 30 | afterEach(() => { |
| 31 | cleanup(); |
| 32 | sessionStorage.clear(); |
| 33 | vi.clearAllMocks(); |
| 34 | }); |
| 35 | |
| 36 | it("submits the selected lesson note and narration format", async () => { |
| 37 | podcastApi.postPodcastJob.mockResolvedValue({ job_id: "j1", state: "queued", eta_secs: 30 }); |
| 38 | |
| 39 | render(() => ( |
| 40 | <PreferencesProvider> |
| 41 | <PodcastPanel noteId="note-7" /> |
| 42 | </PreferencesProvider> |
| 43 | )); |
| 44 | |
| 45 | fireEvent.change(await screen.findByLabelText("Narration style"), { target: { value: "tek_ogretici" } }); |
| 46 | fireEvent.click(screen.getByRole("button", { name: "Create podcast" })); |
| 47 | |
| 48 | await waitFor(() => { |
| 49 | expect(podcastApi.postPodcastJob).toHaveBeenCalledWith({ source_id: "note-7", format: "tek_ogretici" }); |
| 50 | }); |
| 51 | expect(sessionStorage.getItem("hezarfen.podcast.note-7")).toBe("j1"); |
| 52 | expect(screen.getByText("Waiting in queue")).toBeTruthy(); |
| 53 | }); |
| 54 | |
| 55 | it("points the audio element at the job's audio door, not the artifact path", async () => { |
| 56 | // A finished job restored from sessionStorage: the panel must address the |
| 57 | // stream by `job_id`. Handing it `audio_id` (the output-root path) composes |
| 58 | // a URL the backend has no route for, and the player dies silently. |
| 59 | sessionStorage.setItem("hezarfen.podcast.note-7", "pj1"); |
| 60 | podcastApi.getPodcastJobById.mockResolvedValue({ job_id: "pj1", state: "done", stage: "done", progress: 1 }); |
| 61 | podcastApi.getPodcastJobResultById.mockResolvedValue({ |
| 62 | job_id: "pj1", |
| 63 | audio_id: "ses/duz_okuma/pj1/episode.mp3", |
| 64 | duration_secs: 42, |
| 65 | format: "duz_okuma", |
| 66 | }); |
| 67 | |
| 68 | render(() => ( |
| 69 | <PreferencesProvider> |
| 70 | <PodcastPanel noteId="note-7" /> |
| 71 | </PreferencesProvider> |
| 72 | )); |
| 73 | |
| 74 | await waitFor(() => { |
| 75 | expect(screen.getByText("Your episode is ready.")).toBeTruthy(); |
| 76 | }); |
| 77 | expect(podcastApi.podcastAudioUrl).toHaveBeenCalledWith("pj1"); |
| 78 | expect(document.querySelector("audio")?.getAttribute("src")).toBe("/api/podcast/jobs/pj1/audio"); |
| 79 | }); |
| 80 | |
| 81 | it("downloads the finished episode under the note's own title", async () => { |
| 82 | sessionStorage.setItem("hezarfen.podcast.note-7", "pj1"); |
| 83 | podcastApi.getPodcastJobById.mockResolvedValue({ job_id: "pj1", state: "done", stage: "done", progress: 1 }); |
| 84 | podcastApi.getPodcastJobResultById.mockResolvedValue({ |
| 85 | job_id: "pj1", |
| 86 | audio_id: "ses/duz_okuma/pj1/episode.mp3", |
| 87 | duration_secs: 42, |
| 88 | format: "duz_okuma", |
| 89 | }); |
| 90 | |
| 91 | render(() => ( |
| 92 | <PreferencesProvider> |
| 93 | <PodcastPanel noteId="note-7" noteTitle="Hücre ve Canlıların Ortak Özellikleri" /> |
| 94 | </PreferencesProvider> |
| 95 | )); |
| 96 | |
| 97 | await waitFor(() => { |
| 98 | expect(screen.getByText("Your episode is ready.")).toBeTruthy(); |
| 99 | }); |
| 100 | const link = screen.getByRole("link", { name: "Download episode" }) as HTMLAnchorElement; |
| 101 | expect(link.getAttribute("href")).toBe("/api/podcast/jobs/pj1/audio"); |
| 102 | expect(link.getAttribute("download")).toBe("Hücre ve Canlıların Ortak Özellikleri.mp3"); |
| 103 | }); |
| 104 | |
| 105 | it("shows no download control while the episode is still being made", async () => { |
| 106 | sessionStorage.setItem("hezarfen.podcast.note-7", "pj2"); |
| 107 | podcastApi.getPodcastJobById.mockResolvedValue({ job_id: "pj2", state: "running", stage: "script", progress: 0.4 }); |
| 108 | |
| 109 | render(() => ( |
| 110 | <PreferencesProvider> |
| 111 | <PodcastPanel noteId="note-7" noteTitle="Hücre" /> |
| 112 | </PreferencesProvider> |
| 113 | )); |
| 114 | |
| 115 | await waitFor(() => { |
| 116 | expect(screen.getByText("Creating audio")).toBeTruthy(); |
| 117 | }); |
| 118 | expect(screen.queryByRole("link", { name: "Download episode" })).toBeNull(); |
| 119 | }); |
| 120 | }); |