Skip to content

Commit 3ffde17

Browse files
authored
Merge pull request #23 from intoinside/feature/issue-16
feat: implement praxis intent validate command (issue #16)
2 parents 147122f + 160d123 commit 3ffde17

5 files changed

Lines changed: 258 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ List is a wip and may change in future.
131131
|`praxis init <project-name>`|Initialize a new project to be used with Praxis. Templates are provided in the `templates` directory.|✅|
132132
|`praxis intent create <intent-description>`|Create a new intent.|✅|
133133
|`praxis intent update <intent-id>`|Update an existing intent.|❔​|
134-
|`praxis intent validate`|Validate all intents for completeness and consistency.|❌|
134+
|`praxis intent validate <intent-id>`|Validate all intents for completeness and consistency.|❌|
135135
|`praxis intent list`|List all defined intents.|✅|
136136

137137
---
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
});

‎src/commands/intent/validate.ts‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
/**
5+
* Action for 'praxis intent validate <intent-id>'
6+
*/
7+
export async function intentValidateAction(intentId: string) {
8+
const rootDir = process.cwd();
9+
const intentsDir = path.join(rootDir, '.praxis', 'intents');
10+
const templatePath = path.join(rootDir, '.praxis', 'templates', 'intent-template.md');
11+
12+
if (!fs.existsSync(intentsDir)) {
13+
console.log('No intents found (directory .praxis/intents does not exist).');
14+
return;
15+
}
16+
17+
// 1. Find intent directory
18+
const intentDirectory = findIntentDirectory(intentsDir, intentId);
19+
if (!intentDirectory) {
20+
console.error(`Error: Intent with ID "${intentId}" not found.`);
21+
return;
22+
}
23+
24+
// 2. Read template
25+
let templateContent = 'No intent template found.';
26+
if (fs.existsSync(templatePath)) {
27+
templateContent = fs.readFileSync(templatePath, 'utf8');
28+
}
29+
30+
// 3. Collect artifacts
31+
const intentFile = path.join(intentDirectory, 'intent.md');
32+
let intentContent = '';
33+
if (fs.existsSync(intentFile)) {
34+
intentContent = fs.readFileSync(intentFile, 'utf8');
35+
}
36+
37+
const otherArtifacts: { name: string, content: string }[] = [];
38+
const files = fs.readdirSync(intentDirectory);
39+
for (const file of files) {
40+
if (file === 'intent.md' || file === '.gitkeep') continue;
41+
const filePath = path.join(intentDirectory, file);
42+
if (fs.statSync(filePath).isFile()) {
43+
otherArtifacts.push({
44+
name: file,
45+
content: fs.readFileSync(filePath, 'utf8')
46+
});
47+
}
48+
}
49+
50+
// 4. Generate Prompt
51+
const prompt = generateValidatePrompt(intentId, intentContent, templateContent, otherArtifacts);
52+
53+
// 5. Output
54+
console.log('\n--- IDE AI CHAT PROMPT ---');
55+
console.log('Copy and paste the following into your IDE chat to validate the intent:');
56+
console.log('--------------------------');
57+
console.log(prompt);
58+
console.log('--------------------------\n');
59+
}
60+
61+
export function findIntentDirectory(dir: string, targetId: string): string | null {
62+
try {
63+
const list = fs.readdirSync(dir);
64+
65+
// Check if direct match exists
66+
const directPath = path.join(dir, targetId);
67+
if (fs.existsSync(directPath) && fs.statSync(directPath).isDirectory()) {
68+
if (fs.existsSync(path.join(directPath, 'intent.md'))) {
69+
return directPath;
70+
}
71+
}
72+
73+
// Recursive search
74+
for (const file of list) {
75+
const filePath = path.join(dir, file);
76+
const stat = fs.statSync(filePath);
77+
78+
if (stat.isDirectory()) {
79+
if (file === targetId) {
80+
if (fs.existsSync(path.join(filePath, 'intent.md'))) {
81+
return filePath;
82+
}
83+
}
84+
const found = findIntentDirectory(filePath, targetId);
85+
if (found) return found;
86+
}
87+
}
88+
} catch (e) {
89+
// Ignore errors
90+
}
91+
return null;
92+
}
93+
94+
export function generateValidatePrompt(intentId: string, intentContent: string, templateContent: string, artifacts: { name: string, content: string }[]): string {
95+
let artifactsSection = '';
96+
if (artifacts.length > 0) {
97+
artifactsSection = '\n**Additional Artifacts found in intent directory**:\n';
98+
artifacts.forEach(art => {
99+
artifactsSection += `\n- **${art.name}**:\n\`\`\`\n${art.content}\n\`\`\`\n`;
100+
});
101+
}
102+
103+
return `You are an expert software architect and quality assurance engineer.
104+
I need to validate the Intent with ID: **${intentId}**.
105+
106+
The validation MUST take into account:
107+
1. The **Intent Content** against the **Intent Template**.
108+
2. Any **Additional Artifacts** related to this intent.
109+
3. The overall consistency and completeness of the intent definition.
110+
111+
**INTENT TEMPLATE**:
112+
\`\`\`markdown
113+
${templateContent}
114+
\`\`\`
115+
116+
**INTENT CONTENT**:
117+
\`\`\`markdown
118+
${intentContent || 'No intent.md content found.'}
119+
\`\`\`
120+
${artifactsSection}
121+
122+
Please perform a thorough validation and suggest any necessary modifications to the artifacts to improve clarity, completeness, or alignment with the template and project goals.
123+
Your output must result in a modification of the artifacts if any issues are found.`;
124+
}

‎src/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { manifest } from './manifest.js';
77
import { initCommand } from './commands/init.js';
88
import { intentCreateAction } from './commands/intent/create.js';
99
import { intentListAction } from './commands/intent/list.js';
10+
import { intentValidateAction } from './commands/intent/validate.js';
1011
import { generateSlashCommandsAction } from './commands/integration/generate.js';
1112

1213
import { specDeriveAction } from './commands/spec/derive.js';
@@ -54,6 +55,9 @@ manifest.forEach((cmdDef) => {
5455
await intentCreateAction(description);
5556
} else if (cmdDef.name === 'intent' && subCmdDef.name === 'list') {
5657
await intentListAction();
58+
} else if (cmdDef.name === 'intent' && subCmdDef.name === 'validate') {
59+
const [intentId] = args;
60+
await intentValidateAction(intentId);
5761
} else if (cmdDef.name === 'integration' && subCmdDef.name === 'generate-slash-commands') {
5862
const [tool] = args;
5963
await generateSlashCommandsAction(tool);

‎src/manifest.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ export const manifest: CommandDefinition[] = [
5353
},
5454
{
5555
name: 'validate',
56-
description: 'Validate all intents for completeness and consistency',
56+
description: 'Validate an intent for completeness and consistency',
57+
arguments: [
58+
{
59+
name: 'intent-id',
60+
description: 'The ID of the intent to validate',
61+
required: true,
62+
}
63+
]
5764
},
5865
{
5966
name: 'list',

0 commit comments

Comments
 (0)