File size: 7,469 Bytes
933d2c0 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 714ab7d 3bd60b5 c681d98 3bd60b5 714ab7d 3bd60b5 714ab7d c681d98 3bd60b5 c681d98 3bd60b5 714ab7d 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 3bd60b5 c681d98 714ab7d c681d98 3bd60b5 933d2c0 c681d98 | 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | import { test, expect, describe, beforeEach, afterEach, mock } from 'bun:test'
import { WebSearchTool } from '../WebSearchTool'
describe('WebSearchTool', () => {
describe('Tool Properties', () => {
test('should have correct tool name', () => {
expect(WebSearchTool.name).toBe('WebSearch')
})
test('should have correct description', () => {
expect(WebSearchTool.description).toContain('SearXNG')
})
test('should be enabled', () => {
expect(WebSearchTool.isEnabled()).toBe(true)
})
test('should be concurrency safe', () => {
expect(WebSearchTool.isConcurrencySafe()).toBe(true)
})
test('should be read only', () => {
expect(WebSearchTool.isReadOnly()).toBe(true)
})
})
describe('Input Validation', () => {
test('should accept valid query', async () => {
const result = await WebSearchTool.validateInput(
{ query: 'typescript' },
{}
)
expect(result.result).toBe(true)
})
test('should reject empty query', async () => {
const result = await WebSearchTool.validateInput(
{ query: '' },
{}
)
expect(result.result).toBe(false)
})
test('should reject missing input', async () => {
const result = await WebSearchTool.validateInput(
{} as any,
{}
)
expect(result.result).toBe(false)
})
})
describe('Permissions', () => {
test('should allow all web search requests', async () => {
const result = await WebSearchTool.checkPermissions(
{ query: 'typescript' },
{}
)
expect(result.behavior).toBe('allow')
})
})
describe('Tool Call - Successful Search', () => {
test('should perform web search', async () => {
const result = await WebSearchTool.call(
{ query: 'typescript programming' },
{},
() => {},
null
)
expect(result).toBeDefined()
expect(result.data.query).toBe('typescript programming')
expect(Array.isArray(result.data.results)).toBe(true)
expect(result.data.durationSeconds).toBeGreaterThan(0)
}, 60000)
test('should return structured results', async () => {
const result = await WebSearchTool.call(
{ query: 'javascript' },
{},
() => {},
null
)
expect(result.data.results.length).toBeGreaterThanOrEqual(0)
const first = result.data.results[0]
if (first && typeof first !== 'string') {
expect(first).toHaveProperty('tool_use_id')
expect(Array.isArray(first.content)).toBe(true)
}
}, 60000)
})
describe('Tool Call - Error Handling', () => {
test('should handle missing query', async () => {
const result = await WebSearchTool.call(
{ query: '' } as any,
{},
() => {},
null
)
expect(result.data.results.some(r => typeof r === 'string' && r.includes('Error'))).toBe(true)
})
test('should not crash on failure', async () => {
const result = await WebSearchTool.call(
{ query: 'test' },
{},
() => {},
null
)
expect(result).toBeDefined()
}, 60000)
})
describe('Schema Validation', () => {
test('should have valid input schema', () => {
const schema = WebSearchTool.inputSchema
const parsed = schema.safeParse({ query: 'test' })
expect(parsed.success).toBe(true)
})
test('should have valid output schema', () => {
expect(WebSearchTool.outputSchema).toBeDefined()
})
})
describe('Tool Metadata', () => {
test('should provide activity description', () => {
const desc = WebSearchTool.getActivityDescription({
query: 'typescript'
})
expect(desc).toContain('Searching')
})
test('should provide tool use summary', () => {
const summary = WebSearchTool.getToolUseSummary({
query: 'javascript'
})
expect(summary).toBeDefined()
})
test('should return empty search text', () => {
const text = WebSearchTool.extractSearchText?.({
results: ['test']
} as any)
expect(text).toBe('')
})
})
describe('Auto Classifier Input', () => {
test('should format input correctly', () => {
const input = WebSearchTool.toAutoClassifierInput({
query: 'typescript'
})
expect(input).toBe('typescript')
})
})
describe('Tool Result Mapping', () => {
test('should map tool result correctly', () => {
const output = {
query: 'test',
results: [{
tool_use_id: '1',
content: [{
title: 'Test',
url: 'https://example.com',
snippet: 'snippet'
}]
}],
durationSeconds: 1
}
const result = WebSearchTool.mapToolResultToToolResultBlockParam(
output,
'id'
)
expect(result.type).toBe('tool_result')
expect(typeof result.content).toBe('string')
})
test('should handle empty results', () => {
const output = {
query: 'test',
results: [],
durationSeconds: 1
}
const result = WebSearchTool.mapToolResultToToolResultBlockParam(
output,
'id'
)
expect(result.content).toContain('Results')
})
})
describe('SearXNG Integration', () => {
test('should search using SearXNG', async () => {
const result = await WebSearchTool.call(
{ query: 'milet 的最新动态' },
{},
() => {},
null
)
expect(result).toBeDefined()
expect(result.data.query).toBe('milet 的最新动态')
expect(Array.isArray(result.data.results)).toBe(true)
expect(result.data.durationSeconds).toBeGreaterThan(0)
}, 60000)
})
describe('Tavily Integration', () => {
let originalTavilyKey: string | undefined
beforeEach(() => {
originalTavilyKey = process.env.TAVILY_API_KEY
})
afterEach(() => {
if (originalTavilyKey !== undefined) {
process.env.TAVILY_API_KEY = originalTavilyKey
} else {
delete process.env.TAVILY_API_KEY
}
})
test('should use SearXNG when TAVILY_API_KEY is not set', async () => {
delete process.env.TAVILY_API_KEY
const result = await WebSearchTool.call(
{ query: 'test query' },
{},
() => {},
null
)
expect(result).toBeDefined()
expect(result.data.query).toBe('test query')
expect(Array.isArray(result.data.results)).toBe(true)
}, 60000)
test('should use Tavily when TAVILY_API_KEY is set', async () => {
process.env.TAVILY_API_KEY = 'tvly-test-key'
const result = await WebSearchTool.call(
{ query: 'test tavily query' },
{},
() => {},
null
)
// With a fake key, Tavily will fail and we should get an error result
expect(result).toBeDefined()
expect(result.data.query).toBe('test tavily query')
expect(Array.isArray(result.data.results)).toBe(true)
// The result should contain an error since the API key is invalid
const hasError = result.data.results.some(
r => typeof r === 'string' && r.includes('Error')
)
expect(hasError).toBe(true)
}, 60000)
test('should include Tavily in description when key is set', () => {
expect(WebSearchTool.description).toContain('Tavily')
})
})
})
|