d3ro-voice/apps/desktop/tests/main/services/meeting-diarization.test.ts

133 lines
4.5 KiB
TypeScript

// tests/main/services/meeting-diarization.test.ts
// GAP-MTG-02: MeetingModeService.diarizeSession pyannote HTTP 경로 및 LLM 폴백 검증
import fs from 'fs'
import path from 'path'
import { eq } from 'drizzle-orm'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { app } from 'electron'
import { useRedHarness } from '../../red/harness'
const llmCapture = vi.hoisted(() => ({
calls: [] as Array<{ text: string; systemPrompt?: string }>,
}))
vi.mock('../../../src/main/services/LocalLLMService', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../../src/main/services/LocalLLMService')>()
return {
...actual,
getLocalLLMService: () => ({
isAvailable: () => true,
generate: async (text: string, options?: { systemPrompt?: string }) => {
llmCapture.calls.push({ text, systemPrompt: options?.systemPrompt })
return {
text: '[00:02] [화자 1] 안녕하세요\n[00:08] [화자 2] 반갑습니다',
model: 'gemma-test',
}
},
cancelGeneration: vi.fn(),
}),
}
})
useRedHarness()
import { configSet } from '../../../src/main/services/ConfigService'
import { getMeetingModeService } from '../../../src/main/services/MeetingModeService'
import { getDatabase } from '../../../src/main/db'
import { meetingSessions } from '../../../src/main/db/schema'
function insertCompletedSession(sessionId: string, rawTranscript: string): string {
const startedAt = 1_700_000_000_000
getDatabase()
.insert(meetingSessions)
.values({
id: sessionId,
title: '화자 구분 테스트 회의',
status: 'completed',
startedAt,
endedAt: startedAt + 15_000,
durationMs: 15_000,
rawTranscript,
createdAt: startedAt,
updatedAt: startedAt,
})
.run()
const audioDir = path.join(app.getPath('userData'), 'meeting-audio')
fs.mkdirSync(audioDir, { recursive: true })
const wavPath = path.join(audioDir, `${sessionId}.wav`)
fs.writeFileSync(wavPath, Buffer.from('RIFF0000WAVEfmt '))
return wavPath
}
describe('MeetingModeService.diarizeSession (GAP-MTG-02)', () => {
beforeEach(() => {
llmCapture.calls = []
configSet('llmBackend', 'local')
})
afterEach(() => {
vi.restoreAllMocks()
})
it('화자분리_사이드카diarize성공시_pyannote구간을1기반화자라벨로저장한다', async () => {
const sessionId = '11111111-1111-4111-8111-111111111111'
insertCompletedSession(sessionId, '[00:02] 안녕하세요\n[00:08] 반갑습니다')
configSet('hfToken', 'hf_valid_token')
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(
JSON.stringify({
status: 'ok',
num_speakers: 2,
segments: [
{ speaker: 'SPEAKER_00', start: 0.0, end: 5.0 },
{ speaker: 'SPEAKER_01', start: 5.5, end: 12.0 },
],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
),
)
await getMeetingModeService().diarizeSession(sessionId, 2)
expect(fetchSpy).toHaveBeenCalledTimes(1)
expect(String(fetchSpy.mock.calls[0]?.[0])).toContain('/diarize')
expect(llmCapture.calls).toHaveLength(0)
const updated = getDatabase()
.select()
.from(meetingSessions)
.where(eq(meetingSessions.id, sessionId))
.get()
expect(updated?.editedTranscript).toBe('[00:02] [화자 1] 안녕하세요\n[00:08] [화자 2] 반갑습니다')
})
it('화자분리_사이드카pyannote미설치503응답시_LLM화자추정으로폴백한다', async () => {
const sessionId = '22222222-2222-4222-8222-222222222222'
insertCompletedSession(sessionId, '[00:02] 안녕하세요\n[00:08] 반갑습니다')
configSet('hfToken', 'hf_valid_token')
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(
JSON.stringify({
status: 'unavailable',
code: 'pyannote_not_installed',
message: 'pyannote.audio 패키지가 설치되어 있지 않습니다',
}),
{ status: 503, headers: { 'Content-Type': 'application/json' } },
),
)
await getMeetingModeService().diarizeSession(sessionId, 2)
expect(llmCapture.calls).toHaveLength(1)
const updated = getDatabase()
.select()
.from(meetingSessions)
.where(eq(meetingSessions.id, sessionId))
.get()
expect(updated?.editedTranscript).toBe('[00:02] [화자 1] 안녕하세요\n[00:08] [화자 2] 반갑습니다')
})
})