fix(#1022): stop channel adds overwriting occupied slots, including Public
Adding #qcchams and #qcchhv from the phone lost Public, from the client and the radio. Three faults combined, all client-side; the firmware's CMD_SET_CHANNEL is a plain indexed write by design and did what it was told. 1. The slot was chosen when the add dialog opened (_showAddChannelDialog computed nextIndex before showDialog and closed over it), so it was stale by the time Add was pressed. 2. The connector empties its channel list for the duration of a sync and refills it slot by slot, so a dialog opened mid-sync saw every slot as free. 3. _findNextAvailableIndex answered 'no free slot' with return 0, which is where Public normally lives, turning a full radio into a silent overwrite of Public. The decision moves into a pure helper, decideChannelSlot, called at the moment of each write and never earlier. It refuses while isSyncingChannels is true and refuses when full, returning a named reason rather than slot 0. Deliberately isSyncingChannels and not hasLoadedChannels: the latter stays false after a FAILED sync while the cached list is restored into use, so gating on it would disable adding a channel permanently on a flaky link. Add Public no longer hardcodes slot 0, which it wrote with no occupancy check. Public is identified by its PSK, not its index, so it takes a free slot like any other channel, and is refused if it is already present. Owner ruling: block the unsafe write but keep the control live, since a greyed-out button is itself a silent failure. Every refusal explains itself and passes persist: true, because showDismissibleSnackBar defaults to a 4 second toast that SAFELANE 6 rules out for an error. A refused add leaves the dialog open so the user can retry. Mutation-tested rather than trusted: restoring return 0 fails 2 tests, and removing the sync guard fails 4, including the exact reported scenario. Full suite 1779 pass. New strings are English-only with the regenerated untranslated.json, matching the convention of every recent string-adding commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>fix/1896-client-slot0-public
parent
3364895a9f
commit
0506738925
@ -0,0 +1,60 @@
|
||||
import '../models/channel.dart';
|
||||
|
||||
/// Why a channel slot could not be allocated.
|
||||
enum ChannelSlotRefusal {
|
||||
/// The channel list is mid-rewrite, so no slot can be trusted to be free.
|
||||
syncing,
|
||||
|
||||
/// Every slot on the radio is in use.
|
||||
full,
|
||||
}
|
||||
|
||||
/// Either a slot that is safe to write, or the reason it was refused.
|
||||
class ChannelSlotDecision {
|
||||
const ChannelSlotDecision.slot(int this.slot) : refusal = null;
|
||||
const ChannelSlotDecision.refused(ChannelSlotRefusal this.refusal)
|
||||
: slot = null;
|
||||
|
||||
final int? slot;
|
||||
final ChannelSlotRefusal? refusal;
|
||||
|
||||
bool get isAllowed => slot != null;
|
||||
}
|
||||
|
||||
/// Decide which channel slot a new channel may occupy (#1021).
|
||||
///
|
||||
/// Pulled out of the channels screen so it can be tested without a widget, and
|
||||
/// so the QR and config-import paths can share one answer instead of each
|
||||
/// carrying their own (there were three divergent implementations, and only one
|
||||
/// of them had the bug).
|
||||
///
|
||||
/// Two refusals, both deliberate:
|
||||
///
|
||||
/// * [isSyncing] true means the connector has emptied its channel list and is
|
||||
/// refilling it slot by slot. Any index would look free. Choosing one here is
|
||||
/// what overwrote Public: a slot picked during a sync pointed at an occupied
|
||||
/// slot by the time it was written.
|
||||
/// * No free index means the radio is full. The caller must be told, never
|
||||
/// handed slot 0 as a consolation, which is the old `return 0` bug and is
|
||||
/// where Public normally lives.
|
||||
///
|
||||
/// Callers pass the connector's `isSyncingChannels`, not `hasLoadedChannels`:
|
||||
/// the latter stays false after a *failed* sync while the cached list is
|
||||
/// restored into use, so gating on it would refuse forever on a flaky link.
|
||||
///
|
||||
/// An absent index genuinely means a free slot, because the connector never
|
||||
/// adds an empty channel to its list.
|
||||
ChannelSlotDecision decideChannelSlot({
|
||||
required List<Channel> channels,
|
||||
required int maxChannels,
|
||||
required bool isSyncing,
|
||||
}) {
|
||||
if (isSyncing) {
|
||||
return const ChannelSlotDecision.refused(ChannelSlotRefusal.syncing);
|
||||
}
|
||||
final used = channels.map((c) => c.index).toSet();
|
||||
for (var i = 0; i < maxChannels; i++) {
|
||||
if (!used.contains(i)) return ChannelSlotDecision.slot(i);
|
||||
}
|
||||
return const ChannelSlotDecision.refused(ChannelSlotRefusal.full);
|
||||
}
|
||||
@ -0,0 +1,181 @@
|
||||
// Channel slot allocation (#1021). Adding a channel could overwrite another,
|
||||
// including Public, losing it from the client and the radio.
|
||||
//
|
||||
// Three faults combined: the slot was chosen when the add dialog opened, the
|
||||
// connector empties its channel list during a sync so any index looked free,
|
||||
// and the chooser answered "no free slot" with slot 0, which is where Public
|
||||
// normally lives. These pin the decision so none of them can come back.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/helpers/channel_slot_decision.dart';
|
||||
import 'package:meshcore_open/models/channel.dart';
|
||||
|
||||
Channel _ch(int index, {String name = 'ch', String? pskHex}) => Channel(
|
||||
index: index,
|
||||
name: name,
|
||||
psk: Channel.parsePskHex(pskHex ?? '00112233445566778899aabbccddeeff'),
|
||||
);
|
||||
|
||||
void main() {
|
||||
group('decideChannelSlot', () {
|
||||
test('takes the lowest free slot', () {
|
||||
final d = decideChannelSlot(
|
||||
channels: [_ch(0), _ch(1), _ch(3)],
|
||||
maxChannels: 8,
|
||||
isSyncing: false,
|
||||
);
|
||||
expect(d.isAllowed, isTrue);
|
||||
expect(d.slot, 2);
|
||||
expect(d.refusal, isNull);
|
||||
});
|
||||
|
||||
test('takes slot 0 when the radio is empty', () {
|
||||
final d = decideChannelSlot(
|
||||
channels: const [],
|
||||
maxChannels: 8,
|
||||
isSyncing: false,
|
||||
);
|
||||
expect(d.slot, 0);
|
||||
});
|
||||
|
||||
test('refuses with full, NEVER slot 0, when every slot is used', () {
|
||||
final d = decideChannelSlot(
|
||||
channels: [for (var i = 0; i < 8; i++) _ch(i)],
|
||||
maxChannels: 8,
|
||||
isSyncing: false,
|
||||
);
|
||||
expect(d.isAllowed, isFalse);
|
||||
expect(d.refusal, ChannelSlotRefusal.full);
|
||||
expect(
|
||||
d.slot,
|
||||
isNull,
|
||||
reason: 'the old return 0 fallback is what destroyed Public',
|
||||
);
|
||||
});
|
||||
|
||||
test('refuses while syncing, even though every slot looks free', () {
|
||||
// This is the reported bug. Mid-sync the connector has emptied its list,
|
||||
// so an empty list plus a free-looking slot 0 is exactly the state that
|
||||
// overwrote Public.
|
||||
final d = decideChannelSlot(
|
||||
channels: const [],
|
||||
maxChannels: 8,
|
||||
isSyncing: true,
|
||||
);
|
||||
expect(d.isAllowed, isFalse);
|
||||
expect(d.refusal, ChannelSlotRefusal.syncing);
|
||||
expect(d.slot, isNull);
|
||||
});
|
||||
|
||||
test('syncing is refused even when a slot genuinely is free', () {
|
||||
final d = decideChannelSlot(
|
||||
channels: [_ch(0)],
|
||||
maxChannels: 8,
|
||||
isSyncing: true,
|
||||
);
|
||||
expect(d.refusal, ChannelSlotRefusal.syncing);
|
||||
});
|
||||
|
||||
test('syncing takes precedence over full', () {
|
||||
final d = decideChannelSlot(
|
||||
channels: [for (var i = 0; i < 8; i++) _ch(i)],
|
||||
maxChannels: 8,
|
||||
isSyncing: true,
|
||||
);
|
||||
expect(
|
||||
d.refusal,
|
||||
ChannelSlotRefusal.syncing,
|
||||
reason: 'a list being rewritten cannot be trusted to say it is full',
|
||||
);
|
||||
});
|
||||
|
||||
// The regression test for the gate I nearly got wrong. After a FAILED sync
|
||||
// the connector leaves hasLoadedChannels false but restores the cached
|
||||
// list into use. Gating on that flag would refuse forever on a flaky link,
|
||||
// so the decision takes isSyncing only, and a restored list allocates
|
||||
// normally.
|
||||
test('allocates normally after a failed sync, from the restored list', () {
|
||||
final d = decideChannelSlot(
|
||||
channels: [_ch(0), _ch(1)],
|
||||
maxChannels: 8,
|
||||
isSyncing: false,
|
||||
);
|
||||
expect(
|
||||
d.isAllowed,
|
||||
isTrue,
|
||||
reason: 'a failed sync must not disable adding channels',
|
||||
);
|
||||
expect(d.slot, 2);
|
||||
});
|
||||
|
||||
test('a single-slot radio can be filled and then refuses', () {
|
||||
expect(
|
||||
decideChannelSlot(
|
||||
channels: const [],
|
||||
maxChannels: 1,
|
||||
isSyncing: false,
|
||||
).slot,
|
||||
0,
|
||||
);
|
||||
expect(
|
||||
decideChannelSlot(
|
||||
channels: [_ch(0)],
|
||||
maxChannels: 1,
|
||||
isSyncing: false,
|
||||
).refusal,
|
||||
ChannelSlotRefusal.full,
|
||||
);
|
||||
});
|
||||
|
||||
test('an out-of-range index in the list does not block a free slot', () {
|
||||
// Defensive: a stale or odd index must not make the radio look full.
|
||||
final d = decideChannelSlot(
|
||||
channels: [_ch(99)],
|
||||
maxChannels: 4,
|
||||
isSyncing: false,
|
||||
);
|
||||
expect(d.slot, 0);
|
||||
});
|
||||
|
||||
test('Public holds no claim on slot 0, since it is identified by PSK', () {
|
||||
// `Channel.isPublicChannel` compares the PSK, not the index, so Public
|
||||
// can live anywhere and nothing may assume slot 0 belongs to it.
|
||||
final public = Channel(
|
||||
index: 3,
|
||||
name: 'Public',
|
||||
psk: Channel.parsePskHex(Channel.publicChannelPsk),
|
||||
);
|
||||
expect(public.isPublicChannel, isTrue);
|
||||
final d = decideChannelSlot(
|
||||
channels: [public],
|
||||
maxChannels: 8,
|
||||
isSyncing: false,
|
||||
);
|
||||
expect(d.slot, 0, reason: 'slot 0 is free here and may be used');
|
||||
});
|
||||
|
||||
test('a decision is either a slot or a refusal, never both', () {
|
||||
final allowed = decideChannelSlot(
|
||||
channels: const [],
|
||||
maxChannels: 4,
|
||||
isSyncing: false,
|
||||
);
|
||||
final refused = decideChannelSlot(
|
||||
channels: const [],
|
||||
maxChannels: 4,
|
||||
isSyncing: true,
|
||||
);
|
||||
expect(allowed.slot != null && allowed.refusal == null, isTrue);
|
||||
expect(refused.slot == null && refused.refusal != null, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Uint8List psk sanity', () {
|
||||
test('the helper builds channels with a real 16 byte psk', () {
|
||||
expect(_ch(0).psk, isA<Uint8List>());
|
||||
expect(_ch(0).psk.length, 16);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue