mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Split apart skills.ts from plugin.ts
This commit is contained in:
8 files changed
+718
-561
No files matched your search
+1
-386
@@ -1,14 +1,6 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import type { JSRuntimeFS } from './JSRuntime';
|
||||
import {
|
||||
getPlugins,
|
||||
getPluginSkills,
|
||||
getProjectSkills,
|
||||
getAllSkills,
|
||||
findSkill,
|
||||
readSkillContent,
|
||||
isValidSkillName,
|
||||
} from './plugins';
|
||||
import { getPlugins } from './plugins';
|
||||
|
||||
// Mock filesystem for testing
|
||||
class MockFS implements JSRuntimeFS {
|
||||
@@ -164,24 +156,6 @@ describe('plugins', () => {
|
||||
fs = new MockFS();
|
||||
});
|
||||
|
||||
describe('isValidSkillName', () => {
|
||||
it('should validate alphanumeric lowercase names with hyphens', () => {
|
||||
expect(isValidSkillName('test')).toBe(true);
|
||||
expect(isValidSkillName('test123')).toBe(true);
|
||||
expect(isValidSkillName('abc')).toBe(true);
|
||||
expect(isValidSkillName('test-name')).toBe(true);
|
||||
expect(isValidSkillName('test-123')).toBe(true);
|
||||
expect(isValidSkillName('condition-based-waiting')).toBe(true);
|
||||
});
|
||||
|
||||
it('should reject invalid names', () => {
|
||||
expect(isValidSkillName('Test')).toBe(false);
|
||||
expect(isValidSkillName('test_name')).toBe(false);
|
||||
expect(isValidSkillName('test name')).toBe(false);
|
||||
expect(isValidSkillName('Test-Name')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPlugins', () => {
|
||||
it('should return empty array when plugins directory does not exist', async () => {
|
||||
const plugins = await getPlugins(fs, '/plugins');
|
||||
@@ -199,363 +173,4 @@ describe('plugins', () => {
|
||||
expect(plugins).not.toContain('file.txt');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPluginSkills', () => {
|
||||
it('should return empty array when no plugins exist', async () => {
|
||||
const skills = await getPluginSkills(fs, '/plugins');
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return skills from all plugins', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/myskill';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: myskill
|
||||
description: A test skill
|
||||
---
|
||||
|
||||
# My Skill
|
||||
|
||||
This is a test skill.`
|
||||
);
|
||||
|
||||
const skills = await getPluginSkills(fs, '/plugins');
|
||||
expect(skills).toHaveLength(1);
|
||||
expect(skills[0]).toEqual({
|
||||
name: 'myskill',
|
||||
description: 'A test skill',
|
||||
path: skillPath,
|
||||
plugin: 'testplugin',
|
||||
});
|
||||
});
|
||||
|
||||
it('should skip skills with invalid names', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/InvalidSkill';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: InvalidSkill
|
||||
description: Has uppercase letters
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getPluginSkills(fs, '/plugins');
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
|
||||
it('should skip skills missing required frontmatter', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/incomplete';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: incomplete
|
||||
---
|
||||
|
||||
Missing description`
|
||||
);
|
||||
|
||||
const skills = await getPluginSkills(fs, '/plugins');
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAllSkills', () => {
|
||||
it('should combine plugin and project skills', async () => {
|
||||
const pluginPath = '/plugins/testplugin/skills/pluginskill';
|
||||
const projectPath = '/projects/myproject';
|
||||
const projectSkillPath = `${projectPath}/skills/projectskill`;
|
||||
|
||||
// Create plugin skill
|
||||
await fs.mkdir(pluginPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${pluginPath}/SKILL.md`,
|
||||
`---
|
||||
name: plugin-skill
|
||||
description: A plugin skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
// Create project skill
|
||||
await fs.mkdir(projectSkillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${projectSkillPath}/SKILL.md`,
|
||||
`---
|
||||
name: project-skill
|
||||
description: A project skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getAllSkills(fs, '/plugins', projectPath);
|
||||
|
||||
expect(skills).toHaveLength(2);
|
||||
expect(skills.map((s) => s.name)).toContain('plugin-skill');
|
||||
expect(skills.map((s) => s.name)).toContain('project-skill');
|
||||
});
|
||||
|
||||
it('should give precedence to project skills over plugin skills with same name', async () => {
|
||||
const pluginPath = '/plugins/testplugin/skills/samename';
|
||||
const projectPath = '/projects/myproject';
|
||||
const projectSkillPath = `${projectPath}/skills/samename`;
|
||||
|
||||
// Create plugin skill
|
||||
await fs.mkdir(pluginPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${pluginPath}/SKILL.md`,
|
||||
`---
|
||||
name: same-name
|
||||
description: Plugin version
|
||||
---
|
||||
|
||||
Plugin content`
|
||||
);
|
||||
|
||||
// Create project skill with same name
|
||||
await fs.mkdir(projectSkillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${projectSkillPath}/SKILL.md`,
|
||||
`---
|
||||
name: same-name
|
||||
description: Project version
|
||||
---
|
||||
|
||||
Project content`
|
||||
);
|
||||
|
||||
const skills = await getAllSkills(fs, '/plugins', projectPath);
|
||||
|
||||
expect(skills).toHaveLength(1);
|
||||
expect(skills[0].name).toBe('same-name');
|
||||
expect(skills[0].description).toBe('Project version');
|
||||
expect(skills[0].plugin).toBe('project');
|
||||
});
|
||||
|
||||
it('should return only plugin skills when project has no skills', async () => {
|
||||
const pluginPath = '/plugins/testplugin/skills/pluginskill';
|
||||
|
||||
await fs.mkdir(pluginPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${pluginPath}/SKILL.md`,
|
||||
`---
|
||||
name: plugin-skill
|
||||
description: A plugin skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getAllSkills(fs, '/plugins', '/projects/emptyproject');
|
||||
|
||||
expect(skills).toHaveLength(1);
|
||||
expect(skills[0].name).toBe('plugin-skill');
|
||||
expect(skills[0].plugin).toBe('testplugin');
|
||||
});
|
||||
|
||||
it('should return only project skills when there are no plugins', async () => {
|
||||
const projectPath = '/projects/myproject';
|
||||
const projectSkillPath = `${projectPath}/skills/projectskill`;
|
||||
|
||||
await fs.mkdir(projectSkillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${projectSkillPath}/SKILL.md`,
|
||||
`---
|
||||
name: project-skill
|
||||
description: A project skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getAllSkills(fs, '/empty-plugins', projectPath);
|
||||
|
||||
expect(skills).toHaveLength(1);
|
||||
expect(skills[0].name).toBe('project-skill');
|
||||
expect(skills[0].plugin).toBe('project');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findSkill', () => {
|
||||
it('should find a plugin skill by name', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/findme';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: findme
|
||||
description: Find this skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skill = await findSkill(fs, '/plugins', '/projects/myproject', 'findme');
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('findme');
|
||||
expect(skill?.plugin).toBe('testplugin');
|
||||
});
|
||||
|
||||
it('should find a project skill by name', async () => {
|
||||
const projectPath = '/projects/myproject';
|
||||
const skillPath = `${projectPath}/skills/projectskill`;
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: projectskill
|
||||
description: A project skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skill = await findSkill(fs, '/plugins', projectPath, 'projectskill');
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('projectskill');
|
||||
expect(skill?.plugin).toBe('project');
|
||||
});
|
||||
|
||||
it('should prefer project skill over plugin skill with same name', async () => {
|
||||
const pluginPath = '/plugins/testplugin/skills/samename';
|
||||
const projectPath = '/projects/myproject';
|
||||
const projectSkillPath = `${projectPath}/skills/samename`;
|
||||
|
||||
// Create plugin skill
|
||||
await fs.mkdir(pluginPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${pluginPath}/SKILL.md`,
|
||||
`---
|
||||
name: samename
|
||||
description: Plugin version
|
||||
---
|
||||
|
||||
Plugin content`
|
||||
);
|
||||
|
||||
// Create project skill with same name
|
||||
await fs.mkdir(projectSkillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${projectSkillPath}/SKILL.md`,
|
||||
`---
|
||||
name: samename
|
||||
description: Project version
|
||||
---
|
||||
|
||||
Project content`
|
||||
);
|
||||
|
||||
const skill = await findSkill(fs, '/plugins', projectPath, 'samename');
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('samename');
|
||||
expect(skill?.description).toBe('Project version');
|
||||
expect(skill?.plugin).toBe('project');
|
||||
});
|
||||
|
||||
it('should return null when skill not found in either plugins or project', async () => {
|
||||
const skill = await findSkill(fs, '/plugins', '/projects/myproject', 'nonexistent');
|
||||
expect(skill).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
describe('readSkillContent', () => {
|
||||
it('should read the full SKILL.md content', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/myskill';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
const content = `---
|
||||
name: myskill
|
||||
description: A test skill
|
||||
---
|
||||
|
||||
# My Skill
|
||||
|
||||
This is the skill content.`;
|
||||
|
||||
await fs.writeFile(`${skillPath}/SKILL.md`, content);
|
||||
|
||||
const skill = {
|
||||
name: 'myskill',
|
||||
description: 'A test skill',
|
||||
path: skillPath,
|
||||
plugin: 'testplugin',
|
||||
};
|
||||
|
||||
const readContent = await readSkillContent(fs, skill);
|
||||
expect(readContent).toBe(content);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getProjectSkills', () => {
|
||||
it('should get skills from project skills directory', async () => {
|
||||
const projectPath = '/projects/myproject';
|
||||
const skillPath1 = `${projectPath}/skills/skill1`;
|
||||
const skillPath2 = `${projectPath}/skills/skill2`;
|
||||
|
||||
await fs.mkdir(skillPath1, { recursive: true });
|
||||
await fs.mkdir(skillPath2, { recursive: true });
|
||||
|
||||
await fs.writeFile(
|
||||
`${skillPath1}/SKILL.md`,
|
||||
`---
|
||||
name: project-skill-1
|
||||
description: First project skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
await fs.writeFile(
|
||||
`${skillPath2}/SKILL.md`,
|
||||
`---
|
||||
name: project-skill-2
|
||||
description: Second project skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getProjectSkills(fs, projectPath);
|
||||
|
||||
expect(skills).toHaveLength(2);
|
||||
expect(skills[0].name).toBe('project-skill-1');
|
||||
expect(skills[0].plugin).toBe('project');
|
||||
expect(skills[1].name).toBe('project-skill-2');
|
||||
expect(skills[1].plugin).toBe('project');
|
||||
});
|
||||
|
||||
it('should return empty array when project has no skills directory', async () => {
|
||||
const skills = await getProjectSkills(fs, '/projects/emptyproject');
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
|
||||
it('should skip skills with invalid names', async () => {
|
||||
const projectPath = '/projects/myproject';
|
||||
const skillPath = `${projectPath}/skills/badskill`;
|
||||
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: Invalid_Skill_Name
|
||||
description: This has uppercase and underscores
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getProjectSkills(fs, projectPath);
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user