import { beforeEach, describe, expect, it, vi } from 'vitest' import { ErrorCode } from '@d3ro/core/errors' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' const handlers = vi.hoisted( () => new Map unknown>() ) const search = vi.hoisted(() => vi.fn()) vi.mock('electron', () => ({ ipcMain: { handle: vi.fn((channel: string, handler: (event: unknown, params: unknown) => unknown) => { handlers.set(channel, handler) }), }, })) vi.mock('../../../src/main/services/UnifiedSearchService', async (importOriginal) => { const actual = await importOriginal() return { ...actual, getUnifiedSearchService: () => ({ search }), } }) beforeEach(async () => { vi.clearAllMocks() handlers.clear() const mod = await import('../../../src/main/ipc/unified-search-handlers') mod.registerUnifiedSearchHandlers() }) describe('UNIFIED_SEARCH.QUERY', () => { it.each([ null, {}, { query: 42 }, { query: '계획', limit: Number.NaN }, { query: '계획', limit: Number.NEGATIVE_INFINITY }, ])('returns ConfigInvalidValue for malformed renderer input: %o', async (params) => { const handler = handlers.get(IPC_CHANNELS.UNIFIED_SEARCH.QUERY) if (!handler) throw new Error('unified search handler was not registered') const result = await handler({}, params) expect(result).toMatchObject({ success: false, error: { code: ErrorCode.ConfigInvalidValue }, }) expect(search).not.toHaveBeenCalled() }) it('passes validated input to the service and returns IPC success', async () => { const handler = handlers.get(IPC_CHANNELS.UNIFIED_SEARCH.QUERY) if (!handler) throw new Error('unified search handler was not registered') search.mockReturnValue({ query: '계획', results: [], total: 0 }) const result = await handler({}, { query: '계획', limit: 10 }) expect(search).toHaveBeenCalledWith({ query: '계획', limit: 10 }) expect(result).toEqual({ success: true, data: { query: '계획', results: [], total: 0 }, }) }) })