|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { intentValidateAction, findIntentDirectory, generateValidatePrompt } from './validate.js'; |
| 5 | + |
| 6 | +vi.mock('fs'); |
| 7 | + |
| 8 | +describe('intentValidateAction', () => { |
| 9 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation((...args) => { console.info(...args); }); |
| 10 | + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation((...args) => { console.info('ERROR:', ...args); }); |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + vi.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should generate and log a prompt when the intent exists', async () => { |
| 17 | + const intentId = 'test-intent'; |
| 18 | + const templateContent = 'template content'; |
| 19 | + const intentContent = 'intent content'; |
| 20 | + |
| 21 | + // Mock existsSync for different paths |
| 22 | + vi.mocked(fs.existsSync).mockImplementation((p) => { |
| 23 | + if (p.toString().includes('.praxis' + path.sep + 'intents')) return true; |
| 24 | + if (p.toString().includes('intent-template.md')) return true; |
| 25 | + if (p.toString().includes('intent.md')) return true; |
| 26 | + if (p.toString().includes(intentId)) return true; |
| 27 | + return false; |
| 28 | + }); |
| 29 | + |
| 30 | + vi.mocked(fs.readdirSync).mockImplementation((p) => { |
| 31 | + if (p.toString().includes('.praxis' + path.sep + 'intents')) return [intentId] as any; |
| 32 | + if (p.toString().includes(intentId)) return ['intent.md'] as any; |
| 33 | + return [] as any; |
| 34 | + }); |
| 35 | + |
| 36 | + vi.mocked(fs.readFileSync).mockImplementation((p) => { |
| 37 | + if (p.toString().includes('intent-template.md')) return templateContent; |
| 38 | + if (p.toString().includes('intent.md')) return intentContent; |
| 39 | + return ''; |
| 40 | + }); |
| 41 | + |
| 42 | + vi.mocked(fs.statSync).mockReturnValue({ isDirectory: () => true, isFile: () => true } as any); |
| 43 | + |
| 44 | + await intentValidateAction(intentId); |
| 45 | + |
| 46 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('--- IDE AI CHAT PROMPT ---')); |
| 47 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining(intentId)); |
| 48 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining(intentContent)); |
| 49 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining(templateContent)); |
| 50 | + expect(consoleErrorSpy).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should log an error when the intent is missing', async () => { |
| 54 | + const intentId = 'missing-intent'; |
| 55 | + |
| 56 | + vi.mocked(fs.existsSync).mockImplementation((p) => { |
| 57 | + if (p.toString().includes('.praxis' + path.sep + 'intents')) return true; |
| 58 | + return false; |
| 59 | + }); |
| 60 | + vi.mocked(fs.readdirSync).mockImplementation((p) => { |
| 61 | + if (p.toString().includes('.praxis' + path.sep + 'intents')) return [] as any; |
| 62 | + return [] as any; |
| 63 | + }); |
| 64 | + vi.mocked(fs.statSync).mockReturnValue({ isDirectory: () => true } as any); |
| 65 | + |
| 66 | + await intentValidateAction(intentId); |
| 67 | + |
| 68 | + expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining(`Error: Intent with ID "${intentId}" not found.`)); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('findIntentDirectory', () => { |
| 73 | + it('should find direct match', () => { |
| 74 | + vi.mocked(fs.readdirSync).mockReturnValue(['test-intent'] as any); |
| 75 | + vi.mocked(fs.existsSync).mockImplementation((p) => { |
| 76 | + if (p.toString().endsWith('test-intent')) return true; |
| 77 | + if (p.toString().endsWith('intent.md')) return true; |
| 78 | + return false; |
| 79 | + }); |
| 80 | + vi.mocked(fs.statSync).mockReturnValue({ isDirectory: () => true } as any); |
| 81 | + |
| 82 | + const result = findIntentDirectory('/root', 'test-intent'); |
| 83 | + expect(result).toContain('test-intent'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should find nested match', () => { |
| 87 | + vi.mocked(fs.readdirSync).mockImplementation((p) => { |
| 88 | + if (p.toString() === '/root') return ['subdir'] as any; |
| 89 | + if (p.toString().endsWith('subdir')) return ['test-intent'] as any; |
| 90 | + return [] as any; |
| 91 | + }); |
| 92 | + vi.mocked(fs.existsSync).mockImplementation((p) => { |
| 93 | + if (p.toString().endsWith('test-intent')) return true; |
| 94 | + if (p.toString().endsWith('intent.md')) return true; |
| 95 | + if (p.toString().endsWith('subdir')) return true; |
| 96 | + return false; |
| 97 | + }); |
| 98 | + vi.mocked(fs.statSync).mockReturnValue({ isDirectory: () => true } as any); |
| 99 | + |
| 100 | + const result = findIntentDirectory('/root', 'test-intent'); |
| 101 | + expect(result).toContain('test-intent'); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('generateValidatePrompt', () => { |
| 106 | + it('should include all required sections in the prompt', () => { |
| 107 | + const intentId = 'test-id'; |
| 108 | + const intentContent = 'test intent content'; |
| 109 | + const templateContent = 'test template content'; |
| 110 | + const artifacts = [{ name: 'extra.md', content: 'extra content' }]; |
| 111 | + |
| 112 | + const prompt = generateValidatePrompt(intentId, intentContent, templateContent, artifacts); |
| 113 | + |
| 114 | + expect(prompt).toContain(intentId); |
| 115 | + expect(prompt).toContain(intentContent); |
| 116 | + expect(prompt).toContain(templateContent); |
| 117 | + expect(prompt).toContain('extra.md'); |
| 118 | + expect(prompt).toContain('extra content'); |
| 119 | + expect(prompt).toContain('validation and suggest any necessary modifications'); |
| 120 | + }); |
| 121 | +}); |
0 commit comments