Files
openchamber-android-native/test/scheduled_tasks_test.dart
T
blumia da4bfb6826 feat: add scheduled tasks behind a 全部功能 button
- the sidebar footer gains 全部功能 left of 设置; its page lists the
  features the server offers, planned tasks for now
- 计划任务 follows the web client: a project (or the chats scope) picked
  at the top, then its tasks with schedule, last result, next run, model
  and error; the switch pauses, the menu runs now, edits or deletes
- the editor covers name, prompt, daily/weekly/once/cron schedules
  (Chinese date and time pickers), several times a day, weekdays, cron
  examples, enabled, and either the session defaults or a fixed model
  and agent
- the same /api/projects/:id/scheduled-tasks endpoints the web uses; a
  server without them says so instead of failing silently
- the app now pins zh_CN localizations, so the built-in pickers and
  menus are Chinese too
- bump to 0.5.1
2026-11-26 00:36:27 +08:00

206 lines
7.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/api/scheduled_tasks.dart';
import 'package:openchamber/screens/features_screen.dart';
import 'package:openchamber/screens/scheduled_tasks_screen.dart';
import 'package:openchamber/state.dart';
import 'package:openchamber/widgets/scheduled_task_editor.dart';
Map<String, dynamic> dailyJson() => {
'id': 'task_1',
'name': '每日同步',
'enabled': true,
'schedule': {
'kind': 'daily',
'times': ['09:00', '18:30'],
'timezone': 'Asia/Shanghai',
},
'execution': {
'prompt': '总结未完成任务',
'providerID': 'axonhub',
'modelID': 'deepseek-flash',
'agent': 'build',
},
'state': {
'lastStatus': 'success',
'lastRunAt': 1000,
'nextRunAt': 2000,
'lastDurationMs': 4000,
},
};
void main() {
group('scheduled task wire shape', () {
test('a daily task reads its schedule, execution and state', () {
final task = ScheduledTask.fromJson(dailyJson());
expect(task.id, 'task_1');
expect(task.name, '每日同步');
expect(task.enabled, isTrue);
expect(task.kind, 'daily');
expect(task.times, ['09:00', '18:30']);
expect(task.timezone, 'Asia/Shanghai');
expect(task.prompt, '总结未完成任务');
expect(task.useDefaults, isFalse);
expect(task.providerId, 'axonhub');
expect(task.modelId, 'deepseek-flash');
expect(task.agent, 'build');
expect(task.lastStatus, 'success');
expect(task.nextRunAt, 2000);
expect(scheduledScheduleText(task), '每天 09:00、18:30');
expect(task.modelLabel, 'axonhub/deepseek-flash · build');
});
test('missing useDefaults means the session defaults', () {
final json = dailyJson();
(json['execution'] as Map<String, dynamic>)
..remove('providerID')
..remove('modelID')
..remove('agent');
final task = ScheduledTask.fromJson(json);
expect(task.useDefaults, isTrue);
expect(task.modelLabel, '使用默认模型与智能体');
expect(scheduledScheduleText(task), '每天 09:00、18:30');
});
test('weekly, once and cron schedules read as the web client says', () {
final weekly = ScheduledTask.fromJson({
'schedule': {
'kind': 'weekly',
'times': ['09:00'],
'weekdays': [1, 3],
},
});
expect(scheduledScheduleText(weekly), '每周 周一、周三 09:00');
final once = ScheduledTask.fromJson({
'schedule': {
'kind': 'once',
'date': '2026-12-01',
'time': '08:05',
},
});
expect(scheduledScheduleText(once), '一次性 2026-12-01 08:05');
final cron = ScheduledTask.fromJson({
'schedule': {'kind': 'cron', 'cron': '*/5 * * * *'},
});
expect(scheduledScheduleText(cron), 'Cron:*/5 * * * *');
});
test('saving sends only the schedule fields the kind uses', () {
const daily = ScheduledTask(
id: 'task_9',
name: '每日同步',
kind: 'daily',
times: ['09:00'],
prompt: '总结',
useDefaults: true,
);
final json = daily.toJson();
expect(json['id'], 'task_9');
expect(json['schedule'], {'kind': 'daily', 'times': ['09:00']});
expect(json['execution'], {'prompt': '总结', 'useDefaults': true});
expect(json.containsKey('targetSessionId'), isFalse);
const once = ScheduledTask(
name: '一次性',
kind: 'once',
date: '2026-12-01',
time: '08:05',
prompt: '提醒',
useDefaults: false,
providerId: 'axonhub',
modelId: 'deepseek-flash',
agent: 'plan',
);
final onceJson = once.toJson();
expect(onceJson.containsKey('id'), isFalse);
expect(onceJson['schedule'],
{'kind': 'once', 'date': '2026-12-01', 'time': '08:05'});
expect(onceJson['execution'], {
'prompt': '提醒',
'useDefaults': false,
'providerID': 'axonhub',
'modelID': 'deepseek-flash',
'agent': 'plan',
});
});
test('status labels and relative times', () {
expect(scheduledStatusLabel('success'), '成功');
expect(scheduledStatusLabel('running'), '运行中');
expect(scheduledStatusLabel('failed'), '错误');
expect(scheduledStatusLabel(''), '空闲');
final soon = scheduledRelativeTime(
DateTime.now().add(const Duration(minutes: 5)).millisecondsSinceEpoch);
expect(soon, contains('分钟后'));
final past = scheduledRelativeTime(
DateTime.now().subtract(const Duration(hours: 2)).millisecondsSinceEpoch);
expect(past, contains('小时前'));
expect(scheduledRelativeTime(0), '');
});
});
group('screens', () {
testWidgets('全部功能 lists the scheduled tasks entry', (tester) async {
final app = AppState();
addTearDown(app.dispose);
await tester.pumpWidget(MaterialApp(home: FeaturesScreen(app: app)));
expect(find.text('全部功能'), findsOneWidget);
expect(find.text('计划任务'), findsOneWidget);
expect(find.text('按时间向会话发送提示词'), findsOneWidget);
await tester.tap(find.text('计划任务'));
await tester.pumpAndSettle();
// No client in the test: the page says so instead of listing tasks.
expect(find.byType(ScheduledTasksScreen), findsOneWidget);
expect(find.text('未连接服务器'), findsOneWidget);
});
testWidgets('the editor validates and hands back the draft', (tester) async {
final app = AppState();
addTearDown(app.dispose);
ScheduledTask? saved;
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: SizedBox(
height: 900,
child: ScheduledTaskEditor(
app: app,
scopeLabel: 'openchamber-android-native',
onSave: (draft) async {
saved = draft;
return false;
},
),
),
),
));
expect(find.text('新建计划任务'), findsOneWidget);
expect(find.text('运行位置:openchamber-android-native'), findsOneWidget);
// An empty name is refused with a message, not saved.
await tester.tap(find.widgetWithText(TextButton, '保存'));
await tester.pump();
expect(find.text('任务名称为必填项'), findsOneWidget);
expect(saved, isNull);
await tester.enterText(find.byType(TextField).at(0), '每日同步');
await tester.enterText(find.byType(TextField).at(1), '总结未完成任务');
await tester.tap(find.widgetWithText(TextButton, '保存'));
await tester.pumpAndSettle();
expect(saved, isNotNull);
expect(saved!.name, '每日同步');
expect(saved!.prompt, '总结未完成任务');
expect(saved!.kind, 'daily');
expect(saved!.times, ['09:00']);
expect(saved!.enabled, isTrue);
expect(saved!.useDefaults, isTrue);
});
});
}