|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 4 | +import { buildToolRoutes } from '../routes/tool-routes.js'; |
| 5 | +import { AIService } from '../ai-service.js'; |
| 6 | +import { InMemoryConversationService } from '../conversation/in-memory-conversation-service.js'; |
| 7 | +import { ToolRegistry } from '../tools/tool-registry.js'; |
| 8 | +import type { Logger } from '@objectstack/spec/contracts'; |
| 9 | + |
| 10 | +const silentLogger: Logger = { |
| 11 | + debug: vi.fn(), |
| 12 | + info: vi.fn(), |
| 13 | + warn: vi.fn(), |
| 14 | + error: vi.fn(), |
| 15 | + fatal: vi.fn(), |
| 16 | +}; |
| 17 | + |
| 18 | +describe('Tool Routes', () => { |
| 19 | + let aiService: AIService; |
| 20 | + let routes: ReturnType<typeof buildToolRoutes>; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + const conversationService = new InMemoryConversationService(); |
| 24 | + aiService = new AIService({ |
| 25 | + adapter: 'memory', |
| 26 | + conversationService, |
| 27 | + }); |
| 28 | + |
| 29 | + // Register a test tool |
| 30 | + aiService.toolRegistry.register({ |
| 31 | + name: 'test_tool', |
| 32 | + description: 'A test tool for playground', |
| 33 | + parameters: { |
| 34 | + type: 'object', |
| 35 | + properties: { |
| 36 | + message: { type: 'string' }, |
| 37 | + }, |
| 38 | + required: ['message'], |
| 39 | + }, |
| 40 | + handler: async (params: any) => { |
| 41 | + return { echo: params.message }; |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + routes = buildToolRoutes(aiService, silentLogger); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('GET /api/v1/ai/tools', () => { |
| 49 | + it('should list all registered tools', async () => { |
| 50 | + const listRoute = routes.find(r => r.method === 'GET' && r.path === '/api/v1/ai/tools'); |
| 51 | + expect(listRoute).toBeDefined(); |
| 52 | + |
| 53 | + const response = await listRoute!.handler({}); |
| 54 | + expect(response.status).toBe(200); |
| 55 | + expect(response.body).toHaveProperty('tools'); |
| 56 | + expect(Array.isArray((response.body as any).tools)).toBe(true); |
| 57 | + |
| 58 | + const tools = (response.body as any).tools; |
| 59 | + expect(tools.length).toBeGreaterThan(0); |
| 60 | + expect(tools.some((t: any) => t.name === 'test_tool')).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should require authentication', () => { |
| 64 | + const listRoute = routes.find(r => r.method === 'GET' && r.path === '/api/v1/ai/tools'); |
| 65 | + expect(listRoute?.auth).toBe(true); |
| 66 | + expect(listRoute?.permissions).toContain('ai:tools'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('POST /api/v1/ai/tools/:toolName/execute', () => { |
| 71 | + it('should execute a tool with parameters', async () => { |
| 72 | + const executeRoute = routes.find( |
| 73 | + r => r.method === 'POST' && r.path === '/api/v1/ai/tools/:toolName/execute' |
| 74 | + ); |
| 75 | + expect(executeRoute).toBeDefined(); |
| 76 | + |
| 77 | + const response = await executeRoute!.handler({ |
| 78 | + params: { toolName: 'test_tool' }, |
| 79 | + body: { |
| 80 | + parameters: { message: 'Hello, Playground!' }, |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(response.status).toBe(200); |
| 85 | + expect(response.body).toHaveProperty('result'); |
| 86 | + expect((response.body as any).result).toEqual({ echo: 'Hello, Playground!' }); |
| 87 | + expect((response.body as any).toolName).toBe('test_tool'); |
| 88 | + expect((response.body as any).duration).toBeTypeOf('number'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return 404 for non-existent tool', async () => { |
| 92 | + const executeRoute = routes.find( |
| 93 | + r => r.method === 'POST' && r.path === '/api/v1/ai/tools/:toolName/execute' |
| 94 | + ); |
| 95 | + |
| 96 | + const response = await executeRoute!.handler({ |
| 97 | + params: { toolName: 'non_existent_tool' }, |
| 98 | + body: { |
| 99 | + parameters: {}, |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(response.status).toBe(404); |
| 104 | + expect((response.body as any).error).toContain('not found'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return 400 when toolName is missing', async () => { |
| 108 | + const executeRoute = routes.find( |
| 109 | + r => r.method === 'POST' && r.path === '/api/v1/ai/tools/:toolName/execute' |
| 110 | + ); |
| 111 | + |
| 112 | + const response = await executeRoute!.handler({ |
| 113 | + body: { |
| 114 | + parameters: {}, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + expect(response.status).toBe(400); |
| 119 | + expect((response.body as any).error).toContain('toolName'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should return 400 when parameters are missing', async () => { |
| 123 | + const executeRoute = routes.find( |
| 124 | + r => r.method === 'POST' && r.path === '/api/v1/ai/tools/:toolName/execute' |
| 125 | + ); |
| 126 | + |
| 127 | + const response = await executeRoute!.handler({ |
| 128 | + params: { toolName: 'test_tool' }, |
| 129 | + body: {}, |
| 130 | + }); |
| 131 | + |
| 132 | + expect(response.status).toBe(400); |
| 133 | + expect((response.body as any).error).toContain('parameters'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should handle tool execution errors', async () => { |
| 137 | + // Register a tool that throws an error |
| 138 | + aiService.toolRegistry.register({ |
| 139 | + name: 'error_tool', |
| 140 | + description: 'A tool that throws an error', |
| 141 | + parameters: { type: 'object', properties: {} }, |
| 142 | + handler: async () => { |
| 143 | + throw new Error('Tool execution failed'); |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + const executeRoute = routes.find( |
| 148 | + r => r.method === 'POST' && r.path === '/api/v1/ai/tools/:toolName/execute' |
| 149 | + ); |
| 150 | + |
| 151 | + const response = await executeRoute!.handler({ |
| 152 | + params: { toolName: 'error_tool' }, |
| 153 | + body: { |
| 154 | + parameters: {}, |
| 155 | + }, |
| 156 | + }); |
| 157 | + |
| 158 | + expect(response.status).toBe(500); |
| 159 | + expect((response.body as any).error).toContain('Tool execution failed'); |
| 160 | + expect((response.body as any).duration).toBeTypeOf('number'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should require authentication and permissions', () => { |
| 164 | + const executeRoute = routes.find( |
| 165 | + r => r.method === 'POST' && r.path === '/api/v1/ai/tools/:toolName/execute' |
| 166 | + ); |
| 167 | + |
| 168 | + expect(executeRoute?.auth).toBe(true); |
| 169 | + expect(executeRoute?.permissions).toContain('ai:tools'); |
| 170 | + expect(executeRoute?.permissions).toContain('ai:execute'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('Route Configuration', () => { |
| 175 | + it('should register exactly 2 routes', () => { |
| 176 | + expect(routes).toHaveLength(2); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should have descriptive route descriptions', () => { |
| 180 | + routes.forEach(route => { |
| 181 | + expect(route.description).toBeTruthy(); |
| 182 | + expect(route.description.length).toBeGreaterThan(10); |
| 183 | + }); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments