-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtool.test.ts
More file actions
176 lines (159 loc) · 4.72 KB
/
Copy pathtool.test.ts
File metadata and controls
176 lines (159 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { describe, it, expect } from 'vitest';
import {
ToolSchema,
ToolCategorySchema,
defineTool,
type Tool,
} from './tool.zod';
describe('ToolCategorySchema', () => {
it('should accept all tool categories', () => {
const categories = ['data', 'action', 'flow', 'integration', 'vector_search', 'analytics', 'utility'] as const;
categories.forEach(category => {
expect(ToolCategorySchema.parse(category)).toBe(category);
});
});
it('should reject invalid category', () => {
expect(() => ToolCategorySchema.parse('unknown')).toThrow();
});
});
describe('ToolSchema', () => {
it('should accept minimal tool', () => {
const tool: Tool = {
name: 'list_records',
label: 'List Records',
description: 'List records from a data object',
parameters: {
type: 'object',
properties: {
objectName: { type: 'string' },
},
required: ['objectName'],
},
};
const result = ToolSchema.parse(tool);
expect(result.name).toBe('list_records');
expect(result.active).toBe(true);
expect(result.builtIn).toBe(false);
expect(result.requiresConfirmation).toBe(false);
});
it('should accept full tool', () => {
const tool = {
name: 'create_case',
label: 'Create Support Case',
description: 'Creates a new support case record',
category: 'action' as const,
parameters: {
type: 'object',
properties: {
subject: { type: 'string', description: 'Case subject' },
priority: { type: 'string', enum: ['low', 'medium', 'high'] },
},
required: ['subject'],
},
outputSchema: {
type: 'object',
properties: {
id: { type: 'string' },
caseNumber: { type: 'string' },
},
},
objectName: 'support_case',
requiresConfirmation: true,
permissions: ['case.create', 'support.agent'],
active: true,
builtIn: false,
};
const result = ToolSchema.parse(tool);
expect(result.name).toBe('create_case');
expect(result.category).toBe('action');
expect(result.objectName).toBe('support_case');
expect(result.requiresConfirmation).toBe(true);
expect(result.permissions).toEqual(['case.create', 'support.agent']);
});
it('should enforce snake_case for tool name', () => {
const validNames = ['list_records', 'create_case', '_internal_tool', 'query_orders'];
validNames.forEach(name => {
expect(() => ToolSchema.parse({
name,
label: 'Test',
description: 'Test',
parameters: {},
})).not.toThrow();
});
const invalidNames = ['listRecords', 'Create-Case', '123tool'];
invalidNames.forEach(name => {
expect(() => ToolSchema.parse({
name,
label: 'Test',
description: 'Test',
parameters: {},
})).toThrow();
});
});
it('should enforce snake_case for objectName', () => {
expect(() => ToolSchema.parse({
name: 'test_tool',
label: 'Test',
description: 'Test',
parameters: {},
objectName: 'supportCase',
})).toThrow();
expect(() => ToolSchema.parse({
name: 'test_tool',
label: 'Test',
description: 'Test',
parameters: {},
objectName: 'support_case',
})).not.toThrow();
});
it('should accept built-in tool flag', () => {
const tool = ToolSchema.parse({
name: 'describe_object',
label: 'Describe Object',
description: 'Get object schema and field metadata',
parameters: { type: 'object', properties: { objectName: { type: 'string' } } },
builtIn: true,
});
expect(tool.builtIn).toBe(true);
});
});
describe('defineTool', () => {
it('should return a parsed tool', () => {
const tool = defineTool({
name: 'query_records',
label: 'Query Records',
description: 'Search and filter records',
category: 'data',
parameters: {
type: 'object',
properties: {
objectName: { type: 'string' },
filters: { type: 'object' },
},
required: ['objectName'],
},
});
expect(tool.name).toBe('query_records');
expect(tool.category).toBe('data');
expect(tool.active).toBe(true);
});
it('should apply defaults', () => {
const tool = defineTool({
name: 'simple_tool',
label: 'Simple Tool',
description: 'A simple tool',
parameters: {},
});
expect(tool.active).toBe(true);
expect(tool.builtIn).toBe(false);
expect(tool.requiresConfirmation).toBe(false);
});
it('should throw on invalid tool name', () => {
expect(() => defineTool({
name: 'InvalidName',
label: 'Test',
description: 'Test',
parameters: {},
})).toThrow();
});
});