feat(block2): persist chat history (messages survive leaving the chat)

Messages were in-memory only — gone on leaving the conversation.
- MessageStore: keystore-backed (no plaintext), a capped per-peer JSON list
  (last 200). Simple; the encrypted Drift DB is the eventual home at scale.
- MessagesCubit: loads saved history on start (subscribes FIRST, then loads, so
  a message arriving during the load isn't dropped) and persists every sent and
  received message. Wired via DI + TaneApp(messageStore).

Tests (plain 'test', no hang risk): MessageStore round-trip/per-peer/cap, and a
cubit test that reopens a fresh cubit and sees the saved conversation. Analyzer
clean; run 'flutter test' locally to confirm.
This commit is contained in:
vjrj 2026-07-10 12:20:25 +02:00
parent 847590ff11
commit 6d16656911
8 changed files with 186 additions and 22 deletions

View file

@ -0,0 +1,46 @@
import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/services/message_store.dart';
import '../support/test_support.dart';
void main() {
late MessageStore store;
setUp(() => store = MessageStore(InMemorySecretStore()));
PrivateMessage msg(String from, String text, int atMs) =>
PrivateMessage(
fromPubkey: from,
text: text,
at: DateTime.fromMillisecondsSinceEpoch(atMs));
test('history is empty for an unknown peer', () async {
expect(await store.history('peer'), isEmpty);
});
test('append then history round-trips, oldest first', () async {
await store.append('peer', msg('peer', 'hi', 1000));
await store.append('peer', msg('me', 'hello', 2000));
final history = await store.history('peer');
expect(history.map((m) => m.text), ['hi', 'hello']);
expect(history.first.fromPubkey, 'peer');
expect(history.last.at.millisecondsSinceEpoch, 2000);
});
test('conversations are kept separate per peer', () async {
await store.append('a', msg('a', 'toA', 1));
await store.append('b', msg('b', 'toB', 1));
expect((await store.history('a')).single.text, 'toA');
expect((await store.history('b')).single.text, 'toB');
});
test('history is capped to the most recent 200', () async {
for (var i = 0; i < 210; i++) {
await store.append('peer', msg('me', 'm$i', i));
}
final history = await store.history('peer');
expect(history, hasLength(200));
expect(history.first.text, 'm10'); // oldest 10 dropped
expect(history.last.text, 'm209');
});
}