|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, it } from 'vitest'; |
| 8 | +import { |
| 9 | + buildToolRegistry, |
| 10 | + resolveToolName, |
| 11 | + getToolsByCategory, |
| 12 | + type ToolCategory, |
| 13 | +} from '../utils/tool-registry.js'; |
| 14 | + |
| 15 | +describe('tool-registry', () => { |
| 16 | + const registry = buildToolRegistry(); |
| 17 | + |
| 18 | + describe('buildToolRegistry', () => { |
| 19 | + it('includes all canonical built-in tools', () => { |
| 20 | + expect(registry.totalTools).toBeGreaterThanOrEqual(26); |
| 21 | + }); |
| 22 | + |
| 23 | + it('every tool has a valid category', () => { |
| 24 | + for (const [name, entry] of registry.tools) { |
| 25 | + expect(entry.category).toBeTruthy(); |
| 26 | + expect(entry.name).toBe(name); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + it('byCategory entries match tools map', () => { |
| 31 | + let categoryTotal = 0; |
| 32 | + for (const [, entries] of registry.byCategory) { |
| 33 | + for (const entry of entries) { |
| 34 | + expect(registry.tools.get(entry.name)).toBe(entry); |
| 35 | + } |
| 36 | + categoryTotal += entries.length; |
| 37 | + } |
| 38 | + expect(categoryTotal).toBe(registry.totalTools); |
| 39 | + }); |
| 40 | + |
| 41 | + it('aliasLookup covers every canonical name', () => { |
| 42 | + for (const name of registry.tools.keys()) { |
| 43 | + expect(registry.aliasLookup.get(name)).toBe(name); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it('aliasLookup covers every legacy alias', () => { |
| 48 | + for (const [, entry] of registry.tools) { |
| 49 | + for (const alias of entry.aliases) { |
| 50 | + expect(registry.aliasLookup.get(alias)).toBe(entry.name); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('is deterministic across calls', () => { |
| 56 | + const second = buildToolRegistry(); |
| 57 | + expect([...second.tools.keys()]).toEqual([...registry.tools.keys()]); |
| 58 | + expect(second.totalTools).toBe(registry.totalTools); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('resolveToolName', () => { |
| 63 | + it('resolves canonical names to themselves', () => { |
| 64 | + expect(resolveToolName(registry, 'grep_search')).toBe('grep_search'); |
| 65 | + expect(resolveToolName(registry, 'run_shell_command')).toBe( |
| 66 | + 'run_shell_command', |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('resolves legacy alias to canonical name', () => { |
| 71 | + expect(resolveToolName(registry, 'search_file_content')).toBe( |
| 72 | + 'grep_search', |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns undefined for unknown tool names', () => { |
| 77 | + expect(resolveToolName(registry, 'nonexistent_tool')).toBeUndefined(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns undefined for empty string', () => { |
| 81 | + expect(resolveToolName(registry, '')).toBeUndefined(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('getToolsByCategory', () => { |
| 86 | + it('returns file-system tools', () => { |
| 87 | + const tools = getToolsByCategory(registry, 'file-system'); |
| 88 | + const names = tools.map((t) => t.name); |
| 89 | + expect(names).toContain('glob'); |
| 90 | + expect(names).toContain('grep_search'); |
| 91 | + expect(names).toContain('read_file'); |
| 92 | + expect(names).toContain('write_file'); |
| 93 | + expect(names).toContain('replace'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns task-tracker tools', () => { |
| 97 | + const tools = getToolsByCategory(registry, 'task-tracker'); |
| 98 | + const names = tools.map((t) => t.name); |
| 99 | + expect(names).toContain('tracker_create_task'); |
| 100 | + expect(names).toContain('tracker_update_task'); |
| 101 | + expect(names).toContain('tracker_get_task'); |
| 102 | + expect(names).toContain('tracker_list_tasks'); |
| 103 | + expect(names).toContain('tracker_add_dependency'); |
| 104 | + expect(names).toContain('tracker_visualize'); |
| 105 | + expect(names).toHaveLength(6); |
| 106 | + }); |
| 107 | + |
| 108 | + it('returns agent tools', () => { |
| 109 | + const tools = getToolsByCategory(registry, 'agent'); |
| 110 | + const names = tools.map((t) => t.name); |
| 111 | + expect(names).toContain('invoke_agent'); |
| 112 | + expect(names).toContain('complete_task'); |
| 113 | + expect(names).toContain('update_topic'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('returns empty array for unknown category', () => { |
| 117 | + expect( |
| 118 | + getToolsByCategory(registry, 'nonexistent' as ToolCategory), |
| 119 | + ).toEqual([]); |
| 120 | + }); |
| 121 | + |
| 122 | + it('every defined category has at least one tool', () => { |
| 123 | + const expectedCategories: ToolCategory[] = [ |
| 124 | + 'file-system', |
| 125 | + 'shell', |
| 126 | + 'web', |
| 127 | + 'planning', |
| 128 | + 'user-interaction', |
| 129 | + 'skills', |
| 130 | + 'task-tracker', |
| 131 | + 'agent', |
| 132 | + 'mcp', |
| 133 | + ]; |
| 134 | + for (const cat of expectedCategories) { |
| 135 | + expect(getToolsByCategory(registry, cat).length).toBeGreaterThan(0); |
| 136 | + } |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments