import { describe, expect, it } from 'vitest'; import { childPluginId, createCapabilitySlot, rootPluginId, type RuntimeSnapshot, } from '@zhin.js/plugin-runtime'; import { FeatureDiscovery, type DirectoryEntry, type DiscoveryHost, } from '@zhin.js/feature-kit'; import skillFeature, { SkillIndex, parseSkillMarkdown, skillFeatureId, } from '../src/index.js'; describe('Skill Feature', () => { it('discovers only skills//SKILL.md and keeps colocated files as ordinary material', async () => { const source = '/project/skills/research/SKILL.md'; const host = new MemoryHost({ '/project/skills': [ { name: 'research', kind: 'directory' }, { name: 'helper.md', kind: 'file' }, { name: 'references', kind: 'directory' }, ], '/project/skills/research': [ { name: 'SKILL.md', kind: 'file' }, { name: 'notes.md', kind: 'file' }, ], '/project/skills/references': [{ name: 'README.md', kind: 'file' }], }, new Map([[source, '# Research\n\nUse primary sources.']])); const slots = await new FeatureDiscovery(host).discover(skillFeature, [{ owner: rootPluginId(), packageRoot: '/project', }]); expect(slots).toHaveLength(1); expect(slots[0]?.localName).toBe('research'); expect(slots[0]?.definition).toMatchObject({ name: 'research', description: 'Research', instructions: '# Research\n\nUse primary sources.', }); }); it('uses nearest-owner Skill overrides', () => { const root = rootPluginId(); const child = childPluginId(root, 'child'); const definition = (owner: typeof root, instructions: string) => createCapabilitySlot({ owner, feature: skillFeatureId, localName: 'review', source: `/${owner}/skills/review/SKILL.md`, definition: parseSkillMarkdown(instructions, { owner, feature: skillFeatureId, localName: 'review', source: `/${owner}/skills/review/SKILL.md`, }), }); const slots = [definition(root, '# Root review'), definition(child, '# Child review')]; const index = new SkillIndex(slots, snapshot(slots)); expect(index.get(child, 'review')?.description).toBe('Child review'); expect(index.get(root, 'review')?.description).toBe('Root review'); }); it('binds colocated private tools and keeps Agent-private Skills scoped', async () => { const publicSource = '/project/skills/research/SKILL.md'; const privateSource = '/project/agents/reviewer/skills/audit/SKILL.md'; const host = new MemoryHost({ '/project/skills': [{ name: 'research', kind: 'directory' }], '/project/skills/research': [{ name: 'SKILL.md', kind: 'file' }], '/project/skills/research/tools': [{ name: 'search', kind: 'directory' }], '/project/skills/research/tools/search': [{ name: 'index.ts', kind: 'file' }], '/project/agents': [{ name: 'reviewer', kind: 'directory' }], '/project/agents/reviewer/skills': [{ name: 'audit', kind: 'directory' }], '/project/agents/reviewer/skills/audit': [{ name: 'SKILL.md', kind: 'file' }], '/project/agents/reviewer/skills/audit/tools': [{ name: 'report', kind: 'directory' }], '/project/agents/reviewer/skills/audit/tools/report': [{ name: 'index.ts', kind: 'file' }], }, new Map([ [publicSource, '# Research'], [privateSource, '# Audit'], ])); const slots = await new FeatureDiscovery(host).discover(skillFeature, [{ owner: rootPluginId(), packageRoot: '/project', }]); expect(slots.map((slot) => slot.localName)).toEqual(['research', 'agent/reviewer/audit']); expect(slots[0]?.definition.toolNames).toEqual(['skill__research__search']); expect(slots[1]?.definition).toMatchObject({ name: 'audit', agentName: 'reviewer', toolNames: ['agent__reviewer__skill__audit__report'], }); }); it('parses governed frontmatter and keeps only instructions in the prompt payload', () => { const definition = parseSkillMarkdown(`--- name: research description: Evidence-first research tools: [web_search, read_file] platforms: [qq] scopes: [private, group] permissions: [role(trusted)] keywords: [sources, citations] tags: [research] always: true --- # Research Use primary sources.`, { owner: rootPluginId(), feature: skillFeatureId, localName: 'research', source: '/project/skills/research/SKILL.md', }); expect(definition).toMatchObject({ name: 'research', description: 'Evidence-first research', instructions: '# Research\n\nUse primary sources.', toolNames: ['web_search', 'read_file'], platforms: ['qq'], scopes: ['private', 'group'], permissions: ['role(trusted)'], keywords: ['sources', 'citations'], tags: ['research'], always: true, }); expect(Object.isFrozen(definition.toolNames)).toBe(true); }); it('rejects frontmatter that can redirect identity or weaken scope validation', () => { const context = { owner: rootPluginId(), feature: skillFeatureId, localName: 'research', source: '/project/skills/research/SKILL.md', }; expect(() => parseSkillMarkdown('---\nname: deploy\n---\n# Research', context)) .toThrow('must match directory research'); expect(() => parseSkillMarkdown('---\nscopes: [dm]\n---\n# Research', context)) .toThrow('scopes must be private, group, or channel'); }); }); function snapshot(slots: readonly ReturnType[]): RuntimeSnapshot { const root = rootPluginId(); const child = childPluginId(root, 'child'); return { generation: 1, root, tree: new Map([ [root, { id: root, instanceKey: 'root', packageName: '@test/root', packageRoot: '/project', children: [child] }], [child, { id: child, instanceKey: 'child', packageName: '@test/child', packageRoot: '/project/plugins/child', parent: root, children: [] }], ]), config: new Map([[root, {}], [child, {}]]), resources: new Map([[root, new Map()], [child, new Map()]]), capabilities: new Map(slots.map((slot) => [slot.id, slot])), projections: new Map(), }; } class MemoryHost implements DiscoveryHost { constructor( private readonly directories: Readonly>, private readonly files: ReadonlyMap, ) {} async list(path: string): Promise { return this.directories[path] ?? []; } async loadModule(): Promise { throw new Error('Not implemented'); } async readText(source: string): Promise { return this.files.get(source) ?? ''; } } describe('Skill requiresCapabilities (ADR 0044)', () => { it('reads the capability paths a Skill needs from its frontmatter', () => { const owner = rootPluginId(); const definition = parseSkillMarkdown({ markdown: '---\nname: moderation\ndescription: Moderate\nrequiresCapabilities:\n - actions.mute\n---\nBody', }, { owner, feature: skillFeatureId, localName: 'moderation', source: '/p/skills/moderation/SKILL.md' }); expect(definition.requiresCapabilities).toEqual(['actions.mute']); const plain = parseSkillMarkdown({ markdown: '---\nname: plain\ndescription: Plain\n---\nBody' }, { owner, feature: skillFeatureId, localName: 'plain', source: '/p/skills/plain/SKILL.md' }); expect(plain.requiresCapabilities).toBeUndefined(); }); });