forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation_location_capture_test.dart
More file actions
227 lines (204 loc) · 8.12 KB
/
Copy pathconversation_location_capture_test.dart
File metadata and controls
227 lines (204 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:geolocator/geolocator.dart';
import 'package:omi/backend/schema/geolocation.dart';
import 'package:omi/services/capture/conversation_location_capture.dart';
void main() {
test('capture without upload defers the compatibility write', () async {
var uploads = 0;
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.always,
getCurrentPosition: () async => _position(latitude: 9, longitude: 8),
getLastKnownPosition: () async => null,
upload: (_) async {
uploads++;
return true;
},
);
final result = await capture.capture();
expect(result?.latitude, 9);
expect(uploads, 0);
await capture.uploadCompatibilitySnapshot(result!);
expect(uploads, 1);
});
test('uploads a fresh position and returns the recording-owned snapshot', () async {
Geolocation? uploaded;
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.always,
getCurrentPosition: () async => _position(latitude: 28.6139, longitude: 77.2090),
getLastKnownPosition: () async => null,
upload: (geolocation) async {
uploaded = geolocation;
return true;
},
);
final result = await capture.captureAndUpload();
expect(result?.latitude, 28.6139);
expect(uploaded?.longitude, 77.2090);
expect(result?.captureSource, 'current_position');
});
test('uses a fresh last-known position without waiting for GPS', () async {
var currentCalls = 0;
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.whileInUse,
getCurrentPosition: () {
currentCalls++;
return Completer<Position>().future;
},
getLastKnownPosition: () async => _position(latitude: 51.5072, longitude: -0.1276),
upload: (_) async => true,
now: () => DateTime.utc(2026, 7, 21, 0, 4),
);
final result = await capture.captureAndUpload();
expect(result?.captureSource, 'last_known_position');
expect(currentCalls, 0);
});
test('homepage check-only path does not request permission when denied', () async {
var requests = 0;
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.denied,
requestPermission: () async {
requests++;
return LocationPermission.whileInUse;
},
getCurrentPosition: () async => _position(latitude: 1, longitude: 2),
getLastKnownPosition: () async => null,
upload: (_) async => true,
);
expect(await capture.captureAndUpload(promptIfDenied: false), isNull);
expect(requests, 0);
});
test('record start requests permission and runs the post-grant hook', () async {
var requests = 0;
var grants = 0;
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.denied,
requestPermission: () async {
requests++;
return LocationPermission.whileInUse;
},
getCurrentPosition: () async => _position(latitude: 37.7749, longitude: -122.4194),
getLastKnownPosition: () async => null,
onNewlyGranted: () async {
grants++;
},
upload: (_) async => true,
);
expect(await capture.captureAndUpload(), isNotNull);
expect(requests, 1);
expect(grants, 1);
});
test('permission prompt is outside the location deadline', () async {
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.denied,
requestPermission: () async {
await Future<void>.delayed(const Duration(milliseconds: 30));
return LocationPermission.whileInUse;
},
getCurrentPosition: () async => _position(latitude: 40.7128, longitude: -74.0060),
getLastKnownPosition: () async => null,
upload: (_) async => true,
totalTimeout: const Duration(milliseconds: 10),
);
expect((await capture.captureAndUpload())?.latitude, 40.7128);
});
test('rejects stale and future-dated last-known positions when GPS does not resolve', () async {
for (final timestamp in [DateTime.utc(2026, 7, 20, 23), DateTime.utc(2026, 7, 21, 0, 20)]) {
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.whileInUse,
getCurrentPosition: () => Completer<Position>().future,
getLastKnownPosition: () async => _position(latitude: 1, longitude: 2, timestamp: timestamp),
upload: (_) async => true,
currentPositionTimeout: const Duration(milliseconds: 1),
totalTimeout: const Duration(milliseconds: 10),
now: () => DateTime.utc(2026, 7, 21, 0, 10),
);
expect(await capture.captureAndUpload(), isNull);
}
});
test('preserves the snapshot when the compatibility upload fails', () async {
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.always,
getCurrentPosition: () async => _position(latitude: 1, longitude: 2),
getLastKnownPosition: () async => null,
upload: (_) async => false,
);
expect((await capture.captureAndUpload())?.latitude, 1);
});
test('shares one deadline between capture and compatibility upload', () async {
final uploadStarted = Completer<void>();
var uploadCompleted = false;
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.always,
getCurrentPosition: () async {
await Future<void>.delayed(const Duration(milliseconds: 10));
return _position(latitude: 1, longitude: 2);
},
getLastKnownPosition: () async => null,
upload: (_) async {
uploadStarted.complete();
await Future<void>.delayed(const Duration(milliseconds: 60));
uploadCompleted = true;
return true;
},
totalTimeout: const Duration(milliseconds: 40),
);
final result = await capture.captureAndUpload();
expect(result?.latitude, 1);
expect(uploadStarted.isCompleted, isTrue);
expect(uploadCompleted, isFalse);
});
test('serializes compatibility uploads after an earlier timed-out upload', () async {
final firstUploadStarted = Completer<void>();
final releaseFirstUpload = Completer<void>();
final uploadedLatitudes = <double?>[];
var positionIndex = 0;
final capture = ConversationLocationCapture(
isLocationServiceEnabled: () async => true,
checkPermission: () async => LocationPermission.always,
getCurrentPosition: () async => _position(latitude: (++positionIndex).toDouble(), longitude: 2),
getLastKnownPosition: () async => null,
upload: (geolocation) async {
uploadedLatitudes.add(geolocation.latitude);
if (uploadedLatitudes.length == 1) {
firstUploadStarted.complete();
await releaseFirstUpload.future;
}
return true;
},
totalTimeout: const Duration(milliseconds: 10),
);
final first = capture.captureAndUpload();
await firstUploadStarted.future;
expect((await first)?.latitude, 1);
final second = capture.captureAndUpload();
expect((await second)?.latitude, 2);
expect(uploadedLatitudes, [1]);
releaseFirstUpload.complete();
await Future<void>.delayed(Duration.zero);
expect(uploadedLatitudes, [1, 2]);
});
}
Position _position({required double latitude, required double longitude, DateTime? timestamp}) {
return Position(
latitude: latitude,
longitude: longitude,
timestamp: timestamp ?? DateTime.utc(2026, 7, 21),
accuracy: 8,
altitude: 220,
altitudeAccuracy: 2,
heading: 0,
headingAccuracy: 0,
speed: 0,
speedAccuracy: 0,
);
}