d3ro-voice/apps/desktop/tests/main/services/caption-aux-release.test.ts
Yun Chan 8d003c8fca fix(caption): unload the caption-only STT model when a start is cancelled or fails
stop() during model loading saw no aux model yet and skipped the unload, and
the failure path (e.g. no microphone) never unloaded it, so the caption model
kept VRAM alongside the dictation model until the next session ended.
2026-10-21 00:25:35 +09:00

101 lines
3.5 KiB
TypeScript

// tests/main/services/caption-aux-release.test.ts
// 자막 전용 보조 STT 모델은 시작이 취소되거나 실패해도 내린다 — 안 그러면 다음 세션을 정지할 때까지
// 받아쓰기 모델과 함께 VRAM 을 잡는다.
import { beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('../../../src/main/services/LoggerService', () => ({
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }),
}))
const config = vi.hoisted(() => ({ values: {} as Record<string, unknown> }))
vi.mock('../../../src/main/services/ConfigService', () => ({
configGet: vi.fn((key: string) => config.values[key]),
configSet: vi.fn(),
}))
const stt = vi.hoisted(() => ({
gate: null as Promise<void> | null,
releaseAuxModel: vi.fn(async () => undefined),
}))
vi.mock('../../../src/main/services/LocalSTTService', () => ({
getLocalSTTService: () => ({
initialize: vi.fn(async () => undefined),
ensureAuxModel: vi.fn(async () => {
if (stt.gate) await stt.gate
}),
releaseAuxModel: stt.releaseAuxModel,
transcribe: vi.fn(async () => ({ text: '' })),
}),
}))
const audio = vi.hoisted(() => ({ start: vi.fn(async () => undefined), stop: vi.fn(async () => undefined) }))
vi.mock('../../../src/main/services/AudioCaptureService', () => ({
getAudioCaptureService: () => ({ ...audio, on: vi.fn(), off: vi.fn() }),
calculateRMS: () => 0.1,
}))
vi.mock('../../../src/main/services/SoundEffectService', () => ({
getSoundEffectService: () => ({ play: vi.fn() }),
}))
vi.mock('../../../src/main/services/LocalLLMService', () => ({
getLocalLLMService: () => ({ isAvailable: () => false, generate: vi.fn() }),
}))
vi.mock('../../../src/main/services/HistoryService', () => ({
getHistoryService: () => ({ create: vi.fn(() => ({ id: 'h' })) }),
}))
vi.mock('../../../src/main/services/caption/StreamingCaptionTrack', () => ({
StreamingCaptionTrack: class {
push(): void {}
async tick(): Promise<void> {}
async flush(): Promise<void> {}
},
}))
type CaptionModule = typeof import('../../../src/main/services/CaptionService')
let mod: CaptionModule
beforeEach(async () => {
vi.resetModules()
vi.clearAllMocks()
config.values = { captionSttModelId: 'caption-small' }
stt.gate = null
audio.start.mockImplementation(async () => undefined)
mod = await import('../../../src/main/services/CaptionService')
mod.resetCaptionServiceForTests()
})
describe('CaptionService — 자막 전용 보조 모델 해제', () => {
it('보조 모델 로딩 중에 정지하면, 뒤늦게 올라온 모델을 내린다', async () => {
let finishLoad: () => void = () => undefined
stt.gate = new Promise<void>((r) => {
finishLoad = r
})
const caption = mod.getCaptionService()
const starting = caption.start({ audioSource: 'mic', showOverlay: false })
await new Promise((r) => setTimeout(r, 0))
await caption.stop()
stt.releaseAuxModel.mockClear()
finishLoad()
await expect(starting).rejects.toBeTruthy()
expect(stt.releaseAuxModel).toHaveBeenCalledTimes(1)
expect(caption.getState()).toBe('inactive')
})
it('오디오 시작이 실패해도 올려 둔 보조 모델을 내린다', async () => {
audio.start.mockImplementation(async () => {
throw new Error('no microphone')
})
const caption = mod.getCaptionService()
await expect(caption.start({ audioSource: 'mic', showOverlay: false })).rejects.toBeTruthy()
expect(stt.releaseAuxModel).toHaveBeenCalledTimes(1)
expect(caption.getState()).toBe('inactive')
})
})