tane/apps/app_seeds/test/services/message_store_test.dart
vjrj 73ee98206f feat(chat): app-wide inbox listener so messages arrive without opening the chat
Reception only ran inside an open ChatScreen for a known peer, so a first
message from a new peer was invisible: nothing in local storage -> nothing
in the inbox list -> you never opened the chat -> you never subscribed.

Add InboxService: one long-lived NIP-17 inbox subscription for the whole
app (foreground), persisting every incoming message to MessageStore and
firing a 'changes' signal the inbox list live-reloads on. Reconnects when
the network returns; degrades to nothing offline. Started from main when a
social identity exists.

Make MessageStore.append idempotent (dedup by sender+timestamp+text) and
serialized behind a write lock — the global listener and an open chat's own
subscription now write the same conversation concurrently and relays
re-deliver stored gift wraps on every resubscribe. Tests for both.

Known trade-offs (follow-ups): foreground-only (no push yet); each of
InboxService/ChatScreen/MarketScreen opens its own RelayPool (a shared
connection is a later optimization).
2026-07-10 16:53:03 +02:00

71 lines
2.7 KiB
Dart

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('conversations lists peers newest-first with their last message',
() async {
await store.append('peerA', msg('me', 'hi A', 1000));
await store.append('peerB', msg('peerB', 'yo B', 3000));
await store.append('peerA', msg('peerA', 'back A', 2000));
final convos = await store.conversations();
expect(convos.map((c) => c.peerPubkey), ['peerB', 'peerA']); // 3000 > 2000
expect(convos.first.lastText, 'yo B');
expect(convos.last.lastText, 'back A');
});
test('append is idempotent: a re-delivered message is not duplicated',
() async {
// Same sender + timestamp + text = the same gift wrap redelivered by a
// relay on resubscribe. It must not pile up.
expect(await store.append('peer', msg('peer', 'hola', 1000)), isTrue);
expect(await store.append('peer', msg('peer', 'hola', 1000)), isFalse);
expect(await store.history('peer'), hasLength(1));
// A genuinely different message (later timestamp) still lands.
expect(await store.append('peer', msg('peer', 'hola', 2000)), isTrue);
expect(await store.history('peer'), hasLength(2));
});
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');
});
}