forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

studio-output-library.test.tsx92 lines, 3.1 KiBHistoryRawDownload
1import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3import type { PodcastJobSummary, RagOutput } from "@/api/client";
4import { StudioOutputLibrary } from "@/components/ai/studio-output-library";
5import { PreferencesProvider } from "@/stores/preferences-context";
6
7const podcastApi = vi.hoisted(() => ({ listPodcastJobs: vi.fn() }));
8const courseNotesApi = vi.hoisted(() => ({ getCourseNoteRag: vi.fn() }));
9
10vi.mock("@/api/podcast", () => podcastApi);
11vi.mock("@/api/course-notes", () => courseNotesApi);
12
13const notes = [
14 { id: "note-1", title: "Hücre", courseTitle: "Biyoloji" },
15 { id: "note-2", title: "Kuvvet", courseTitle: "Fizik" },
16 { id: "note-3", title: "Polinomlar", courseTitle: "Matematik" },
17];
18
19function page<T>(items: T[]) {
20 return { items, total: items.length, limit: 100, offset: 0 };
21}
22
23function podcast(over: Partial<PodcastJobSummary>): PodcastJobSummary {
24 return {
25 job_id: "job-1",
26 state: "done",
27 format: "duz_okuma",
28 source_id: "note-1",
29 source_title: "Hücre",
30 created_at: Date.UTC(2026, 0, 2),
31 finished_at: Date.UTC(2026, 0, 2),
32 duration_secs: 120,
33 error_code: null,
34 ...over,
35 };
36}
37
38function summary(noteId: string): RagOutput {
39 return {
40 id: `rag-${noteId}`,
41 course_note: noteId,
42 course: "course-1",
43 sources: [],
44 payload: { summary: "Hazır" },
45 generated_at: Date.UTC(2026, 0, 3),
46 };
47}
48
49describe("StudioOutputLibrary", () => {
50 beforeEach(() => {
51 localStorage.setItem("hezarfen.locale", "tr");
52 podcastApi.listPodcastJobs.mockResolvedValue(page([
53 podcast({ job_id: "job-1" }),
54 podcast({ job_id: "job-2" }),
55 podcast({ job_id: "job-failed", source_id: "note-3", state: "failed" }),
56 ]));
57 courseNotesApi.getCourseNoteRag.mockImplementation(async (noteId: string) =>
58 page(noteId === "note-2" ? [summary(noteId)] : []),
59 );
60 });
61
62 afterEach(() => {
63 cleanup();
64 localStorage.clear();
65 vi.clearAllMocks();
66 });
67
68 it("shows only notes with finished outputs and selects one directly", async () => {
69 const onSelect = vi.fn();
70
71 render(() => (
72 <PreferencesProvider>
73 <StudioOutputLibrary notes={notes} selectedId="note-1" onSelect={onSelect} />
74 </PreferencesProvider>
75 ));
76
77 await waitFor(() => expect(screen.getByText("Hücre")).toBeTruthy());
78 expect(screen.getByText("Kuvvet")).toBeTruthy();
79 expect(screen.queryByText("Polinomlar")).toBeNull();
80 expect(screen.getByText("2 podcast")).toBeTruthy();
81 expect(screen.getByText("Yapay zekâ özeti")).toBeTruthy();
82 expect(screen.getByText("2 not")).toBeTruthy();
83
84 const selected = screen.getByRole("button", { name: /Hücre/ });
85 expect(selected.getAttribute("aria-current")).toBe("true");
86
87 fireEvent.click(screen.getByRole("button", { name: /Kuvvet/ }));
88 expect(onSelect).toHaveBeenCalledWith("note-2");
89 expect(podcastApi.listPodcastJobs).toHaveBeenCalledWith({ limit: 100 });
90 expect(courseNotesApi.getCourseNoteRag).toHaveBeenCalledTimes(3);
91 });
92});