完善自主作业下发、暂停恢复和轨迹保留

This commit is contained in:
2026-11-21 14:51:37 +08:00
parent a37c448391
commit 4a1ce4199d
18 changed files with 556 additions and 127 deletions

View File

@@ -0,0 +1,77 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:maibu_satabot_v2/core/di/adapters/device_task_control_adapter.dart';
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/device_task_cubit.dart';
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/device_task_state.dart';
class FakeDeviceTaskCubit implements DeviceTaskCubit {
@override
DeviceTaskState state = const DeviceTaskState();
final calls = <String>[];
String? failure;
@override
void updateCurrentTaskId(int taskId) {
state = state.copyWith(currentTaskId: taskId);
}
@override
Future<void> pauseTask(String deviceId) async {
calls.add('pause/$deviceId/${state.currentTaskId}');
state = state.copyWith(errorMessage: failure);
}
@override
Future<void> recoveryTask(String deviceId) async {
calls.add('recovery/$deviceId/${state.currentTaskId}');
state = state.copyWith(errorMessage: failure);
}
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
void main() {
test(
'reuses the existing Cubit pause and recovery with the selected task ID',
() async {
final cubit = FakeDeviceTaskCubit();
final adapter = DeviceTaskControlAdapter(cubit);
expect(
(await adapter(deviceId: 'SN-1', taskId: 42, resume: false)).isOk,
isTrue,
);
expect(
(await adapter(deviceId: 'SN-1', taskId: 42, resume: true)).isOk,
isTrue,
);
expect(cubit.calls, ['pause/SN-1/42', 'recovery/SN-1/42']);
},
);
test('returns the error reported by the existing Cubit', () async {
final cubit = FakeDeviceTaskCubit()..failure = '恢复失败';
final result = await DeviceTaskControlAdapter(cubit)(
deviceId: 'SN-1',
taskId: 42,
resume: true,
);
expect(result.isOk, isFalse);
expect(result.error, '恢复失败');
});
test(
'does not replace the active task while the shared Cubit is busy',
() async {
final cubit = FakeDeviceTaskCubit()
..state = const DeviceTaskState(isLoading: true, currentTaskId: 41);
final result = await DeviceTaskControlAdapter(cubit)(
deviceId: 'SN-1',
taskId: 42,
resume: false,
);
expect(result.isOk, isFalse);
expect(cubit.state.currentTaskId, 41);
expect(cubit.calls, isEmpty);
},
);
}