test(#717): pin the frame budget per platform, so this cannot regress silently

One getter decides the composer's byte budget on every platform, and it had
collapsed that budget to zero on three of them before anyone noticed: Windows
(#686), Linux (#717) and Web (#718). Each surfaced as a user reporting "the
keyboard doesn't respond", two months and three platforms apart. Nothing
exercised the getter, so nothing went red.

Extracts the decision into `MeshCoreConnector.resolveFrameBudget`, a pure
function of (transport, isWeb, mtuNow). The getter is now a one-line delegate
and behaves identically. This is what makes the web branch testable at all:
`kIsWeb` is a compile-time constant, so a VM test could never enter it and CI
runs the suite on the VM only.

19 new cases, covering what we now know rather than what we guessed:
- wired transports are never floored, and ignore a bogus MTU
- web is exempt from the floor, and both composers stay usable at MTU 23
- native BLE keeps the #395 write-safety floor: unknown MTU yields 20, never
  the full frame, because defaulting an unknown link to the largest size would
  authorize an oversized write
- the MTUs actually measured on hardware (Android 247, Windows 176) leave a
  usable channel composer
- the original failure stays reproducible: an unreported MTU still gives a
  0-byte channel budget and 4 bytes for a DM, the two numbers users reported

Also adds widget tests for the #684 notice, including the disconnect case the
pre-PR review caught, and the severity split between a zero and a small budget.

analyze clean, 981 tests pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fix/717-linux-web-mtu
diffwireauto 2 months ago
parent 91f3cdfa3b
commit 2aea299f65

@ -503,13 +503,34 @@ class MeshCoreConnector extends ChangeNotifier {
/// handle); a device that negotiates a smaller MTU than [maxFrameSize] assumes
/// would otherwise reject a max-length message and wedge the send path (#395).
/// USB/TCP have no such per-write cap, so they take the full [maxFrameSize].
int get effectiveMaxFrameSize {
if (_activeTransport != MeshCoreTransportType.bluetooth) {
int get effectiveMaxFrameSize => resolveFrameBudget(
transport: _activeTransport,
isWeb: PlatformInfo.isWeb,
mtuNow: _device?.mtuNow ?? 0,
);
/// The frame-budget decision, as a pure function of its three inputs.
///
/// Extracted from [effectiveMaxFrameSize] so every branch is reachable from a
/// VM test. In particular the web branch is not otherwise testable at all:
/// `kIsWeb` is a compile-time constant, so a VM test can never enter it, and
/// CI runs the suite on the VM only. This getter decides the composer's byte
/// budget on every platform and had collapsed it to zero on three of them
/// (#592, #686, #717, #718), so it is pinned by test rather than by comment.
///
/// [mtuNow] is 0 when no device is attached.
@visibleForTesting
static int resolveFrameBudget({
required MeshCoreTransportType transport,
required bool isWeb,
required int mtuNow,
}) {
if (transport != MeshCoreTransportType.bluetooth) {
return maxFrameSize;
}
// Web Bluetooth exposes no ATT MTU to page script: there is no API for it,
// so `mtuNow` can only ever be the 23-byte default and the floor below
// would cap every composer at 0-4 bytes forever (#718, the #592 symptom).
// so mtuNow can only ever be the 23-byte default and the floor below would
// cap every composer at 0-4 bytes forever (#718, the #592 symptom).
//
// The floor exists because a raw single characteristic write larger than
// the PDU is rejected and wedges the send path (#395). That failure mode is
@ -517,17 +538,16 @@ class MeshCoreConnector extends ChangeNotifier {
// spec caps one write at 512 bytes, rejecting anything larger with
// InvalidModificationError. maxFrameSize (172) is well inside that, so the
// write cannot be oversized no matter what the link negotiated.
if (PlatformInfo.isWeb) {
if (isWeb) {
return maxFrameSize;
}
final mtu = _device?.mtuNow ?? 0;
// ATT_MTU 23 is the BLE minimum; a single write then carries 23 - 3 = 20
// bytes. An unknown MTU must fall back to that floor, never [maxFrameSize],
// defaulting an unknown link to the largest size would authorize an
// oversized write (#395 review).
const minWritable = 20;
if (mtu <= 0) return minWritable;
final writable = mtu - 3;
if (mtuNow <= 0) return minWritable;
final writable = mtuNow - 3;
if (writable >= maxFrameSize) return maxFrameSize;
return writable < minWritable ? minWritable : writable;
}

@ -0,0 +1,133 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
/// Regression lock for the frame budget, per platform.
///
/// This one getter decides the composer's byte budget everywhere, and it had
/// collapsed that budget to zero on three of the five platforms before anyone
/// noticed, each time surfacing as a user reporting "the keyboard doesn't
/// respond":
///
/// Windows (#686) - the WinRT plugin never created a GattSession, so no MTU
/// was ever reported and mtuNow stayed on its 23 default.
/// Linux (#717) - the plugin's onMtuChanged returned Stream.empty().
/// Web (#718) - Web Bluetooth exposes no MTU at all, so the BLE floor
/// applied forever.
///
/// None of those were caught by a test, because nothing exercised this
/// function. They were caught by users, two months and three platforms apart.
/// These cases exist so a regression is a red build instead of a bug report.
int _budget({
MeshCoreTransportType transport = MeshCoreTransportType.bluetooth,
bool isWeb = false,
int mtuNow = 0,
}) => MeshCoreConnector.resolveFrameBudget(
transport: transport,
isWeb: isWeb,
mtuNow: mtuNow,
);
void main() {
group('non-BLE transports are never floored', () {
test('usb gets the full frame', () {
expect(_budget(transport: MeshCoreTransportType.usb), maxFrameSize);
});
test('tcp gets the full frame', () {
expect(_budget(transport: MeshCoreTransportType.tcp), maxFrameSize);
});
test('a wired transport ignores a bogus MTU entirely', () {
expect(
_budget(transport: MeshCoreTransportType.usb, mtuNow: 23),
maxFrameSize,
);
});
});
group('web BLE is exempt from the floor (#718)', () {
// The branch that cannot be reached any other way: kIsWeb is a
// compile-time constant, so without the pure function this is untestable.
test('web gets the full frame even with no MTU reported', () {
expect(_budget(isWeb: true, mtuNow: 0), maxFrameSize);
});
test('web gets the full frame even at the 23-byte ATT default', () {
expect(_budget(isWeb: true, mtuNow: 23), maxFrameSize);
});
test('a web budget leaves BOTH composers usable', () {
// The actual regression: this is what was 0 before #718.
final budget = _budget(isWeb: true, mtuNow: 23);
expect(
isComposerBudgetUsable(
maxChannelMessageBytes('KB9QDI-JOE', maxFrameBytes: budget),
),
isTrue,
);
expect(
isComposerBudgetUsable(maxContactMessageBytes(maxFrameBytes: budget)),
isTrue,
);
});
});
group('native BLE keeps the write-safety floor (#395)', () {
test('no device reported yields the floor, never the full frame', () {
// Must NOT be maxFrameSize: defaulting an unknown link to the largest
// size would authorize an oversized write, which is what the floor is
// for. The floor is correct here even though it is what made the
// composer unusable; the fix was reporting the MTU, not removing this.
expect(_budget(mtuNow: 0), 20);
});
test('the 23-byte ATT minimum yields the floor', () {
expect(_budget(mtuNow: 23), 20);
});
test('a negotiated MTU is honoured minus the 3-byte ATT header', () {
expect(_budget(mtuNow: 100), 97);
});
test('a large MTU is capped at the frame size', () {
expect(_budget(mtuNow: 517), maxFrameSize);
});
test('never returns below the floor for an absurdly small MTU', () {
expect(_budget(mtuNow: 5), 20);
});
});
group('the MTUs actually measured on hardware leave the composer usable', () {
// Pinning the real numbers, so a regression that silently reintroduces the
// floor on a healthy link fails here rather than in someone's chat window.
for (final probe in <({String platform, int mtu})>[
(platform: 'Android', mtu: 247),
(platform: 'Windows after #686', mtu: 176),
]) {
test('${probe.platform} (MTU ${probe.mtu})', () {
final budget = _budget(mtuNow: probe.mtu);
expect(budget, maxFrameSize);
expect(
isComposerBudgetUsable(
maxChannelMessageBytes('KB9QDI-JOE', maxFrameBytes: budget),
),
isTrue,
reason: 'a healthy link must leave a usable channel composer',
);
});
}
});
group('the exact failure users reported is still reproducible', () {
test('an unreported MTU collapses the channel composer to zero', () {
// Not a fix to preserve, a fact to keep visible: when the MTU is
// genuinely unknown the budget IS unusable, which is why #684's guard
// has to exist and why reporting the MTU was the real fix.
final budget = _budget(mtuNow: 0);
expect(maxChannelMessageBytes('KB9QDI-JOE', maxFrameBytes: budget), 0);
expect(maxContactMessageBytes(maxFrameBytes: budget), 4);
});
});
}

@ -0,0 +1,115 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/widgets/composer_budget_notice.dart';
/// #684: a composer whose byte budget has collapsed must say so, instead of
/// presenting a field that silently rejects every keystroke (#592 was reported
/// three times as "the keyboard doesn't respond"). Equally it must stay
/// invisible the rest of the time, or it becomes noise that teaches people to
/// ignore it.
Widget _wrap(Widget child) => MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(body: child),
);
void main() {
group('stays invisible when it should (#684)', () {
testWidgets('healthy budget renders nothing', (tester) async {
await tester.pumpWidget(
_wrap(
const ComposerBudgetNotice(
maxBytes: 148, // a channel composer on a 172-byte frame budget
transport: MeshCoreTransportType.bluetooth,
isConnected: true,
),
),
);
expect(find.byIcon(Icons.error_outline), findsNothing);
expect(find.byIcon(Icons.info_outline), findsNothing);
expect(find.byType(Text), findsNothing);
});
testWidgets('collapsed budget while DISCONNECTED renders nothing', (
tester,
) async {
// The regression the pre-PR review caught. While disconnected the
// transport still reads bluetooth and there is no device, so the budget
// collapses to the same floor an unreported MTU produces. Without the
// gate this banner appeared on every disconnect and blamed the link for
// a small packet size when there was no link at all.
await tester.pumpWidget(
_wrap(
const ComposerBudgetNotice(
maxBytes: 0,
transport: MeshCoreTransportType.bluetooth,
isConnected: false,
),
),
);
expect(find.byIcon(Icons.error_outline), findsNothing);
expect(find.byType(Text), findsNothing);
});
});
group('explains itself when the budget has collapsed (#684)', () {
testWidgets('zero budget reads as a failure, not a hint', (tester) async {
await tester.pumpWidget(
_wrap(
const ComposerBudgetNotice(
maxBytes: 0,
transport: MeshCoreTransportType.bluetooth,
isConnected: true,
),
),
);
expect(find.byIcon(Icons.error_outline), findsOneWidget);
expect(find.byIcon(Icons.info_outline), findsNothing);
// Names the cause in plain words rather than exposing byte arithmetic.
expect(find.textContaining('Bluetooth'), findsOneWidget);
});
testWidgets('a small but nonzero budget is stated, not alarmed about', (
tester,
) async {
// 4 bytes is the measured DM budget at the unknown-MTU floor. The link
// works, it is just tiny, so this must not render as an error.
await tester.pumpWidget(
_wrap(
const ComposerBudgetNotice(
maxBytes: 4,
transport: MeshCoreTransportType.bluetooth,
isConnected: true,
),
),
);
expect(find.byIcon(Icons.info_outline), findsOneWidget);
expect(find.byIcon(Icons.error_outline), findsNothing);
// The actual number is surfaced so the user knows what they have.
expect(find.textContaining('4'), findsOneWidget);
});
testWidgets('a non-bluetooth transport does not blame Bluetooth', (
tester,
) async {
await tester.pumpWidget(
_wrap(
const ComposerBudgetNotice(
maxBytes: 0,
transport: MeshCoreTransportType.usb,
isConnected: true,
),
),
);
expect(find.byIcon(Icons.error_outline), findsOneWidget);
expect(find.textContaining('Bluetooth'), findsNothing);
});
});
}
Loading…
Cancel
Save

Powered by TurnKey Linux.