import { beforeEach, describe, expect, it, vi } from "vitest"; const { invokeMock, listenMock } = vi.hoisted(() => ({ invokeMock: vi.fn(), listenMock: vi.fn(), })); vi.mock("@tauri-apps/api/core", () => ({ invoke: invokeMock, })); vi.mock("@tauri-apps/api/event", () => ({ listen: listenMock, })); import { deleteSession, enumerateDisplays, enumerateWindows, exportMarkdown, exportMp4, generateShareLink, getSessionMetadata, getSessionSteps, getTerminalStatus, getTerminalWsUrl, greet, hideRecordingOverlay, isOverlayVisible, listSessions, loadSteps, logMarker, onMarkerHotkey, onToggleRecordingHotkey, renameSession, recordStepMarker, resizeRecordingOverlay, saveHotspots, saveSteps, showRecordingOverlay, startRecording, startTerminalRecording, stopRecording, stopTerminalRecording, loadSession, } from "./api"; describe("lib/api wrappers", () => { beforeEach(() => { invokeMock.mockReset(); listenMock.mockReset(); invokeMock.mockResolvedValue("ok"); }); it("calls invoke with command names that have no args", async () => { await enumerateDisplays(); expect(invokeMock).toHaveBeenLastCalledWith("enumerate_displays"); await enumerateWindows(); expect(invokeMock).toHaveBeenLastCalledWith("enumerate_windows"); await stopRecording(); expect(invokeMock).toHaveBeenLastCalledWith("stop_recording"); await stopTerminalRecording(); expect(invokeMock).toHaveBeenLastCalledWith("stop_terminal_recording"); await listSessions(); expect(invokeMock).toHaveBeenLastCalledWith("list_sessions"); await showRecordingOverlay(); expect(invokeMock).toHaveBeenLastCalledWith("show_recording_overlay"); await hideRecordingOverlay(); expect(invokeMock).toHaveBeenLastCalledWith("hide_recording_overlay"); await isOverlayVisible(); expect(invokeMock).toHaveBeenLastCalledWith("is_overlay_visible"); }); it("passes args for commands that require payloads", async () => { await startRecording("display-1", 1920, 1080); expect(invokeMock).toHaveBeenLastCalledWith("start_recording", { displayId: "display-1", width: 1920, height: 1080, }); await greet("Fahim"); expect(invokeMock).toHaveBeenLastCalledWith("greet", { name: "Fahim" }); await startTerminalRecording("session-1", 120, 40, 1234); expect(invokeMock).toHaveBeenLastCalledWith("start_terminal_recording", { sessionId: "session-1", width: 120, height: 40, recordingStartMs: 1234, }); await getTerminalWsUrl("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("get_terminal_ws_url", { sessionId: "session-1", }); await getTerminalStatus("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("get_terminal_status", { sessionId: "session-1", }); await deleteSession("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("delete_session", { sessionId: "session-1" }); await renameSession("session-1", "Renamed"); expect(invokeMock).toHaveBeenLastCalledWith("rename_session", { sessionId: "session-1", title: "Renamed", }); await getSessionMetadata("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("get_session_metadata", { sessionId: "session-1", }); await generateShareLink("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("generate_share_link", { sessionId: "session-1", }); await saveHotspots("session-1", []); expect(invokeMock).toHaveBeenLastCalledWith("save_hotspots", { sessionId: "session-1", hotspots: [], }); await exportMarkdown("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("export_markdown", { sessionId: "session-1" }); await exportMarkdown("session-1", "/tmp/exports"); expect(invokeMock).toHaveBeenLastCalledWith("export_markdown", { sessionId: "session-1", destPath: "/tmp/exports", }); await getSessionSteps("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("get_session_steps", { sessionId: "session-1", }); await loadSession("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("load_session", { sessionId: "session-1", }); await loadSteps("session-1"); expect(invokeMock).toHaveBeenLastCalledWith("load_steps", { sessionId: "session-1", }); await saveSteps("session-1", { version: 1, steps: [] }); expect(invokeMock).toHaveBeenLastCalledWith("save_steps", { sessionId: "session-1", stepsJson: { version: 1, steps: [] }, }); await recordStepMarker("checkpoint"); expect(invokeMock).toHaveBeenLastCalledWith("record_step_marker", { label: "checkpoint", }); await exportMp4("session-1", "/tmp/out.mp4", { include_chapter_cards: true }); expect(invokeMock).toHaveBeenLastCalledWith("export_mp4", { sessionId: "session-1", destPath: "/tmp/out.mp4", options: { include_chapter_cards: true }, }); await logMarker("session-1", "Manual Step", 900); expect(invokeMock).toHaveBeenLastCalledWith("log_marker", { sessionId: "session-1", label: "Manual Step", timestampMs: 900, }); await resizeRecordingOverlay(true); expect(invokeMock).toHaveBeenLastCalledWith("resize_recording_overlay", { recording: true, }); }); it("registers hotkey listener and triggers callback", async () => { const cb = vi.fn(); const unlisten = vi.fn(); let handler: (() => void) | undefined; listenMock.mockImplementation(async (_eventName: string, fn: () => void) => { handler = fn; return unlisten; }); const result = await onToggleRecordingHotkey(cb); expect(listenMock).toHaveBeenCalledWith("hotkey-toggle-recording", expect.any(Function)); handler?.(); expect(cb).toHaveBeenCalledTimes(1); expect(result).toBe(unlisten); }); it("registers marker hotkey listener and triggers callback", async () => { const cb = vi.fn(); const unlisten = vi.fn(); let handler: (() => void) | undefined; listenMock.mockImplementation(async (_eventName: string, fn: () => void) => { handler = fn; return unlisten; }); const result = await onMarkerHotkey(cb); expect(listenMock).toHaveBeenCalledWith("hotkey-mark-step", expect.any(Function)); handler?.(); expect(cb).toHaveBeenCalledTimes(1); expect(result).toBe(unlisten); }); });