mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
feat: Add MET history isolation
This commit is contained in:
69
test/providers/contacts_provider_profile_scope_test.dart
Normal file
69
test/providers/contacts_provider_profile_scope_test.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_sar_app/models/contact.dart';
|
||||
import 'package:meshcore_sar_app/providers/contacts_provider.dart';
|
||||
import 'package:meshcore_sar_app/services/contact_storage_service.dart';
|
||||
import 'package:meshcore_sar_app/services/profiles_feature_service.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
Contact createContact({
|
||||
required Uint8List key,
|
||||
required String name,
|
||||
}) {
|
||||
return Contact(
|
||||
publicKey: key,
|
||||
type: ContactType.chat,
|
||||
flags: 0,
|
||||
outPathLen: 0,
|
||||
outPath: Uint8List(64),
|
||||
advName: name,
|
||||
lastAdvert: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
advLat: (46.0569 * 1e6).round(),
|
||||
advLon: (14.5058 * 1e6).round(),
|
||||
lastMod: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
);
|
||||
}
|
||||
|
||||
setUp(() {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
ProfileStorageScope.setScope(
|
||||
profilesEnabled: true,
|
||||
activeProfileId: 'default',
|
||||
);
|
||||
});
|
||||
|
||||
test('initializeEarly respects the active profile storage namespace', () async {
|
||||
final storage = ContactStorageService();
|
||||
final defaultContact = createContact(
|
||||
key: Uint8List.fromList(List<int>.filled(32, 1)),
|
||||
name: 'Default Contact',
|
||||
);
|
||||
final alphaContact = createContact(
|
||||
key: Uint8List.fromList(List<int>.filled(32, 2)),
|
||||
name: 'Alpha Contact',
|
||||
);
|
||||
|
||||
await storage.saveContacts([defaultContact]);
|
||||
await storage.saveContacts([alphaContact], namespace: 'alpha');
|
||||
|
||||
ProfileStorageScope.setScope(
|
||||
profilesEnabled: true,
|
||||
activeProfileId: 'alpha',
|
||||
);
|
||||
|
||||
final provider = ContactsProvider();
|
||||
await provider.initializeEarly();
|
||||
|
||||
final names = provider.contacts
|
||||
.where((contact) => !contact.isChannel)
|
||||
.map((contact) => contact.advName)
|
||||
.toList();
|
||||
|
||||
expect(names, <String>['Alpha Contact']);
|
||||
expect(provider.storageNamespace, 'alpha');
|
||||
});
|
||||
}
|
||||
55
test/services/bthome_met_history_test.dart
Normal file
55
test/services/bthome_met_history_test.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_sar_app/models/contact.dart';
|
||||
import 'package:meshcore_sar_app/services/bthome_met_history.dart';
|
||||
|
||||
void main() {
|
||||
test('parses a valid BTHome MET history response', () {
|
||||
final parsed = BTHomeMetHistoryParser.parse('1,0,4,11.2,11.8,12.1,12.3');
|
||||
|
||||
expect(parsed.measurement, BTHomeMetMeasurement.temperature);
|
||||
expect(parsed.page, 0);
|
||||
expect(parsed.values, <double>[11.2, 11.8, 12.1, 12.3]);
|
||||
expect(parsed.latest, 12.3);
|
||||
expect(parsed.minimum, 11.2);
|
||||
expect(parsed.maximum, 12.3);
|
||||
});
|
||||
|
||||
test('rejects malformed BTHome MET history counts', () {
|
||||
expect(
|
||||
() => BTHomeMetHistoryParser.parse('2,0,3,41,42'),
|
||||
throwsA(isA<BTHomeMetHistoryFormatException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('detects available BTHome MET measurements from telemetry', () {
|
||||
final contact = Contact(
|
||||
publicKey: Uint8List(32),
|
||||
type: ContactType.sensor,
|
||||
flags: 0,
|
||||
outPathLen: 0,
|
||||
outPath: Uint8List(64),
|
||||
advName: 'WX',
|
||||
lastAdvert: 0,
|
||||
advLat: 0,
|
||||
advLon: 0,
|
||||
lastMod: 0,
|
||||
telemetry: ContactTelemetry(
|
||||
temperature: 20.1,
|
||||
humidity: 52,
|
||||
extraSensorData: const {'speed_2': 3.1, 'gust_2': 4.8, 'rain_2': 12.3},
|
||||
timestamp: DateTime(2026, 3, 21, 12),
|
||||
),
|
||||
);
|
||||
|
||||
expect(bTHomeMetMeasurementsForContact(contact), <BTHomeMetMeasurement>[
|
||||
BTHomeMetMeasurement.temperature,
|
||||
BTHomeMetMeasurement.humidity,
|
||||
BTHomeMetMeasurement.windSpeed,
|
||||
BTHomeMetMeasurement.gust,
|
||||
BTHomeMetMeasurement.rain,
|
||||
]);
|
||||
expect(supportsBTHomeMetHistory(contact), isTrue);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user