Skip to content

Commit cccc72d

Browse files
authored
Merge pull request #24 from intoinside/feature/issue-17-spec-validate
feat: implement spec validate command (issue #17)
2 parents 3ffde17 + cd842a2 commit cccc72d

7 files changed

Lines changed: 316 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
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 <intent-id>`|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
---
@@ -150,7 +150,7 @@ List is a wip and may change in future.
150150
|-|-|-|
151151
|`praxis spec derive --from-intent <intent-id>`|Generate initial specifications from an intent.|✅|
152152
|`praxis spec refine <spec-id>`|Manually refine a specification while preserving intent traceability.|❔​|
153-
|`praxis spec validate <spec-id>`|Validate specs for internal consistency and completeness.|❌|
153+
|`praxis spec validate <spec-id>`|Validate specs for internal consistency and completeness.|✅|
154154
|`praxis spec apply <spec-id>`|Implement a specification in code.|✅|
155155
|`praxis spec archive <spec-id>`|Archive a specification implemented.|✅|
156156
|`praxis spec delete <spec-id>`|Delete a specific spec.|✅|

‎src/commands/intent/validate.test.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ describe('intentValidateAction', () => {
2020

2121
// Mock existsSync for different paths
2222
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;
23+
const pStr = p.toString();
24+
if (pStr.endsWith('.praxis' + path.sep + 'intents')) return true;
25+
if (pStr.includes('intent-template.md')) return true;
26+
if (pStr.endsWith('intent.md')) return true;
27+
if (pStr.endsWith(intentId)) return true;
2728
return false;
2829
});
2930

@@ -54,7 +55,8 @@ describe('intentValidateAction', () => {
5455
const intentId = 'missing-intent';
5556

5657
vi.mocked(fs.existsSync).mockImplementation((p) => {
57-
if (p.toString().includes('.praxis' + path.sep + 'intents')) return true;
58+
const pStr = p.toString();
59+
if (pStr.endsWith('.praxis' + path.sep + 'intents')) return true;
5860
return false;
5961
});
6062
vi.mocked(fs.readdirSync).mockImplementation((p) => {

‎src/commands/spec/validate.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import {
5+
specValidateAction,
6+
findSpecDirectory,
7+
extractIntentId,
8+
generateSpecValidatePrompt
9+
} from './validate.js';
10+
11+
vi.mock('fs');
12+
13+
describe('spec/validate.ts', () => {
14+
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
15+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
16+
17+
beforeEach(() => {
18+
vi.resetAllMocks();
19+
});
20+
21+
describe('findSpecDirectory', () => {
22+
it('should find a spec directory directly', () => {
23+
vi.mocked(fs.readdirSync).mockReturnValue(['my-spec'] as any);
24+
vi.mocked(fs.existsSync).mockImplementation((p) => {
25+
const pStr = p.toString();
26+
if (pStr.endsWith('my-spec')) return true;
27+
if (pStr.endsWith('spec.md')) return true;
28+
return false;
29+
});
30+
vi.mocked(fs.statSync).mockReturnValue({ isDirectory: () => true } as any);
31+
32+
const result = findSpecDirectory('/root', 'my-spec');
33+
expect(result).toContain('my-spec');
34+
});
35+
36+
it('should return null if spec directory not found', () => {
37+
vi.mocked(fs.readdirSync).mockReturnValue([]);
38+
const result = findSpecDirectory('/root', 'missing-spec');
39+
expect(result).toBeNull();
40+
});
41+
});
42+
43+
describe('extractIntentId', () => {
44+
it('should extract intent id from spec content', () => {
45+
const content = '# Spec\n\n**Intent**: my-intent-id\n\nContent...';
46+
expect(extractIntentId(content)).toBe('my-intent-id');
47+
});
48+
49+
it('should return null if intent id not found', () => {
50+
const content = '# Spec\n\nNo intent here...';
51+
expect(extractIntentId(content)).toBeNull();
52+
});
53+
});
54+
55+
describe('generateSpecValidatePrompt', () => {
56+
it('should contain all required sections', () => {
57+
const prompt = generateSpecValidatePrompt(
58+
'spec-1',
59+
'spec content',
60+
'template content',
61+
'intent content',
62+
[{ name: 'art.sql', content: 'SELECT 1' }]
63+
);
64+
65+
expect(prompt).toContain('spec-1');
66+
expect(prompt).toContain('spec content');
67+
expect(prompt).toContain('template content');
68+
expect(prompt).toContain('intent content');
69+
expect(prompt).toContain('art.sql');
70+
expect(prompt).toContain('SELECT 1');
71+
expect(prompt).toContain('quality assurance engineer');
72+
});
73+
});
74+
75+
describe('specValidateAction', () => {
76+
it('should generate a prompt when all files exist', async () => {
77+
vi.mocked(fs.existsSync).mockReturnValue(true);
78+
vi.mocked(fs.readdirSync).mockImplementation((p) => {
79+
const s = p.toString();
80+
if (s.includes('specs')) return ['spec-1'] as any;
81+
if (s.includes('spec-1')) return ['spec.md', 'artifact.ts'] as any;
82+
if (s.includes('intents')) return ['intent-1'] as any;
83+
return [] as any;
84+
});
85+
vi.mocked(fs.statSync).mockImplementation((p) => ({
86+
isDirectory: () => !p.toString().endsWith('.md') && !p.toString().endsWith('.ts'),
87+
isFile: () => p.toString().endsWith('.md') || p.toString().endsWith('.ts')
88+
} as any));
89+
vi.mocked(fs.readFileSync).mockImplementation((p) => {
90+
if (p.toString().includes('spec.md')) return '# Spec\n**Intent**: intent-1';
91+
return 'file content';
92+
});
93+
94+
await specValidateAction('spec-1');
95+
96+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('--- IDE AI CHAT PROMPT ---'));
97+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('intent-1'));
98+
});
99+
100+
it('should log error if spec not found', async () => {
101+
vi.mocked(fs.existsSync).mockReturnValue(true);
102+
vi.mocked(fs.readdirSync).mockReturnValue([]);
103+
104+
await specValidateAction('missing-spec');
105+
106+
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('not found'));
107+
});
108+
});
109+
});

‎src/commands/spec/validate.ts‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
/**
5+
* Action for 'praxis spec validate <spec-id>'
6+
*/
7+
export async function specValidateAction(specId: string) {
8+
const rootDir = process.cwd();
9+
const specsDir = path.join(rootDir, '.praxis', 'specs', 'wip');
10+
const intentsDir = path.join(rootDir, '.praxis', 'intents', 'wip');
11+
const specTemplatePath = path.join(rootDir, '.praxis', 'templates', 'spec-template.md');
12+
13+
if (!fs.existsSync(specsDir)) {
14+
console.log('No specs found (directory .praxis/specs does not exist).');
15+
return;
16+
}
17+
18+
// 1. Find spec directory
19+
const specDirectory = findSpecDirectory(specsDir, specId);
20+
if (!specDirectory) {
21+
console.error(`Error: Spec with ID "${specId}" not found.`);
22+
return;
23+
}
24+
25+
// 2. Read spec template
26+
let templateContent = 'No spec template found.';
27+
if (fs.existsSync(specTemplatePath)) {
28+
templateContent = fs.readFileSync(specTemplatePath, 'utf8');
29+
}
30+
31+
// 3. Read spec content and artifacts
32+
const specFile = path.join(specDirectory, 'spec.md');
33+
let specContent = '';
34+
if (fs.existsSync(specFile)) {
35+
specContent = fs.readFileSync(specFile, 'utf8');
36+
}
37+
38+
const otherArtifacts: { name: string, content: string }[] = [];
39+
const files = fs.readdirSync(specDirectory);
40+
for (const file of files) {
41+
if (file === 'spec.md' || file === '.gitkeep') continue;
42+
const filePath = path.join(specDirectory, file);
43+
if (fs.statSync(filePath).isFile()) {
44+
otherArtifacts.push({
45+
name: file,
46+
content: fs.readFileSync(filePath, 'utf8')
47+
});
48+
}
49+
}
50+
51+
// 4. Extract intent-id and find intent content
52+
const intentId = extractIntentId(specContent);
53+
let intentContent = 'No associated intent found.';
54+
if (intentId) {
55+
const intentDir = findIntentDirectory(intentsDir, intentId);
56+
if (intentDir) {
57+
const intentFile = path.join(intentDir, 'intent.md');
58+
if (fs.existsSync(intentFile)) {
59+
intentContent = fs.readFileSync(intentFile, 'utf8');
60+
}
61+
}
62+
}
63+
64+
// 5. Generate Prompt
65+
const prompt = generateSpecValidatePrompt(specId, specContent, templateContent, intentContent, otherArtifacts);
66+
67+
// 6. Output
68+
console.log('\n--- IDE AI CHAT PROMPT ---');
69+
console.log('Copy and paste the following into your IDE chat to validate the specification:');
70+
console.log('--------------------------');
71+
console.log(prompt);
72+
console.log('--------------------------\n');
73+
}
74+
75+
export function findSpecDirectory(dir: string, targetId: string): string | null {
76+
try {
77+
const list = fs.readdirSync(dir);
78+
79+
// Check if direct match exists
80+
const directPath = path.join(dir, targetId);
81+
if (fs.existsSync(directPath) && fs.statSync(directPath).isDirectory()) {
82+
if (fs.existsSync(path.join(directPath, 'spec.md'))) {
83+
return directPath;
84+
}
85+
}
86+
87+
// Recursive search
88+
for (const file of list) {
89+
const filePath = path.join(dir, file);
90+
const stat = fs.statSync(filePath);
91+
92+
if (stat.isDirectory()) {
93+
if (file === targetId) {
94+
if (fs.existsSync(path.join(filePath, 'spec.md'))) {
95+
return filePath;
96+
}
97+
}
98+
const found = findSpecDirectory(filePath, targetId);
99+
if (found) return found;
100+
}
101+
}
102+
} catch (e) {
103+
// Ignore errors
104+
}
105+
return null;
106+
}
107+
108+
export function findIntentDirectory(dir: string, targetId: string): string | null {
109+
try {
110+
if (!fs.existsSync(dir)) return null;
111+
const list = fs.readdirSync(dir);
112+
113+
const directPath = path.join(dir, targetId);
114+
if (fs.existsSync(directPath) && fs.statSync(directPath).isDirectory()) {
115+
if (fs.existsSync(path.join(directPath, 'intent.md'))) {
116+
return directPath;
117+
}
118+
}
119+
120+
for (const file of list) {
121+
const filePath = path.join(dir, file);
122+
const stat = fs.statSync(filePath);
123+
124+
if (stat.isDirectory()) {
125+
if (file === targetId) {
126+
if (fs.existsSync(path.join(filePath, 'intent.md'))) {
127+
return filePath;
128+
}
129+
}
130+
const found = findIntentDirectory(filePath, targetId);
131+
if (found) return found;
132+
}
133+
}
134+
} catch (e) {
135+
// Ignore errors
136+
}
137+
return null;
138+
}
139+
140+
export function extractIntentId(specContent: string): string | null {
141+
const match = specContent.match(/\*\*Intent\*\*:\s*([^\s\r\n]+)/);
142+
return match ? match[1] : null;
143+
}
144+
145+
export function generateSpecValidatePrompt(
146+
specId: string,
147+
specContent: string,
148+
templateContent: string,
149+
intentContent: string,
150+
artifacts: { name: string, content: string }[]
151+
): string {
152+
let artifactsSection = '';
153+
if (artifacts.length > 0) {
154+
artifactsSection = '\n**Additional Artifacts found in spec directory**:\n';
155+
artifacts.forEach(art => {
156+
artifactsSection += `\n- **${art.name}**:\n\`\`\`\n${art.content}\n\`\`\`\n`;
157+
});
158+
}
159+
160+
return `You are an expert software architect and quality assurance engineer.
161+
I need to validate the Specification with ID: **${specId}**.
162+
163+
The validation MUST take into account:
164+
1. The **Specification Content** against the **Specification Template**.
165+
2. The **Specification Content** against its parent **Intent Content** (to ensure traceability and alignment).
166+
3. Any **Additional Artifacts** related to this specification.
167+
4. The overall internal consistency and completeness of the specification.
168+
169+
**SPECIFICATION TEMPLATE**:
170+
\`\`\`markdown
171+
${templateContent}
172+
\`\`\`
173+
174+
**PARENT INTENT CONTENT**:
175+
\`\`\`markdown
176+
${intentContent}
177+
\`\`\`
178+
179+
**SPECIFICATION CONTENT**:
180+
\`\`\`markdown
181+
${specContent || 'No spec.md content found.'}
182+
\`\`\`
183+
${artifactsSection}
184+
185+
Please perform a thorough validation and suggest any necessary modifications to the artifacts to improve clarity, completeness, or alignment with the template and the parent intent.
186+
Your output must result in a modification of the artifacts if any issues are found.`;
187+
}

