feat(block2): offline outbox — share now, publish when connected
Sender-side offline delivery hardening.
- OfferOutbox: a keystore-backed (no plaintext) queue of lot ids to publish
later. Stores ids, not offers, so on flush each offer is rebuilt from the
lot's CURRENT state — an offline edit is respected, a deleted lot drops out.
- flushOutbox(): on reconnect, republishes queued lots and clears them (no-op
offline; drops ids whose lot is gone).
- Market screen: sharing offline parks the lots ('we'll share these when you're
connected'); opening the market online auto-flushes the queue first.
- Wired via DI + TaneApp(outbox); i18n en/es/pt.
Tests: 4 outbox + 3 flush + market UI, 20 green. Analyzer clean for new code.
This commit is contained in:
parent
2a6b4b0947
commit
cf5d0243ee
15 changed files with 222 additions and 8 deletions
31
apps/app_seeds/test/services/offer_outbox_test.dart
Normal file
31
apps/app_seeds/test/services/offer_outbox_test.dart
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/services/offer_outbox.dart';
|
||||
|
||||
import '../support/test_support.dart';
|
||||
|
||||
void main() {
|
||||
late OfferOutbox outbox;
|
||||
setUp(() => outbox = OfferOutbox(InMemorySecretStore()));
|
||||
|
||||
test('starts empty', () async {
|
||||
expect(await outbox.pending(), isEmpty);
|
||||
});
|
||||
|
||||
test('enqueue adds ids; it is a set (no duplicates)', () async {
|
||||
await outbox.enqueue(['a', 'b']);
|
||||
await outbox.enqueue(['b', 'c']);
|
||||
expect(await outbox.pending(), {'a', 'b', 'c'});
|
||||
});
|
||||
|
||||
test('remove drops the given ids', () async {
|
||||
await outbox.enqueue(['a', 'b', 'c']);
|
||||
await outbox.remove(['b']);
|
||||
expect(await outbox.pending(), {'a', 'c'});
|
||||
});
|
||||
|
||||
test('clear empties it', () async {
|
||||
await outbox.enqueue(['a', 'b']);
|
||||
await outbox.clear();
|
||||
expect(await outbox.pending(), isEmpty);
|
||||
});
|
||||
}
|
||||
|
|
@ -5,8 +5,11 @@ import 'package:flutter_test/flutter_test.dart';
|
|||
import 'package:tane/data/variety_repository.dart';
|
||||
import 'package:tane/db/enums.dart';
|
||||
import 'package:tane/services/offer_mapper.dart';
|
||||
import 'package:tane/services/offer_outbox.dart';
|
||||
import 'package:tane/state/offers_cubit.dart';
|
||||
|
||||
import '../support/test_support.dart';
|
||||
|
||||
/// In-memory [OfferTransport] for tests: addressable by id, discover matches by
|
||||
/// geohash prefix. No relay, no network.
|
||||
class FakeOfferTransport implements OfferTransport {
|
||||
|
|
@ -196,6 +199,63 @@ void main() {
|
|||
await noArea.close();
|
||||
});
|
||||
|
||||
test('flushOutbox publishes queued lots, then clears them', () async {
|
||||
final outbox = OfferOutbox(InMemorySecretStore());
|
||||
await outbox.enqueue(['lot-1', 'lot-2']);
|
||||
final cubit = OffersCubit(FakeOfferTransport());
|
||||
final count = await flushOutbox(
|
||||
outbox: outbox,
|
||||
cubit: cubit,
|
||||
shareableLots: const [
|
||||
ShareableLot(
|
||||
lotId: 'lot-1', summary: 'Tomate', offerStatus: OfferStatus.shared),
|
||||
ShareableLot(
|
||||
lotId: 'lot-2',
|
||||
summary: 'Judía',
|
||||
offerStatus: OfferStatus.exchange),
|
||||
ShareableLot(
|
||||
lotId: 'lot-3', summary: 'Otro', offerStatus: OfferStatus.shared),
|
||||
],
|
||||
authorPubkeyHex: 'ab' * 32,
|
||||
areaGeohash: 'sp3e9',
|
||||
);
|
||||
expect(count, 2);
|
||||
expect(await outbox.pending(), isEmpty);
|
||||
await cubit.close();
|
||||
});
|
||||
|
||||
test('flushOutbox offline keeps the queue for later', () async {
|
||||
final outbox = OfferOutbox(InMemorySecretStore());
|
||||
await outbox.enqueue(['lot-1']);
|
||||
final cubit = OffersCubit(null); // offline
|
||||
final count = await flushOutbox(
|
||||
outbox: outbox,
|
||||
cubit: cubit,
|
||||
shareableLots: const [],
|
||||
authorPubkeyHex: 'ab' * 32,
|
||||
areaGeohash: 'sp3e9',
|
||||
);
|
||||
expect(count, 0);
|
||||
expect(await outbox.pending(), {'lot-1'});
|
||||
await cubit.close();
|
||||
});
|
||||
|
||||
test('flushOutbox drops queued lots that no longer exist', () async {
|
||||
final outbox = OfferOutbox(InMemorySecretStore());
|
||||
await outbox.enqueue(['gone']);
|
||||
final cubit = OffersCubit(FakeOfferTransport());
|
||||
final count = await flushOutbox(
|
||||
outbox: outbox,
|
||||
cubit: cubit,
|
||||
shareableLots: const [],
|
||||
authorPubkeyHex: 'ab' * 32,
|
||||
areaGeohash: 'sp3e9',
|
||||
);
|
||||
expect(count, 0);
|
||||
expect(await outbox.pending(), isEmpty);
|
||||
await cubit.close();
|
||||
});
|
||||
|
||||
test('offline (no transport): degrades gracefully, never throws', () async {
|
||||
final cubit = OffersCubit(null);
|
||||
expect(cubit.isOnline, isFalse);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue