// tests/main/ipc/llm-handlers.test.ts // LLM.PROCESS 핸들러가 지시문 프롬프트를 시스템 프롬프트 자리로 정규화하는지 잠근다. // 명령 화면 테스트 벤치가 이 경로를 타므로, 프로덕션(VoiceModeService)과 // 동일한 인자 배치·치환이 적용되어야 한다. import { describe, it, expect, beforeEach, vi } from 'vitest' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import type { LLMProcessParams } from '@d3ro/core/types' vi.mock('../../../src/main/services/LoggerService', () => ({ getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }) })) const handlers = vi.hoisted( () => new Map Promise>() ) vi.mock('electron', async (importOriginal) => { const actual = await importOriginal>() return { ...actual, ipcMain: { handle: vi.fn((channel: string, fn: (event: unknown, params: unknown) => Promise) => { handlers.set(channel, fn) }), on: vi.fn(), removeHandler: vi.fn() } } }) vi.mock('../../../src/main/services/ConfigService', () => ({ configGet: vi.fn(() => 'local'), configSet: vi.fn() })) const mockLocalLLM = vi.hoisted(() => ({ processText: vi.fn(), cancelGeneration: vi.fn(), on: vi.fn(), off: vi.fn() })) vi.mock('../../../src/main/services/LocalLLMService', () => ({ getLocalLLMService: () => mockLocalLLM })) vi.mock('../../../src/main/services/PremiumLLMService', () => ({ getPremiumLLMService: () => ({ cancelGeneration: vi.fn(), on: vi.fn(), off: vi.fn() }) })) vi.mock('../../../src/main/services/OnlineLLMService', () => ({ getOnlineLLMService: () => mockLocalLLM })) async function invokeProcess(params: LLMProcessParams): Promise { const handler = handlers.get(IPC_CHANNELS.LLM.PROCESS) if (!handler) throw new Error('LLM.PROCESS handler not registered') return handler({}, params) } beforeEach(async () => { vi.resetModules() vi.clearAllMocks() handlers.clear() mockLocalLLM.processText.mockResolvedValue('LLM 결과') const mod = await import('../../../src/main/ipc/llm-handlers') mod.registerLLMHandlers() }) describe('LLM.PROCESS — 지시문 인자 정규화', () => { it('custom 액션의 지시문을 시스템 프롬프트로, 입력 텍스트를 처리 대상으로 넘긴다', async () => { await invokeProcess({ text: '오늘 배포 일정을 정했습니다', action: 'custom', customPrompt: '다음 텍스트의 핵심 내용을 3줄 이내로 요약해주세요.' }) const [text, action, , systemPrompt] = mockLocalLLM.processText.mock.calls[0] expect(text).toBe('오늘 배포 일정을 정했습니다') expect(action).toBe('custom') expect(systemPrompt).toBe('다음 텍스트의 핵심 내용을 3줄 이내로 요약해주세요.') }) it('{{targetLanguage}}를 치환해 시스템 프롬프트로 넘긴다', async () => { await invokeProcess({ text: '안녕하세요', action: 'custom', customPrompt: '다음 텍스트를 {{targetLanguage}}로 번역해주세요.' }) const [text, , , systemPrompt] = mockLocalLLM.processText.mock.calls[0] expect(text).toBe('안녕하세요') expect(systemPrompt).toBe('다음 텍스트를 English로 번역해주세요.') expect(systemPrompt).not.toContain('{{') }) it('{{userPrompt}}를 입력 텍스트로 치환한다', async () => { await invokeProcess({ text: '피보나치 짜줘', action: 'custom', customPrompt: '{{userPrompt}}' }) const [text, , , systemPrompt] = mockLocalLLM.processText.mock.calls[0] expect(text).toBe('피보나치 짜줘') expect(systemPrompt).toBe('피보나치 짜줘') }) it('{{text}}를 쓰는 지시문은 치환 결과를 처리 대상 텍스트로 넘긴다', async () => { await invokeProcess({ text: '가 나 다', action: 'custom', customPrompt: '아래를 불릿으로 정리해줘:\n{{text}}' }) const [text, , , systemPrompt] = mockLocalLLM.processText.mock.calls[0] expect(text).toBe('아래를 불릿으로 정리해줘:\n가 나 다') expect(systemPrompt).toBeUndefined() }) it('명시적 targetLanguage를 그대로 쓴다', async () => { await invokeProcess({ text: '안녕', action: 'custom', customPrompt: '{{targetLanguage}}로 번역해줘.', targetLanguage: '프랑스어' }) const [, , targetLanguage, systemPrompt] = mockLocalLLM.processText.mock.calls[0] expect(targetLanguage).toBe('프랑스어') expect(systemPrompt).toBe('프랑스어로 번역해줘.') }) it('일반 액션은 지시문 정규화를 거치지 않고 그대로 전달한다', async () => { await invokeProcess({ text: '다듬어줘 이 문장', action: 'refine' }) const [text, action, targetLanguage, systemPrompt] = mockLocalLLM.processText.mock.calls[0] expect(text).toBe('다듬어줘 이 문장') expect(action).toBe('refine') expect(targetLanguage).toBeUndefined() expect(systemPrompt).toBeUndefined() }) it('실패를 성공으로 위장하지 않는다', async () => { mockLocalLLM.processText.mockRejectedValue(new Error('Ollama unreachable')) const result = (await invokeProcess({ text: '아무 말', action: 'refine' })) as { success: boolean error: { message: string } } expect(result.success).toBe(false) expect(result.error.message).toContain('Ollama unreachable') }) })