Files
openchamber-android-native/test/scheduled_tasks_test.dart
T
blumia b65498d73e feat: stats page, and the client becomes Chamber Go
- 全部功能 gains 统计 (Stats): ranges Today/7/30/90 days/all time, a
  project scope (reusing the shared project sheet, plus "All projects"),
  session/prompt/token/cost/active-days/streak tiles, a daily activity
  chart (weekly past 92 days), token composition, efficiency tiles, model
  ranking by tokens/cost/steps and on-request tool-call stats; data comes
  from /api/experimental/session/stats and the day buckets use the device's
  IANA zone (a small platform channel; desktop falls back to UTC)
- the model bars draw a full-width track with a left-aligned fill, so a
  small token share reads as a sliver instead of an empty bar
- AI token strings read 词元 (not 令牌) in Chinese
- the client's display name is Chamber Go (launcher, iOS, Linux window,
  About), package unchanged; pairing/login labels and the user-agent
  follow, so a newly paired device shows "Chamber Go" on the web client's
  "Connect to this server" list
- bump to 0.5.6
2026-11-26 17:18:09 +08:00

208 lines
7.2 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);
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);
});
});
}