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:
vjrj 2026-07-10 10:45:53 +02:00
parent 2a6b4b0947
commit cf5d0243ee
15 changed files with 222 additions and 8 deletions

View file

@ -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);