‎src/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { specDeleteAction } from './commands/spec/delete.js';
1515
import { specApplyAction } from './commands/spec/apply.js';
1616
import { specArchiveAction } from './commands/spec/archive.js';
1717
import { specListAction } from './commands/spec/list.js';
18+
import { specValidateAction } from './commands/spec/validate.js';
1819

1920
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2021
const packageJsonPath = path.resolve(__dirname, '../package.json');
@@ -76,6 +77,9 @@ manifest.forEach((cmdDef) => {
7677
} else if (cmdDef.name === 'spec' && subCmdDef.name === 'list') {
7778
const [options] = args;
7879
await specListAction(options);
80+
} else if (cmdDef.name === 'spec' && subCmdDef.name === 'validate') {
81+
const [specId] = args;
82+
await specValidateAction(specId);
7983
} else {
8084
console.log(`Executing ${cmdDef.name} ${subCmdDef.name}...`);
8185
// Implementation will go here

‎src/manifest.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ export const manifest: CommandDefinition[] = [
105105
{
106106
name: 'validate',
107107
description: 'Validate specs for internal consistency and completeness',
108+
arguments: [
109+
{
110+
name: 'spec-id',
111+
description: 'The ID of the spec to validate',
112+
required: true,
113+
},
114+
],
108115
},
109116
{
110117
name: 'lock',

‎test_output.txt‎

3.39 KB
Binary file not shown.

0 commit comments

Comments
 (0)