接入设备故障告警和场站实时任务进度
This commit is contained in:
701
test/device_alarms_test.dart
Normal file
701
test/device_alarms_test.dart
Normal file
@@ -0,0 +1,701 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:maibu_satabot_v2/core/theme/AppTheme.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/waring_center/presentation/widgets/alarm_item_card.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/waring_center/data/datasources/impl/alarm_remote_datasource_impl.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/waring_center/data/repositories/alarm_repository_impl.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/waring_center/domain/usecases/get_alarm_list_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/waring_center/presentation/constants/alarm_constants.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/presentation/cubit/device_alarm_history_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/env/env_config.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/interfaces/mqtt_client.dart';
|
||||
import 'package:maibu_satabot_v2/core/network/mqtt/domain/models/mqtt_message.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/data/datasources/device_alarm_mqtt_datasource.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/data/datasources/device_alarm_remote_datasource.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/data/repositories/device_alarm_repository_impl.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/domain/usecases/device_alarm_usecases.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/domain/usecases/load_site_alarm_targets_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/presentation/cubit/device_alarms_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/presentation/widgets/device_alarm_dialog.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/presentation/widgets/device_alarm_history_tab.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/presentation/widgets/device_alarm_section.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/presentation/widgets/device_alarm_summary_card.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_alarms/presentation/widgets/site_alarm_button.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/device_list/domain/entities/device_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/device_list/domain/repositories/device_repository.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/device_list/domain/usecases/get_site_devices_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
|
||||
import 'support/region_test_localizations.dart';
|
||||
|
||||
DeviceEntity _vehicle(String sn, {int siteId = 1, bool isRobot = true}) =>
|
||||
DeviceEntity(
|
||||
deviceId: 'DATABASE-ID-$sn',
|
||||
name: sn,
|
||||
type: 'robot',
|
||||
status: '离线',
|
||||
power: 0,
|
||||
todayEnergy: 0,
|
||||
isRobot: isRobot,
|
||||
siteId: siteId,
|
||||
);
|
||||
|
||||
class _Devices implements DeviceRepository {
|
||||
final rows = <int, List<DeviceEntity>>{
|
||||
1: [_vehicle('DEVICE-A')],
|
||||
2: [_vehicle('DEVICE-B', siteId: 2)],
|
||||
3: [_vehicle('DEVICE-C', siteId: 3)],
|
||||
};
|
||||
final pending = <int, Completer<List<DeviceEntity>>>{};
|
||||
final started = <int, Completer<void>>{};
|
||||
|
||||
@override
|
||||
Future<List<DeviceEntity>> getSiteDevices(int siteId) {
|
||||
final signal = started.putIfAbsent(siteId, () => Completer<void>());
|
||||
if (!signal.isCompleted) signal.complete();
|
||||
return pending[siteId]?.future ?? Future.value(rows[siteId] ?? []);
|
||||
}
|
||||
|
||||
@override
|
||||
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
class _MqttClient implements MqttClient {
|
||||
@override
|
||||
bool get isConnected => true;
|
||||
|
||||
final messages = StreamController<MqttMessage>.broadcast(sync: true);
|
||||
final subscribed = <String>[];
|
||||
final unsubscribed = <String>[];
|
||||
|
||||
@override
|
||||
Stream<MqttMessage> get messageStream => messages.stream;
|
||||
|
||||
@override
|
||||
Future<void> subscribe(String topic) async => subscribed.add(topic);
|
||||
|
||||
@override
|
||||
Future<void> unsubscribe(String topic) async => unsubscribed.add(topic);
|
||||
|
||||
void push(List<Map<String, dynamic>> alarms, {String device = 'DEVICE-A'}) {
|
||||
messages.add(
|
||||
MqttMessage(
|
||||
topic: 'device/$device/error/post',
|
||||
payload: jsonEncode({
|
||||
'deviceId': device,
|
||||
'type': 'device_error_push',
|
||||
'alarms': alarms,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void pushRecord(Map<String, dynamic> alarm, {String device = 'DEVICE-A'}) {
|
||||
messages.add(
|
||||
MqttMessage(
|
||||
topic: 'device/$device/error/post',
|
||||
payload: jsonEncode(alarm),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _alarm(int id, {String? time, int status = 1}) => {
|
||||
'id': id,
|
||||
'alarmTitle': 'Alarm $id',
|
||||
'alarmLevel': 2,
|
||||
'alarmType': 1,
|
||||
'alarmTime': time ?? '2026-11-16 10:00:00',
|
||||
'handleStatus': status,
|
||||
};
|
||||
|
||||
Future<void> _flush() => Future<void>.delayed(Duration.zero);
|
||||
|
||||
Map<String, dynamic> _singlePush({
|
||||
String code = 'SAFETY_FAULT_HEARTBEAT_TIMEOUT',
|
||||
String content = '整车控制器心跳反馈超时',
|
||||
String device = 'DEVICE-A',
|
||||
}) => {
|
||||
'id': null,
|
||||
'alarmNo': code,
|
||||
'alarmLevel': 'WARNING',
|
||||
'alarmType': 'MOWER_FAULT',
|
||||
'alarmTitle': code,
|
||||
'alarmContent': content,
|
||||
'deviceId': device,
|
||||
'deviceName': device,
|
||||
'deviceType': 'MOWER',
|
||||
'alarmTime': [2026, 11, 17, 9, 55, 41, 833000000],
|
||||
'recoverTime': null,
|
||||
'handleStatus': null,
|
||||
};
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
setUpAll(RegionTestLocalizations.loadAssets);
|
||||
late _MqttClient mqtt;
|
||||
late _Devices devices;
|
||||
late Dio dio;
|
||||
late DeviceAlarmsCubit cubit;
|
||||
late List<RequestOptions> requests;
|
||||
late Completer<void> requestStarted;
|
||||
late dynamic responseBody;
|
||||
Completer<Map<String, dynamic>>? responseGate;
|
||||
var networkFailed = false;
|
||||
|
||||
setUp(() async {
|
||||
mqtt = _MqttClient();
|
||||
devices = _Devices();
|
||||
dio = Dio();
|
||||
requests = [];
|
||||
requestStarted = Completer<void>();
|
||||
responseBody = {'code': 200};
|
||||
responseGate = null;
|
||||
networkFailed = false;
|
||||
// 所有 HTTP 请求在本地拦截,不接触实际设备或后端。
|
||||
dio.interceptors.add(
|
||||
InterceptorsWrapper(
|
||||
onRequest: (options, handler) async {
|
||||
requests.add(options);
|
||||
if (!requestStarted.isCompleted) requestStarted.complete();
|
||||
if (networkFailed) {
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: options,
|
||||
type: DioExceptionType.connectionError,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
final body = responseGate == null
|
||||
? responseBody
|
||||
: await responseGate!.future;
|
||||
handler.resolve(
|
||||
Response<dynamic>(
|
||||
requestOptions: options,
|
||||
statusCode: 200,
|
||||
data: body,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
final repository = DeviceAlarmRepositoryImpl(
|
||||
DeviceAlarmMqttDataSource(mqtt),
|
||||
DeviceAlarmRemoteDataSource(dio),
|
||||
);
|
||||
GetIt.I.registerSingleton<GetAlarmListUseCase>(
|
||||
GetAlarmListUseCase(AlarmRepositoryImpl(AlarmRemoteDataSourceImpl(dio))),
|
||||
);
|
||||
cubit = DeviceAlarmsCubit(
|
||||
WatchDeviceAlarmsUseCase(repository),
|
||||
HandleDeviceAlarmUseCase(repository),
|
||||
LoadSiteAlarmTargetsUseCase(GetSiteDevicesUseCase(devices)),
|
||||
);
|
||||
await cubit.loadSite(1);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (!cubit.isClosed) await cubit.close();
|
||||
await mqtt.messages.close();
|
||||
dio.close(force: true);
|
||||
await GetIt.I.unregister<GetAlarmListUseCase>();
|
||||
});
|
||||
|
||||
test(
|
||||
'subscribes on entry, receives every alarm and releases on exit',
|
||||
() async {
|
||||
expect(mqtt.subscribed, ['device/DEVICE-A/error/post']);
|
||||
expect(cubit.state.hasReceived, isFalse);
|
||||
mqtt.push([_alarm(99)], device: 'DEVICE-B');
|
||||
mqtt.push([_alarm(1), _alarm(2, time: '2026-11-16 11:00:00')]);
|
||||
await _flush();
|
||||
expect(cubit.state.alarms.map((alarm) => alarm.id), [2, 1]);
|
||||
expect(cubit.state.alarms.first.level, '2');
|
||||
expect(cubit.state.pendingCount, 2);
|
||||
mqtt.push([_alarm(1), _alarm(3)]);
|
||||
await _flush();
|
||||
expect(cubit.state.alarms.map((alarm) => alarm.id), [2, 3, 1]);
|
||||
await cubit.close();
|
||||
expect(mqtt.unsubscribed, ['device/DEVICE-A/error/post']);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'accepts the real single-object push with null ID and Java date array',
|
||||
() async {
|
||||
mqtt.pushRecord(_singlePush());
|
||||
await _flush();
|
||||
final alarm = cubit.state.alarms.single;
|
||||
expect(alarm.id, 0);
|
||||
expect(alarm.sourceSn, 'DEVICE-A');
|
||||
expect(alarm.content, '整车控制器心跳反馈超时');
|
||||
expect(alarm.level, 'WARNING');
|
||||
expect(alarm.type, 'MOWER_FAULT');
|
||||
expect(alarm.alarmTime, DateTime(2026, 11, 17, 9, 55, 41, 833));
|
||||
expect(cubit.state.failedSns, isEmpty);
|
||||
expect(cubit.state.hasReceived, isTrue);
|
||||
await cubit.handle(alarm.id);
|
||||
expect(requests, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'single pushes append distinct faults and update repeats without replacing other devices',
|
||||
() async {
|
||||
devices.rows[1] = [_vehicle('DEVICE-A'), _vehicle('DEVICE-B')];
|
||||
await cubit.loadSite(1, force: true);
|
||||
mqtt.pushRecord(_singlePush());
|
||||
mqtt.pushRecord(_singlePush(content: '重复推送更新内容'));
|
||||
mqtt.pushRecord(_singlePush(code: 'OTHER_FAULT'));
|
||||
mqtt.pushRecord(_singlePush(device: 'DEVICE-B'), device: 'DEVICE-B');
|
||||
await _flush();
|
||||
expect(cubit.state.alarms, hasLength(3));
|
||||
expect(cubit.state.forDevice('DEVICE-A').alarms, hasLength(2));
|
||||
expect(cubit.state.forDevice('DEVICE-B').alarms, hasLength(1));
|
||||
expect(
|
||||
cubit.state.alarms.where((alarm) => alarm.content == '重复推送更新内容'),
|
||||
hasLength(1),
|
||||
);
|
||||
// Missing-ID closure must not mark every other missing-ID alarm closed.
|
||||
mqtt.pushRecord({..._singlePush(), 'handleStatus': 2});
|
||||
await _flush();
|
||||
expect(cubit.state.alarms.where((alarm) => alarm.isClosed), hasLength(1));
|
||||
expect(cubit.state.closedIds, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'POST closes only selected alarm after success and blocks duplicate taps',
|
||||
() async {
|
||||
mqtt.push([_alarm(1), _alarm(2)]);
|
||||
await _flush();
|
||||
responseGate = Completer<Map<String, dynamic>>();
|
||||
final handling = cubit.handle(1);
|
||||
await requestStarted.future;
|
||||
await cubit.handle(1);
|
||||
expect(requests, hasLength(1));
|
||||
final request = requests.single;
|
||||
expect(request.method, 'POST');
|
||||
expect(request.uri.origin, EnvConfig.baseUrl);
|
||||
expect(request.uri.path, '/iot/alarm/handle');
|
||||
expect(request.data, {'alarmId': 1, 'handleStatus': 2});
|
||||
expect(cubit.state.alarms.every((alarm) => !alarm.isClosed), isTrue);
|
||||
expect(cubit.state.handlingIds, {1});
|
||||
responseGate!.complete({'code': 200});
|
||||
await handling;
|
||||
expect(
|
||||
cubit.state.alarms.singleWhere((alarm) => alarm.id == 1).isClosed,
|
||||
isTrue,
|
||||
);
|
||||
expect(
|
||||
cubit.state.alarms.singleWhere((alarm) => alarm.id == 2).isClosed,
|
||||
isFalse,
|
||||
);
|
||||
expect(cubit.state.handlingIds, isEmpty);
|
||||
mqtt.push([_alarm(1)]);
|
||||
await _flush();
|
||||
expect(
|
||||
cubit.state.alarms.singleWhere((alarm) => alarm.id == 1).isClosed,
|
||||
isTrue,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'rejected, malformed and network responses keep alarm pending and allow retry',
|
||||
() async {
|
||||
mqtt.push([_alarm(1)]);
|
||||
await _flush();
|
||||
for (final body in [
|
||||
{'code': 500, 'msg': '处理失败'},
|
||||
{'code': 200, 'data': false},
|
||||
{'data': true},
|
||||
'invalid response',
|
||||
]) {
|
||||
responseBody = body;
|
||||
await cubit.handle(1);
|
||||
expect(cubit.state.alarms.single.isClosed, isFalse);
|
||||
expect(cubit.state.handleErrors.containsKey(1), isTrue);
|
||||
expect(cubit.state.handlingIds, isEmpty);
|
||||
}
|
||||
networkFailed = true;
|
||||
await cubit.handle(1);
|
||||
expect(cubit.state.alarms.single.isClosed, isFalse);
|
||||
networkFailed = false;
|
||||
responseBody = {'code': '200', 'data': true};
|
||||
await cubit.handle(1);
|
||||
expect(cubit.state.alarms.single.isClosed, isTrue);
|
||||
expect(cubit.state.handleErrors, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'bad frame does not end reception; resubscribe retains existing records',
|
||||
() async {
|
||||
mqtt.messages.add(
|
||||
MqttMessage(topic: 'device/DEVICE-A/error/post', payload: '{bad json'),
|
||||
);
|
||||
await _flush();
|
||||
expect(cubit.state.subscriptionFailed, isTrue);
|
||||
mqtt.push([_alarm(1)]);
|
||||
await _flush();
|
||||
expect(cubit.state.subscriptionFailed, isFalse);
|
||||
await cubit.handle(1);
|
||||
await cubit.retry();
|
||||
mqtt.push([_alarm(1), _alarm(2)]);
|
||||
await _flush();
|
||||
expect(cubit.state.alarms, hasLength(2));
|
||||
expect(
|
||||
cubit.state.alarms.singleWhere((alarm) => alarm.id == 1).isClosed,
|
||||
isTrue,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'site switch releases previous topic and ignores its late HTTP completion',
|
||||
() async {
|
||||
mqtt.push([_alarm(1)]);
|
||||
await _flush();
|
||||
responseGate = Completer<Map<String, dynamic>>();
|
||||
final handling = cubit.handle(1);
|
||||
await requestStarted.future;
|
||||
await cubit.loadSite(2);
|
||||
expect(mqtt.unsubscribed, ['device/DEVICE-A/error/post']);
|
||||
expect(mqtt.subscribed.last, 'device/DEVICE-B/error/post');
|
||||
mqtt.push([_alarm(9)]);
|
||||
mqtt.push([_alarm(1)], device: 'DEVICE-B');
|
||||
await _flush();
|
||||
responseGate!.complete({'code': 200});
|
||||
await handling;
|
||||
expect(cubit.state.alarms.single.id, 1);
|
||||
expect(cubit.state.alarms.single.isClosed, isFalse);
|
||||
expect(cubit.state.handleErrors, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'site subscribes every vehicle by SN and scopes all/device data independently',
|
||||
() async {
|
||||
devices.rows[1] = [
|
||||
_vehicle('DEVICE-A'),
|
||||
_vehicle('DEVICE-B'),
|
||||
_vehicle('DEVICE-A'),
|
||||
_vehicle('STXZ0126101500001198D', isRobot: false),
|
||||
_vehicle('OTHER-SITE', siteId: 9),
|
||||
];
|
||||
await cubit.loadSite(1, force: true);
|
||||
expect(mqtt.subscribed, [
|
||||
'device/DEVICE-A/error/post',
|
||||
'device/DEVICE-B/error/post',
|
||||
'device/STXZ0126101500001198D/error/post',
|
||||
]);
|
||||
mqtt.push([_alarm(1)]);
|
||||
mqtt.push([_alarm(2)], device: 'DEVICE-B');
|
||||
await _flush();
|
||||
expect(cubit.state.alarms, hasLength(2));
|
||||
expect(cubit.state.forDevice('DEVICE-A').alarms.single.id, 1);
|
||||
expect(cubit.state.forDevice('DEVICE-B').alarms.single.id, 2);
|
||||
await cubit.handle(1);
|
||||
expect(cubit.state.forDevice('DEVICE-A').pendingCount, 0);
|
||||
expect(cubit.state.forDevice('DEVICE-B').pendingCount, 1);
|
||||
expect(cubit.state.pendingCount, 1);
|
||||
|
||||
devices.rows[1] = [_vehicle('DEVICE-A'), _vehicle('DEVICE-C')];
|
||||
await cubit.loadSite(1, force: true);
|
||||
expect(mqtt.unsubscribed, [
|
||||
'device/DEVICE-B/error/post',
|
||||
'device/STXZ0126101500001198D/error/post',
|
||||
]);
|
||||
expect(
|
||||
mqtt.subscribed.where((topic) => topic.contains('DEVICE-A')),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(mqtt.subscribed.last, 'device/DEVICE-C/error/post');
|
||||
expect(cubit.state.alarms.single.isClosed, isTrue);
|
||||
await cubit.loadSite(null);
|
||||
await cubit.retry();
|
||||
expect(cubit.state.isLoading, isFalse);
|
||||
expect(cubit.state.alarms, isEmpty);
|
||||
expect(
|
||||
mqtt.unsubscribed,
|
||||
containsAll([
|
||||
'device/DEVICE-A/error/post',
|
||||
'device/DEVICE-C/error/post',
|
||||
]),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('rapid site changes ignore late roster and old-site messages', () async {
|
||||
final roster = Completer<List<DeviceEntity>>();
|
||||
devices.pending[2] = roster;
|
||||
devices.started[2] = Completer<void>();
|
||||
mqtt.push([_alarm(1)]);
|
||||
await _flush();
|
||||
final oldLoad = cubit.loadSite(2);
|
||||
await devices.started[2]!.future;
|
||||
expect(cubit.state.alarms, isEmpty);
|
||||
expect(mqtt.unsubscribed, ['device/DEVICE-A/error/post']);
|
||||
await cubit.loadSite(3);
|
||||
roster.complete(devices.rows[2]!);
|
||||
await oldLoad;
|
||||
mqtt.push([_alarm(1)]);
|
||||
mqtt.push([_alarm(2)], device: 'DEVICE-B');
|
||||
mqtt.push([_alarm(3)], device: 'DEVICE-C');
|
||||
await _flush();
|
||||
expect(cubit.state.siteId, 3);
|
||||
expect(cubit.state.alarms.single.id, 3);
|
||||
expect(mqtt.subscribed, [
|
||||
'device/DEVICE-A/error/post',
|
||||
'device/DEVICE-C/error/post',
|
||||
]);
|
||||
});
|
||||
|
||||
test(
|
||||
'history reuses the old list API and filters raw device IDs across pages',
|
||||
() async {
|
||||
final history = DeviceAlarmHistoryCubit(
|
||||
GetIt.I<GetAlarmListUseCase>(),
|
||||
siteId: 1,
|
||||
deviceSn: 'DEVICE-A',
|
||||
deviceId: 'DATABASE-ID-DEVICE-A',
|
||||
);
|
||||
responseBody = {
|
||||
'code': 200,
|
||||
'rows': List.generate(
|
||||
20,
|
||||
(index) => {
|
||||
..._alarm(index + 1),
|
||||
'siteId': 1,
|
||||
'deviceId': index == 0
|
||||
? 'DATABASE-ID-DEVICE-A'
|
||||
: 'DATABASE-ID-DEVICE-B',
|
||||
'deviceName': 'Display alias',
|
||||
'handleStatus': '1',
|
||||
'alarmLevel': '1',
|
||||
},
|
||||
),
|
||||
};
|
||||
await history.refresh();
|
||||
final request = requests.single;
|
||||
expect(request.method, 'GET');
|
||||
expect(request.uri.path, '/iot/alarm/list');
|
||||
expect(request.queryParameters, {'siteId': 1, 'page': 1, 'pageSize': 20});
|
||||
expect(history.state.alarms.single.id, '1');
|
||||
expect(history.state.alarms.single.status, AlarmStatus.pending);
|
||||
expect(history.state.alarms.single.level, AlarmLevel.danger);
|
||||
expect(history.state.hasMore, isTrue);
|
||||
responseBody = {
|
||||
'code': 200,
|
||||
'rows': [
|
||||
{..._alarm(21), 'deviceId': 'DATABASE-ID-DEVICE-A', 'siteId': 1},
|
||||
{..._alarm(22), 'deviceId': 'DATABASE-ID-DEVICE-A', 'siteId': 2},
|
||||
],
|
||||
};
|
||||
await history.loadMore();
|
||||
expect(
|
||||
history.state.alarms.map((alarm) => alarm.id),
|
||||
containsAll(['1', '21']),
|
||||
);
|
||||
expect(history.state.alarms, hasLength(2));
|
||||
expect(history.state.hasMore, isFalse);
|
||||
expect(requests.last.queryParameters['page'], 2);
|
||||
await history.close();
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'history retries the failed page and keeps cached rows on refresh errors',
|
||||
() async {
|
||||
final history = DeviceAlarmHistoryCubit(
|
||||
GetIt.I<GetAlarmListUseCase>(),
|
||||
siteId: 1,
|
||||
);
|
||||
responseBody = {
|
||||
'code': 200,
|
||||
'rows': List.generate(20, (i) => _alarm(i + 1)),
|
||||
};
|
||||
await history.refresh();
|
||||
expect(history.state.alarms, hasLength(20));
|
||||
networkFailed = true;
|
||||
await history.refresh();
|
||||
expect(history.state.error, isNotNull);
|
||||
expect(history.state.alarms, hasLength(20));
|
||||
final count = requests.length;
|
||||
await history.loadMore();
|
||||
expect(requests, hasLength(count));
|
||||
networkFailed = false;
|
||||
await history.retry();
|
||||
expect(requests.last.queryParameters['page'], 1);
|
||||
expect(history.state.error, isNull);
|
||||
networkFailed = true;
|
||||
await history.loadMore();
|
||||
expect(requests.last.queryParameters['page'], 2);
|
||||
networkFailed = false;
|
||||
await history.retry();
|
||||
expect(requests.last.queryParameters['page'], 2);
|
||||
expect(history.state.error, isNull);
|
||||
await history.close();
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'history connection errors show a short retry state without raw exception or load-more',
|
||||
(tester) async {
|
||||
networkFailed = true;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
locale: AppLocalizations.chinese,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
localizationsDelegates: const [
|
||||
RegionTestLocalizations(),
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
home: Scaffold(body: DeviceAlarmHistoryTab(cubit: cubit)),
|
||||
),
|
||||
);
|
||||
await tester.runAsync(
|
||||
() => Future<void>.delayed(const Duration(milliseconds: 40)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('历史告警加载失败,请重试'), findsOneWidget);
|
||||
expect(find.text('重试'), findsOneWidget);
|
||||
expect(find.text('加载更多'), findsNothing);
|
||||
expect(find.textContaining('DioException'), findsNothing);
|
||||
expect(find.textContaining('/iot/alarm/list'), findsNothing);
|
||||
networkFailed = false;
|
||||
responseBody = {'code': 200, 'rows': []};
|
||||
await tester.tap(find.text('重试'));
|
||||
await tester.runAsync(
|
||||
() => Future<void>.delayed(const Duration(milliseconds: 40)),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('历史告警加载失败,请重试'), findsNothing);
|
||||
expect(find.text('暂无历史告警记录'), findsOneWidget);
|
||||
expect(requests.last.queryParameters['page'], 1);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'history-only record uses latest handle API and shares closed state',
|
||||
() async {
|
||||
expect(cubit.state.alarms, isEmpty);
|
||||
await cubit.handle(501, historySiteId: 1);
|
||||
expect(requests.single.method, 'POST');
|
||||
expect(requests.single.uri.path, '/iot/alarm/handle');
|
||||
expect(requests.single.data, {'alarmId': 501, 'handleStatus': 2});
|
||||
expect(cubit.state.closedIds, contains(501));
|
||||
expect(cubit.state.alarms, isEmpty);
|
||||
await cubit.retry();
|
||||
expect(cubit.state.closedIds, contains(501));
|
||||
await cubit.handle(501, historySiteId: 1);
|
||||
expect(requests, hasLength(1));
|
||||
await cubit.loadSite(2);
|
||||
await cubit.handle(502, historySiteId: 1);
|
||||
expect(requests, hasLength(1));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'overview title/card open device alarms; top icon opens all site alarms',
|
||||
(tester) async {
|
||||
await tester.runAsync(() async {
|
||||
devices.rows[1] = [_vehicle('DEVICE-A'), _vehicle('DEVICE-B')];
|
||||
await cubit.loadSite(1, force: true);
|
||||
mqtt.push([_alarm(1)]);
|
||||
mqtt.push([_alarm(2)], device: 'DEVICE-B');
|
||||
await _flush();
|
||||
});
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(extensions: const [AppColors.light]),
|
||||
locale: AppLocalizations.chinese,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
localizationsDelegates: const [
|
||||
RegionTestLocalizations(),
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
home: Scaffold(
|
||||
appBar: AppBar(actions: [SiteAlarmButton(cubit: cubit)]),
|
||||
body: DeviceAlarmSection(cubit: cubit, deviceSn: 'DEVICE-A'),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('故障状态'));
|
||||
await tester.pumpAndSettle();
|
||||
final dialog = find.byType(DeviceAlarmDialog);
|
||||
expect(dialog, findsOneWidget);
|
||||
expect(
|
||||
find.descendant(of: dialog, matching: find.text('Alarm 1')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: dialog, matching: find.text('Alarm 2')),
|
||||
findsNothing,
|
||||
);
|
||||
await tester.tap(find.byTooltip('关闭'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byType(DeviceAlarmSummaryCard));
|
||||
await tester.pumpAndSettle();
|
||||
expect(dialog, findsOneWidget);
|
||||
await tester.tap(find.byTooltip('关闭'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byType(SiteAlarmButton));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('场站故障与告警'), findsOneWidget);
|
||||
expect(
|
||||
find.descendant(of: dialog, matching: find.text('Alarm 2')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(mqtt.subscribed, hasLength(2));
|
||||
expect(mqtt.unsubscribed, isEmpty);
|
||||
responseBody = {
|
||||
'code': 200,
|
||||
'rows': [
|
||||
{
|
||||
..._alarm(501),
|
||||
'siteId': 1,
|
||||
'deviceId': 'DATABASE-ID-DEVICE-A',
|
||||
'deviceName': 'DEVICE-A',
|
||||
},
|
||||
],
|
||||
};
|
||||
await tester.tap(find.text('历史'));
|
||||
await tester.pump(const Duration(milliseconds: 350));
|
||||
await tester.runAsync(() async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlarmItemCard), findsOneWidget);
|
||||
expect(find.text('去处理'), findsOneWidget);
|
||||
await tester.tap(find.byKey(const ValueKey('history-handle-501')));
|
||||
await tester.runAsync(() async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
expect(cubit.state.closedIds, contains(501));
|
||||
expect(requests.last.method, 'POST');
|
||||
expect(requests.last.uri.path, '/iot/alarm/handle');
|
||||
expect(requests.last.data, {'alarmId': 501, 'handleStatus': 2});
|
||||
expect(find.text('去处理'), findsNothing);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user