Files
buzz/mobile/test/shared/push/notification_body_test.dart
Tom Brow 34c8f5c7a5 feat(mobile): add Dart notification body formatter (#9556)
Adds a pure Dart function, `notificationBody`, that turns a message's
Markdown content into the plain-text notification body defined by [ADR
0002](https://github.com/block/buzz/blob/main/docs/adr/0002-notification-message-text.md).
It is a port of the Swift `BuzzPushNotificationResolver.previewBody` in
`BuzzPushKit`, which today is the only implementation.

Android notifications will be built from the app's Dart code, as in the
[Android push design](https://github.com/block/buzz/pull/9555), and will
reuse this function so the body reads the same on both platforms.
Nothing calls it yet and the Swift side is unchanged.

Notes on the port:

- The cap stays at 256 UTF-16 code units, the same as iOS (the fixtures
set `maxLength` to 256).
- Dart's regex engine differs from the ICU one the Swift code uses
(`\s`, `.` and `$`), so the patterns spell those out to keep identical
results. Trimming also matches Swift's `whitespacesAndNewlines`, which
trims U+200B and keeps U+FEFF, where Dart's `trim` does the opposite for
both.
- Truncation counts whole characters (extended grapheme clusters) via
`package:characters`.

### Validation

- A table-driven test runs every case in
`test-fixtures/notification-body.json` (47 cases) and checks the
fixture's `maxLength` against the function's cap.
- Added tests: 8 rows in the shared fixture pin the ICU details the port
must match. U+000B and U+0085 beside a marker keep it, U+000B and U+000C
inside a run keep its markers, U+200B is trimmed at the edges and
doesn't count toward the cap, and U+FEFF at the edges is kept. The Swift
fixture test runs them too and passes them unchanged.
- Proved falsifiable: with code-span and fence handling disabled, 18
fixture cases fail; restored, all pass.
- `just mobile-install mobile-check mobile-test` passes.

### Regressions

None: new code that nothing calls yet. The Swift formatter is untouched.
Its fixture test now also runs the 8 new rows and passes them unchanged.

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-20 00:40:40 -05:00

31 lines
888 B
Dart

import 'dart:convert';
import 'dart:io';
import 'package:buzz/shared/push/notification_body.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// The contract every platform runs: docs/adr/0002-notification-message-text.md.
final fixture =
jsonDecode(
File('../test-fixtures/notification-body.json').readAsStringSync(),
)
as Map<String, dynamic>;
final cases = fixture['cases'] as List;
test('the fixture is the one this formatter was written for', () {
expect(fixture['schemaVersion'], 1);
expect(notificationBodyLimit, fixture['maxLength']);
expect(cases, isNotEmpty);
});
for (final testCase in cases) {
test('notification-body fixture: ${testCase['name']}', () {
expect(
notificationBody(testCase['input'] as String),
testCase['expected'] as String,
);
});
}
}