|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { CustomService } from './custom.service'; |
| 3 | +import * as fs from 'fs/promises'; |
| 4 | + |
| 5 | +vi.mock('fs/promises'); |
| 6 | + |
| 7 | +describe('CustomService', () => { |
| 8 | + let service: CustomService; |
| 9 | + const mockFs = vi.mocked(fs); |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + vi.clearAllMocks(); |
| 13 | + service = new CustomService(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('findCustomPath', () => { |
| 17 | + it('returns path when .codingbuddy exists', async () => { |
| 18 | + mockFs.access.mockResolvedValue(undefined); |
| 19 | + |
| 20 | + const result = await service.findCustomPath('/project'); |
| 21 | + |
| 22 | + expect(result).toBe('/project/.codingbuddy'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns null when .codingbuddy does not exist', async () => { |
| 26 | + mockFs.access.mockRejectedValue(new Error('ENOENT')); |
| 27 | + |
| 28 | + const result = await service.findCustomPath('/project'); |
| 29 | + |
| 30 | + expect(result).toBeNull(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('listCustomRules', () => { |
| 35 | + it('returns rules from .codingbuddy/rules/', async () => { |
| 36 | + mockFs.access.mockResolvedValue(undefined); |
| 37 | + mockFs.readdir.mockResolvedValue([ |
| 38 | + { name: 'api.md', isFile: () => true, isDirectory: () => false }, |
| 39 | + { name: 'naming.md', isFile: () => true, isDirectory: () => false }, |
| 40 | + ] as any); |
| 41 | + mockFs.readFile.mockResolvedValue('# Rule content'); |
| 42 | + |
| 43 | + const result = await service.listCustomRules('/project'); |
| 44 | + |
| 45 | + expect(result).toHaveLength(2); |
| 46 | + expect(result[0].name).toBe('api.md'); |
| 47 | + expect(result[0].source).toBe('custom'); |
| 48 | + expect(result[0].content).toBe('# Rule content'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns empty array when no .codingbuddy folder', async () => { |
| 52 | + mockFs.access.mockRejectedValue(new Error('ENOENT')); |
| 53 | + |
| 54 | + const result = await service.listCustomRules('/project'); |
| 55 | + |
| 56 | + expect(result).toEqual([]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('filters non-md files', async () => { |
| 60 | + mockFs.access.mockResolvedValue(undefined); |
| 61 | + mockFs.readdir.mockResolvedValue([ |
| 62 | + { name: 'api.md', isFile: () => true, isDirectory: () => false }, |
| 63 | + { name: 'readme.txt', isFile: () => true, isDirectory: () => false }, |
| 64 | + ] as any); |
| 65 | + mockFs.readFile.mockResolvedValue('# Content'); |
| 66 | + |
| 67 | + const result = await service.listCustomRules('/project'); |
| 68 | + |
| 69 | + expect(result).toHaveLength(1); |
| 70 | + expect(result[0].name).toBe('api.md'); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('listCustomAgents', () => { |
| 75 | + it('returns valid agents from .codingbuddy/agents/', async () => { |
| 76 | + const validAgent = JSON.stringify({ |
| 77 | + name: 'API Specialist', |
| 78 | + description: 'API design expert', |
| 79 | + role: { |
| 80 | + title: 'API Specialist', |
| 81 | + expertise: ['REST'], |
| 82 | + }, |
| 83 | + }); |
| 84 | + mockFs.access.mockResolvedValue(undefined); |
| 85 | + mockFs.readdir.mockResolvedValue([ |
| 86 | + { name: 'api.json', isFile: () => true, isDirectory: () => false }, |
| 87 | + ] as any); |
| 88 | + mockFs.readFile.mockResolvedValue(validAgent); |
| 89 | + |
| 90 | + const result = await service.listCustomAgents('/project'); |
| 91 | + |
| 92 | + expect(result).toHaveLength(1); |
| 93 | + expect(result[0].parsed.name).toBe('API Specialist'); |
| 94 | + expect(result[0].source).toBe('custom'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('skips invalid JSON files', async () => { |
| 98 | + mockFs.access.mockResolvedValue(undefined); |
| 99 | + mockFs.readdir.mockResolvedValue([ |
| 100 | + { name: 'invalid.json', isFile: () => true, isDirectory: () => false }, |
| 101 | + ] as any); |
| 102 | + mockFs.readFile.mockResolvedValue('not valid json'); |
| 103 | + |
| 104 | + const result = await service.listCustomAgents('/project'); |
| 105 | + |
| 106 | + expect(result).toEqual([]); |
| 107 | + }); |
| 108 | + |
| 109 | + it('skips agents missing required fields', async () => { |
| 110 | + const invalidAgent = JSON.stringify({ |
| 111 | + name: 'Missing role and description', |
| 112 | + }); |
| 113 | + mockFs.access.mockResolvedValue(undefined); |
| 114 | + mockFs.readdir.mockResolvedValue([ |
| 115 | + { name: 'bad.json', isFile: () => true, isDirectory: () => false }, |
| 116 | + ] as any); |
| 117 | + mockFs.readFile.mockResolvedValue(invalidAgent); |
| 118 | + |
| 119 | + const result = await service.listCustomAgents('/project'); |
| 120 | + |
| 121 | + expect(result).toEqual([]); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('listCustomSkills', () => { |
| 126 | + it('returns skills from .codingbuddy/skills/*/SKILL.md', async () => { |
| 127 | + mockFs.access.mockResolvedValue(undefined); |
| 128 | + mockFs.readdir.mockResolvedValue([ |
| 129 | + { name: 'my-workflow', isFile: () => false, isDirectory: () => true }, |
| 130 | + ] as any); |
| 131 | + mockFs.readFile.mockResolvedValue('# Skill content'); |
| 132 | + |
| 133 | + const result = await service.listCustomSkills('/project'); |
| 134 | + |
| 135 | + expect(result).toHaveLength(1); |
| 136 | + expect(result[0].name).toBe('my-workflow'); |
| 137 | + expect(result[0].source).toBe('custom'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('skips folders without SKILL.md', async () => { |
| 141 | + mockFs.access.mockResolvedValue(undefined); |
| 142 | + mockFs.readdir.mockResolvedValue([ |
| 143 | + { name: 'incomplete', isFile: () => false, isDirectory: () => true }, |
| 144 | + ] as any); |
| 145 | + mockFs.readFile.mockRejectedValue(new Error('ENOENT')); |
| 146 | + |
| 147 | + const result = await service.listCustomSkills('/project'); |
| 148 | + |
| 149 | + expect(result).toEqual([]); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments