140 lines
4.4 KiB
Dart
140 lines
4.4 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
||
import 'package:latlong2/latlong.dart';
|
||
import 'package:maibu_satabot_v2/features/pv_control/presentation/models/work_trace_recorder.dart';
|
||
|
||
void main() {
|
||
const a = LatLng(31.1, 120.1);
|
||
const b = LatLng(31.1001, 120.1001);
|
||
const c = LatLng(31.1002, 120.1002);
|
||
const d = LatLng(31.1003, 120.1003);
|
||
late WorkTraceRecorder recorder;
|
||
|
||
void sync({
|
||
String device = 'device',
|
||
String? task = '42',
|
||
bool recording = true,
|
||
bool visible = true,
|
||
List<LatLng> completed = const [],
|
||
}) {
|
||
recorder.synchronize(
|
||
deviceId: device,
|
||
input: WorkTraceInput(
|
||
taskId: task,
|
||
recording: recording,
|
||
visible: visible,
|
||
completedPoints: completed,
|
||
),
|
||
);
|
||
}
|
||
|
||
setUp(() => recorder = WorkTraceRecorder());
|
||
|
||
test('未开始、无设备或无任务时不累计定位', () {
|
||
sync(recording: false);
|
||
expect(recorder.addPosition(a), isFalse);
|
||
for (final input in [
|
||
const WorkTraceInput(taskId: '42', recording: true, visible: false),
|
||
const WorkTraceInput(taskId: null, recording: true, visible: true),
|
||
]) {
|
||
recorder.synchronize(deviceId: 'device', input: input);
|
||
expect(recorder.addPosition(a), isFalse);
|
||
expect(recorder.points, isEmpty);
|
||
}
|
||
sync(device: '');
|
||
expect(recorder.addPosition(a), isFalse);
|
||
});
|
||
|
||
test('首个到达点之前按定位累计,两个有效位置即可画线', () {
|
||
sync();
|
||
expect(recorder.addPosition(a), isTrue);
|
||
expect(recorder.addPosition(b), isTrue);
|
||
expect(recorder.addPosition(b), isFalse);
|
||
expect(recorder.points, [a, b]);
|
||
expect(recorder.hasCompletedPoints, isFalse);
|
||
});
|
||
|
||
test('首个到达点切换为到达点加最新车位,不保留之前的GPS折线', () {
|
||
sync();
|
||
recorder.addPosition(a);
|
||
recorder.addPosition(b);
|
||
sync(completed: [a]);
|
||
expect(recorder.hasCompletedPoints, isTrue);
|
||
expect(recorder.points, [a, b]);
|
||
recorder.addPosition(c);
|
||
recorder.addPosition(d);
|
||
expect(recorder.points, [a, d]);
|
||
});
|
||
|
||
test('仅收到一个到达点时,下一次定位就能组成绿色线段', () {
|
||
sync(completed: [a]);
|
||
expect(recorder.points, [a]);
|
||
recorder.addPosition(b);
|
||
expect(recorder.points, [a, b]);
|
||
});
|
||
|
||
test('新到达点替代实时尾点,重合坐标不产生重复尾点', () {
|
||
sync(completed: [a]);
|
||
recorder.addPosition(b);
|
||
sync(completed: [a, b]);
|
||
expect(recorder.points, [a, b]);
|
||
recorder.addPosition(c);
|
||
expect(recorder.points, [a, b, c]);
|
||
expect(recorder.addPosition(c), isFalse);
|
||
});
|
||
|
||
test('暂停冻结尾点,恢复后接入当前位置', () {
|
||
sync(completed: [a]);
|
||
recorder.addPosition(b);
|
||
sync(recording: false, completed: [a]);
|
||
expect(recorder.addPosition(c), isFalse);
|
||
expect(recorder.points, [a, b]);
|
||
sync(completed: [a]);
|
||
expect(recorder.addPosition(c), isTrue);
|
||
expect(recorder.points, [a, c]);
|
||
});
|
||
|
||
test('结束隐藏并清空,不能带着旧GPS开始下一次记录', () {
|
||
sync();
|
||
recorder.addPosition(a);
|
||
recorder.addPosition(b);
|
||
sync(visible: false, recording: false, completed: [a]);
|
||
expect(recorder.points, isEmpty);
|
||
sync();
|
||
recorder.addPosition(c);
|
||
expect(recorder.points, [c]);
|
||
});
|
||
|
||
test('切换任务或设备清理之前的GPS和到达点', () {
|
||
sync(completed: [a]);
|
||
recorder.addPosition(b);
|
||
sync(task: '43');
|
||
expect(recorder.points, isEmpty);
|
||
recorder.addPosition(c);
|
||
sync(device: 'other-device', task: '43');
|
||
recorder.addPosition(d);
|
||
expect(recorder.points, [d]);
|
||
});
|
||
|
||
test('GPS轨迹限制长度并忽略小于阈值的抖动', () {
|
||
recorder = WorkTraceRecorder(maxGpsPoints: 2);
|
||
sync();
|
||
recorder.addPosition(a);
|
||
expect(recorder.addPosition(const LatLng(31.1000001, 120.1)), isFalse);
|
||
recorder.addPosition(b);
|
||
recorder.addPosition(c);
|
||
expect(recorder.points, [b, c]);
|
||
});
|
||
|
||
test('过滤无效坐标并复制输入,外部不能更改已记录的轨迹', () {
|
||
final completed = [a, a, const LatLng(0, 120), b];
|
||
sync(completed: completed);
|
||
completed.clear();
|
||
expect(recorder.points, [a, b]);
|
||
expect(recorder.addPosition(const LatLng(31, 0)), isFalse);
|
||
expect(() => recorder.points.add(c), throwsUnsupportedError);
|
||
recorder.reset();
|
||
expect(recorder.points, isEmpty);
|
||
expect(recorder.addPosition(c), isFalse);
|
||
});
|
||
}
|