120df903d2
- 前端: 添加 SSE 流式支持、stateSnapshot 事件、路由导航工具 - 前端: 实现工具调用审批流程,支持 pending 状态展示 - 后端: Agent 状态管理与会话持久化相关重构 - 文档: 新增 agent-agui-full-alignance 设计文档 - 测试: 补充相关单元测试和集成测试
133 lines
3.8 KiB
Dart
133 lines
3.8 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:social_app/features/chat/data/tools/route_navigation_tool.dart';
|
|
import 'package:social_app/features/chat/data/tools/tool_registry.dart';
|
|
|
|
void main() {
|
|
setUp(() {
|
|
ToolRegistry.initialize();
|
|
});
|
|
|
|
tearDown(() {
|
|
RouteNavigationTool.instance.clearNavigator();
|
|
});
|
|
|
|
group('getTool', () {
|
|
test('returns tool definition for create_calendar_event', () {
|
|
final tool = ToolRegistry.getTool('create_calendar_event');
|
|
|
|
expect(tool, isNotNull);
|
|
expect(tool!.name, 'create_calendar_event');
|
|
expect(tool.description, isNotEmpty);
|
|
});
|
|
|
|
test('returns null for unknown tool', () {
|
|
expect(ToolRegistry.getTool('unknown_tool'), isNull);
|
|
});
|
|
});
|
|
|
|
group('validateArgs', () {
|
|
test('returns error for empty args (missing title)', () {
|
|
final result = ToolRegistry.validateArgs('create_calendar_event', {});
|
|
|
|
expect(result.ok, false);
|
|
expect(result.error, contains('title'));
|
|
});
|
|
|
|
test('returns error when missing startAt', () {
|
|
final result = ToolRegistry.validateArgs('create_calendar_event', {
|
|
'title': 'Test Event',
|
|
});
|
|
|
|
expect(result.ok, false);
|
|
expect(result.error, contains('startAt'));
|
|
});
|
|
|
|
test('returns ok: true for valid args with title and startAt', () {
|
|
final result = ToolRegistry.validateArgs('create_calendar_event', {
|
|
'title': 'x',
|
|
'startAt': 'x',
|
|
});
|
|
|
|
expect(result.ok, true);
|
|
expect(result.error, isNull);
|
|
});
|
|
|
|
test('returns error for unknown tool', () {
|
|
final result = ToolRegistry.validateArgs('unknown_tool', {});
|
|
|
|
expect(result.ok, false);
|
|
expect(result.error, contains('Tool not found'));
|
|
});
|
|
});
|
|
|
|
group('execute', () {
|
|
test('returns eventId on success', () async {
|
|
final result = await ToolRegistry.execute('create_calendar_event', {
|
|
'title': 'Test Meeting',
|
|
'startAt': '2026-03-01T10:00:00Z',
|
|
});
|
|
|
|
expect(result['eventId'], isNotNull);
|
|
expect(result['ok'], true);
|
|
expect(result['title'], 'Test Meeting');
|
|
});
|
|
|
|
test('throws ToolNotFoundException for unknown tool', () async {
|
|
expect(
|
|
() => ToolRegistry.execute('unknown_tool', {}),
|
|
throwsA(isA<ToolNotFoundException>()),
|
|
);
|
|
});
|
|
|
|
test('includes optional fields in result', () async {
|
|
final result = await ToolRegistry.execute('create_calendar_event', {
|
|
'title': 'Test',
|
|
'startAt': '2026-03-01T10:00:00Z',
|
|
'description': 'Description',
|
|
'location': 'Room A',
|
|
'endAt': '2026-03-01T11:00:00Z',
|
|
});
|
|
|
|
expect(result['description'], 'Description');
|
|
expect(result['location'], 'Room A');
|
|
expect(result['endAt'], '2026-03-01T11:00:00Z');
|
|
});
|
|
|
|
test('navigate_to_route rejects disallowed target', () async {
|
|
final result = await ToolRegistry.execute('navigate_to_route', {
|
|
'target': '/admin',
|
|
});
|
|
|
|
expect(result['ok'], false);
|
|
expect(result['error'], contains('not allowed'));
|
|
});
|
|
|
|
test('navigate_to_route executes allowed target when navigator is bound', () async {
|
|
String? navigatedTo;
|
|
bool replaced = false;
|
|
RouteNavigationTool.instance.bindNavigator((target, {replace = false}) {
|
|
navigatedTo = target;
|
|
replaced = replace;
|
|
});
|
|
|
|
final result = await ToolRegistry.execute('navigate_to_route', {
|
|
'target': '/settings',
|
|
'replace': true,
|
|
});
|
|
|
|
expect(result['ok'], true);
|
|
expect(navigatedTo, '/settings');
|
|
expect(replaced, true);
|
|
});
|
|
});
|
|
|
|
group('getAllTools', () {
|
|
test('returns list of tool definitions', () {
|
|
final tools = ToolRegistry.getAllTools();
|
|
|
|
expect(tools, isNotEmpty);
|
|
expect(tools.any((t) => t.name == 'create_calendar_event'), true);
|
|
});
|
|
});
|
|
}
|