Files
meshcore-open/test/helpers/utf8_length_limiter_test.dart
T
zjs81 25f197c979 Fix review findings, add default flood scope, and address recent issues
Review fixes:
- Keep stored message history beyond the 200-message window; stop
  messages leaking between radios; reset link/session state on disconnect
- Accept firmware's 176-byte RX frames over USB/TCP
- Hold APP_START during contact sync; contact-sync watchdog; replay DMs
  from not-yet-known senders; tombstone late command replies; finish
  channel sync on ERR_NOT_FOUND; fail region-scoped sends cleanly
- DM retry watchdog when RESP_CODE_SENT never arrives; late repeater CLI
  replies; ACK hash collisions; UTF-8-safe name truncation
- Image pipeline: delete-during-decode, lifecycle cancels, decoder memory
  release, entry cap, dead worker recovery, atomic model writes
- Storage: deterministic reaction hash across web/native, Smaz
  double-decode, per-entry parsing, per-radio path history and regions,
  review-mode data isolation
- UI: safe channel slot selection, byte-length name limits, double-send
  guard, mounted checks, localized hardcoded strings
- Skip persisted-store reload on repeat SELF_INFO

Features:
- Radio default flood scope (firmware ver 11+) and explicit unscoped
  sends (ver 12+)
- Scan for nearby nodes (#570)
- Optional distinct first-hop repeater counting for channel resend (#589)
- Optional region next to hop count (#580)

Issues:
- Message text follows system font size (#569)
- Repeater/room password field accepts 20 chars (#576)
- TCP auto-reconnect and keepalive (#571)
- Order feeds by local receive time; flag future timestamps (#584)
- Coding rate help text (#568), French "Favoris" (#575)
- iOS/macOS app badge now clears

Translations for all new strings in every locale.
2026-10-11 01:25:31 -07:00

101 lines
3.3 KiB
Dart
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/utf8_length_limiter.dart';
TextEditingValue _value(String text, [int? cursor]) => TextEditingValue(
text: text,
selection: TextSelection.collapsed(offset: cursor ?? text.length),
);
void main() {
const formatter = Utf8LengthLimitingTextInputFormatter(5);
test('accepts edits within the limit', () {
final result = formatter.formatEditUpdate(_value('ab'), _value('abc'));
expect(result.text, 'abc');
});
test('clips only the inserted text and keeps the end of the draft', () {
// Paste "XYZ" at the start of "abc" (limit 5 bytes).
final result = formatter.formatEditUpdate(
_value('abc', 0),
_value('XYZabc', 3),
);
expect(result.text, 'XYabc');
expect(result.selection.baseOffset, 2);
});
test('rejects an insertion in the middle when full', () {
final result = formatter.formatEditUpdate(
_value('abcde', 2),
_value('abXcde', 3),
);
expect(result.text, 'abcde');
expect(result.selection.baseOffset, 2);
});
test('does not split a multi-code-point grapheme', () {
// Family emoji is 18 UTF-8 bytes, so it cannot fit in the 4 bytes left.
final result = formatter.formatEditUpdate(
_value('a'),
_value('a\u{1F468}‍\u{1F469}‍\u{1F467}'),
);
expect(result.text, 'a');
});
test('keeps whole emoji that fit', () {
const limiter = Utf8LengthLimitingTextInputFormatter(9);
final result = limiter.formatEditUpdate(
_value('a'),
_value('a\u{1F600}\u{1F601}'),
);
expect(result.text, 'a\u{1F600}\u{1F601}');
final clipped = const Utf8LengthLimitingTextInputFormatter(
8,
).formatEditUpdate(_value('a'), _value('a\u{1F600}\u{1F601}'));
expect(clipped.text, 'a\u{1F600}');
});
test('replacing one emoji with another sharing a surrogate stays valid', () {
const limiter = Utf8LengthLimitingTextInputFormatter(5);
final result = limiter.formatEditUpdate(
_value('a\u{1F600}'),
_value('a\u{1F601}\u{1F602}'),
);
expect(result.text, 'a\u{1F601}');
});
test('allows deletions even when the text is already over the limit', () {
final result = formatter.formatEditUpdate(
_value('abcdefg'),
_value('abcdef'),
);
expect(result.text, 'abcdef');
});
test('truncateUtf8 never splits a multi-byte character', () {
// 'й' is 2 bytes; 31 bytes would land mid-character.
final name = 'й' * 20;
final cut = truncateUtf8(name, maxRadioNameBytes);
expect(utf8ByteLength(cut), 30);
expect(cut, 'й' * 15);
expect(truncateUtf8('short', maxRadioNameBytes), 'short');
});
test('fitRadioName keeps the suffix and fits the radio field', () {
final fitted = fitRadioName('A' * 40, 'Public');
expect(fitted.endsWith(' Public'), isTrue);
expect(utf8ByteLength(fitted), maxRadioNameBytes);
expect(fitRadioName('Hikers', 'Public'), 'Hikers Public');
});
test('encoder counts a hashtag prefix toward the byte limit', () {
final formatter = Utf8LengthLimitingTextInputFormatter(
maxRadioNameBytes,
encoder: (text) => '#$text',
);
final result = formatter.formatEditUpdate(_value(''), _value('a' * 40));
expect(result.text, 'a' * 30);
});
}