d3ro-voice/apps/desktop/tests/main/services/VoiceConversationService.test.ts
Yun Chan d63854db67
Some checks failed
ci / 정본·보안·린트·타입·테스트 (push) Successful in 56s
ci / .NET API 서버 테스트 (push) Successful in 10s
deploy-site / deploy (push) Successful in 38s
ci / 모바일 린트·타입·Jest (push) Successful in 44s
ci / 워크스페이스 빌드 검증 (push) Successful in 54s
ci / Supabase Edge Functions + Cloudflare Worker (push) Successful in 22s
portable-unsigned / portable-windows (push) Failing after 35s
release / release-windows (push) Failing after 13s
fix(desktop): stop pinning local models in memory and cap audio buffers
The PC-wide slowdown was not a leak but model residency: boot warmup loaded
gemma4:e4b (~11.6 GB RAM incl. 5.9 GB pinned host buffer, no mmap on Windows
CUDA) and kept it for 30m, next to the large-v3 sidecar. Measured on the HEAD
build: free RAM 7.9 GB -> 32 MB within 20 s; after this change 10.8 GB with no
Ollama model loaded until typing starts.

- single LOCAL_LLM_KEEP_ALIVE (10m) for every local request; no boot warmup
- unload this app's resident Ollama models (keep_alive 0) on quit
- sidecar POST /unload for the aux slot; captions release it on stop
- partial dictation copies only the tail window, not the whole recording
- cap the voice-conversation listen buffer at 5 minutes
- mic test: generation guard so re-entry cannot orphan lease/timers
- startRealtime: single-flight and removeChannel to stop duplicate bindings
- meeting WAV save writes header and PCM without a concat copy

Docs: AI-01 policy note, GAP-PERF-01 with remaining work.
2026-10-11 11:35:15 +09:00

