d3ro-voice/tests/main/services/CustomInstructionService.test.ts
Yun Chan 3f4d0c5828 Phase 7~8 구현: 테스트 + 빌드 + CI/CD + SoundEffect + AutoLaunch + UI 리디자인
- vitest 41개 단위 테스트 (HistoryService, DictionaryService, CustomInstructionService, VoiceModeService, D3ROError)
- electron-builder.yml (NSIS, asarUnpack, extraResources)
- .gitlab-ci.yml (lint, typecheck, test, build, release)
- SoundEffectService: WAV 프리로드 + PowerShell 재생 + VoiceMode 연동
- AutoLaunchService: app.setLoginItemSettings + ConfigService 동기화
- TextInsertService: 간이 삽입 검증 (EditMonitor 경량)
- 번들링 인프라: SoX 다운로드 스크립트, PyInstaller 빌드, 경로 해상도 유틸
- AudioCaptureService/LocalSTTService: 번들 경로 자동 감지
- 08-design-system.md 기반 MUI 테마 (다크+라이트+auto 테마 시스템)
- 전체 UI 컴포넌트 리디자인: AppLayout, Dashboard, StatusBar, History, Dictionary, Commands, Settings
- 효과음 WAV 생성: recording-start, recording-stop, error
- EPIPE 에러 핸들링 추가
2026-04-05 09:12:56 +09:00

190 lines
5.6 KiB
TypeScript

// tests/main/services/CustomInstructionService.test.ts
// CRUD + 프리셋 보호 + 순서 변경 테스트
import { describe, it, expect, beforeEach, vi } from 'vitest'
// ConfigService 모킹
const mockStore: Record<string, unknown> = {}
vi.mock('../../../src/main/services/ConfigService', () => ({
configGet: vi.fn((key: string) => mockStore[key]),
configSet: vi.fn((key: string, value: unknown) => {
mockStore[key] = value
})
}))
vi.mock('../../../src/main/services/LoggerService', () => ({
getLogger: () => ({
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn()
})
}))
let getCustomInstructionService: () => ReturnType<typeof import('../../../src/main/services/CustomInstructionService')['getCustomInstructionService']>
beforeEach(async () => {
// 매번 모듈과 스토어 초기화
for (const key of Object.keys(mockStore)) {
delete mockStore[key]
}
vi.resetModules()
const mod = await import('../../../src/main/services/CustomInstructionService')
getCustomInstructionService = mod.getCustomInstructionService
})
describe('CustomInstructionService', () => {
describe('initialize + getAll', () => {
it('첫 실행 시 5개 프리셋이 생성된다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const all = svc.getAll()
expect(all.length).toBe(5)
expect(all.every((i) => i.isBuiltin)).toBe(true)
})
it('order 순으로 정렬하여 반환한다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const all = svc.getAll()
for (let i = 1; i < all.length; i++) {
expect(all[i].order).toBeGreaterThanOrEqual(all[i - 1].order)
}
})
})
describe('getById', () => {
it('존재하는 ID로 명령어를 반환한다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const inst = svc.getById('builtin-translate')
expect(inst).not.toBeNull()
expect(inst!.name).toBe('번역')
})
it('없는 ID에 대해 null을 반환한다', () => {
const svc = getCustomInstructionService()
svc.initialize()
expect(svc.getById('nonexistent')).toBeNull()
})
})
describe('create', () => {
it('사용자 정의 명령어를 추가한다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const created = svc.create({
name: 'My Custom',
description: 'Test custom instruction',
prompt: 'Custom prompt: {{text}}',
icon: 'Star'
})
expect(created.isBuiltin).toBe(false)
expect(created.name).toBe('My Custom')
expect(svc.getAll().length).toBe(6)
})
})
describe('update', () => {
it('사용자 정의 명령어의 모든 필드를 수정할 수 있다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const created = svc.create({
name: 'Original',
description: 'Desc',
prompt: 'Prompt',
icon: 'Star'
})
const updated = svc.update(created.id, { name: 'Updated', prompt: 'New prompt' })
expect(updated).not.toBeNull()
expect(updated!.name).toBe('Updated')
expect(updated!.prompt).toBe('New prompt')
})
it('프리셋은 프롬프트만 수정 가능하다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const updated = svc.update('builtin-translate', { name: 'Changed Name', prompt: 'New prompt' })
expect(updated).not.toBeNull()
expect(updated!.name).toBe('번역') // 이름은 변경 안 됨
expect(updated!.prompt).toBe('New prompt') // 프롬프트는 변경됨
})
})
describe('delete', () => {
it('프리셋은 삭제할 수 없다', () => {
const svc = getCustomInstructionService()
svc.initialize()
expect(svc.delete('builtin-translate')).toBe(false)
expect(svc.getAll().length).toBe(5)
})
it('사용자 정의 명령어는 삭제할 수 있다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const created = svc.create({
name: 'To Delete',
description: '',
prompt: '',
icon: ''
})
expect(svc.delete(created.id)).toBe(true)
expect(svc.getAll().length).toBe(5)
})
it('없는 ID 삭제 시 false를 반환한다', () => {
const svc = getCustomInstructionService()
svc.initialize()
expect(svc.delete('nonexistent')).toBe(false)
})
})
describe('reorder', () => {
it('지정된 순서대로 order를 재설정한다', () => {
const svc = getCustomInstructionService()
svc.initialize()
const all = svc.getAll()
const reversed = [...all].reverse().map((i) => i.id)
svc.reorder(reversed)
const reordered = svc.getAll()
expect(reordered[0].id).toBe(reversed[0])
expect(reordered[4].id).toBe(reversed[4])
})
})
describe('resetBuiltins', () => {
it('프리셋을 초기값으로 복원하고 사용자 명령어는 유지한다', () => {
const svc = getCustomInstructionService()
svc.initialize()
// 프롬프트 수정
svc.update('builtin-translate', { prompt: 'modified prompt' })
// 사용자 명령어 추가
svc.create({ name: 'User', description: '', prompt: '', icon: '' })
svc.resetBuiltins()
const all = svc.getAll()
const translate = all.find((i) => i.id === 'builtin-translate')
expect(translate!.prompt).toContain('{{targetLanguage}}') // 원래 프롬프트
expect(all.length).toBe(6) // 프리셋 5 + 사용자 1
})
})
})