import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' const mockConfigStore = new Map() vi.mock('../../../src/main/services/ConfigService', () => ({ configGet: vi.fn((key: string) => mockConfigStore.get(key)), configSet: vi.fn((key: string, value: unknown) => { mockConfigStore.set(key, value) }), })) import { getOnlineLLMService, resetOnlineLLMServiceForTests, } from '../../../src/main/services/OnlineLLMService' import { secureWipeAudioChunks } from '@d3ro/core/utils/secure-memory' describe('클라우드 LLM PII 가명화·복원 배선 (W5-01)', () => { const originalFetch = globalThis.fetch beforeEach(() => { mockConfigStore.clear() mockConfigStore.set('authToken', 'jwt-test-token') mockConfigStore.set('onlineApiUrl', 'http://127.0.0.1:5000') mockConfigStore.set('llmModelId', 'd3ro-gpt4o-mini') resetOnlineLLMServiceForTests() }) afterEach(() => { globalThis.fetch = originalFetch resetOnlineLLMServiceForTests() }) it('processText_민감정보포함프롬프트_클라우드전송시토큰화하고응답에서원문을복원한다', async () => { let capturedBody: { prompt?: string } = {} globalThis.fetch = vi.fn(async (_input, init) => { capturedBody = JSON.parse(String(init?.body ?? '{}')) as { prompt?: string } const sentPrompt = capturedBody.prompt ?? '' return new Response( JSON.stringify({ text: `정리됨: ${sentPrompt}`, model: 'd3ro-gpt4o-mini', promptTokens: 10, completionTokens: 10, totalDurationMs: 25, cost: 0, }), { status: 200, headers: { 'Content-Type': 'application/json' } }, ) }) as typeof fetch const result = await getOnlineLLMService().processText( '연락처 010-1234-5678 및 주민번호 950101-1234567 확인 부탁드립니다', 'refine', ) expect(capturedBody.prompt).not.toContain('010-1234-5678') expect(capturedBody.prompt).not.toContain('950101-1234567') expect(capturedBody.prompt).toContain('[REDACTED_PHONE_NUMBER_') expect(capturedBody.prompt).toContain('[REDACTED_KR_RRN_') expect(result).toBe( '정리됨: 연락처 010-1234-5678 및 주민번호 950101-1234567 확인 부탁드립니다', ) }) it('chatStream_다중메시지민감정보_요청메시지에서PII를숨기고스트림응답에서복원한다', async () => { let capturedMessages: Array<{ role: string; content: string }> = [] globalThis.fetch = vi.fn(async (_input, init) => { const parsed = JSON.parse(String(init?.body ?? '{}')) as { messages: Array<{ role: string; content: string }> } capturedMessages = parsed.messages return new Response( JSON.stringify({ text: `확인된 이메일은 ${parsed.messages[0]?.content ?? ''} 입니다.`, model: 'd3ro-gpt4o-mini', promptTokens: 8, completionTokens: 8, totalDurationMs: 20, cost: 0, }), { status: 200, headers: { 'Content-Type': 'application/json' } }, ) }) as typeof fetch const chunks: string[] = [] for await (const chunk of getOnlineLLMService().chatStream([ { role: 'user', content: 'founder@d3ro.ai' }, ])) { chunks.push(chunk) } expect(capturedMessages[0]?.content).not.toContain('founder@d3ro.ai') expect(capturedMessages[0]?.content).toContain('[REDACTED_EMAIL_') expect(chunks.join('')).toBe('확인된 이메일은 founder@d3ro.ai 입니다.') }) it('secureWipeAudioChunks_오디오버퍼폐기시_내부바이트를전부0으로덮어쓰고배열을비운다', () => { const chunkA = Buffer.from([12, 34, 56, 78]) const chunkB = Buffer.from([90, 11, 22, 33]) const chunks = [chunkA, chunkB] secureWipeAudioChunks(chunks) expect(chunks).toHaveLength(0) expect(Array.from(chunkA.values())).toEqual([0, 0, 0, 0]) expect(Array.from(chunkB.values())).toEqual([0, 0, 0, 0]) }) })