morpheus-cli 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import { SkillLoader } from '../loader.js';
3
+ import fs from 'fs-extra';
4
+ import path from 'path';
5
+ /**
6
+ * Helper to create SKILL.md with YAML frontmatter
7
+ */
8
+ function createSkillMd(dir, frontmatter, content = '') {
9
+ const lines = [];
10
+ for (const [key, value] of Object.entries(frontmatter)) {
11
+ if (Array.isArray(value)) {
12
+ lines.push(`${key}:`);
13
+ for (const item of value) {
14
+ lines.push(` - ${item}`);
15
+ }
16
+ }
17
+ else {
18
+ lines.push(`${key}: ${value}`);
19
+ }
20
+ }
21
+ const yaml = lines.join('\n');
22
+ const md = `---\n${yaml}\n---\n${content}`;
23
+ fs.writeFileSync(path.join(dir, 'SKILL.md'), md);
24
+ }
25
+ describe('SkillLoader', () => {
26
+ const testDir = path.join(process.cwd(), 'test-skills');
27
+ let loader;
28
+ beforeEach(() => {
29
+ fs.ensureDirSync(testDir);
30
+ loader = new SkillLoader(testDir);
31
+ });
32
+ afterEach(() => {
33
+ fs.removeSync(testDir);
34
+ });
35
+ describe('scan()', () => {
36
+ it('should return empty list for non-existent directory', async () => {
37
+ fs.removeSync(testDir);
38
+ const result = await loader.scan();
39
+ expect(result.skills).toHaveLength(0);
40
+ expect(result.errors).toHaveLength(0);
41
+ });
42
+ it('should return empty list for empty directory', async () => {
43
+ const result = await loader.scan();
44
+ expect(result.skills).toHaveLength(0);
45
+ expect(result.errors).toHaveLength(0);
46
+ });
47
+ it('should load valid skill with all metadata', async () => {
48
+ const skillDir = path.join(testDir, 'test-skill');
49
+ fs.ensureDirSync(skillDir);
50
+ createSkillMd(skillDir, {
51
+ name: 'test-skill',
52
+ description: 'A test skill for unit testing',
53
+ version: '1.0.0',
54
+ author: 'Test Author',
55
+ enabled: true,
56
+ execution_mode: 'sync',
57
+ tags: ['test', 'unit'],
58
+ examples: ['do something', 'do another thing'],
59
+ }, '# Test Skill\n\nInstructions here.');
60
+ const result = await loader.scan();
61
+ expect(result.skills).toHaveLength(1);
62
+ expect(result.errors).toHaveLength(0);
63
+ const skill = result.skills[0];
64
+ expect(skill.name).toBe('test-skill');
65
+ expect(skill.description).toBe('A test skill for unit testing');
66
+ expect(skill.version).toBe('1.0.0');
67
+ expect(skill.author).toBe('Test Author');
68
+ expect(skill.enabled).toBe(true);
69
+ expect(skill.execution_mode).toBe('sync');
70
+ expect(skill.tags).toEqual(['test', 'unit']);
71
+ expect(skill.examples).toEqual(['do something', 'do another thing']);
72
+ expect(skill.content).toBe('# Test Skill\n\nInstructions here.');
73
+ });
74
+ it('should load skill with minimal metadata (defaults)', async () => {
75
+ const skillDir = path.join(testDir, 'minimal-skill');
76
+ fs.ensureDirSync(skillDir);
77
+ createSkillMd(skillDir, {
78
+ name: 'minimal-skill',
79
+ description: 'A minimal skill',
80
+ }, 'Minimal instructions');
81
+ const result = await loader.scan();
82
+ expect(result.skills).toHaveLength(1);
83
+ expect(result.errors).toHaveLength(0);
84
+ const skill = result.skills[0];
85
+ expect(skill.name).toBe('minimal-skill');
86
+ expect(skill.enabled).toBe(true); // default
87
+ expect(skill.execution_mode).toBe('sync'); // default
88
+ });
89
+ it('should report error for missing SKILL.md', async () => {
90
+ const skillDir = path.join(testDir, 'no-md-skill');
91
+ fs.ensureDirSync(skillDir);
92
+ // No SKILL.md file created
93
+ const result = await loader.scan();
94
+ expect(result.skills).toHaveLength(0);
95
+ expect(result.errors).toHaveLength(1);
96
+ expect(result.errors[0].directory).toBe('no-md-skill');
97
+ expect(result.errors[0].message).toContain('Missing SKILL.md');
98
+ });
99
+ it('should report error for SKILL.md without frontmatter', async () => {
100
+ const skillDir = path.join(testDir, 'no-frontmatter');
101
+ fs.ensureDirSync(skillDir);
102
+ fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '# No Frontmatter\n\nJust plain markdown.');
103
+ const result = await loader.scan();
104
+ expect(result.skills).toHaveLength(0);
105
+ expect(result.errors).toHaveLength(1);
106
+ expect(result.errors[0].directory).toBe('no-frontmatter');
107
+ expect(result.errors[0].message).toContain('Invalid format');
108
+ });
109
+ it('should report error for schema validation failure', async () => {
110
+ const skillDir = path.join(testDir, 'bad-schema');
111
+ fs.ensureDirSync(skillDir);
112
+ // Missing required 'description' field
113
+ createSkillMd(skillDir, {
114
+ name: 'bad-schema',
115
+ }, 'No description provided');
116
+ const result = await loader.scan();
117
+ expect(result.skills).toHaveLength(0);
118
+ expect(result.errors).toHaveLength(1);
119
+ expect(result.errors[0].message).toContain('Schema validation failed');
120
+ });
121
+ it('should reject invalid skill name format', async () => {
122
+ const skillDir = path.join(testDir, 'invalid-name');
123
+ fs.ensureDirSync(skillDir);
124
+ createSkillMd(skillDir, {
125
+ name: 'Invalid Name With Spaces!',
126
+ description: 'Should fail validation',
127
+ }, 'Content');
128
+ const result = await loader.scan();
129
+ expect(result.skills).toHaveLength(0);
130
+ expect(result.errors).toHaveLength(1);
131
+ expect(result.errors[0].message).toContain('Schema validation failed');
132
+ });
133
+ it('should load multiple skills', async () => {
134
+ // Create skill 1
135
+ const skill1Dir = path.join(testDir, 'skill-one');
136
+ fs.ensureDirSync(skill1Dir);
137
+ createSkillMd(skill1Dir, { name: 'skill-one', description: 'First skill' }, 'Instructions 1');
138
+ // Create skill 2
139
+ const skill2Dir = path.join(testDir, 'skill-two');
140
+ fs.ensureDirSync(skill2Dir);
141
+ createSkillMd(skill2Dir, { name: 'skill-two', description: 'Second skill' }, 'Instructions 2');
142
+ const result = await loader.scan();
143
+ expect(result.skills).toHaveLength(2);
144
+ expect(result.errors).toHaveLength(0);
145
+ const names = result.skills.map(s => s.name).sort();
146
+ expect(names).toEqual(['skill-one', 'skill-two']);
147
+ });
148
+ it('should ignore non-directory entries', async () => {
149
+ // Create a file instead of directory
150
+ fs.writeFileSync(path.join(testDir, 'not-a-dir.yaml'), 'name: test');
151
+ const result = await loader.scan();
152
+ expect(result.skills).toHaveLength(0);
153
+ expect(result.errors).toHaveLength(0);
154
+ });
155
+ it('should load async skill correctly', async () => {
156
+ const skillDir = path.join(testDir, 'async-skill');
157
+ fs.ensureDirSync(skillDir);
158
+ createSkillMd(skillDir, {
159
+ name: 'async-skill',
160
+ description: 'An async skill',
161
+ execution_mode: 'async',
162
+ }, 'Long-running task instructions');
163
+ const result = await loader.scan();
164
+ expect(result.skills).toHaveLength(1);
165
+ expect(result.skills[0].execution_mode).toBe('async');
166
+ });
167
+ });
168
+ describe('content handling', () => {
169
+ it('should include content in skill object', async () => {
170
+ const skillDir = path.join(testDir, 'content-skill');
171
+ fs.ensureDirSync(skillDir);
172
+ const mdContent = '# Content Skill\n\nThis is the instruction content.';
173
+ createSkillMd(skillDir, { name: 'content-skill', description: 'Test' }, mdContent);
174
+ const result = await loader.scan();
175
+ expect(result.skills).toHaveLength(1);
176
+ expect(result.skills[0].content).toBe(mdContent);
177
+ });
178
+ it('should handle empty content after frontmatter', async () => {
179
+ const skillDir = path.join(testDir, 'empty-content');
180
+ fs.ensureDirSync(skillDir);
181
+ createSkillMd(skillDir, { name: 'empty-content', description: 'Test' }, '');
182
+ const result = await loader.scan();
183
+ expect(result.skills).toHaveLength(1);
184
+ expect(result.skills[0].content).toBe('');
185
+ });
186
+ });
187
+ });
@@ -0,0 +1,201 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
+ import fs from 'fs-extra';
3
+ import path from 'path';
4
+ // Define test directory as a static string path
5
+ const TEST_DIR = process.cwd() + '/test-skills-registry';
6
+ // Mock PATHS to use test directory
7
+ vi.mock('../../../config/paths.js', () => ({
8
+ PATHS: {
9
+ skills: process.cwd() + '/test-skills-registry',
10
+ },
11
+ }));
12
+ // Import after mock setup
13
+ import { SkillRegistry } from '../registry.js';
14
+ /**
15
+ * Helper to create SKILL.md with YAML frontmatter
16
+ */
17
+ function createSkillMd(dir, frontmatter, content = '') {
18
+ const lines = [];
19
+ for (const [key, value] of Object.entries(frontmatter)) {
20
+ if (Array.isArray(value)) {
21
+ lines.push(`${key}:`);
22
+ for (const item of value) {
23
+ lines.push(` - ${item}`);
24
+ }
25
+ }
26
+ else if (typeof value === 'boolean') {
27
+ lines.push(`${key}: ${value ? 'true' : 'false'}`);
28
+ }
29
+ else {
30
+ lines.push(`${key}: ${value}`);
31
+ }
32
+ }
33
+ const yaml = lines.join('\n');
34
+ const md = `---\n${yaml}\n---\n${content}`;
35
+ fs.writeFileSync(path.join(dir, 'SKILL.md'), md);
36
+ }
37
+ describe('SkillRegistry', () => {
38
+ beforeEach(() => {
39
+ SkillRegistry.resetInstance();
40
+ fs.ensureDirSync(TEST_DIR);
41
+ });
42
+ afterEach(() => {
43
+ fs.removeSync(TEST_DIR);
44
+ });
45
+ describe('singleton pattern', () => {
46
+ it('should return the same instance', () => {
47
+ const instance1 = SkillRegistry.getInstance();
48
+ const instance2 = SkillRegistry.getInstance();
49
+ expect(instance1).toBe(instance2);
50
+ });
51
+ it('should reset instance correctly', () => {
52
+ const instance1 = SkillRegistry.getInstance();
53
+ SkillRegistry.resetInstance();
54
+ const instance2 = SkillRegistry.getInstance();
55
+ expect(instance1).not.toBe(instance2);
56
+ });
57
+ });
58
+ describe('load()', () => {
59
+ it('should load skills from directory', async () => {
60
+ const skillDir = path.join(TEST_DIR, 'test-skill');
61
+ fs.ensureDirSync(skillDir);
62
+ createSkillMd(skillDir, { name: 'test-skill', description: 'Test skill' }, 'Instructions');
63
+ const registry = SkillRegistry.getInstance();
64
+ await registry.load();
65
+ expect(registry.getAll()).toHaveLength(1);
66
+ expect(registry.get('test-skill')).toBeDefined();
67
+ });
68
+ it('should clear previous skills on reload', async () => {
69
+ const skillDir = path.join(TEST_DIR, 'skill-a');
70
+ fs.ensureDirSync(skillDir);
71
+ createSkillMd(skillDir, { name: 'skill-a', description: 'Skill A' }, 'Instructions');
72
+ const registry = SkillRegistry.getInstance();
73
+ await registry.load();
74
+ expect(registry.getAll()).toHaveLength(1);
75
+ // Remove the skill and reload
76
+ fs.removeSync(skillDir);
77
+ await registry.reload();
78
+ expect(registry.getAll()).toHaveLength(0);
79
+ });
80
+ });
81
+ describe('enable() / disable()', () => {
82
+ it('should enable a disabled skill', async () => {
83
+ const skillDir = path.join(TEST_DIR, 'toggle-skill');
84
+ fs.ensureDirSync(skillDir);
85
+ createSkillMd(skillDir, { name: 'toggle-skill', description: 'Toggle test', enabled: false }, 'Instructions');
86
+ const registry = SkillRegistry.getInstance();
87
+ await registry.load();
88
+ expect(registry.get('toggle-skill')?.enabled).toBe(false);
89
+ expect(registry.getEnabled()).toHaveLength(0);
90
+ const result = registry.enable('toggle-skill');
91
+ expect(result).toBe(true);
92
+ expect(registry.get('toggle-skill')?.enabled).toBe(true);
93
+ expect(registry.getEnabled()).toHaveLength(1);
94
+ });
95
+ it('should disable an enabled skill', async () => {
96
+ const skillDir = path.join(TEST_DIR, 'toggle-skill');
97
+ fs.ensureDirSync(skillDir);
98
+ createSkillMd(skillDir, { name: 'toggle-skill', description: 'Toggle test', enabled: true }, 'Instructions');
99
+ const registry = SkillRegistry.getInstance();
100
+ await registry.load();
101
+ expect(registry.get('toggle-skill')?.enabled).toBe(true);
102
+ const result = registry.disable('toggle-skill');
103
+ expect(result).toBe(true);
104
+ expect(registry.get('toggle-skill')?.enabled).toBe(false);
105
+ expect(registry.getEnabled()).toHaveLength(0);
106
+ });
107
+ it('should return false for non-existent skill', async () => {
108
+ const registry = SkillRegistry.getInstance();
109
+ await registry.load();
110
+ expect(registry.enable('non-existent')).toBe(false);
111
+ expect(registry.disable('non-existent')).toBe(false);
112
+ });
113
+ });
114
+ describe('getEnabled()', () => {
115
+ it('should return only enabled skills', async () => {
116
+ // Create enabled skill
117
+ const enabledDir = path.join(TEST_DIR, 'enabled-skill');
118
+ fs.ensureDirSync(enabledDir);
119
+ createSkillMd(enabledDir, { name: 'enabled-skill', description: 'Enabled', enabled: true }, 'Instructions');
120
+ // Create disabled skill
121
+ const disabledDir = path.join(TEST_DIR, 'disabled-skill');
122
+ fs.ensureDirSync(disabledDir);
123
+ createSkillMd(disabledDir, { name: 'disabled-skill', description: 'Disabled', enabled: false }, 'Instructions');
124
+ const registry = SkillRegistry.getInstance();
125
+ await registry.load();
126
+ expect(registry.getAll()).toHaveLength(2);
127
+ expect(registry.getEnabled()).toHaveLength(1);
128
+ expect(registry.getEnabled()[0].name).toBe('enabled-skill');
129
+ });
130
+ });
131
+ describe('getSystemPromptSection()', () => {
132
+ it('should generate prompt section with sync skills', async () => {
133
+ const skillDir = path.join(TEST_DIR, 'prompt-skill');
134
+ fs.ensureDirSync(skillDir);
135
+ createSkillMd(skillDir, {
136
+ name: 'prompt-skill',
137
+ description: 'A skill for prompts',
138
+ execution_mode: 'sync',
139
+ examples: ['example usage'],
140
+ }, 'Instructions for prompt skill');
141
+ const registry = SkillRegistry.getInstance();
142
+ await registry.load();
143
+ const section = registry.getSystemPromptSection();
144
+ expect(section).toContain('Available Skills');
145
+ expect(section).toContain('prompt-skill');
146
+ expect(section).toContain('A skill for prompts');
147
+ expect(section).toContain('skill_execute');
148
+ });
149
+ it('should generate prompt section with async skills', async () => {
150
+ const skillDir = path.join(TEST_DIR, 'async-skill');
151
+ fs.ensureDirSync(skillDir);
152
+ createSkillMd(skillDir, {
153
+ name: 'async-skill',
154
+ description: 'An async skill',
155
+ execution_mode: 'async',
156
+ }, 'Instructions for async skill');
157
+ const registry = SkillRegistry.getInstance();
158
+ await registry.load();
159
+ const section = registry.getSystemPromptSection();
160
+ expect(section).toContain('Async Skills');
161
+ expect(section).toContain('async-skill');
162
+ expect(section).toContain('skill_delegate');
163
+ });
164
+ it('should return empty string when no skills', async () => {
165
+ const registry = SkillRegistry.getInstance();
166
+ await registry.load();
167
+ const section = registry.getSystemPromptSection();
168
+ expect(section).toBe('');
169
+ });
170
+ it('should not include disabled skills', async () => {
171
+ const skillDir = path.join(TEST_DIR, 'disabled-prompt');
172
+ fs.ensureDirSync(skillDir);
173
+ createSkillMd(skillDir, {
174
+ name: 'disabled-prompt',
175
+ description: 'Disabled skill',
176
+ enabled: false,
177
+ }, 'Instructions');
178
+ const registry = SkillRegistry.getInstance();
179
+ await registry.load();
180
+ const section = registry.getSystemPromptSection();
181
+ expect(section).toBe('');
182
+ });
183
+ });
184
+ describe('getContent()', () => {
185
+ it('should return skill content from loaded skill', async () => {
186
+ const skillDir = path.join(TEST_DIR, 'content-skill');
187
+ fs.ensureDirSync(skillDir);
188
+ createSkillMd(skillDir, { name: 'content-skill', description: 'Test' }, '# Instructions\n\nDo the thing.');
189
+ const registry = SkillRegistry.getInstance();
190
+ await registry.load();
191
+ const content = registry.getContent('content-skill');
192
+ expect(content).toBe('# Instructions\n\nDo the thing.');
193
+ });
194
+ it('should return null for non-existent skill', async () => {
195
+ const registry = SkillRegistry.getInstance();
196
+ await registry.load();
197
+ const content = registry.getContent('non-existent');
198
+ expect(content).toBeNull();
199
+ });
200
+ });
201
+ });
@@ -0,0 +1,266 @@
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
2
+ // Use vi.hoisted to define mocks before they're used in vi.mock calls
3
+ const { mockRegistry, mockRepository, mockDisplay, mockContext, mockKeymaker } = vi.hoisted(() => ({
4
+ mockRegistry: {
5
+ get: vi.fn(),
6
+ getEnabled: vi.fn(() => []),
7
+ getContent: vi.fn(() => null),
8
+ },
9
+ mockRepository: {
10
+ createTask: vi.fn(),
11
+ },
12
+ mockDisplay: {
13
+ log: vi.fn(),
14
+ },
15
+ mockContext: {
16
+ get: vi.fn(() => ({
17
+ origin_channel: 'telegram',
18
+ session_id: 'test-session',
19
+ origin_message_id: '123',
20
+ origin_user_id: 'user-1',
21
+ })),
22
+ findDuplicateDelegation: vi.fn(() => null),
23
+ canEnqueueDelegation: vi.fn(() => true),
24
+ setDelegationAck: vi.fn(),
25
+ },
26
+ mockKeymaker: {
27
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
28
+ executeKeymakerTask: vi.fn((_skill, _obj, _ctx) => Promise.resolve('Keymaker result')),
29
+ },
30
+ }));
31
+ vi.mock('../registry.js', () => ({
32
+ SkillRegistry: {
33
+ getInstance: () => mockRegistry,
34
+ },
35
+ }));
36
+ vi.mock('../../tasks/repository.js', () => ({
37
+ TaskRepository: {
38
+ getInstance: () => mockRepository,
39
+ },
40
+ }));
41
+ vi.mock('../../tasks/context.js', () => ({
42
+ TaskRequestContext: mockContext,
43
+ }));
44
+ vi.mock('../../display.js', () => ({
45
+ DisplayManager: {
46
+ getInstance: () => mockDisplay,
47
+ },
48
+ }));
49
+ vi.mock('../../keymaker.js', () => ({
50
+ executeKeymakerTask: (skillName, objective, context) => mockKeymaker.executeKeymakerTask(skillName, objective, context),
51
+ }));
52
+ // Now import the module under test
53
+ import { SkillExecuteTool, SkillDelegateTool, getSkillExecuteDescription, getSkillDelegateDescription } from '../tool.js';
54
+ describe('SkillExecuteTool (sync)', () => {
55
+ beforeEach(() => {
56
+ vi.clearAllMocks();
57
+ mockRegistry.get.mockReset();
58
+ mockRegistry.getEnabled.mockReset();
59
+ mockKeymaker.executeKeymakerTask.mockReset();
60
+ mockKeymaker.executeKeymakerTask.mockResolvedValue('Keymaker result');
61
+ mockRegistry.getEnabled.mockReturnValue([]);
62
+ });
63
+ describe('getSkillExecuteDescription()', () => {
64
+ it('should include enabled sync skills in description', () => {
65
+ mockRegistry.getEnabled.mockReturnValue([
66
+ { name: 'code-review', description: 'Review code for issues', execution_mode: 'sync' },
67
+ { name: 'git-ops', description: 'Git operations helper', execution_mode: 'sync' },
68
+ { name: 'deploy', description: 'Deploy to prod', execution_mode: 'async' }, // should not appear
69
+ ]);
70
+ const description = getSkillExecuteDescription();
71
+ expect(description).toContain('code-review: Review code for issues');
72
+ expect(description).toContain('git-ops: Git operations helper');
73
+ expect(description).not.toContain('deploy');
74
+ });
75
+ it('should show no sync skills message when none enabled', () => {
76
+ mockRegistry.getEnabled.mockReturnValue([]);
77
+ const description = getSkillExecuteDescription();
78
+ expect(description).toContain('(no sync skills enabled)');
79
+ });
80
+ });
81
+ describe('invoke()', () => {
82
+ it('should execute sync skill via Keymaker', async () => {
83
+ mockRegistry.get.mockReturnValue({
84
+ name: 'test-skill',
85
+ description: 'Test',
86
+ enabled: true,
87
+ execution_mode: 'sync',
88
+ content: 'Instructions here',
89
+ });
90
+ mockRegistry.getEnabled.mockReturnValue([{ name: 'test-skill', execution_mode: 'sync' }]);
91
+ const result = await SkillExecuteTool.invoke({
92
+ skillName: 'test-skill',
93
+ objective: 'do the thing',
94
+ });
95
+ expect(mockKeymaker.executeKeymakerTask).toHaveBeenCalledWith('test-skill', 'do the thing', expect.objectContaining({
96
+ origin_channel: 'telegram',
97
+ session_id: 'test-session',
98
+ }));
99
+ expect(result).toBe('Keymaker result');
100
+ });
101
+ it('should return error for non-existent skill', async () => {
102
+ mockRegistry.get.mockReturnValue(undefined);
103
+ mockRegistry.getEnabled.mockReturnValue([
104
+ { name: 'other-skill', execution_mode: 'sync' },
105
+ ]);
106
+ const result = await SkillExecuteTool.invoke({
107
+ skillName: 'non-existent',
108
+ objective: 'do something',
109
+ });
110
+ expect(result).toContain('Error');
111
+ expect(result).toContain('not found');
112
+ expect(result).toContain('other-skill');
113
+ });
114
+ it('should return error for async skill', async () => {
115
+ mockRegistry.get.mockReturnValue({
116
+ name: 'async-skill',
117
+ description: 'Async only',
118
+ enabled: true,
119
+ execution_mode: 'async',
120
+ });
121
+ const result = await SkillExecuteTool.invoke({
122
+ skillName: 'async-skill',
123
+ objective: 'do something',
124
+ });
125
+ expect(result).toContain('Error');
126
+ expect(result).toContain('async-only');
127
+ expect(result).toContain('skill_delegate');
128
+ });
129
+ });
130
+ });
131
+ describe('SkillDelegateTool (async)', () => {
132
+ beforeEach(() => {
133
+ vi.clearAllMocks();
134
+ mockRegistry.get.mockReset();
135
+ mockRegistry.getEnabled.mockReset();
136
+ mockRepository.createTask.mockReset();
137
+ mockDisplay.log.mockReset();
138
+ mockContext.findDuplicateDelegation.mockReturnValue(null);
139
+ mockContext.canEnqueueDelegation.mockReturnValue(true);
140
+ mockRegistry.getEnabled.mockReturnValue([]);
141
+ });
142
+ describe('getSkillDelegateDescription()', () => {
143
+ it('should include enabled async skills in description', () => {
144
+ mockRegistry.getEnabled.mockReturnValue([
145
+ { name: 'deploy-staging', description: 'Deploy to staging', execution_mode: 'async' },
146
+ { name: 'batch-process', description: 'Process batch jobs', execution_mode: 'async' },
147
+ { name: 'code-review', description: 'Review code', execution_mode: 'sync' }, // should not appear
148
+ ]);
149
+ const description = getSkillDelegateDescription();
150
+ expect(description).toContain('deploy-staging: Deploy to staging');
151
+ expect(description).toContain('batch-process: Process batch jobs');
152
+ expect(description).not.toContain('code-review');
153
+ });
154
+ it('should show no async skills message when none enabled', () => {
155
+ mockRegistry.getEnabled.mockReturnValue([]);
156
+ const description = getSkillDelegateDescription();
157
+ expect(description).toContain('(no async skills enabled)');
158
+ });
159
+ });
160
+ describe('invoke()', () => {
161
+ it('should create task for valid async skill', async () => {
162
+ mockRegistry.get.mockReturnValue({
163
+ name: 'deploy-staging',
164
+ description: 'Deploy',
165
+ enabled: true,
166
+ execution_mode: 'async',
167
+ });
168
+ mockRegistry.getEnabled.mockReturnValue([{ name: 'deploy-staging', execution_mode: 'async' }]);
169
+ mockRepository.createTask.mockReturnValue({
170
+ id: 'task-123',
171
+ agent: 'keymaker',
172
+ status: 'pending',
173
+ });
174
+ const result = await SkillDelegateTool.invoke({
175
+ skillName: 'deploy-staging',
176
+ objective: 'deploy to staging',
177
+ });
178
+ expect(mockRepository.createTask).toHaveBeenCalledWith(expect.objectContaining({
179
+ agent: 'keymaker',
180
+ input: 'deploy to staging',
181
+ context: JSON.stringify({ skill: 'deploy-staging' }),
182
+ origin_channel: 'telegram',
183
+ session_id: 'test-session',
184
+ }));
185
+ expect(result).toContain('task-123');
186
+ expect(result).toContain('queued');
187
+ });
188
+ it('should return error for sync skill', async () => {
189
+ mockRegistry.get.mockReturnValue({
190
+ name: 'sync-skill',
191
+ description: 'Sync',
192
+ enabled: true,
193
+ execution_mode: 'sync',
194
+ });
195
+ const result = await SkillDelegateTool.invoke({
196
+ skillName: 'sync-skill',
197
+ objective: 'do something',
198
+ });
199
+ expect(result).toContain('Error');
200
+ expect(result).toContain('sync');
201
+ expect(result).toContain('skill_execute');
202
+ expect(mockRepository.createTask).not.toHaveBeenCalled();
203
+ });
204
+ it('should return error for non-existent skill', async () => {
205
+ mockRegistry.get.mockReturnValue(undefined);
206
+ mockRegistry.getEnabled.mockReturnValue([
207
+ { name: 'other-skill', execution_mode: 'async' },
208
+ ]);
209
+ const result = await SkillDelegateTool.invoke({
210
+ skillName: 'non-existent',
211
+ objective: 'do something',
212
+ });
213
+ expect(result).toContain('Error');
214
+ expect(result).toContain('not found');
215
+ expect(mockRepository.createTask).not.toHaveBeenCalled();
216
+ });
217
+ it('should return error for disabled skill', async () => {
218
+ mockRegistry.get.mockReturnValue({
219
+ name: 'disabled-skill',
220
+ description: 'Disabled',
221
+ enabled: false,
222
+ execution_mode: 'async',
223
+ });
224
+ const result = await SkillDelegateTool.invoke({
225
+ skillName: 'disabled-skill',
226
+ objective: 'do something',
227
+ });
228
+ expect(result).toContain('Error');
229
+ expect(result).toContain('disabled');
230
+ expect(mockRepository.createTask).not.toHaveBeenCalled();
231
+ });
232
+ it('should deduplicate delegation requests', async () => {
233
+ mockRegistry.get.mockReturnValue({
234
+ name: 'dup-skill',
235
+ enabled: true,
236
+ execution_mode: 'async',
237
+ });
238
+ mockContext.findDuplicateDelegation.mockReturnValue({
239
+ task_id: 'existing-task',
240
+ agent: 'keymaker',
241
+ task: 'dup-skill:objective',
242
+ });
243
+ const result = await SkillDelegateTool.invoke({
244
+ skillName: 'dup-skill',
245
+ objective: 'objective',
246
+ });
247
+ expect(result).toContain('existing-task');
248
+ expect(result).toContain('already queued');
249
+ expect(mockRepository.createTask).not.toHaveBeenCalled();
250
+ });
251
+ it('should block when delegation limit reached', async () => {
252
+ mockRegistry.get.mockReturnValue({
253
+ name: 'limit-skill',
254
+ enabled: true,
255
+ execution_mode: 'async',
256
+ });
257
+ mockContext.canEnqueueDelegation.mockReturnValue(false);
258
+ const result = await SkillDelegateTool.invoke({
259
+ skillName: 'limit-skill',
260
+ objective: 'objective',
261
+ });
262
+ expect(result).toContain('limit reached');
263
+ expect(mockRepository.createTask).not.toHaveBeenCalled();
264
+ });
265
+ });
266
+ });
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Skills System - Public API
3
+ */
4
+ export { SkillRegistry } from './registry.js';
5
+ export { SkillLoader } from './loader.js';
6
+ export { SkillMetadataSchema } from './schema.js';
7
+ export { SkillExecuteTool, SkillDelegateTool, getSkillExecuteDescription, getSkillDelegateDescription, updateSkillToolDescriptions, updateSkillDelegateDescription, // backwards compat
8
+ } from './tool.js';