import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { resolve, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { resetCounter } from '../../scanners/lib/output.mjs'; import { scan, discoverPlugins } from '../../scanners/plugin-health-scanner.mjs'; const __dirname = fileURLToPath(new URL('.', import.meta.url)); const FIXTURES = resolve(__dirname, '../fixtures'); const TEST_PLUGIN = resolve(FIXTURES, 'test-plugin'); const BROKEN_PLUGIN = resolve(FIXTURES, 'broken-plugin'); describe('discoverPlugins', () => { it('discovers a single plugin when pointed at plugin dir', async () => { const plugins = await discoverPlugins(TEST_PLUGIN); assert.equal(plugins.length, 1); assert.ok(plugins[0].endsWith('test-plugin')); }); it('discovers multiple plugins in parent dir', async () => { const plugins = await discoverPlugins(FIXTURES); // Should find test-plugin and broken-plugin (both have .claude-plugin/plugin.json) assert.ok(plugins.length >= 2, `Expected >=2, got ${plugins.length}`); }); it('returns empty array for dir with no plugins', async () => { const plugins = await discoverPlugins(resolve(FIXTURES, 'empty-project')); assert.equal(plugins.length, 0); }); }); describe('scan on valid test-plugin', () => { it('returns ok status', async () => { resetCounter(); const result = await scan(TEST_PLUGIN); assert.equal(result.scanner, 'PLH'); assert.equal(result.status, 'ok'); }); it('finds commands and agents', async () => { resetCounter(); const result = await scan(TEST_PLUGIN); assert.ok(result.files_scanned >= 1, 'Should scan at least 1 plugin'); // Valid plugin should have few or no findings const criticals = result.findings.filter(f => f.severity === 'critical'); assert.equal(criticals.length, 0, 'Valid plugin should have no critical findings'); }); it('no findings for missing plugin.json fields', async () => { resetCounter(); const result = await scan(TEST_PLUGIN); const missingFields = result.findings.filter(f => f.title.includes('Missing required field')); assert.equal(missingFields.length, 0, 'All required fields present in test-plugin'); }); it('no findings for missing CLAUDE.md sections', async () => { resetCounter(); const result = await scan(TEST_PLUGIN); const missingSections = result.findings.filter(f => f.title.includes('missing') && f.title.includes('section')); assert.equal(missingSections.length, 0, 'All sections present in test-plugin CLAUDE.md'); }); }); describe('scan on broken-plugin', () => { it('detects missing plugin.json fields', async () => { resetCounter(); const result = await scan(BROKEN_PLUGIN); const missingFields = result.findings.filter(f => f.title.includes('Missing required field')); assert.ok(missingFields.length >= 2, 'Should detect missing description and version'); }); it('detects missing CLAUDE.md', async () => { resetCounter(); const result = await scan(BROKEN_PLUGIN); const missingMd = result.findings.filter(f => f.title === 'Missing CLAUDE.md'); assert.equal(missingMd.length, 1, 'Should detect missing CLAUDE.md'); }); it('detects command without frontmatter', async () => { resetCounter(); const result = await scan(BROKEN_PLUGIN); const noFrontmatter = result.findings.filter(f => f.title === 'Command missing frontmatter'); assert.equal(noFrontmatter.length, 1, 'Should detect command without frontmatter'); }); it('detects agent missing required frontmatter fields', async () => { resetCounter(); const result = await scan(BROKEN_PLUGIN); const missingAgent = result.findings.filter(f => f.title.startsWith('Agent missing frontmatter field:') ); // bad-agent.md has name+description but missing model and tools assert.ok(missingAgent.length >= 2, `Should detect missing model and tools, got ${missingAgent.length}: ${missingAgent.map(f => f.title).join(', ')}`); }); }); describe('scan with no plugins', () => { it('returns info finding for empty directory', async () => { resetCounter(); const result = await scan(resolve(FIXTURES, 'empty-project')); assert.equal(result.findings.length, 1); assert.equal(result.findings[0].title, 'No plugins found'); assert.equal(result.findings[0].severity, 'info'); }); }); describe('cross-plugin command conflict detection', () => { it('scans fixtures dir and reports findings for all plugins', async () => { resetCounter(); const result = await scan(FIXTURES); assert.equal(result.scanner, 'PLH'); assert.ok(result.files_scanned >= 2, 'Should scan multiple plugins'); }); }); describe('finding format', () => { it('findings have standard fields', async () => { resetCounter(); const result = await scan(BROKEN_PLUGIN); assert.ok(result.findings.length > 0); const f = result.findings[0]; assert.ok(f.id.startsWith('CA-PLH-')); assert.equal(f.scanner, 'PLH'); assert.ok(['critical', 'high', 'medium', 'low', 'info'].includes(f.severity)); assert.ok(f.title); assert.ok(f.description); }); });