193 lines
6.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ErrorCode } from '@d3ro/core/errors'
import {
getVoiceConversationService,
resetVoiceConversationServiceForTests,
} from '../../../src/main/services/VoiceConversationService'
type ChatOptions = {
signal?: AbortSignal
maxTokens?: number
timeoutMs?: number
keepAlive?: string
}
const mocks = vi.hoisted(() => ({
audioStart: vi.fn(async () => undefined),
audioStop: vi.fn(async () => undefined),
chatStream: vi.fn(),
premiumCancel: vi.fn(),
speakSentences: vi.fn(async () => undefined),
ttsStop: vi.fn(),
send: vi.fn(),
}))
vi.mock('../../../src/main/services/LoggerService', () => ({
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
}))
vi.mock('../../../src/main/services/AudioCaptureService', () => ({
getAudioCaptureService: () => ({
start: mocks.audioStart,
stop: mocks.audioStop,
on: vi.fn(),
off: vi.fn(),
}),
}))
vi.mock('../../../src/main/services/LocalSTTService', () => ({
getLocalSTTService: () => ({ initialize: vi.fn(async () => undefined) }),
}))
vi.mock('../../../src/main/services/PremiumLLMService', () => ({
getPremiumLLMService: () => ({
cancelGeneration: mocks.premiumCancel,
isAvailable: () => false,
}),
}))
vi.mock('../../../src/main/services/LocalLLMService', () => ({
getLocalLLMService: () => ({
isAvailable: () => true,
chatStream: mocks.chatStream,
}),
}))
vi.mock('../../../src/main/services/TTSPlaybackService', () => ({
getTTSPlaybackService: () => ({
speakSentences: mocks.speakSentences,
stop: mocks.ttsStop,
}),
}))
vi.mock('../../../src/main/services/SoundEffectService', () => ({
getSoundEffectService: () => ({ play: vi.fn() }),
}))
vi.mock('../../../src/main/services/ConfigService', () => ({
configGet: () => undefined,
}))
vi.mock('../../../src/main/services/LicenseService', () => ({
getLicenseService: () => ({
canUse: () => ({ allowed: true }),
promptUpgrade: vi.fn(),
}),
}))
vi.mock('../../../src/main/windows/WindowManager', () => ({
getMainWindow: () => ({
isDestroyed: () => false,
webContents: { send: mocks.send },
}),
}))
function waitForAbort(signal: AbortSignal): Promise<void> {
if (signal.aborted) return Promise.resolve()
return new Promise((resolve) => signal.addEventListener('abort', () => resolve(), { once: true }))
}
async function* waitUntilAborted(options?: ChatOptions): AsyncGenerator<string, string> {
if (!options?.signal) throw new Error('Expected caller-owned abort signal')
await waitForAbort(options.signal)
return ''
}
describe('VoiceConversationService local response ownership', () => {
beforeEach(() => {
resetVoiceConversationServiceForTests()
vi.clearAllMocks()
mocks.chatStream.mockImplementation((_messages: unknown, options?: ChatOptions) => waitUntilAborted(options))
})
afterEach(() => {
resetVoiceConversationServiceForTests()
})
it('rejects a second send without starting a second local chat or adding duplicate history', async () => {
const service = getVoiceConversationService()
await service.startSession()
const firstResponse = service.sendTextMessage('first')
await vi.waitFor(() => expect(mocks.chatStream).toHaveBeenCalledTimes(1))
await expect(service.sendTextMessage('second')).rejects.toMatchObject({
code: ErrorCode.ConversationLLMFailed,
})
expect(mocks.chatStream).toHaveBeenCalledTimes(1)
expect(service.getHistory().map((message) => message.content)).toEqual(['first'])
service.stopSession()
await firstResponse
})
it.each([
['stopSession', (service: ReturnType<typeof getVoiceConversationService>) => service.stopSession()],
['cancelResponse', (service: ReturnType<typeof getVoiceConversationService>) => service.cancelResponse()],
])('aborts the local stream signal when %s is called', async (_action, cancel) => {
const service = getVoiceConversationService()
await service.startSession()
const response = service.sendTextMessage('cancel me')
await vi.waitFor(() => expect(mocks.chatStream).toHaveBeenCalledTimes(1))
const options = mocks.chatStream.mock.calls[0][1] as ChatOptions
cancel(service)
expect(options.signal?.aborted).toBe(true)
await response
})
it('sets bounded local chat options for voice responses', async () => {
const service = getVoiceConversationService()
await service.startSession()
const response = service.sendTextMessage('options')
await vi.waitFor(() => expect(mocks.chatStream).toHaveBeenCalledTimes(1))
expect(mocks.chatStream).toHaveBeenCalledWith(
expect.any(Array),
expect.objectContaining({
signal: expect.any(AbortSignal),
maxTokens: 512,
timeoutMs: 60_000,
}),
)
service.stopSession()
await response
})
it('does not publish late assistant output or start TTS after cancellation', async () => {
let releaseLateToken: (() => void) | undefined
const lateToken = new Promise<void>((resolve) => {
releaseLateToken = resolve
})
mocks.chatStream.mockImplementation(() => (async function* (): AsyncGenerator<string, string> {
await lateToken
yield 'late answer.'
return 'late answer.'
})())
const service = getVoiceConversationService()
await service.startSession()
const response = service.sendTextMessage('cancel before response')
await vi.waitFor(() => expect(mocks.chatStream).toHaveBeenCalledTimes(1))
service.cancelResponse()
mocks.send.mockClear()
releaseLateToken?.()
await response
const lateConversationEvents = mocks.send.mock.calls.filter(([channel]) => (
channel === IPC_CHANNELS.VOICE_CONVERSATION.ASSISTANT_DELTA
|| channel === IPC_CHANNELS.VOICE_CONVERSATION.ASSISTANT_MESSAGE
|| channel === IPC_CHANNELS.VOICE_CONVERSATION.TTS_STARTED
|| channel === IPC_CHANNELS.VOICE_CONVERSATION.TTS_FINISHED
))
expect(lateConversationEvents).toEqual([])
expect(mocks.speakSentences).not.toHaveBeenCalled()
expect(service.getHistory().map((message) => message.content)).toEqual(['cancel before response'])
})
})