diff --git a/README.md b/README.md index 24fbf612..5b3d0986 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ skillkit cicd pre-commit | OpenCode | SKILL.md | `.opencode/skills/` | | Antigravity | SKILL.md | `.antigravity/skills/` | | Amp | SKILL.md | `.amp/skills/` | -| Clawdbot | SKILL.md | `skills/` | +| Clawdbot | SKILL.md | `skills/` or `~/.clawdbot/skills/` | | Droid (Factory) | SKILL.md | `.factory/skills/` | | GitHub Copilot | Markdown | `.github/skills/` | | Goose | SKILL.md | `.goose/skills/` | diff --git a/packages/agents/src/__tests__/clawdbot.test.ts b/packages/agents/src/__tests__/clawdbot.test.ts new file mode 100644 index 00000000..3030ab6c --- /dev/null +++ b/packages/agents/src/__tests__/clawdbot.test.ts @@ -0,0 +1,412 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; +import { ClawdbotAdapter } from '../clawdbot.js'; +import type { Skill } from '@skillkit/core'; +import { existsSync } from 'node:fs'; +import { join } from 'node:path'; +import { homedir } from 'node:os'; + +// Mock filesystem functions +vi.mock('node:fs', () => ({ + existsSync: vi.fn(), +})); + +describe('ClawdbotAdapter', () => { + let adapter: ClawdbotAdapter; + const mockExistsSync = vi.mocked(existsSync); + + beforeEach(() => { + adapter = new ClawdbotAdapter(); + mockExistsSync.mockClear(); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + describe('configuration', () => { + it('should have correct type', () => { + expect(adapter.type).toBe('clawdbot'); + }); + + it('should have correct name', () => { + expect(adapter.name).toBe('Clawdbot'); + }); + + it('should have correct skillsDir', () => { + expect(adapter.skillsDir).toBe('skills'); + }); + + it('should have correct configFile', () => { + expect(adapter.configFile).toBe('AGENTS.md'); + }); + }); + + describe('generateConfig', () => { + it('should return empty string for no enabled skills', () => { + const skills: Skill[] = [ + { + name: 'test-skill', + enabled: false, + path: '/path/to/skill', + content: '# Test', + }, + ]; + + const config = adapter.generateConfig(skills); + expect(config).toBe(''); + }); + + it('should generate config with enabled skills', () => { + const skills: Skill[] = [ + { + name: 'test-skill', + description: 'A test skill', + enabled: true, + path: '/path/to/skill', + content: '# Test', + }, + ]; + + const config = adapter.generateConfig(skills); + + expect(config).toContain(''); + expect(config).toContain('## Available Skills'); + expect(config).toContain('test-skill'); + expect(config).toContain('A test skill'); + expect(config).toContain('skillkit read '); + }); + + it('should generate config with multiple skills', () => { + const skills: Skill[] = [ + { + name: 'skill-1', + description: 'First skill', + enabled: true, + path: '/path/to/skill1', + content: '# Skill 1', + }, + { + name: 'skill-2', + description: 'Second skill', + enabled: true, + path: '/path/to/skill2', + content: '# Skill 2', + }, + ]; + + const config = adapter.generateConfig(skills); + + expect(config).toContain('skill-1'); + expect(config).toContain('skill-2'); + expect(config).toContain('First skill'); + expect(config).toContain('Second skill'); + }); + + it('should filter out disabled skills', () => { + const skills: Skill[] = [ + { + name: 'enabled-skill', + enabled: true, + path: '/path/to/enabled', + content: '# Enabled', + }, + { + name: 'disabled-skill', + enabled: false, + path: '/path/to/disabled', + content: '# Disabled', + }, + ]; + + const config = adapter.generateConfig(skills); + + expect(config).toContain('enabled-skill'); + expect(config).not.toContain('disabled-skill'); + }); + + it('should include usage notes', () => { + const skills: Skill[] = [ + { + name: 'test-skill', + enabled: true, + path: '/path/to/skill', + content: '# Test', + }, + ]; + + const config = adapter.generateConfig(skills); + + expect(config).toContain(''); + expect(config).toContain('When users ask you to perform tasks'); + expect(config).toContain('How to use skills:'); + expect(config).toContain('- Invoke:'); + expect(config).toContain('Usage notes:'); + }); + + it('should include available_skills section', () => { + const skills: Skill[] = [ + { + name: 'test-skill', + enabled: true, + path: '/path/to/skill', + content: '# Test', + }, + ]; + + const config = adapter.generateConfig(skills); + + expect(config).toContain(''); + expect(config).toContain(''); + }); + }); + + describe('parseConfig', () => { + it('should parse skill names from config', () => { + const config = ` + + + + skill-1 + Test skill 1 + + + skill-2 + Test skill 2 + + + + `; + + const skillNames = adapter.parseConfig(config); + + expect(skillNames).toEqual(['skill-1', 'skill-2']); + }); + + it('should return empty array for no skills', () => { + const config = ''; + + const skillNames = adapter.parseConfig(config); + + expect(skillNames).toEqual([]); + }); + + it('should handle whitespace in skill names', () => { + const config = ` + + skill-with-spaces + + `; + + const skillNames = adapter.parseConfig(config); + + expect(skillNames).toEqual(['skill-with-spaces']); + }); + + it('should handle multiple name tags', () => { + const config = ` + skill-1 + skill-2 + skill-3 + `; + + const skillNames = adapter.parseConfig(config); + + expect(skillNames).toEqual(['skill-1', 'skill-2', 'skill-3']); + }); + }); + + describe('getInvokeCommand', () => { + it('should return correct invoke command', () => { + const command = adapter.getInvokeCommand('test-skill'); + + expect(command).toBe('skillkit read test-skill'); + }); + + it('should work with skill names containing hyphens', () => { + const command = adapter.getInvokeCommand('my-test-skill'); + + expect(command).toBe('skillkit read my-test-skill'); + }); + + it('should work with skill names containing underscores', () => { + const command = adapter.getInvokeCommand('my_test_skill'); + + expect(command).toBe('skillkit read my_test_skill'); + }); + }); + + describe('isDetected', () => { + it('should detect Clawdbot from workspace skills directory', async () => { + mockExistsSync.mockImplementation((path) => { + return path === join(process.cwd(), 'skills'); + }); + + const detected = await adapter.isDetected(); + + expect(detected).toBe(true); + }); + + it('should detect Clawdbot from global directory', async () => { + mockExistsSync.mockImplementation((path) => { + return path === join(homedir(), '.clawdbot'); + }); + + const detected = await adapter.isDetected(); + + expect(detected).toBe(true); + }); + + it('should detect Clawdbot from config file', async () => { + mockExistsSync.mockImplementation((path) => { + return path === join(process.cwd(), 'clawdbot.json'); + }); + + const detected = await adapter.isDetected(); + + expect(detected).toBe(true); + }); + + it('should not detect Clawdbot when no indicators present', async () => { + mockExistsSync.mockReturnValue(false); + + const detected = await adapter.isDetected(); + + expect(detected).toBe(false); + }); + + it('should detect when multiple indicators are present', async () => { + mockExistsSync.mockReturnValue(true); + + const detected = await adapter.isDetected(); + + expect(detected).toBe(true); + }); + + it('should check all expected paths', async () => { + mockExistsSync.mockReturnValue(false); + + await adapter.isDetected(); + + expect(mockExistsSync).toHaveBeenCalledWith(join(process.cwd(), 'skills')); + expect(mockExistsSync).toHaveBeenCalledWith(join(homedir(), '.clawdbot')); + expect(mockExistsSync).toHaveBeenCalledWith(join(process.cwd(), 'clawdbot.json')); + }); + }); + + describe('integration', () => { + it('should work end-to-end with typical workflow', () => { + const skills: Skill[] = [ + { + name: 'react-patterns', + description: 'React best practices', + enabled: true, + path: '/skills/react-patterns', + content: '# React Patterns\n\nBest practices...', + }, + { + name: 'typescript-strict', + description: 'TypeScript strict mode', + enabled: true, + path: '/skills/typescript-strict', + content: '# TypeScript Strict\n\nUse strict mode...', + }, + ]; + + // Generate config + const config = adapter.generateConfig(skills); + + // Parse config + const parsedSkills = adapter.parseConfig(config); + + // Verify round-trip + expect(parsedSkills).toEqual(['react-patterns', 'typescript-strict']); + + // Get invoke commands + const command1 = adapter.getInvokeCommand('react-patterns'); + const command2 = adapter.getInvokeCommand('typescript-strict'); + + expect(command1).toBe('skillkit read react-patterns'); + expect(command2).toBe('skillkit read typescript-strict'); + }); + + it('should preserve skill order during generation and parsing', () => { + const skills: Skill[] = [ + { name: 'a-skill', enabled: true, path: '/a', content: '' }, + { name: 'z-skill', enabled: true, path: '/z', content: '' }, + { name: 'm-skill', enabled: true, path: '/m', content: '' }, + ]; + + const config = adapter.generateConfig(skills); + const parsed = adapter.parseConfig(config); + + expect(parsed).toEqual(['a-skill', 'z-skill', 'm-skill']); + }); + }); + + describe('edge cases', () => { + it('should handle empty skills array', () => { + const config = adapter.generateConfig([]); + expect(config).toBe(''); + }); + + it('should handle skills without descriptions', () => { + const skills: Skill[] = [ + { + name: 'no-desc-skill', + enabled: true, + path: '/path', + content: '# No description', + }, + ]; + + const config = adapter.generateConfig(skills); + + expect(config).toContain('no-desc-skill'); + expect(config).toContain(''); + }); + + it('should handle special characters in skill names', () => { + const command = adapter.getInvokeCommand('skill-with-special_chars.123'); + + expect(command).toBe('skillkit read skill-with-special_chars.123'); + }); + + it('should handle empty config string during parsing', () => { + const parsed = adapter.parseConfig(''); + + expect(parsed).toEqual([]); + }); + + it('should handle malformed XML during parsing', () => { + const config = 'skill-1broken'; + + const parsed = adapter.parseConfig(config); + + // Should still extract valid names + expect(parsed).toContain('broken'); + }); + }); + + describe('metadata handling', () => { + it('should include skill metadata in generated config', () => { + const skills: Skill[] = [ + { + name: 'meta-skill', + description: 'Has metadata', + enabled: true, + path: '/path', + content: '# Meta', + metadata: { + version: '1.0.0', + author: 'Test Author', + }, + }, + ]; + + const config = adapter.generateConfig(skills); + + expect(config).toContain('meta-skill'); + expect(config).toContain('Has metadata'); + }); + }); +}); diff --git a/packages/agents/src/base.ts b/packages/agents/src/base.ts index 6e025717..77c105d5 100644 --- a/packages/agents/src/base.ts +++ b/packages/agents/src/base.ts @@ -20,7 +20,8 @@ export function createSkillXml(skill: Skill): string { `; } -export function escapeXml(text: string): string { +export function escapeXml(text: string | undefined): string { + if (!text) return ''; return text .replace(/&/g, '&') .replace(/ { const projectSkills = join(process.cwd(), 'skills'); const globalClawdbot = join(homedir(), '.clawdbot'); + const clawdbotConfig = join(process.cwd(), 'clawdbot.json'); - return existsSync(globalClawdbot) || (existsSync(projectSkills) && existsSync(join(process.cwd(), '.clawdbot'))); + return existsSync(projectSkills) || existsSync(globalClawdbot) || existsSync(clawdbotConfig); } }