feat(#825): region discovery lifecycle service (Epic #814 B1)

RegionDiscoveryService (ChangeNotifier) drives one discovery run via an injected discoverer function and exposes a distinct state for each outcome: idle/loading/success/empty/timeout/error. Regions deduped by Region value-equality. Concurrent starts ignored. Injected seam keeps it unit-testable without a live connector.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
fix/812-land-814-815-on-dev
diffwireauto 2 months ago
parent 586a481bec
commit fbfc419a7c

@ -0,0 +1,92 @@
import 'package:flutter/foundation.dart';
import '../connector/meshcore_protocol.dart';
import '../models/region.dart';
/// Where a discovery run can land. Each non-success state is distinct so the UI
/// can render it as itself (an empty reply is not success; a timeout is not an
/// error). (#814)
enum RegionDiscoveryStatus { idle, loading, success, empty, timeout, error }
/// Sends one regions-discovery request to a repeater and returns the reply, or
/// null on timeout. In production this is [MeshCoreConnector.discoverRegions];
/// tests inject a fake so the lifecycle can be exercised without a radio.
typedef RegionsDiscoverer =
Future<RegionsReply?> Function(Uint8List repeaterPubKey, Duration timeout);
/// Drives Discover Regions: ask a chosen repeater which flood regions it
/// advertises, dedupe them, and expose a single discovery state for the UI.
class RegionDiscoveryService extends ChangeNotifier {
RegionDiscoveryService(this._discover);
final RegionsDiscoverer _discover;
RegionDiscoveryStatus _status = RegionDiscoveryStatus.idle;
List<Region> _regions = const [];
int? _clock;
String? _errorMessage;
RegionDiscoveryStatus get status => _status;
List<Region> get regions => _regions;
/// The repeater's clock from the last successful reply (for clock display).
int? get clock => _clock;
/// A human-readable reason when [status] is [RegionDiscoveryStatus.error].
String? get errorMessage => _errorMessage;
bool get isLoading => _status == RegionDiscoveryStatus.loading;
/// Discover regions from [repeaterPubKey] (a 32-byte node key). Concurrent
/// starts are ignored while one is in flight.
Future<void> discover(
Uint8List repeaterPubKey, {
Duration timeout = const Duration(seconds: 10),
}) async {
if (_status == RegionDiscoveryStatus.loading) return;
_status = RegionDiscoveryStatus.loading;
_regions = const [];
_errorMessage = null;
notifyListeners();
try {
final reply = await _discover(repeaterPubKey, timeout);
if (reply == null) {
_status = RegionDiscoveryStatus.timeout;
} else {
_clock = reply.clock;
final regions = _dedupe(reply.regionNames);
if (regions.isEmpty) {
_status = RegionDiscoveryStatus.empty;
} else {
_regions = regions;
_status = RegionDiscoveryStatus.success;
}
}
} catch (e) {
_status = RegionDiscoveryStatus.error;
_errorMessage = e.toString();
}
notifyListeners();
}
/// Return to the idle state, clearing any prior result.
void reset() {
_status = RegionDiscoveryStatus.idle;
_regions = const [];
_clock = null;
_errorMessage = null;
notifyListeners();
}
/// Dedupe by [Region] value-equality, preserving first-seen order.
List<Region> _dedupe(List<String> names) {
final seen = <Region>{};
final out = <Region>[];
for (final name in names) {
final region = Region(name);
if (seen.add(region)) out.add(region);
}
return out;
}
}

@ -0,0 +1,84 @@
// Epic #814 T B1: region discovery lifecycle service.
// Uses an injected discoverer function as the seam, so no live connector or
// drift codegen is needed to exercise every state.
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/models/region.dart';
import 'package:meshcore_open/services/region_discovery_service.dart';
void main() {
final pub = Uint8List(32);
RegionsReply reply(List<String> names) =>
RegionsReply(tag: 1, clock: 42, regionNames: names);
test('success exposes deduped regions in order', () async {
final svc = RegionDiscoveryService(
(_, _) async => reply(['oki', 'test', 'oki']),
);
await svc.discover(pub);
expect(svc.status, RegionDiscoveryStatus.success);
expect(svc.regions, const [Region('oki'), Region('test')]);
expect(svc.clock, 42);
});
test('empty reply surfaces empty, never success', () async {
final svc = RegionDiscoveryService((_, _) async => reply([]));
await svc.discover(pub);
expect(svc.status, RegionDiscoveryStatus.empty);
expect(svc.regions, isEmpty);
});
test('null reply surfaces timeout', () async {
final svc = RegionDiscoveryService((_, _) async => null);
await svc.discover(pub);
expect(svc.status, RegionDiscoveryStatus.timeout);
});
test('a thrown error (e.g. unsupported firmware) surfaces error', () async {
final svc = RegionDiscoveryService(
(_, _) async =>
throw StateError('firmware does not support region scoping'),
);
await svc.discover(pub);
expect(svc.status, RegionDiscoveryStatus.error);
expect(svc.errorMessage, contains('region scoping'));
});
test(
'status is loading synchronously and notifies on start + finish',
() async {
var notifications = 0;
final svc = RegionDiscoveryService((_, _) async => reply(['x']));
svc.addListener(() => notifications++);
final future = svc.discover(pub);
expect(svc.status, RegionDiscoveryStatus.loading);
await future;
expect(svc.status, RegionDiscoveryStatus.success);
expect(notifications, greaterThanOrEqualTo(2));
},
);
test('a second discover while loading is ignored', () async {
var calls = 0;
final svc = RegionDiscoveryService((_, _) async {
calls++;
await Future<void>.delayed(const Duration(milliseconds: 20));
return reply(['a']);
});
final first = svc.discover(pub);
await svc.discover(pub); // no-op while the first is in flight
await first;
expect(calls, 1);
});
test('reset returns to idle', () async {
final svc = RegionDiscoveryService((_, _) async => reply(['a']));
await svc.discover(pub);
svc.reset();
expect(svc.status, RegionDiscoveryStatus.idle);
expect(svc.regions, isEmpty);
});
}
Loading…
Cancel
Save

Powered by TurnKey Linux.