314 lines
11 KiB
Dart
314 lines
11 KiB
Dart
|
|
import 'dart:async';
|
||
|
|
import 'dart:convert';
|
||
|
|
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||
|
|
import 'package:flutter_test/flutter_test.dart';
|
||
|
|
import 'package:maibu_satabot_v2/core/localization/app_localizations.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/v2/device_list/presentation/widgets/autonomous/work_task_status_card.dart';
|
||
|
|
|
||
|
|
import 'support/region_test_localizations.dart';
|
||
|
|
|
||
|
|
const _sn = 'STXZ0126101500001198D';
|
||
|
|
const _topic = 'mower/$_sn/task/status/post';
|
||
|
|
|
||
|
|
Map<String, dynamic> _sample() => {
|
||
|
|
'protocol_version': '1.4.7',
|
||
|
|
'message_id': '$_sn-00009349',
|
||
|
|
'sn': _sn,
|
||
|
|
'task_id': '33394',
|
||
|
|
'command_id': '33394_1794815329286',
|
||
|
|
'mode': 'coverage',
|
||
|
|
'status': 'running',
|
||
|
|
'timestamp': 1794815389009,
|
||
|
|
'data': {
|
||
|
|
'distance_remaining': 91.3607406616211,
|
||
|
|
'number_of_recoveries': 1,
|
||
|
|
'elapsed_s': 57.541494845,
|
||
|
|
'eta_s': 569.789080477,
|
||
|
|
'progress': 0.09172400832176208,
|
||
|
|
'current_cell_index': 0,
|
||
|
|
'total_cells': 1,
|
||
|
|
'distance_traveled': 13.053206443786621,
|
||
|
|
'speed': 0.13422368466854095,
|
||
|
|
'phase': 3,
|
||
|
|
'task_phase': 3,
|
||
|
|
'phase_name': 'COVERING',
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
class _Mqtt extends Fake implements MqttClient {
|
||
|
|
final messages = StreamController<MqttMessage>.broadcast();
|
||
|
|
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 send(Map<String, dynamic> json, {String topic = _topic, DateTime? at}) {
|
||
|
|
messages.add(
|
||
|
|
MqttMessage(topic: topic, payload: jsonEncode(json), timestamp: at),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class _UiSubscriptions extends Fake implements SiteDeviceRealtimeSubscriptions {
|
||
|
|
_UiSubscriptions(this.repository);
|
||
|
|
final MowerRealtimeRepositoryImpl repository;
|
||
|
|
|
||
|
|
@override
|
||
|
|
Stream<MowerTaskStatusEntity?> watchTaskStatus({
|
||
|
|
required int? siteId,
|
||
|
|
required String deviceSn,
|
||
|
|
}) => repository.watchTaskStatus(deviceSn);
|
||
|
|
}
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||
|
|
setUpAll(RegionTestLocalizations.loadAssets);
|
||
|
|
|
||
|
|
test('parses the supplied protocol sample without losing precision', () {
|
||
|
|
final status = MowerTaskStatusEntity.fromJson(_sample());
|
||
|
|
expect(status.protocolVersion, '1.4.7');
|
||
|
|
expect(status.sn, _sn);
|
||
|
|
expect(status.taskId, '33394');
|
||
|
|
expect(status.status, 'running');
|
||
|
|
expect(status.phaseName, 'COVERING');
|
||
|
|
expect(status.progress, closeTo(0.091724, 0.000001));
|
||
|
|
expect(status.distanceRemaining, closeTo(91.36074, 0.00001));
|
||
|
|
expect(status.currentCellIndex, 0);
|
||
|
|
expect(status.numberOfRecoveries, 1);
|
||
|
|
expect(status.timestamp, 1794815389009);
|
||
|
|
expect(status.raw, _sample());
|
||
|
|
});
|
||
|
|
|
||
|
|
test('missing and invalid values stay unknown while real zeros survive', () {
|
||
|
|
for (final data in [null, [], 'bad', <String, dynamic>{}]) {
|
||
|
|
final status = MowerTaskStatusEntity.fromJson({'data': data});
|
||
|
|
expect(status.taskId, isNull);
|
||
|
|
expect(status.progress, isNull);
|
||
|
|
expect(status.elapsedSeconds, isNull);
|
||
|
|
expect(status.numberOfRecoveries, isNull);
|
||
|
|
expect(status.currentCellIndex, isNull);
|
||
|
|
expect(status.speed, isNull);
|
||
|
|
}
|
||
|
|
final zeros = MowerTaskStatusEntity.fromJson({
|
||
|
|
'data': {'progress': 0, 'speed': 0, 'number_of_recoveries': 0},
|
||
|
|
});
|
||
|
|
expect(zeros.progress, 0);
|
||
|
|
expect(zeros.speed, 0);
|
||
|
|
expect(zeros.numberOfRecoveries, 0);
|
||
|
|
final invalid = MowerTaskStatusEntity.fromJson({
|
||
|
|
'sn': '',
|
||
|
|
'status': {},
|
||
|
|
'data': {
|
||
|
|
'progress': 9.17,
|
||
|
|
'speed': 'NaN',
|
||
|
|
'elapsed_s': -1,
|
||
|
|
'total_cells': 1.5,
|
||
|
|
'eta_s': true,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
expect(invalid.sn, isNull);
|
||
|
|
expect(invalid.status, isNull);
|
||
|
|
expect(invalid.progress, isNull);
|
||
|
|
expect(invalid.speed, isNull);
|
||
|
|
expect(invalid.elapsedSeconds, isNull);
|
||
|
|
expect(invalid.totalCells, isNull);
|
||
|
|
expect(invalid.etaSeconds, isNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
test(
|
||
|
|
'subscribes by SN, ignores foreign/malformed messages and releases topic',
|
||
|
|
() async {
|
||
|
|
final mqtt = _Mqtt();
|
||
|
|
final source = MowerRealtimeDataSourceImpl(mqtt);
|
||
|
|
final reports = <MowerTaskStatusEntity>[];
|
||
|
|
final subscription = source.watchTaskStatus(_sn).listen(reports.add);
|
||
|
|
expect(mqtt.subscribed, [_topic]);
|
||
|
|
mqtt.send(_sample(), topic: 'mower/other/task/status/post');
|
||
|
|
mqtt.send({..._sample(), 'sn': 'other'});
|
||
|
|
mqtt.messages.add(MqttMessage(topic: _topic, payload: '{broken'));
|
||
|
|
mqtt.messages.add(MqttMessage(topic: _topic, payload: '[]'));
|
||
|
|
mqtt.send(_sample());
|
||
|
|
// A partial frame replaces the previous values instead of retaining them.
|
||
|
|
mqtt.send({
|
||
|
|
'task_id': '33394',
|
||
|
|
'data': {'speed': 0},
|
||
|
|
});
|
||
|
|
await Future<void>.delayed(Duration.zero);
|
||
|
|
expect(reports, hasLength(2));
|
||
|
|
expect(reports.last.speed, 0);
|
||
|
|
expect(reports.last.progress, isNull);
|
||
|
|
await subscription.cancel();
|
||
|
|
expect(mqtt.unsubscribed, [_topic]);
|
||
|
|
source.dispose();
|
||
|
|
await mqtt.messages.close();
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
late _Mqtt mqtt;
|
||
|
|
late MowerRealtimeDataSourceImpl source;
|
||
|
|
late MowerRealtimeRepositoryImpl repository;
|
||
|
|
late _UiSubscriptions subscriptions;
|
||
|
|
|
||
|
|
setUp(() {
|
||
|
|
mqtt = _Mqtt();
|
||
|
|
source = MowerRealtimeDataSourceImpl(mqtt);
|
||
|
|
repository = MowerRealtimeRepositoryImpl(source);
|
||
|
|
subscriptions = _UiSubscriptions(repository);
|
||
|
|
});
|
||
|
|
tearDown(() async {
|
||
|
|
source.dispose();
|
||
|
|
await mqtt.messages.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
Future<void> mount(
|
||
|
|
WidgetTester tester, {
|
||
|
|
String taskId = '33394',
|
||
|
|
bool compact = false,
|
||
|
|
Locale locale = AppLocalizations.chinese,
|
||
|
|
}) async {
|
||
|
|
await tester.pumpWidget(
|
||
|
|
MaterialApp(
|
||
|
|
locale: locale,
|
||
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
||
|
|
localizationsDelegates: const [
|
||
|
|
RegionTestLocalizations(),
|
||
|
|
GlobalMaterialLocalizations.delegate,
|
||
|
|
GlobalWidgetsLocalizations.delegate,
|
||
|
|
GlobalCupertinoLocalizations.delegate,
|
||
|
|
],
|
||
|
|
home: Scaffold(
|
||
|
|
body: Align(
|
||
|
|
alignment: Alignment.topLeft,
|
||
|
|
child: SizedBox(
|
||
|
|
width: compact ? 178 : 248,
|
||
|
|
child: WorkTaskStatusCard(
|
||
|
|
subscriptions: subscriptions,
|
||
|
|
siteId: 1,
|
||
|
|
deviceSn: _sn,
|
||
|
|
expectedTaskId: taskId,
|
||
|
|
compact: compact,
|
||
|
|
maxHeight: compact ? 200 : 540,
|
||
|
|
onCollapse: () {},
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
await tester.pumpAndSettle();
|
||
|
|
}
|
||
|
|
|
||
|
|
testWidgets(
|
||
|
|
'shows unknown before reports and clears values omitted in the next frame',
|
||
|
|
(tester) async {
|
||
|
|
await mount(tester);
|
||
|
|
expect(find.text('等待任务状态上报'), findsOneWidget);
|
||
|
|
expect(find.text('未知'), findsWidgets);
|
||
|
|
mqtt.send(_sample());
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
expect(find.text('9.17%'), findsOneWidget);
|
||
|
|
expect(find.text('执行中'), findsOneWidget);
|
||
|
|
expect(find.text('正在覆盖'), findsOneWidget);
|
||
|
|
expect(find.text('91.36 m'), findsOneWidget);
|
||
|
|
expect(find.text('09:30'), findsOneWidget);
|
||
|
|
mqtt.send({
|
||
|
|
'task_id': '33394',
|
||
|
|
'data': {'progress': 0, 'speed': 0},
|
||
|
|
});
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
expect(find.text('0.00%'), findsOneWidget);
|
||
|
|
expect(find.text('0.00 m/s'), findsOneWidget);
|
||
|
|
expect(find.text('91.36 m'), findsNothing);
|
||
|
|
expect(find.text('执行中'), findsNothing);
|
||
|
|
expect(find.text('未知'), findsWidgets);
|
||
|
|
await tester.pumpWidget(const SizedBox());
|
||
|
|
expect(find.byType(WorkTaskStatusCard), findsNothing);
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
// Widget disposal removes its stream listener immediately. The data-source
|
||
|
|
// test above awaits cancellation and verifies the eventual MQTT release.
|
||
|
|
expect(mqtt.messages.hasListener, isFalse);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
testWidgets(
|
||
|
|
'does not show another task or accept older frames; switching clears the old task',
|
||
|
|
(tester) async {
|
||
|
|
await mount(tester);
|
||
|
|
mqtt.send({..._sample(), 'task_id': 'other'});
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
expect(find.text('9.17%'), findsNothing);
|
||
|
|
mqtt.send(_sample());
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
mqtt.send({..._sample(), 'status': 'paused', 'timestamp': 1794815388000});
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
expect(find.text('执行中'), findsOneWidget);
|
||
|
|
expect(find.text('已暂停'), findsNothing);
|
||
|
|
await mount(tester, taskId: 'new-task');
|
||
|
|
expect(find.text('9.17%'), findsNothing);
|
||
|
|
expect(find.text('等待任务状态上报'), findsOneWidget);
|
||
|
|
await tester.pumpWidget(const SizedBox());
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
testWidgets(
|
||
|
|
'compact card scrolls without overflow, marks stale reports and exposes raw fields',
|
||
|
|
(tester) async {
|
||
|
|
await mount(tester, compact: true);
|
||
|
|
mqtt.send(
|
||
|
|
_sample(),
|
||
|
|
at: DateTime.now().subtract(const Duration(seconds: 20)),
|
||
|
|
);
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
expect(find.text('数据暂未更新,以下为最后一次上报'), findsOneWidget);
|
||
|
|
expect(tester.takeException(), isNull);
|
||
|
|
await tester.ensureVisible(find.text('报文详情'));
|
||
|
|
await tester.tap(find.text('报文详情'));
|
||
|
|
await tester.pumpAndSettle();
|
||
|
|
expect(find.text('phase'), findsOneWidget);
|
||
|
|
expect(find.text('task_phase'), findsOneWidget);
|
||
|
|
expect(find.text('current_cell_index'), findsOneWidget);
|
||
|
|
expect(tester.takeException(), isNull);
|
||
|
|
await tester.pumpWidget(const SizedBox());
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
testWidgets(
|
||
|
|
'English compact layout keeps unknown fields and long IDs readable',
|
||
|
|
(tester) async {
|
||
|
|
await mount(tester, compact: true, locale: AppLocalizations.english);
|
||
|
|
expect(find.text('Unknown'), findsWidgets);
|
||
|
|
mqtt.send(_sample());
|
||
|
|
await tester.pump();
|
||
|
|
await tester.pump();
|
||
|
|
await tester.ensureVisible(find.text('Report details'));
|
||
|
|
await tester.tap(find.text('Report details'));
|
||
|
|
await tester.pumpAndSettle();
|
||
|
|
expect(find.text('$_sn-00009349'), findsOneWidget);
|
||
|
|
expect(tester.takeException(), isNull);
|
||
|
|
await tester.pumpWidget(const SizedBox());
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|