feat: 添加自动化任务(automation_jobs)功能模块
This commit is contained in:
@@ -1,173 +0,0 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:social_app/features/chat/data/ai/ai_decision_engine.dart';
|
||||
|
||||
void main() {
|
||||
late AiDecisionEngine engine;
|
||||
|
||||
setUp(() {
|
||||
engine = AiDecisionEngine();
|
||||
});
|
||||
|
||||
group('matchIntent', () {
|
||||
test('returns searchEvent for "今天有什么日程"', () {
|
||||
expect(engine.matchIntent('今天有什么日程'), Intent.searchEvent);
|
||||
});
|
||||
|
||||
test('returns searchEvent for "查看日程"', () {
|
||||
expect(engine.matchIntent('查看日程'), Intent.searchEvent);
|
||||
});
|
||||
|
||||
test('returns searchEvent for "查询安排"', () {
|
||||
expect(engine.matchIntent('查询安排'), Intent.searchEvent);
|
||||
});
|
||||
|
||||
test('returns createEvent for "提醒我明天开会"', () {
|
||||
expect(engine.matchIntent('提醒我明天开会'), Intent.createEvent);
|
||||
});
|
||||
|
||||
test('returns createEvent for "安排时间"', () {
|
||||
expect(engine.matchIntent('安排时间'), Intent.createEvent);
|
||||
});
|
||||
|
||||
test('returns createEvent for time pattern "明天10点"', () {
|
||||
expect(engine.matchIntent('明天10点'), Intent.createEvent);
|
||||
});
|
||||
|
||||
test('returns unknown for "你好"', () {
|
||||
expect(engine.matchIntent('你好'), Intent.unknown);
|
||||
});
|
||||
|
||||
test('returns unknown for random text', () {
|
||||
expect(engine.matchIntent('随便说点什么'), Intent.unknown);
|
||||
});
|
||||
});
|
||||
|
||||
group('shouldTriggerToolCall', () {
|
||||
test('returns false for "你好"', () {
|
||||
expect(engine.shouldTriggerToolCall('你好'), false);
|
||||
});
|
||||
|
||||
test('returns false for search intent', () {
|
||||
expect(engine.shouldTriggerToolCall('今天有什么日程'), false);
|
||||
});
|
||||
|
||||
test('returns true for create event intent', () {
|
||||
expect(engine.shouldTriggerToolCall('提醒我明天开会'), true);
|
||||
});
|
||||
|
||||
test('returns true for time pattern', () {
|
||||
expect(engine.shouldTriggerToolCall('明天10点开会'), true);
|
||||
});
|
||||
});
|
||||
|
||||
group('tryExtractEventArgs', () {
|
||||
test('returns map with title and startAt for "提醒我明天10点开会"', () {
|
||||
final result = engine.tryExtractEventArgs('提醒我明天10点开会');
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!['title'], isNotNull);
|
||||
expect(result['startAt'], isNotNull);
|
||||
expect(result['timezone'], 'Asia/Shanghai');
|
||||
});
|
||||
|
||||
test('returns null for "你好"', () {
|
||||
expect(engine.tryExtractEventArgs('你好'), isNull);
|
||||
});
|
||||
|
||||
test('returns null for search intent', () {
|
||||
expect(engine.tryExtractEventArgs('今天有什么日程'), isNull);
|
||||
});
|
||||
|
||||
test('extracts title correctly', () {
|
||||
final result = engine.tryExtractEventArgs('提醒我开会明天10点');
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!['title'], contains('开会'));
|
||||
});
|
||||
|
||||
test('parses today time correctly', () {
|
||||
final result = engine.tryExtractEventArgs('开会今天14:30');
|
||||
final now = DateTime.now();
|
||||
|
||||
expect(result, isNotNull);
|
||||
final startAt = DateTime.parse(result!['startAt'] as String);
|
||||
expect(startAt.year, now.year);
|
||||
expect(startAt.month, now.month);
|
||||
expect(startAt.day, now.day);
|
||||
expect(startAt.hour, 14);
|
||||
expect(startAt.minute, 30);
|
||||
});
|
||||
|
||||
test('parses tomorrow time correctly', () {
|
||||
final result = engine.tryExtractEventArgs('开会明天9点');
|
||||
final now = DateTime.now();
|
||||
final expectedTomorrow = DateTime(now.year, now.month, now.day + 1);
|
||||
|
||||
expect(result, isNotNull);
|
||||
final startAt = DateTime.parse(result!['startAt'] as String);
|
||||
expect(startAt.day, equals(expectedTomorrow.day));
|
||||
expect(startAt.hour, 9);
|
||||
expect(startAt.minute, 0);
|
||||
});
|
||||
});
|
||||
|
||||
group('tryForceTrigger', () {
|
||||
test(
|
||||
'returns ForceTriggerResult for "#tool:front.navigate_to_route {}"',
|
||||
() {
|
||||
final result = engine.tryForceTrigger(
|
||||
'#tool:front.navigate_to_route {}',
|
||||
);
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!.toolName, 'front.navigate_to_route');
|
||||
expect(result.args, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'returns ForceTriggerResult with args for "#tool:custom {"key": "value"}"',
|
||||
() {
|
||||
final result = engine.tryForceTrigger('#tool:custom {"key": "value"}');
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!.toolName, 'custom');
|
||||
expect(result.args['key'], 'value');
|
||||
},
|
||||
);
|
||||
|
||||
test('returns null for normal text', () {
|
||||
expect(engine.tryForceTrigger('普通文本'), isNull);
|
||||
});
|
||||
|
||||
test('returns null for empty string', () {
|
||||
expect(engine.tryForceTrigger(''), isNull);
|
||||
});
|
||||
|
||||
test('handles invalid JSON gracefully', () {
|
||||
final result = engine.tryForceTrigger('#tool:test {invalid json}');
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!.toolName, 'test');
|
||||
expect(result.args, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('getToolCallArgs', () {
|
||||
test('returns args for create event intent', () {
|
||||
final result = engine.getToolCallArgs('提醒我明天10点开会');
|
||||
|
||||
expect(result, isNotNull);
|
||||
expect(result!['title'], isNotNull);
|
||||
expect(result['startAt'], isNotNull);
|
||||
});
|
||||
|
||||
test('returns null for non-create intent', () {
|
||||
expect(engine.getToolCallArgs('你好'), isNull);
|
||||
});
|
||||
|
||||
test('returns null for search intent', () {
|
||||
expect(engine.getToolCallArgs('今天有什么日程'), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
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 front.navigate_to_route', () {
|
||||
final tool = ToolRegistry.getTool('front.navigate_to_route');
|
||||
|
||||
expect(tool, isNotNull);
|
||||
expect(tool!.name, 'front.navigate_to_route');
|
||||
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 target)', () {
|
||||
final result = ToolRegistry.validateArgs('front.navigate_to_route', {});
|
||||
|
||||
expect(result.ok, false);
|
||||
expect(result.error, contains('target'));
|
||||
});
|
||||
|
||||
test('returns ok: true for valid args', () {
|
||||
final result = ToolRegistry.validateArgs('front.navigate_to_route', {
|
||||
'target': '/settings',
|
||||
});
|
||||
|
||||
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('throws ToolNotFoundException for unknown tool', () async {
|
||||
expect(
|
||||
() => ToolRegistry.execute('unknown_tool', {}),
|
||||
throwsA(isA<ToolNotFoundException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('front.navigate_to_route rejects disallowed target', () async {
|
||||
final result = await ToolRegistry.execute('front.navigate_to_route', {
|
||||
'target': '/admin',
|
||||
});
|
||||
|
||||
expect(result['ok'], false);
|
||||
expect(result['error'], contains('not allowed'));
|
||||
});
|
||||
|
||||
test(
|
||||
'front.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('front.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 == 'front.navigate_to_route'), true);
|
||||
expect(tools.any((t) => t.name == 'create_calendar_event'), false);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:social_app/features/chat/data/models/chat_list_item.dart';
|
||||
import 'package:social_app/features/home/ui/widgets/home_chat_item_renderer.dart';
|
||||
|
||||
void main() {
|
||||
ToolCallItem _toolCallItem(String toolName) {
|
||||
return ToolCallItem(
|
||||
id: 'tc-1',
|
||||
callId: 'tc-1',
|
||||
toolName: toolName,
|
||||
args: const {},
|
||||
status: ToolCallStatus.pending,
|
||||
timestamp: DateTime(2026, 1, 1),
|
||||
sender: MessageSender.ai,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _pumpToolCallItem(WidgetTester tester, String toolName) async {
|
||||
final widget = MaterialApp(
|
||||
home: Scaffold(body: HomeChatItemRenderer.build(_toolCallItem(toolName))),
|
||||
);
|
||||
await tester.pumpWidget(widget);
|
||||
}
|
||||
|
||||
group('HomeChatItemRenderer tool name localization', () {
|
||||
testWidgets('renders dot style name in Chinese', (tester) async {
|
||||
await _pumpToolCallItem(tester, 'memory.write');
|
||||
expect(find.text('写入记忆'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders snake style alias in Chinese', (tester) async {
|
||||
await _pumpToolCallItem(tester, 'memory_write');
|
||||
expect(find.text('写入记忆'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('falls back to raw name for unknown tool', (tester) async {
|
||||
await _pumpToolCallItem(tester, 'unknown.tool');
|
||||
expect(find.text('unknown.tool'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:social_app/features/settings/data/models/automation_job_model.dart';
|
||||
|
||||
void main() {
|
||||
group('MessageContextConfigModel', () {
|
||||
test('fromJson parses all fields correctly', () {
|
||||
final json = {
|
||||
'source': 'messages',
|
||||
'window_mode': 'week',
|
||||
'window_count': 5,
|
||||
};
|
||||
|
||||
final model = MessageContextConfigModel.fromJson(json);
|
||||
|
||||
expect(model.source, 'messages');
|
||||
expect(model.windowMode, 'week');
|
||||
expect(model.windowCount, 5);
|
||||
});
|
||||
|
||||
test('fromJson uses defaults for missing fields', () {
|
||||
final model = MessageContextConfigModel.fromJson(null);
|
||||
|
||||
expect(model.source, 'latest_chat');
|
||||
expect(model.windowMode, 'day');
|
||||
expect(model.windowCount, 2);
|
||||
});
|
||||
|
||||
test('toJson serializes correctly', () {
|
||||
final model = MessageContextConfigModel(
|
||||
source: 'messages',
|
||||
windowMode: 'week',
|
||||
windowCount: 5,
|
||||
);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
expect(json['source'], 'messages');
|
||||
expect(json['window_mode'], 'week');
|
||||
expect(json['window_count'], 5);
|
||||
});
|
||||
});
|
||||
|
||||
group('AutomationJobConfigModel', () {
|
||||
test('fromJson parses all fields correctly', () {
|
||||
final json = {
|
||||
'input_template': 'Hello {{name}}',
|
||||
'enabled_tools': ['tool1', 'tool2'],
|
||||
'context': {
|
||||
'source': 'messages',
|
||||
'window_mode': 'week',
|
||||
'window_count': 5,
|
||||
},
|
||||
};
|
||||
|
||||
final model = AutomationJobConfigModel.fromJson(json);
|
||||
|
||||
expect(model.inputTemplate, 'Hello {{name}}');
|
||||
expect(model.enabledTools, ['tool1', 'tool2']);
|
||||
expect(model.context.source, 'messages');
|
||||
expect(model.context.windowMode, 'week');
|
||||
expect(model.context.windowCount, 5);
|
||||
});
|
||||
|
||||
test('fromJson uses defaults for null input', () {
|
||||
final model = AutomationJobConfigModel.fromJson(null);
|
||||
|
||||
expect(model.inputTemplate, '');
|
||||
expect(model.enabledTools, []);
|
||||
expect(model.context.source, 'latest_chat');
|
||||
expect(model.context.windowMode, 'day');
|
||||
expect(model.context.windowCount, 2);
|
||||
});
|
||||
});
|
||||
|
||||
group('AutomationJobModel', () {
|
||||
test('fromJson parses all fields correctly', () {
|
||||
final json = {
|
||||
'id': 'job-123',
|
||||
'owner_id': 'user-456',
|
||||
'bootstrap_key': 'key-789',
|
||||
'title': 'Daily Report',
|
||||
'schedule_type': 'DAILY',
|
||||
'run_at': '09:00:00',
|
||||
'timezone': 'America/New_York',
|
||||
'status': 'ACTIVE',
|
||||
'is_system': false,
|
||||
'config': {
|
||||
'input_template': 'Hello',
|
||||
'enabled_tools': ['tool1'],
|
||||
'context': {
|
||||
'source': 'latest_chat',
|
||||
'window_mode': 'day',
|
||||
'window_count': 2,
|
||||
},
|
||||
},
|
||||
'next_run_at': '2024-01-15T09:00:00Z',
|
||||
'last_run_at': '2024-01-14T09:00:00Z',
|
||||
'created_at': '2024-01-01T00:00:00Z',
|
||||
'updated_at': '2024-01-14T12:00:00Z',
|
||||
};
|
||||
|
||||
final model = AutomationJobModel.fromJson(json);
|
||||
|
||||
expect(model.id, 'job-123');
|
||||
expect(model.ownerId, 'user-456');
|
||||
expect(model.bootstrapKey, 'key-789');
|
||||
expect(model.title, 'Daily Report');
|
||||
expect(model.scheduleType, 'DAILY');
|
||||
expect(model.runAt, '09:00:00');
|
||||
expect(model.timezone, 'America/New_York');
|
||||
expect(model.status, 'ACTIVE');
|
||||
expect(model.isSystem, false);
|
||||
expect(model.config.inputTemplate, 'Hello');
|
||||
expect(model.config.enabledTools, ['tool1']);
|
||||
expect(model.config.context.windowCount, 2);
|
||||
expect(model.nextRunAt, DateTime.parse('2024-01-15T09:00:00Z'));
|
||||
expect(model.lastRunAt, DateTime.parse('2024-01-14T09:00:00Z'));
|
||||
expect(model.createdAt, DateTime.parse('2024-01-01T00:00:00Z'));
|
||||
expect(model.updatedAt, DateTime.parse('2024-01-14T12:00:00Z'));
|
||||
});
|
||||
|
||||
test('fromJson throws for missing required date fields', () {
|
||||
final json = <String, dynamic>{
|
||||
'id': 'job-123',
|
||||
'owner_id': 'user-456',
|
||||
'title': 'Test',
|
||||
'schedule_type': 'DAILY',
|
||||
'run_at': '09:00:00',
|
||||
'timezone': 'UTC',
|
||||
'status': 'ACTIVE',
|
||||
'is_system': false,
|
||||
'config': null,
|
||||
};
|
||||
|
||||
expect(
|
||||
() => AutomationJobModel.fromJson(json),
|
||||
throwsA(isA<FormatException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('AutomationJobConfigPatchModel', () {
|
||||
test('toJson only includes non-null fields', () {
|
||||
final model = AutomationJobConfigPatchModel(
|
||||
inputTemplate: 'Updated template',
|
||||
);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
expect(json.containsKey('input_template'), true);
|
||||
expect(json.containsKey('enabled_tools'), false);
|
||||
expect(json.containsKey('context'), false);
|
||||
expect(json['input_template'], 'Updated template');
|
||||
});
|
||||
|
||||
test('toJson includes all fields when set', () {
|
||||
final model = AutomationJobConfigPatchModel(
|
||||
inputTemplate: 'Template',
|
||||
enabledTools: ['tool1', 'tool2'],
|
||||
context: MessageContextConfigModel(
|
||||
source: 'messages',
|
||||
windowMode: 'week',
|
||||
windowCount: 3,
|
||||
),
|
||||
);
|
||||
|
||||
final json = model.toJson();
|
||||
|
||||
expect(json['input_template'], 'Template');
|
||||
expect(json['enabled_tools'], ['tool1', 'tool2']);
|
||||
expect(json['context'], {
|
||||
'source': 'messages',
|
||||
'window_mode': 'week',
|
||||
'window_count': 3,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('AutomationJobUpdateRequest', () {
|
||||
test('toJson only includes non-null fields', () {
|
||||
final request = AutomationJobUpdateRequest(
|
||||
title: 'Updated Title',
|
||||
status: 'INACTIVE',
|
||||
);
|
||||
|
||||
final json = request.toJson();
|
||||
|
||||
expect(json.containsKey('title'), true);
|
||||
expect(json.containsKey('status'), true);
|
||||
expect(json.containsKey('schedule_type'), false);
|
||||
expect(json.containsKey('run_at'), false);
|
||||
expect(json['title'], 'Updated Title');
|
||||
expect(json['status'], 'INACTIVE');
|
||||
});
|
||||
|
||||
test('toJson includes patch config with only non-null fields', () {
|
||||
final request = AutomationJobUpdateRequest(
|
||||
config: AutomationJobConfigPatchModel(inputTemplate: 'New template'),
|
||||
);
|
||||
|
||||
final json = request.toJson();
|
||||
|
||||
expect(json.containsKey('config'), true);
|
||||
final configJson = json['config'] as Map<String, dynamic>;
|
||||
expect(configJson.containsKey('input_template'), true);
|
||||
expect(configJson.containsKey('enabled_tools'), false);
|
||||
expect(configJson.containsKey('context'), false);
|
||||
});
|
||||
});
|
||||
|
||||
group('AutomationJobCreateRequest', () {
|
||||
test('toJson serializes correctly', () {
|
||||
final request = AutomationJobCreateRequest(
|
||||
title: 'New Job',
|
||||
scheduleType: 'DAILY',
|
||||
runAt: '10:00:00',
|
||||
timezone: 'UTC',
|
||||
status: 'ACTIVE',
|
||||
config: AutomationJobConfigModel(
|
||||
inputTemplate: 'Hello',
|
||||
enabledTools: ['tool1'],
|
||||
context: MessageContextConfigModel(
|
||||
source: 'latest_chat',
|
||||
windowMode: 'day',
|
||||
windowCount: 2,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final json = request.toJson();
|
||||
|
||||
expect(json['title'], 'New Job');
|
||||
expect(json['schedule_type'], 'DAILY');
|
||||
expect(json['run_at'], '10:00:00');
|
||||
expect(json['timezone'], 'UTC');
|
||||
expect(json['status'], 'ACTIVE');
|
||||
expect(json['config'], {
|
||||
'input_template': 'Hello',
|
||||
'enabled_tools': ['tool1'],
|
||||
'context': {
|
||||
'source': 'latest_chat',
|
||||
'window_mode': 'day',
|
||||
'window_count': 2,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('AutomationJobListResponse', () {
|
||||
test('fromJson parses items correctly', () {
|
||||
final json = {
|
||||
'items': [
|
||||
{
|
||||
'id': 'job-1',
|
||||
'owner_id': 'user-1',
|
||||
'title': 'Job 1',
|
||||
'schedule_type': 'DAILY',
|
||||
'run_at': '09:00:00',
|
||||
'timezone': 'UTC',
|
||||
'status': 'ACTIVE',
|
||||
'is_system': false,
|
||||
'config': null,
|
||||
'next_run_at': '2024-01-15T09:00:00Z',
|
||||
'created_at': '2024-01-01T00:00:00Z',
|
||||
'updated_at': '2024-01-14T12:00:00Z',
|
||||
},
|
||||
{
|
||||
'id': 'job-2',
|
||||
'owner_id': 'user-1',
|
||||
'title': 'Job 2',
|
||||
'schedule_type': 'HOURLY',
|
||||
'run_at': '00:00:00',
|
||||
'timezone': 'UTC',
|
||||
'status': 'INACTIVE',
|
||||
'is_system': false,
|
||||
'config': null,
|
||||
'next_run_at': '2024-01-15T10:00:00Z',
|
||||
'created_at': '2024-01-02T00:00:00Z',
|
||||
'updated_at': '2024-01-14T12:00:00Z',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
final response = AutomationJobListResponse.fromJson(json);
|
||||
|
||||
expect(response.items.length, 2);
|
||||
expect(response.items[0].id, 'job-1');
|
||||
expect(response.items[1].id, 'job-2');
|
||||
});
|
||||
|
||||
test('fromJson returns empty list for null items', () {
|
||||
final response = AutomationJobListResponse.fromJson(null);
|
||||
|
||||
expect(response.items, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:social_app/features/settings/data/models/automation_job_model.dart';
|
||||
import 'package:social_app/features/settings/data/services/automation_jobs_api.dart';
|
||||
import 'package:social_app/features/settings/presentation/cubits/automation_jobs_cubit.dart';
|
||||
|
||||
class MockAutomationJobsApi extends Mock implements AutomationJobsApi {}
|
||||
|
||||
class FakeAutomationJobUpdateRequest extends Fake
|
||||
implements AutomationJobUpdateRequest {}
|
||||
|
||||
void main() {
|
||||
late AutomationJobsCubit cubit;
|
||||
late MockAutomationJobsApi mockApi;
|
||||
|
||||
final testJob = AutomationJobModel(
|
||||
id: '1',
|
||||
ownerId: 'owner1',
|
||||
title: 'Test Job',
|
||||
scheduleType: 'DAILY',
|
||||
runAt: '08:00:00',
|
||||
timezone: 'UTC',
|
||||
status: 'ACTIVE',
|
||||
isSystem: false,
|
||||
config: AutomationJobConfigModel(
|
||||
inputTemplate: '',
|
||||
enabledTools: const [],
|
||||
context: MessageContextConfigModel(
|
||||
source: 'latest_chat',
|
||||
windowMode: 'day',
|
||||
windowCount: 2,
|
||||
),
|
||||
),
|
||||
nextRunAt: DateTime(2024, 1, 1),
|
||||
createdAt: DateTime(2024, 1, 1),
|
||||
updatedAt: DateTime(2024, 1, 1),
|
||||
);
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(FakeAutomationJobUpdateRequest());
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockApi = MockAutomationJobsApi();
|
||||
cubit = AutomationJobsCubit(mockApi);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
cubit.close();
|
||||
});
|
||||
|
||||
group('AutomationJobsCubit', () {
|
||||
test('initial state is correct', () {
|
||||
expect(cubit.state.jobs, isEmpty);
|
||||
expect(cubit.state.isLoading, isFalse);
|
||||
expect(cubit.state.error, isNull);
|
||||
});
|
||||
|
||||
blocTest<AutomationJobsCubit, AutomationJobsState>(
|
||||
'loadJobs success emits loading then jobs',
|
||||
build: () {
|
||||
when(() => mockApi.list()).thenAnswer((_) async => [testJob]);
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.loadJobs(),
|
||||
expect: () => [
|
||||
isA<AutomationJobsState>().having(
|
||||
(s) => s.isLoading,
|
||||
'isLoading',
|
||||
true,
|
||||
),
|
||||
isA<AutomationJobsState>()
|
||||
.having((s) => s.isLoading, 'isLoading', false)
|
||||
.having((s) => s.jobs, 'jobs', [testJob]),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<AutomationJobsCubit, AutomationJobsState>(
|
||||
'loadJobs failure emits loading then error',
|
||||
build: () {
|
||||
when(() => mockApi.list()).thenThrow(Exception('Network error'));
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.loadJobs(),
|
||||
expect: () => [
|
||||
isA<AutomationJobsState>().having(
|
||||
(s) => s.isLoading,
|
||||
'isLoading',
|
||||
true,
|
||||
),
|
||||
isA<AutomationJobsState>()
|
||||
.having((s) => s.isLoading, 'isLoading', false)
|
||||
.having((s) => s.error, 'error', isNotNull),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<AutomationJobsCubit, AutomationJobsState>(
|
||||
'deleteJob success calls loadJobs to refresh',
|
||||
build: () {
|
||||
when(() => mockApi.delete(any())).thenAnswer((_) async {});
|
||||
when(() => mockApi.list()).thenAnswer((_) async => []);
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.deleteJob('1'),
|
||||
verify: (_) {
|
||||
verify(() => mockApi.delete('1')).called(1);
|
||||
verify(() => mockApi.list()).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<AutomationJobsCubit, AutomationJobsState>(
|
||||
'deleteJob failure emits error without refreshing',
|
||||
build: () {
|
||||
when(() => mockApi.delete(any())).thenThrow(Exception('Delete failed'));
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.deleteJob('1'),
|
||||
expect: () => [
|
||||
isA<AutomationJobsState>().having((s) => s.error, 'error', isNotNull),
|
||||
],
|
||||
verify: (_) {
|
||||
verify(() => mockApi.delete('1')).called(1);
|
||||
verifyNever(() => mockApi.list());
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<AutomationJobsCubit, AutomationJobsState>(
|
||||
'updateJobStatus success replaces target job',
|
||||
build: () {
|
||||
when(
|
||||
() => mockApi.update(any(), any()),
|
||||
).thenAnswer((_) async => testJob.copyWith(status: 'disabled'));
|
||||
return cubit;
|
||||
},
|
||||
seed: () => AutomationJobsState(jobs: [testJob]),
|
||||
act: (c) => c.updateJobStatus(id: '1', enabled: false),
|
||||
expect: () => [
|
||||
isA<AutomationJobsState>().having(
|
||||
(s) => s.jobs.first.status,
|
||||
'updated status',
|
||||
'disabled',
|
||||
),
|
||||
],
|
||||
verify: (_) {
|
||||
verify(() => mockApi.update('1', any())).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<AutomationJobsCubit, AutomationJobsState>(
|
||||
'updateJobStatus failure emits error',
|
||||
build: () {
|
||||
when(
|
||||
() => mockApi.update(any(), any()),
|
||||
).thenThrow(Exception('Update failed'));
|
||||
return cubit;
|
||||
},
|
||||
seed: () => AutomationJobsState(jobs: [testJob]),
|
||||
act: (c) => c.updateJobStatus(id: '1', enabled: false),
|
||||
expect: () => [
|
||||
isA<AutomationJobsState>().having((s) => s.error, 'error', isNotNull),
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:social_app/features/settings/data/models/automation_job_model.dart';
|
||||
import 'package:social_app/features/settings/data/services/automation_jobs_api.dart';
|
||||
import 'package:social_app/features/settings/presentation/cubits/job_detail_cubit.dart';
|
||||
|
||||
class MockAutomationJobsApi extends Mock implements AutomationJobsApi {}
|
||||
|
||||
class FakeAutomationJobUpdateRequest extends Fake
|
||||
implements AutomationJobUpdateRequest {}
|
||||
|
||||
class FakeAutomationJobCreateRequest extends Fake
|
||||
implements AutomationJobCreateRequest {}
|
||||
|
||||
void main() {
|
||||
late JobDetailCubit cubit;
|
||||
late MockAutomationJobsApi mockApi;
|
||||
|
||||
final testJob = AutomationJobModel(
|
||||
id: '1',
|
||||
ownerId: 'owner1',
|
||||
title: 'Test Job',
|
||||
scheduleType: 'DAILY',
|
||||
runAt: '08:00:00',
|
||||
timezone: 'UTC',
|
||||
status: 'ACTIVE',
|
||||
isSystem: false,
|
||||
config: AutomationJobConfigModel(
|
||||
inputTemplate: '',
|
||||
enabledTools: const [],
|
||||
context: MessageContextConfigModel(
|
||||
source: 'latest_chat',
|
||||
windowMode: 'day',
|
||||
windowCount: 2,
|
||||
),
|
||||
),
|
||||
nextRunAt: DateTime(2024, 1, 1),
|
||||
createdAt: DateTime(2024, 1, 1),
|
||||
updatedAt: DateTime(2024, 1, 1),
|
||||
);
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(FakeAutomationJobUpdateRequest());
|
||||
registerFallbackValue(FakeAutomationJobCreateRequest());
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockApi = MockAutomationJobsApi();
|
||||
cubit = JobDetailCubit(mockApi);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
cubit.close();
|
||||
});
|
||||
|
||||
group('JobDetailCubit', () {
|
||||
test('initial state is correct', () {
|
||||
expect(cubit.state.job, isNull);
|
||||
expect(cubit.state.isLoading, isFalse);
|
||||
expect(cubit.state.isSaving, isFalse);
|
||||
expect(cubit.state.error, isNull);
|
||||
});
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'loadJob success emits loading then job',
|
||||
build: () {
|
||||
when(() => mockApi.get(any())).thenAnswer((_) async => testJob);
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.loadJob('1'),
|
||||
expect: () => [
|
||||
isA<JobDetailState>().having((s) => s.isLoading, 'isLoading', true),
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isLoading, 'isLoading', false)
|
||||
.having((s) => s.job, 'job', testJob),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'loadJob failure emits loading then error',
|
||||
build: () {
|
||||
when(() => mockApi.get(any())).thenThrow(Exception('Network error'));
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.loadJob('1'),
|
||||
expect: () => [
|
||||
isA<JobDetailState>().having((s) => s.isLoading, 'isLoading', true),
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isLoading, 'isLoading', false)
|
||||
.having((s) => s.error, 'error', isNotNull),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'updateJob success emits saving then job with saving false',
|
||||
build: () {
|
||||
when(
|
||||
() => mockApi.update(any(), any()),
|
||||
).thenAnswer((_) async => testJob);
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.updateJob('1', AutomationJobUpdateRequest()),
|
||||
expect: () => [
|
||||
isA<JobDetailState>().having((s) => s.isSaving, 'isSaving', true),
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', false)
|
||||
.having((s) => s.job, 'job', testJob),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'updateJob failure emits saving then error',
|
||||
build: () {
|
||||
when(
|
||||
() => mockApi.update(any(), any()),
|
||||
).thenThrow(Exception('Update failed'));
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.updateJob('1', AutomationJobUpdateRequest()),
|
||||
expect: () => [
|
||||
isA<JobDetailState>().having((s) => s.isSaving, 'isSaving', true),
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', false)
|
||||
.having((s) => s.error, 'error', isNotNull),
|
||||
],
|
||||
);
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'deleteJob success emits saving then saving false',
|
||||
build: () {
|
||||
when(() => mockApi.delete(any())).thenAnswer((_) async {});
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.deleteJob('1'),
|
||||
expect: () => [
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', true)
|
||||
.having((s) => s.error, 'error', isNull),
|
||||
isA<JobDetailState>().having((s) => s.isSaving, 'isSaving', false),
|
||||
],
|
||||
verify: (_) {
|
||||
verify(() => mockApi.delete('1')).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'deleteJob failure emits saving then error',
|
||||
build: () {
|
||||
when(() => mockApi.delete(any())).thenThrow(Exception('Delete failed'));
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.deleteJob('1'),
|
||||
expect: () => [
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', true)
|
||||
.having((s) => s.error, 'error', isNull),
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', false)
|
||||
.having((s) => s.error, 'error', isNotNull),
|
||||
],
|
||||
verify: (_) {
|
||||
verify(() => mockApi.delete('1')).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'createJob success emits saving then created job',
|
||||
build: () {
|
||||
when(() => mockApi.create(any())).thenAnswer((_) async => testJob);
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.createJob(
|
||||
AutomationJobCreateRequest(
|
||||
title: 'New Job',
|
||||
scheduleType: 'daily',
|
||||
runAt: '08:00:00',
|
||||
timezone: 'Asia/Shanghai',
|
||||
status: 'active',
|
||||
config: AutomationJobConfigModel(
|
||||
inputTemplate: 'hello',
|
||||
enabledTools: const ['memory.write'],
|
||||
context: MessageContextConfigModel(
|
||||
source: 'latest_chat',
|
||||
windowMode: 'day',
|
||||
windowCount: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
expect: () => [
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', true)
|
||||
.having((s) => s.error, 'error', isNull),
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', false)
|
||||
.having((s) => s.job, 'job', testJob),
|
||||
],
|
||||
verify: (_) {
|
||||
verify(() => mockApi.create(any())).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
blocTest<JobDetailCubit, JobDetailState>(
|
||||
'createJob failure emits saving then error',
|
||||
build: () {
|
||||
when(() => mockApi.create(any())).thenThrow(Exception('Create failed'));
|
||||
return cubit;
|
||||
},
|
||||
act: (c) => c.createJob(
|
||||
AutomationJobCreateRequest(
|
||||
title: 'New Job',
|
||||
scheduleType: 'daily',
|
||||
runAt: '08:00:00',
|
||||
timezone: 'Asia/Shanghai',
|
||||
status: 'active',
|
||||
config: AutomationJobConfigModel(
|
||||
inputTemplate: 'hello',
|
||||
enabledTools: const ['memory.write'],
|
||||
context: MessageContextConfigModel(
|
||||
source: 'latest_chat',
|
||||
windowMode: 'day',
|
||||
windowCount: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
expect: () => [
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', true)
|
||||
.having((s) => s.error, 'error', isNull),
|
||||
isA<JobDetailState>()
|
||||
.having((s) => s.isSaving, 'isSaving', false)
|
||||
.having((s) => s.error, 'error', isNotNull),
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user