diff --git a/apps/desktop/src/main/services/CaptionService.ts b/apps/desktop/src/main/services/CaptionService.ts index cf7547e..5115e30 100644 --- a/apps/desktop/src/main/services/CaptionService.ts +++ b/apps/desktop/src/main/services/CaptionService.ts @@ -256,6 +256,8 @@ class CaptionService extends EventEmitter { // 기다리는 사이 stop()(또는 새 start)이 세션을 가져갔다 — 공유 상태는 건드리지 않고 // 이 시도가 연 자원만 돌려준다(release 는 멱등). await ownAudio?.release() + // stop() 이 내릴 때 아직 로딩 중이던 보조 모델이 뒤늦게 올라왔을 수 있다. 새 시작이 없을 때만 내린다. + if (this._captionModelId && this._state === 'inactive') void getLocalSTTService().releaseAuxModel() logger.info('캡션 시작 취소: 로딩 중 정지됨') throw new D3ROError(ErrorCode.CaptionStartFailed, '캡션 시작이 취소되었습니다') } @@ -267,6 +269,8 @@ class CaptionService extends EventEmitter { this._systemTrack = null await this._audio.release() this._hideOverlaySafely() + // 시작 실패(오디오 장치 없음 등)에도 올려 둔 보조 모델을 내린다 — 안 그러면 다음 세션 종료까지 VRAM 을 잡는다. + if (this._captionModelId) void getLocalSTTService().releaseAuxModel() this._session = { ...DEFAULT_START_OPTIONS } this._setState('inactive') const d3roErr = diff --git a/apps/desktop/tests/main/services/caption-aux-release.test.ts b/apps/desktop/tests/main/services/caption-aux-release.test.ts new file mode 100644 index 0000000..1bfdcee --- /dev/null +++ b/apps/desktop/tests/main/services/caption-aux-release.test.ts @@ -0,0 +1,101 @@ +// 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 })) +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 | 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 {} + async flush(): Promise {} + }, +})) + +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((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') + }) +})