Download src/tools/WebSearchTool/__tests__/WebSearchTool.test.ts from chenbhao/codev: direct link, hf CLI and curl.
- Browser
- Download file 7.47 kB
-
https://huggingface.co/chenbhao/codev/resolve/a67bf89af8b8419ed306cb6932cdb48d39ee4446/src/tools/WebSearchTool/__tests__/WebSearchTool.test.ts
- Command line
-
hf download hf://chenbhao/codev@a67bf89af8b8419ed306cb6932cdb48d39ee4446/src/tools/WebSearchTool/__tests__/WebSearchTool.test.ts
-
curl -L -o WebSearchTool.test.ts https://huggingface.co/chenbhao/codev/resolve/a67bf89af8b8419ed306cb6932cdb48d39ee4446/src/tools/WebSearchTool/__tests__/WebSearchTool.test.ts
7.47 kB
| 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') | |
| }) | |
| }) | |
| }) | |