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? failure; @override void updateCurrentTaskId(int taskId) { state = state.copyWith(currentTaskId: taskId); } @override Future pauseTask(String deviceId) async { calls.add('pause/$deviceId/${state.currentTaskId}'); state = state.copyWith(errorMessage: failure); } @override Future 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); }, ); }