From ea383783ec28d452b1cdb40f47699aa98658be0c Mon Sep 17 00:00:00 2001 From: Amit Haridas Date: Fri, 5 Jun 2026 09:25:02 +0530 Subject: [PATCH] test(renderer): add failing test for ipc wrapper + IpcResult types --- src/renderer/types/ipc.ts | 62 ++++++++++++++++++++++++++++++++++++++ tests/unit/lib/ipc.test.ts | 41 +++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/renderer/types/ipc.ts create mode 100644 tests/unit/lib/ipc.test.ts diff --git a/src/renderer/types/ipc.ts b/src/renderer/types/ipc.ts new file mode 100644 index 0000000..42f94da --- /dev/null +++ b/src/renderer/types/ipc.ts @@ -0,0 +1,62 @@ +export type IpcResult = + | { ok: true; data: T } + | { ok: false; error: { code: string; message: string } }; + +export interface FileEntry { + name: string; + path: string; + isDirectory: boolean; + size?: number; + modifiedAt?: string; +} + +export interface FileResult { + path: string; + content: string; +} + +export interface PdfOptions { + inputPath: string; + outputPath: string; + format?: 'letter' | 'a4' | 'legal'; + margins?: { top: number; right: number; bottom: number; left: number }; + toc?: boolean; + embedFonts?: boolean; +} + +export interface DocxOptions { + inputPath: string; + outputPath: string; + template?: string; + referenceDoc?: string; +} + +export interface HtmlOptions { + inputPath: string; + outputPath: string; + standalone?: boolean; + highlightStyle?: string; +} + +export interface ExportResult { + outputPath: string; + bytes: number; + durationMs: number; +} + +export interface BatchItem { + inputPath: string; + outputPath: string; +} + +export interface BatchOptions { + format: 'pdf' | 'docx' | 'html' | 'png'; + concurrency?: number; +} + +export interface BatchResult { + total: number; + succeeded: number; + failed: number; + results: Array<{ item: BatchItem; ok: boolean; error?: string }>; +} \ No newline at end of file diff --git a/tests/unit/lib/ipc.test.ts b/tests/unit/lib/ipc.test.ts new file mode 100644 index 0000000..7f418e2 --- /dev/null +++ b/tests/unit/lib/ipc.test.ts @@ -0,0 +1,41 @@ +import { describe, it, expect, beforeEach, vi } from 'vitest'; +import { ipc } from '@/lib/ipc'; +import type { FileResult } from '@/types/ipc'; + +describe('ipc wrapper', () => { + beforeEach(() => { + window.electronAPI = { + file: { + read: vi.fn().mockResolvedValue('# hello'), + write: vi.fn().mockResolvedValue(undefined), + list: vi.fn().mockResolvedValue([]), + }, + }; + }); + + it('file.read returns ok result on success', async () => { + const result = await ipc.file.read('/foo.md'); + expect(result.ok).toBe(true); + if (result.ok) { + expect(result.data).toBe('# hello'); + } + }); + + it('file.read returns err result when channel throws', async () => { + window.electronAPI.file.read = vi.fn().mockRejectedValue(new Error('ENOENT')); + const result = await ipc.file.read('/missing.md'); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error.message).toBe('ENOENT'); + } + }); + + it('file.read returns err result when channel missing', async () => { + delete (window.electronAPI.file as any).read; + const result = await ipc.file.read('/foo.md'); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error.code).toBe('CHANNEL_MISSING'); + } + }); +}); \ No newline at end of file