| 1 | import 'package:flutter_test/flutter_test.dart'; |
| 2 | import 'package:localmind/core/models/enums.dart'; |
| 3 | import 'package:localmind/features/chat/data/mcp_server_manager.dart'; |
| 4 | import 'package:localmind/features/chat/data/models/message.dart'; |
| 5 | import 'package:localmind/features/chat/data/tools/builtin_tool_provider.dart'; |
| 6 | import 'package:localmind/features/chat/data/tools/mcp_tool_provider.dart'; |
| 7 | import 'package:localmind/features/chat/data/tools/tool_definition.dart'; |
| 8 | import 'package:localmind/features/chat/data/tools/tool_event.dart'; |
| 9 | import 'package:localmind/features/chat/data/tools/tool_registry.dart'; |
| 10 | |
| 11 | void main() { |
| 12 | test('Message preserves toolEvents in copyWith', () { |
| 13 | final msg = Message( |
| 14 | id: 'm1', |
| 15 | conversationId: 'c1', |
| 16 | role: MessageRole.assistant, |
| 17 | content: 'hi', |
| 18 | createdAt: DateTime(2026, 1, 1), |
| 19 | toolEvents: [ |
| 20 | ToolEvent.requested( |
| 21 | eventId: 'e1', |
| 22 | toolName: 'calc.add', |
| 23 | providerType: ToolProviderType.builtIn, |
| 24 | ), |
| 25 | ], |
| 26 | ); |
| 27 | |
| 28 | expect(msg.copyWith().toolEvents!.length, 1); |
| 29 | }); |
| 30 | |
| 31 | test('registry resolves built-in tools with allow-list filtering', () async { |
| 32 | final registry = ToolRegistry(providers: [BuiltInToolProvider()]); |
| 33 | final tools = await registry.listTools(allowedTools: {'calc.add'}); |
| 34 | expect(tools.map((t) => t.name), ['calc.add']); |
| 35 | }); |
| 36 | |
| 37 | test('registry executes tool from correct provider', () async { |
| 38 | final registry = ToolRegistry(providers: [BuiltInToolProvider()]); |
| 39 | final result = await registry.execute('calc.add', {'a': 1, 'b': 2}); |
| 40 | expect(result.success, true); |
| 41 | expect(result.output, '3'); |
| 42 | }); |
| 43 | |
| 44 | test('registry returns failure for unknown tool', () async { |
| 45 | final registry = ToolRegistry(providers: [BuiltInToolProvider()]); |
| 46 | final result = await registry.execute('unknown.tool', {}); |
| 47 | expect(result.success, false); |
| 48 | }); |
| 49 | |
| 50 | test('registry exposes and executes example MCP tools', () async { |
| 51 | final manager = McpServerManager()..addExampleServer(); |
| 52 | final registry = ToolRegistry( |
| 53 | providers: [ |
| 54 | BuiltInToolProvider(), |
| 55 | McpToolProvider(serverManager: manager), |
| 56 | ], |
| 57 | ); |
| 58 | |
| 59 | final tools = await registry.listTools(); |
| 60 | expect(tools.any((tool) => tool.name == 'example.echo'), true); |
| 61 | |
| 62 | final result = await registry.execute('example.word_count', { |
| 63 | 'text': 'MCP tools are available', |
| 64 | }); |
| 65 | expect(result.success, true); |
| 66 | expect(result.output, '4'); |
| 67 | }); |
| 68 | } |