import fs from 'fs' import os from 'os' import path from 'path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { FileTranscriptionResult } from '@d3ro/core/types' import { ErrorCode } from '@d3ro/core/errors' import { getMeetingModeService } from '../../src/main/services/MeetingModeService' import { getFileTranscriptionService } from '../../src/main/services/FileTranscriptionService' import { getHistoryService } from '../../src/main/services/HistoryService' import { localAudioPath } from '../../src/main/services/sync/audio-sync' import { emitAccountScopeChanged, LOCAL_SCOPE_ID } from '../../src/main/services/account-scope' import { useRedHarness } from './harness' useRedHarness() function transcription(filePath: string, fullText = '가져온 회의 전사'): FileTranscriptionResult { return { jobId: 'job-1', filePath, fileName: path.basename(filePath), fullText, segments: [{ text: fullText, start: 0, end: 3, confidence: 1 }], totalDurationSec: 3, processingTimeMs: 25, } } describe('기존 녹음 파일을 회의로 가져오기', () => { let sourcePath: string beforeEach(() => { sourcePath = path.join(os.tmpdir(), `d3ro-meeting-import-${Date.now()}.mp3`) fs.writeFileSync(sourcePath, 'normalized-audio') }) afterEach(() => { emitAccountScopeChanged(LOCAL_SCOPE_ID) fs.rmSync(sourcePath, { force: true }) }) it('일반 기록 중복 없이 회의·타임스탬프 전사·재생용 WAV를 저장한다', async () => { const historyCreate = vi.spyOn(getHistoryService(), 'create') const transcribe = vi.spyOn(getFileTranscriptionService(), 'startTranscription') .mockImplementation(async (filePath, _language, options) => { await options?.onConverted?.(filePath) return transcription(filePath) }) const detail = await getMeetingModeService().importRecording(sourcePath, 'ko') expect(detail.title).toBe(path.basename(sourcePath, '.mp3')) expect(detail.status).toBe('completed') expect(detail.rawTranscript).toBe('[00:00] 가져온 회의 전사') expect(detail.durationMs).toBe(3_000) expect(transcribe).toHaveBeenCalledWith(sourcePath, 'ko', { saveToHistory: false, onConverted: expect.any(Function), }) expect(historyCreate).not.toHaveBeenCalled() const storedAudio = localAudioPath('meeting', detail.id) expect(storedAudio).not.toBeNull() expect(fs.readFileSync(storedAudio!, 'utf8')).toBe('normalized-audio') expect(getMeetingModeService().getSessions(1, 20).total).toBe(1) }) it('빈 전사는 완료된 회의나 오디오로 남기지 않는다', async () => { vi.spyOn(getFileTranscriptionService(), 'startTranscription') .mockImplementation(async (filePath, _language, options) => { await options?.onConverted?.(filePath) return transcription(filePath, '') }) await expect(getMeetingModeService().importRecording(sourcePath)).rejects.toThrow('발화가 없습니다') expect(getMeetingModeService().getSessions(1, 20).total).toBe(0) }) it.each(['before-copy', 'after-copy', 'round-trip'] as const)( '가져오기 중 계정이 바뀌면 저장하지 않고 임시 오디오를 정리한다 (%s)', async (timing) => { const copy = vi.spyOn(fs.promises, 'copyFile') vi.spyOn(getFileTranscriptionService(), 'startTranscription') .mockImplementation(async (filePath, _language, options) => { if (timing === 'before-copy') emitAccountScopeChanged('other-account') await options?.onConverted?.(filePath) if (timing !== 'before-copy') emitAccountScopeChanged('other-account') if (timing === 'round-trip') emitAccountScopeChanged(LOCAL_SCOPE_ID) return transcription(filePath) }) await expect(getMeetingModeService().importRecording(sourcePath)).rejects.toMatchObject({ code: ErrorCode.FileTranscriptionCancelled, }) expect(getMeetingModeService().getSessions(1, 20).total).toBe(0) for (const [, destination] of copy.mock.calls) { expect(fs.existsSync(destination)).toBe(false) } if (timing === 'before-copy') expect(copy).not.toHaveBeenCalled() }, ) })