tane/apps/app_seeds/test/ui/unread_badge_test.dart
vjrj 6cff0d0b11 feat(chat): usable 1:1 chat — bottom-anchored, Drift-backed, dated
- Anchor the message list to the bottom (`reverse: true`) so new messages
  stay in view instead of landing below the fold.
- Move chat history from the OS keystore (O(n²) JSON blob, silent 200-msg
  cap) to a separate encrypted Drift/SQLCipher DB (`ChatDatabase`):
  indexed append, uncapped history, dedup as a unique-key invariant. It's
  an ephemeral per-device cache, isolated from the inventory schema, its
  migrations, and its sync. No data migration (pre-release).
- Add day separators (Today/Yesterday/locale date) and a per-bubble time,
  all via ICU (12/24h per locale; Localizations locale maps Asturian →
  Spanish for intl date symbols).
- Add peer avatars (deterministic colour from the pubkey + name initial),
  surface send failures that were previously silent, and make bubble text
  selectable (addresses, links).
- New i18n keys in en/es/pt/ast; tests for grouping, formatting, avatars,
  scroll anchoring, storage and send errors.

Docs: docs/design/chat-storage.md + open-decisions.md.
2026-07-11 06:40:50 +02:00

74 lines
2.3 KiB
Dart

import 'package:commons_core/commons_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/services/message_store.dart';
import 'package:tane/services/unread_service.dart';
import 'package:tane/ui/unread_badge.dart';
import '../support/test_support.dart';
/// The badge is driven by [UnreadService]: it shows a count when there are
/// unread messages and clears when they're read — live, over the service's
/// `changes` stream. Injecting the service avoids the `getIt` lookup. The store
/// reads are real async, so mutations run inside `runAsync`.
void main() {
late MessageStore store;
late UnreadService unread;
setUp(() {
store = MessageStore(newTestChatDatabase());
unread = UnreadService(store, InMemorySecretStore());
});
tearDown(() => unread.dispose());
Widget host() => MaterialApp(
home: Scaffold(
body: UnreadBadge(
peer: 'alice',
service: unread,
child: const Icon(Icons.chat_bubble),
),
),
);
Future<void> receive(int atMs) async {
final m = PrivateMessage(
fromPubkey: 'alice',
text: 'hi',
at: DateTime.fromMillisecondsSinceEpoch(atMs));
await store.append('alice', m);
await unread.onMessageReceived('alice', m);
}
// Lets pending async (store reads + the widget's stream-driven refresh)
// resolve in the real zone, then renders a frame.
Future<void> pumpRefresh(WidgetTester tester) async {
for (var i = 0; i < 5; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)));
await tester.pump();
}
}
testWidgets('shows an existing unread count on build', (tester) async {
await tester.runAsync(() => receive(1000));
await tester.pumpWidget(host());
await pumpRefresh(tester);
expect(find.text('1'), findsOneWidget);
});
testWidgets('reacts to a new message and to being read', (tester) async {
await tester.pumpWidget(host());
await pumpRefresh(tester);
expect(find.text('1'), findsNothing);
await tester.runAsync(() => receive(1000));
await pumpRefresh(tester);
expect(find.text('1'), findsOneWidget);
await tester.runAsync(() => unread.markRead('alice'));
await pumpRefresh(tester);
expect(find.text('1'), findsNothing);
});
}