// tests/main/services/transcript-glossary.test.ts // 학습한 어휘(사용자 사전 + 개인 문구)가 전사 교열에 실제로 쓰이는지 검증한다. // 예전에는 자막이 고정 힌트로만 인식되고, 전사된 줄은 용어 사전 없이 다듬어져 // 사용자가 키보드로 배운 표기가 회의록에 반영되지 않았다. import { describe, expect, it } from 'vitest' import { useRedHarness } from '../../red/harness' import { getTranscriptGlossaryService, TranscriptGlossaryService, } from '../../../src/main/services/TranscriptGlossaryService' import { getDictionaryService } from '../../../src/main/services/DictionaryService' import { getPersonalGraphService } from '../../../src/main/services/PersonalGraphService' import { MeetingRecordingSession } from '../../../src/main/services/meeting/MeetingRecordingSession' import type { CaptionSegment } from '@d3ro/core/types' useRedHarness() /** 같은 문장을 n 번 친 것으로 개인 그래프에 남긴다(count 누적). */ function learnPhrase(text: string, times: number): void { for (let i = 0; i < times; i += 1) { getPersonalGraphService().indexText(text, { source: 'typed', appName: 'notepad.exe' }) } } describe('학습한 어휘가 전사 교정에 쓰인다', () => { it('키보드로 배운 어절과 한 글자 다른 전사 어절을 되돌린다', () => { learnPhrase('회의록 정리해줘', 5) const result = getTranscriptGlossaryService().correct('회이록 정리해줘') expect(result.text).toBe('회의록 정리해줘') expect(result.corrections).toEqual([{ from: '회이록', to: '회의록', source: 'learned' }]) }) it('사용자 사전의 발음 표기를 정본 표기로 바꾼다', () => { getDictionaryService().add({ word: 'D3RO', pronunciation: '디에로' }) const result = getTranscriptGlossaryService().correct('디에로 보이스 좋네요') expect(result.text).toBe('D3RO 보이스 좋네요') }) it('STT 힌트에 사전 단어와 학습 어절이 함께 들어간다', () => { getDictionaryService().add({ word: 'Whisper' }) learnPhrase('녹음 파일 올려줘', 4) const hint = getTranscriptGlossaryService().sttHint('다음은 한국어 대화입니다.') expect(hint.startsWith('다음은 한국어 대화입니다.')).toBe(true) expect(hint).toContain('Whisper') expect(hint).toContain('녹음') }) it('한 번만 쓴 어절은 학습 어휘로 쓰지 않는다', () => { learnPhrase('회의록 정리해줘', 1) const result = getTranscriptGlossaryService().correct('회이록 정리해줘') expect(result.text).toBe('회이록 정리해줘') }) it('사전에 단어를 추가하면 캐시를 기다리지 않고 바로 반영된다', () => { expect(getTranscriptGlossaryService().sttHint('')).not.toContain('Supabase') getDictionaryService().add({ word: 'Supabase', pronunciation: '수파베이스' }) expect(getTranscriptGlossaryService().sttHint('')).toContain('Supabase') }) it('LLM 교열 프롬프트에 사용자 용어 블록을 넣는다', () => { getDictionaryService().add({ word: 'D3RO', pronunciation: '디에로' }) learnPhrase('오늘 회의는 세 시에 시작합니다', 3) const section = getTranscriptGlossaryService().promptSection() expect(section).toContain('[사용자 용어') expect(section).toContain('D3RO') expect(section).toContain('오늘 회의는 세 시에 시작합니다') }) }) describe('글로서리 캐시', () => { it('캐시가 살아 있으면 소스를 다시 읽지 않고, 무효화하면 다시 읽는다', () => { let dictionaryReads = 0 let now = 1_000 const service = new TranscriptGlossaryService( { listDictionary: () => { dictionaryReads += 1 return [{ word: 'D3RO', pronunciation: null }] }, listPhrases: () => [{ text: '회의록 정리해줘', count: 5 }], now: () => now, }, 1_000, ) service.get() service.get() expect(dictionaryReads).toBe(1) now += 1_500 service.get() expect(dictionaryReads).toBe(2) service.invalidate() service.get() expect(dictionaryReads).toBe(3) }) it('소스가 실패해도 빈 어휘로 진행한다', () => { const service = new TranscriptGlossaryService({ listDictionary: () => { throw new Error('db down') }, listPhrases: () => [{ text: '회의록', count: 5 }], }) expect(service.get().promptHint).toBe('') expect(service.correct('회이록').text).toBe('회이록') }) }) describe('회의 세션이 다듬어진 줄을 받는다', () => { it('자막이 알린 수정 문장이 회의 세그먼트에 반영된다', () => { const segmentListeners = new Set<(segment: CaptionSegment) => void>() const updateListeners = new Set<(update: { id: string; text: string }) => void>() const updates: Array<{ id: string; text: string }> = [] const session = new MeetingRecordingSession('session-1', 0, { caption: { onSegment: (listener) => { segmentListeners.add(listener) return () => segmentListeners.delete(listener) }, onSegmentUpdated: (listener) => { updateListeners.add(listener) return () => updateListeners.delete(listener) }, stop: async () => null, }, audio: { onChunk: () => () => undefined }, onSegmentUpdated: (update) => updates.push(update), now: () => 1_000, }) session.attachCaption() const segment: CaptionSegment = { id: 'seg-1', text: '회이록 정리해줘', timestamp: 500, isFinal: true } for (const listener of segmentListeners) listener(segment) for (const listener of updateListeners) listener({ id: 'seg-1', text: '회의록 정리해줘' }) expect(session.segments[0].text).toBe('회의록 정리해줘') expect(updates).toEqual([{ id: 'seg-1', text: '회의록 정리해줘' }]) }) })