// tests/main/services/CustomInstructionService.test.ts // CRUD + 프리셋 보호 + 순서 변경 테스트 import { describe, it, expect, beforeEach, vi } from 'vitest' // ConfigService 모킹 const mockStore: Record = {} 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 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: '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: '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 }) }) })