305 lines
10 KiB
Dart
305 lines
10 KiB
Dart
|
|
import 'dart:async';
|
||
|
|
import 'dart:convert';
|
||
|
|
|
||
|
|
import 'package:flutter_test/flutter_test.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/network/mqtt/data/datasources/mower_realtime_datasource.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/network/mqtt/data/repositories/mower_realtime_repository_impl.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/network/mqtt/domain/entities/mower_task_status_entity.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/autonomous_work/data/site_device_realtime_subscriptions.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/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';
|
||
|
|
|
||
|
|
DeviceEntity _device(String sn, {int siteId = 1, bool robot = true}) =>
|
||
|
|
DeviceEntity(
|
||
|
|
deviceId: 'DB-$sn',
|
||
|
|
name: sn,
|
||
|
|
type: 'robot',
|
||
|
|
status: '离线',
|
||
|
|
power: 0,
|
||
|
|
todayEnergy: 0,
|
||
|
|
isRobot: robot,
|
||
|
|
siteId: siteId,
|
||
|
|
);
|
||
|
|
|
||
|
|
class _Devices extends Fake implements DeviceRepository {
|
||
|
|
final rows = <int, List<DeviceEntity>>{
|
||
|
|
1: [
|
||
|
|
_device('A'),
|
||
|
|
_device('B'),
|
||
|
|
_device('A'),
|
||
|
|
_device('PV', robot: false),
|
||
|
|
_device('FOREIGN', siteId: 2),
|
||
|
|
],
|
||
|
|
2: [_device('C', siteId: 2)],
|
||
|
|
3: [_device('D', siteId: 3)],
|
||
|
|
};
|
||
|
|
final calls = <int>[];
|
||
|
|
final pending = <int, Completer<List<DeviceEntity>>>{};
|
||
|
|
@override
|
||
|
|
Future<List<DeviceEntity>> getSiteDevices(int siteId) {
|
||
|
|
calls.add(siteId);
|
||
|
|
return pending[siteId]?.future ?? Future.value(rows[siteId] ?? []);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class _Mqtt extends Fake implements MqttClient {
|
||
|
|
final messages = StreamController<MqttMessage>.broadcast(sync: true);
|
||
|
|
final subscribed = <String>[];
|
||
|
|
final unsubscribed = <String>[];
|
||
|
|
final failNext = <String>{};
|
||
|
|
@override
|
||
|
|
bool get isConnected => true;
|
||
|
|
@override
|
||
|
|
Stream<MqttMessage> get messageStream => messages.stream;
|
||
|
|
@override
|
||
|
|
Future<void> subscribe(String topic) async {
|
||
|
|
subscribed.add(topic);
|
||
|
|
if (failNext.remove(topic)) throw StateError('Subscribe failed');
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<void> unsubscribe(String topic) async => unsubscribed.add(topic);
|
||
|
|
|
||
|
|
void task(String sn, {String id = 'task-1', double progress = 0.25}) =>
|
||
|
|
messages.add(
|
||
|
|
MqttMessage(
|
||
|
|
topic: 'mower/$sn/task/status/post',
|
||
|
|
payload: jsonEncode({
|
||
|
|
'sn': sn,
|
||
|
|
'task_id': id,
|
||
|
|
'data': {'progress': progress},
|
||
|
|
}),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
class _Remote extends Fake implements DeviceAlarmRemoteDataSource {}
|
||
|
|
|
||
|
|
Future<void> _flush() => Future<void>.delayed(Duration.zero);
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
late _Devices devices;
|
||
|
|
late _Mqtt mqtt;
|
||
|
|
late MowerRealtimeDataSourceImpl source;
|
||
|
|
late DeviceAlarmsCubit alarms;
|
||
|
|
late SiteDeviceRealtimeSubscriptions subscriptions;
|
||
|
|
|
||
|
|
setUp(() {
|
||
|
|
devices = _Devices();
|
||
|
|
mqtt = _Mqtt();
|
||
|
|
source = MowerRealtimeDataSourceImpl(mqtt);
|
||
|
|
final alarmRepository = DeviceAlarmRepositoryImpl(
|
||
|
|
DeviceAlarmMqttDataSource(mqtt),
|
||
|
|
_Remote(),
|
||
|
|
);
|
||
|
|
alarms = DeviceAlarmsCubit(
|
||
|
|
WatchDeviceAlarmsUseCase(alarmRepository),
|
||
|
|
HandleDeviceAlarmUseCase(alarmRepository),
|
||
|
|
LoadSiteAlarmTargetsUseCase(GetSiteDevicesUseCase(devices)),
|
||
|
|
);
|
||
|
|
subscriptions = SiteDeviceRealtimeSubscriptions(
|
||
|
|
alarms,
|
||
|
|
MowerRealtimeRepositoryImpl(source),
|
||
|
|
);
|
||
|
|
});
|
||
|
|
tearDown(() async {
|
||
|
|
await subscriptions.dispose();
|
||
|
|
await alarms.close();
|
||
|
|
source.dispose();
|
||
|
|
await mqtt.messages.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
test(
|
||
|
|
'device Tab subscribes both topics for every site vehicle, including offline vehicles',
|
||
|
|
() async {
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
expect(devices.calls, [1]);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed,
|
||
|
|
unorderedEquals([
|
||
|
|
'device/A/error/post',
|
||
|
|
'device/B/error/post',
|
||
|
|
'device/PV/error/post',
|
||
|
|
'mower/A/task/status/post',
|
||
|
|
'mower/B/task/status/post',
|
||
|
|
'mower/PV/task/status/post',
|
||
|
|
]),
|
||
|
|
);
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
await subscriptions.ensureSubscriptions(1, refresh: true);
|
||
|
|
expect(devices.calls, [1, 1]);
|
||
|
|
expect(mqtt.subscribed, hasLength(6));
|
||
|
|
expect(mqtt.unsubscribed, isEmpty);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
test(
|
||
|
|
'reports arrive before opening details and survive closing/reopening the panel',
|
||
|
|
() async {
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
mqtt.task('A');
|
||
|
|
await _flush();
|
||
|
|
final report = await subscriptions
|
||
|
|
.watchTaskStatus(siteId: 1, deviceSn: 'A')
|
||
|
|
.first;
|
||
|
|
expect(report?.progress, 0.25);
|
||
|
|
await _flush();
|
||
|
|
expect(mqtt.unsubscribed, isEmpty);
|
||
|
|
mqtt.task('A', progress: 0.5);
|
||
|
|
await _flush();
|
||
|
|
final next = await subscriptions
|
||
|
|
.watchTaskStatus(siteId: 1, deviceSn: 'A')
|
||
|
|
.first;
|
||
|
|
expect(next?.progress, 0.5);
|
||
|
|
await _flush();
|
||
|
|
expect(mqtt.subscribed, hasLength(6));
|
||
|
|
expect(mqtt.unsubscribed, isEmpty);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
test(
|
||
|
|
'refresh finds new vehicles and retires removed vehicles without resubscribing healthy ones',
|
||
|
|
() async {
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
devices.rows[1] = [_device('A'), _device('NEW')];
|
||
|
|
await subscriptions.ensureSubscriptions(1, refresh: true);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed.where((topic) => topic == 'mower/A/task/status/post'),
|
||
|
|
hasLength(1),
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed,
|
||
|
|
containsAll(['device/NEW/error/post', 'mower/NEW/task/status/post']),
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
mqtt.unsubscribed,
|
||
|
|
containsAll(['device/B/error/post', 'mower/B/task/status/post']),
|
||
|
|
);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
test(
|
||
|
|
'use-time check repairs failed task and alarm listeners while reusing healthy topics',
|
||
|
|
() async {
|
||
|
|
mqtt.failNext.add('mower/A/task/status/post');
|
||
|
|
mqtt.failNext.add('device/A/error/post');
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
await _flush();
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed.where((topic) => topic == 'device/A/error/post').length,
|
||
|
|
greaterThanOrEqualTo(2),
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed.where((topic) => topic == 'mower/A/task/status/post'),
|
||
|
|
hasLength(2),
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed.where((topic) => topic == 'mower/B/task/status/post'),
|
||
|
|
hasLength(1),
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed.where((topic) => topic == 'device/B/error/post'),
|
||
|
|
hasLength(1),
|
||
|
|
);
|
||
|
|
mqtt.task('A');
|
||
|
|
await _flush();
|
||
|
|
expect(
|
||
|
|
(await subscriptions.watchTaskStatus(siteId: 1, deviceSn: 'A').first)
|
||
|
|
?.progress,
|
||
|
|
0.25,
|
||
|
|
);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
test(
|
||
|
|
'site switch clears old panel data and ignores a late device list from an old site',
|
||
|
|
() async {
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
final values = <MowerTaskStatusEntity?>[];
|
||
|
|
final panel = subscriptions
|
||
|
|
.watchTaskStatus(siteId: 1, deviceSn: 'A')
|
||
|
|
.listen(values.add);
|
||
|
|
mqtt.task('A');
|
||
|
|
await _flush();
|
||
|
|
expect(values.last?.progress, 0.25);
|
||
|
|
devices.pending[2] = Completer();
|
||
|
|
final oldLoad = subscriptions.ensureSubscriptions(2);
|
||
|
|
await _flush();
|
||
|
|
await subscriptions.ensureSubscriptions(3);
|
||
|
|
devices.pending[2]!.complete(devices.rows[2]);
|
||
|
|
await oldLoad;
|
||
|
|
await _flush();
|
||
|
|
expect(values.last, isNull);
|
||
|
|
expect(
|
||
|
|
mqtt.unsubscribed,
|
||
|
|
containsAll(['device/A/error/post', 'mower/A/task/status/post']),
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed,
|
||
|
|
containsAll(['device/D/error/post', 'mower/D/task/status/post']),
|
||
|
|
);
|
||
|
|
expect(mqtt.subscribed, isNot(contains('mower/C/task/status/post')));
|
||
|
|
final rosterCalls = devices.calls.length;
|
||
|
|
expect(
|
||
|
|
await subscriptions.watchTaskStatus(siteId: 1, deviceSn: 'A').first,
|
||
|
|
isNull,
|
||
|
|
);
|
||
|
|
expect(devices.calls, hasLength(rosterCalls));
|
||
|
|
await panel.cancel();
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
test(
|
||
|
|
'opening a newly added device repairs the complete site roster',
|
||
|
|
() async {
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
devices.rows[1] = [_device('A'), _device('B'), _device('NEW')];
|
||
|
|
final panel = subscriptions
|
||
|
|
.watchTaskStatus(siteId: 1, deviceSn: 'NEW')
|
||
|
|
.listen((_) {});
|
||
|
|
await subscriptions.ensureSubscriptions(1);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed,
|
||
|
|
containsAll(['mower/NEW/task/status/post', 'device/NEW/error/post']),
|
||
|
|
);
|
||
|
|
expect(
|
||
|
|
mqtt.subscribed.where((topic) => topic == 'mower/A/task/status/post'),
|
||
|
|
hasLength(1),
|
||
|
|
);
|
||
|
|
await panel.cancel();
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
test(
|
||
|
|
'concurrent entry and detail checks share the same in-flight site load',
|
||
|
|
() async {
|
||
|
|
devices.pending[1] = Completer();
|
||
|
|
final first = subscriptions.ensureSubscriptions(1);
|
||
|
|
final second = subscriptions.ensureSubscriptions(1, refresh: true);
|
||
|
|
final panel = subscriptions
|
||
|
|
.watchTaskStatus(siteId: 1, deviceSn: 'A')
|
||
|
|
.listen((_) {});
|
||
|
|
await _flush();
|
||
|
|
expect(devices.calls, [1]);
|
||
|
|
devices.pending[1]!.complete(devices.rows[1]);
|
||
|
|
await Future.wait([first, second]);
|
||
|
|
await _flush();
|
||
|
|
expect(mqtt.subscribed, hasLength(6));
|
||
|
|
await panel.cancel();
|
||
|
|
await subscriptions.ensureSubscriptions(null);
|
||
|
|
expect(mqtt.unsubscribed, hasLength(6));
|
||
|
|
expect(alarms.state.siteId, isNull);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|