Files
BMAD-METHOD/test/unit-test-schema.js
Alex Verkhovsky 6b9ab8b201 feat: add agent schema validation with comprehensive testing
Introduce automated validation for agent YAML files using Zod to ensure
schema compliance across all agent definitions. This feature validates
17 agent files across core and module directories, catching structural
errors and maintaining consistency.

Schema Validation (tools/schema/agent.js):
- Zod-based schema validating metadata, persona, menu, prompts, and critical actions
- Module-aware validation: module field required for src/modules/**/agents/,
  optional for src/core/agents/
- Enforces kebab-case unique triggers and at least one command target per menu item
- Validates persona.principles as array (not string)
- Comprehensive refinements for data integrity

CLI Validator (tools/validate-agent-schema.js):
- Scans src/{core,modules/*}/agents/*.agent.yaml
- Parses with js-yaml and validates using Zod schema
- Reports detailed errors with file paths and field paths
- Exits 1 on failures, 0 on success
- Accepts optional project_root parameter for testing

Testing (679 lines across 3 test files):
- test/test-cli-integration.sh: CLI behavior and error handling tests
- test/unit-test-schema.js: Direct schema validation unit tests
- test/test-agent-schema.js: Comprehensive fixture-based tests
- 50 test fixtures covering valid and invalid scenarios
- ESLint configured to support CommonJS test files
- Prettier configured to ignore intentionally broken fixtures

CI Integration (.github/workflows/lint.yaml):
- Renamed from format-check.yaml to lint.yaml
- Added schema-validation job running npm run validate:schemas
- Runs in parallel with prettier and eslint jobs
- Validates on all pull requests

Data Cleanup:
- Fixed src/core/agents/bmad-master.agent.yaml: converted persona.principles
  from string to array format

Documentation:
- Updated schema-classification.md with validation section
- Documents validator usage, enforcement rules, and CI integration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-20 02:14:33 -07:00

134 lines
3.6 KiB
JavaScript

/**
* Unit Tests for Agent Schema Edge Cases
*
* Tests internal functions to achieve 100% branch coverage
*/
const { validateAgentFile } = require('../tools/schema/agent.js');
console.log('Running edge case unit tests...\n');
let passed = 0;
let failed = 0;
// Test 1: Path with malformed module structure (no slash after module name)
// This tests line 213: slashIndex === -1
console.log('Test 1: Malformed module path (no slash after module name)');
try {
const result = validateAgentFile('src/modules/bmm', {
agent: {
metadata: {
id: 'test',
name: 'Test',
title: 'Test',
icon: '🧪',
},
persona: {
role: 'Test',
identity: 'Test',
communication_style: 'Test',
principles: ['Test'],
},
menu: [{ trigger: 'help', description: 'Help', action: 'help' }],
},
});
if (result.success) {
console.log('✗ Should have failed - missing module field');
failed++;
} else {
console.log('✓ Correctly handled malformed path (treated as core agent)');
passed++;
}
} catch (error) {
console.log('✗ Unexpected error:', error.message);
failed++;
}
console.log('');
// Test 2: Module option with empty string
// This tests line 222: trimmed.length > 0
console.log('Test 2: Module agent with empty string in module field');
try {
const result = validateAgentFile('src/modules/bmm/agents/test.agent.yaml', {
agent: {
metadata: {
id: 'test',
name: 'Test',
title: 'Test',
icon: '🧪',
module: ' ', // Empty after trimming
},
persona: {
role: 'Test',
identity: 'Test',
communication_style: 'Test',
principles: ['Test'],
},
menu: [{ trigger: 'help', description: 'Help', action: 'help' }],
},
});
if (result.success) {
console.log('✗ Should have failed - empty module string');
failed++;
} else {
console.log('✓ Correctly rejected empty module string');
passed++;
}
} catch (error) {
console.log('✗ Unexpected error:', error.message);
failed++;
}
console.log('');
// Test 3: Core agent path (src/core/agents/...) - tests the !filePath.startsWith(marker) branch
console.log('Test 3: Core agent path returns null for module');
try {
const result = validateAgentFile('src/core/agents/test.agent.yaml', {
agent: {
metadata: {
id: 'test',
name: 'Test',
title: 'Test',
icon: '🧪',
// No module field - correct for core agent
},
persona: {
role: 'Test',
identity: 'Test',
communication_style: 'Test',
principles: ['Test'],
},
menu: [{ trigger: 'help', description: 'Help', action: 'help' }],
},
});
if (result.success) {
console.log('✓ Core agent validated correctly (no module required)');
passed++;
} else {
console.log('✗ Core agent should pass without module field');
failed++;
}
} catch (error) {
console.log('✗ Unexpected error:', error.message);
failed++;
}
console.log('');
// Summary
console.log('═══════════════════════════════════════');
console.log('Edge Case Unit Test Results:');
console.log(` Passed: ${passed}`);
console.log(` Failed: ${failed}`);
console.log('═══════════════════════════════════════\n');
if (failed === 0) {
console.log('✨ All edge case tests passed!\n');
process.exit(0);
} else {
console.log('❌ Some edge case tests failed\n');
process.exit(1);
}