Two plugins that declare the same `name` in plugin.json collapse into one component namespace (/name:command, name:skill, agent "name"). Resolution between two installed same-name plugins is undocumented, so one plugin's commands/skills/agents are silently shadowed and unreachable. PLH now flags this at medium severity, keying on the declared `name` (not folder basename, via new declaredName on scanSinglePlugin) with a COL-shaped details.namespaces payload. Name-less plugins are excluded from the collision map. Search-first (code.claude.com/docs/en/plugins): plugin components are namespaced by the declared name, so a plugin component can never shadow a user/project one — only a same-name collision loses components. This refutes the original "plugin vs user vs project shadowing" framing in the backlog. Adds humanizer pattern, fixture (duplicate-plugin-name: 2 colliding + 2 name-less), and 3 tests. Suite 929->932. self-audit A 97 / A 100, scanners 13.
191 lines
8 KiB
JavaScript
191 lines
8 KiB
JavaScript
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');
|
|
const DUP_NAME = resolve(FIXTURES, 'duplicate-plugin-name');
|
|
|
|
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);
|
|
// Anchor on PLH + a title-substring stable across humanizer rewrites.
|
|
// Raw: "Missing required field in plugin.json: <field>". Humanized: "A plugin's manifest is missing a required field".
|
|
const missingFields = result.findings.filter(f =>
|
|
f.scanner === 'PLH' && /(missing.{0,40}(field|manifest))|(manifest.{0,40}missing)/i.test(f.title || '')
|
|
);
|
|
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);
|
|
// Raw: "CLAUDE.md missing '<name>' section". Humanized: "A plugin's instructions file is missing a recommended section".
|
|
const missingSections = result.findings.filter(f =>
|
|
f.scanner === 'PLH' && /missing.{0,40}section/i.test(f.title || '')
|
|
);
|
|
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);
|
|
// CA-PLH-001 (description) and CA-PLH-002 (version) in broken-plugin.
|
|
const missingFields = result.findings.filter(f =>
|
|
f.scanner === 'PLH' && (f.id === 'CA-PLH-001' || f.id === 'CA-PLH-002')
|
|
);
|
|
assert.ok(missingFields.length >= 2, 'Should detect missing description and version');
|
|
});
|
|
|
|
it('detects missing CLAUDE.md', async () => {
|
|
resetCounter();
|
|
const result = await scan(BROKEN_PLUGIN);
|
|
// CA-PLH-003 in broken-plugin = Missing CLAUDE.md.
|
|
const missingMd = result.findings.filter(f => f.scanner === 'PLH' && f.id === 'CA-PLH-003');
|
|
assert.equal(missingMd.length, 1, 'Should detect missing CLAUDE.md');
|
|
});
|
|
|
|
it('detects command without frontmatter', async () => {
|
|
resetCounter();
|
|
const result = await scan(BROKEN_PLUGIN);
|
|
// CA-PLH-004 in broken-plugin = Command missing frontmatter.
|
|
const noFrontmatter = result.findings.filter(f => f.scanner === 'PLH' && f.id === 'CA-PLH-004');
|
|
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);
|
|
// CA-PLH-005 (missing model) and CA-PLH-006 (missing tools) in broken-plugin.
|
|
const missingAgent = result.findings.filter(f =>
|
|
f.scanner === 'PLH' && (f.id === 'CA-PLH-005' || f.id === 'CA-PLH-006')
|
|
);
|
|
// 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.id).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);
|
|
// CA-PLH-001 in empty-project = No plugins found.
|
|
assert.equal(result.findings[0].id, 'CA-PLH-001');
|
|
assert.equal(result.findings[0].scanner, 'PLH');
|
|
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('plugin namespace collision detection', () => {
|
|
const COLLISION_RE = /namespace collision/i;
|
|
|
|
it('flags two plugins that declare the same name', async () => {
|
|
resetCounter();
|
|
const result = await scan(DUP_NAME);
|
|
const collisions = result.findings.filter(f =>
|
|
f.scanner === 'PLH' && COLLISION_RE.test(f.title || '')
|
|
);
|
|
assert.equal(collisions.length, 1, `Expected exactly one namespace collision, got ${collisions.length}`);
|
|
const f = collisions[0];
|
|
assert.equal(f.severity, 'medium', 'Namespace collision is medium severity');
|
|
assert.equal(f.category, 'plugin-hygiene');
|
|
assert.ok(f.title.includes('dup'), `Title should name the colliding namespace: ${f.title}`);
|
|
assert.ok(f.details && Array.isArray(f.details.namespaces), 'Should carry details.namespaces');
|
|
assert.equal(f.details.namespaces.length, 2, 'Two plugins collide on "dup"');
|
|
for (const ns of f.details.namespaces) {
|
|
assert.equal(ns.name, 'dup');
|
|
assert.ok(ns.source.startsWith('plugin:'), `source should be plugin-scoped: ${ns.source}`);
|
|
assert.ok(ns.path, 'each namespace entry carries a path');
|
|
}
|
|
});
|
|
|
|
it('excludes name-less plugins from the collision map', async () => {
|
|
resetCounter();
|
|
const result = await scan(DUP_NAME);
|
|
// gamma + delta declare no name; they must NOT form an undefined/empty collision.
|
|
const collisions = result.findings.filter(f =>
|
|
f.scanner === 'PLH' && COLLISION_RE.test(f.title || '')
|
|
);
|
|
assert.equal(collisions.length, 1, 'Only the real "dup" collision; name-less plugins are ignored');
|
|
assert.ok(
|
|
!collisions.some(f => /undefined|""|''|null/.test(f.title)),
|
|
'No collision finding for an empty/undefined name'
|
|
);
|
|
});
|
|
|
|
it('does not flag a single plugin as a collision', async () => {
|
|
resetCounter();
|
|
const result = await scan(TEST_PLUGIN);
|
|
const collisions = result.findings.filter(f =>
|
|
f.scanner === 'PLH' && COLLISION_RE.test(f.title || '')
|
|
);
|
|
assert.equal(collisions.length, 0, 'A single plugin must not collide with itself');
|
|
});
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|