|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + SkillSchema, |
| 4 | + SkillTriggerConditionSchema, |
| 5 | + defineSkill, |
| 6 | + type Skill, |
| 7 | +} from './skill.zod'; |
| 8 | + |
| 9 | +describe('SkillTriggerConditionSchema', () => { |
| 10 | + it('should accept all operators', () => { |
| 11 | + const operators = ['eq', 'neq', 'in', 'not_in', 'contains'] as const; |
| 12 | + |
| 13 | + operators.forEach(operator => { |
| 14 | + expect(() => SkillTriggerConditionSchema.parse({ |
| 15 | + field: 'objectName', |
| 16 | + operator, |
| 17 | + value: 'support_case', |
| 18 | + })).not.toThrow(); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should accept array value for in/not_in', () => { |
| 23 | + const result = SkillTriggerConditionSchema.parse({ |
| 24 | + field: 'userRole', |
| 25 | + operator: 'in', |
| 26 | + value: ['admin', 'support_agent'], |
| 27 | + }); |
| 28 | + expect(result.value).toEqual(['admin', 'support_agent']); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should accept string value', () => { |
| 32 | + const result = SkillTriggerConditionSchema.parse({ |
| 33 | + field: 'channel', |
| 34 | + operator: 'eq', |
| 35 | + value: 'web', |
| 36 | + }); |
| 37 | + expect(result.value).toBe('web'); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('SkillSchema', () => { |
| 42 | + it('should accept minimal skill', () => { |
| 43 | + const skill: Skill = { |
| 44 | + name: 'case_management', |
| 45 | + label: 'Case Management', |
| 46 | + tools: ['create_case', 'update_case', 'resolve_case'], |
| 47 | + }; |
| 48 | + |
| 49 | + const result = SkillSchema.parse(skill); |
| 50 | + expect(result.name).toBe('case_management'); |
| 51 | + expect(result.active).toBe(true); |
| 52 | + expect(result.tools).toHaveLength(3); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should accept full skill', () => { |
| 56 | + const skill = { |
| 57 | + name: 'order_management', |
| 58 | + label: 'Order Management', |
| 59 | + description: 'Handles order lifecycle operations', |
| 60 | + instructions: 'Use these tools to manage customer orders. Always verify order ownership first.', |
| 61 | + tools: ['create_order', 'update_order', 'cancel_order', 'query_orders'], |
| 62 | + triggerPhrases: ['place an order', 'cancel my order', 'check order status'], |
| 63 | + triggerConditions: [ |
| 64 | + { field: 'objectName', operator: 'eq' as const, value: 'order' }, |
| 65 | + { field: 'userRole', operator: 'in' as const, value: ['sales', 'support'] }, |
| 66 | + ], |
| 67 | + permissions: ['order.manage', 'order.view'], |
| 68 | + active: true, |
| 69 | + }; |
| 70 | + |
| 71 | + const result = SkillSchema.parse(skill); |
| 72 | + expect(result.name).toBe('order_management'); |
| 73 | + expect(result.tools).toHaveLength(4); |
| 74 | + expect(result.triggerPhrases).toHaveLength(3); |
| 75 | + expect(result.triggerConditions).toHaveLength(2); |
| 76 | + expect(result.permissions).toEqual(['order.manage', 'order.view']); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should enforce snake_case for skill name', () => { |
| 80 | + const validNames = ['case_management', 'order_ops', '_internal', 'knowledge_search']; |
| 81 | + validNames.forEach(name => { |
| 82 | + expect(() => SkillSchema.parse({ |
| 83 | + name, |
| 84 | + label: 'Test', |
| 85 | + tools: [], |
| 86 | + })).not.toThrow(); |
| 87 | + }); |
| 88 | + |
| 89 | + const invalidNames = ['caseManagement', 'Order-Ops', '123skill']; |
| 90 | + invalidNames.forEach(name => { |
| 91 | + expect(() => SkillSchema.parse({ |
| 92 | + name, |
| 93 | + label: 'Test', |
| 94 | + tools: [], |
| 95 | + })).toThrow(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should accept empty tools array', () => { |
| 100 | + const result = SkillSchema.parse({ |
| 101 | + name: 'empty_skill', |
| 102 | + label: 'Empty Skill', |
| 103 | + tools: [], |
| 104 | + }); |
| 105 | + expect(result.tools).toHaveLength(0); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should accept skill with instructions', () => { |
| 109 | + const result = SkillSchema.parse({ |
| 110 | + name: 'knowledge_search', |
| 111 | + label: 'Knowledge Search', |
| 112 | + instructions: 'Search the knowledge base before escalating to a human agent.', |
| 113 | + tools: ['search_knowledge', 'get_article'], |
| 114 | + }); |
| 115 | + expect(result.instructions).toContain('knowledge base'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should enforce snake_case for tool name references', () => { |
| 119 | + expect(() => SkillSchema.parse({ |
| 120 | + name: 'valid_skill', |
| 121 | + label: 'Test', |
| 122 | + tools: ['valid_tool', 'another_tool'], |
| 123 | + })).not.toThrow(); |
| 124 | + |
| 125 | + expect(() => SkillSchema.parse({ |
| 126 | + name: 'valid_skill', |
| 127 | + label: 'Test', |
| 128 | + tools: ['InvalidTool'], |
| 129 | + })).toThrow(); |
| 130 | + |
| 131 | + expect(() => SkillSchema.parse({ |
| 132 | + name: 'valid_skill', |
| 133 | + label: 'Test', |
| 134 | + tools: ['valid_tool', 'Invalid-Tool'], |
| 135 | + })).toThrow(); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('defineSkill', () => { |
| 140 | + it('should return a parsed skill', () => { |
| 141 | + const skill = defineSkill({ |
| 142 | + name: 'case_management', |
| 143 | + label: 'Case Management', |
| 144 | + description: 'Handles support case lifecycle', |
| 145 | + instructions: 'Use these tools to create, update, and resolve support cases.', |
| 146 | + tools: ['create_case', 'update_case', 'resolve_case'], |
| 147 | + triggerPhrases: ['create a case', 'open a ticket'], |
| 148 | + }); |
| 149 | + |
| 150 | + expect(skill.name).toBe('case_management'); |
| 151 | + expect(skill.tools).toHaveLength(3); |
| 152 | + expect(skill.active).toBe(true); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should apply defaults', () => { |
| 156 | + const skill = defineSkill({ |
| 157 | + name: 'simple_skill', |
| 158 | + label: 'Simple', |
| 159 | + tools: ['tool_a'], |
| 160 | + }); |
| 161 | + |
| 162 | + expect(skill.active).toBe(true); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should throw on invalid skill name', () => { |
| 166 | + expect(() => defineSkill({ |
| 167 | + name: 'InvalidName', |
| 168 | + label: 'Test', |
| 169 | + tools: [], |
| 170 | + })).toThrow(); |
| 171 | + }); |
| 172 | +}); |
0 commit comments