feat(market): save others' offers as favorites (Wallapop-style)
Adds a Favorites feature: a heart on the offer detail saves another person's listing to an encrypted, per-identity SavedOffersStore (keystore JSON snapshot, no plaintext at rest). A new /favorites screen (wired from the drawer) lists saved offers offline-first and, when a relay is reachable, flags ones that are gone as "no longer available". i18n en/es/pt/ast; store + screen + detail-heart tests.
This commit is contained in:
parent
f6967e8cbb
commit
d6225eed38
19 changed files with 806 additions and 3 deletions
100
apps/app_seeds/test/services/saved_offers_store_test.dart
Normal file
100
apps/app_seeds/test/services/saved_offers_store_test.dart
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
import 'package:commons_core/commons_core.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/services/saved_offers_store.dart';
|
||||
|
||||
import '../support/test_support.dart';
|
||||
|
||||
Offer _offer(
|
||||
String id, {
|
||||
String author = 'aa',
|
||||
OfferType type = OfferType.gift,
|
||||
num? price,
|
||||
String? currency,
|
||||
String geohash = 'u09',
|
||||
}) => Offer(
|
||||
id: id,
|
||||
authorPubkeyHex: author,
|
||||
summary: 'Tomate $id',
|
||||
type: type,
|
||||
approxGeohash: geohash,
|
||||
priceAmount: price,
|
||||
priceCurrency: currency,
|
||||
isOrganic: true,
|
||||
);
|
||||
|
||||
void main() {
|
||||
late SavedOffersStore store;
|
||||
|
||||
setUp(() => store = SavedOffersStore(InMemorySecretStore()));
|
||||
|
||||
test('keyOf is the author:id coordinate', () {
|
||||
expect(SavedOffersStore.keyOf(_offer('x', author: 'bb')), 'bb:x');
|
||||
});
|
||||
|
||||
test('starts empty', () async {
|
||||
expect(await store.list(), isEmpty);
|
||||
expect(await store.isSaved('aa:x'), isFalse);
|
||||
});
|
||||
|
||||
test('save then list round-trips every field', () async {
|
||||
await store.save(
|
||||
_offer('1', type: OfferType.sale, price: 3, currency: 'Ğ1'),
|
||||
savedAt: 100,
|
||||
);
|
||||
final saved = await store.list();
|
||||
expect(saved, hasLength(1));
|
||||
final o = saved.single;
|
||||
expect(o.id, '1');
|
||||
expect(o.authorPubkeyHex, 'aa');
|
||||
expect(o.type, OfferType.sale);
|
||||
expect(o.priceAmount, 3);
|
||||
expect(o.priceCurrency, 'Ğ1');
|
||||
expect(o.isOrganic, isTrue);
|
||||
expect(await store.isSaved('aa:1'), isTrue);
|
||||
});
|
||||
|
||||
test('list is newest-first by savedAt', () async {
|
||||
await store.save(_offer('old'), savedAt: 100);
|
||||
await store.save(_offer('new'), savedAt: 200);
|
||||
expect([for (final o in await store.list()) o.id], ['new', 'old']);
|
||||
});
|
||||
|
||||
test('saving the same coordinate again de-duplicates', () async {
|
||||
await store.save(_offer('1'), savedAt: 100);
|
||||
await store.save(_offer('1'), savedAt: 200); // same author:id
|
||||
expect(await store.list(), hasLength(1));
|
||||
});
|
||||
|
||||
test('remove drops it', () async {
|
||||
await store.save(_offer('1'), savedAt: 100);
|
||||
await store.remove('aa:1');
|
||||
expect(await store.list(), isEmpty);
|
||||
expect(await store.isSaved('aa:1'), isFalse);
|
||||
});
|
||||
|
||||
test('toggle saves then removes and reports the new state', () async {
|
||||
expect(await store.toggle(_offer('1'), savedAt: 100), isTrue);
|
||||
expect(await store.isSaved('aa:1'), isTrue);
|
||||
expect(await store.toggle(_offer('1'), savedAt: 200), isFalse);
|
||||
expect(await store.isSaved('aa:1'), isFalse);
|
||||
});
|
||||
|
||||
test('scopes are isolated by account', () async {
|
||||
final secret = InMemorySecretStore();
|
||||
final a = SavedOffersStore(secret, accountScope: 'acct1');
|
||||
final b = SavedOffersStore(secret, accountScope: 'acct2');
|
||||
await a.save(_offer('1'), savedAt: 100);
|
||||
expect(await a.list(), hasLength(1));
|
||||
expect(await b.list(), isEmpty);
|
||||
});
|
||||
|
||||
test('changes stream fires on save and remove', () async {
|
||||
final events = <void>[];
|
||||
final sub = store.changes.listen(events.add);
|
||||
await store.save(_offer('1'), savedAt: 100);
|
||||
await store.remove('aa:1');
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(events, hasLength(2));
|
||||
await sub.cancel();
|
||||
});
|
||||
}
|
||||
77
apps/app_seeds/test/ui/favorites_screen_test.dart
Normal file
77
apps/app_seeds/test/ui/favorites_screen_test.dart
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import 'package:commons_core/commons_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/i18n/strings.g.dart';
|
||||
import 'package:tane/services/saved_offers_store.dart';
|
||||
import 'package:tane/ui/favorites_screen.dart';
|
||||
|
||||
import '../support/test_support.dart';
|
||||
|
||||
Widget _wrap(Widget child) {
|
||||
LocaleSettings.setLocaleSync(AppLocale.en);
|
||||
return TranslationProvider(
|
||||
child: MaterialApp(
|
||||
locale: AppLocale.en.flutterLocale,
|
||||
supportedLocales: AppLocaleUtils.supportedLocales,
|
||||
localizationsDelegates: const [
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
home: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Offer _offer(String id) => Offer(
|
||||
id: id,
|
||||
authorPubkeyHex: 'ab',
|
||||
summary: 'Tomate $id',
|
||||
type: OfferType.gift,
|
||||
approxGeohash: 'u09',
|
||||
);
|
||||
|
||||
void main() {
|
||||
testWidgets('shows the empty state when nothing is saved', (tester) async {
|
||||
final store = SavedOffersStore(InMemorySecretStore());
|
||||
// No connection → no relay check, just the local snapshots.
|
||||
await tester.pumpWidget(_wrap(FavoritesScreen(savedOffers: store)));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.text('No favorites yet. Save offers you like from the market.'),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('lists saved offers newest-first', (tester) async {
|
||||
final store = SavedOffersStore(InMemorySecretStore());
|
||||
await store.save(_offer('old'), savedAt: 100);
|
||||
await store.save(_offer('new'), savedAt: 200);
|
||||
|
||||
await tester.pumpWidget(_wrap(FavoritesScreen(savedOffers: store)));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Tomate new'), findsOneWidget);
|
||||
expect(find.text('Tomate old'), findsOneWidget);
|
||||
final newY = tester.getTopLeft(find.text('Tomate new')).dy;
|
||||
final oldY = tester.getTopLeft(find.text('Tomate old')).dy;
|
||||
expect(newY, lessThan(oldY)); // newest on top
|
||||
});
|
||||
|
||||
testWidgets('the heart removes a favorite', (tester) async {
|
||||
final store = SavedOffersStore(InMemorySecretStore());
|
||||
await store.save(_offer('1'), savedAt: 100);
|
||||
|
||||
await tester.pumpWidget(_wrap(FavoritesScreen(savedOffers: store)));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Tomate 1'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byKey(const Key('favorites.remove.ab.1')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Tomate 1'), findsNothing);
|
||||
expect(await store.isSaved('ab:1'), isFalse);
|
||||
});
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
|
|||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:tane/i18n/strings.g.dart';
|
||||
import 'package:tane/services/saved_offers_store.dart';
|
||||
import 'package:tane/services/social_connection.dart';
|
||||
import 'package:tane/services/social_service.dart';
|
||||
import 'package:tane/services/social_settings.dart';
|
||||
|
|
@ -103,4 +104,50 @@ void main() {
|
|||
|
||||
expect(find.byType(OfferHeroImage), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('the heart saves and unsaves a stranger\'s offer',
|
||||
(tester) async {
|
||||
final social = await SocialService.fromRootSeedHex('00' * 32);
|
||||
final settings = SocialSettings(InMemorySecretStore());
|
||||
await settings.setRelayUrls(const []);
|
||||
final connection = SocialConnection(social: social, settings: settings);
|
||||
final saved = SavedOffersStore(InMemorySecretStore());
|
||||
|
||||
await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
|
||||
offer: _offer(),
|
||||
connection: connection,
|
||||
savedOffers: saved,
|
||||
)));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final heart = find.byKey(const Key('offerDetail.save'));
|
||||
expect(heart, findsOneWidget);
|
||||
expect(await saved.isSaved('${'ab' * 32}:lot-1'), isFalse);
|
||||
|
||||
await tester.tap(heart);
|
||||
await tester.pumpAndSettle();
|
||||
expect(await saved.isSaved('${'ab' * 32}:lot-1'), isTrue);
|
||||
|
||||
await tester.tap(heart);
|
||||
await tester.pumpAndSettle();
|
||||
expect(await saved.isSaved('${'ab' * 32}:lot-1'), isFalse);
|
||||
});
|
||||
|
||||
testWidgets('no heart on my own listing', (tester) async {
|
||||
final social = await SocialService.fromRootSeedHex('00' * 32);
|
||||
final settings = SocialSettings(InMemorySecretStore());
|
||||
await settings.setRelayUrls(const []);
|
||||
final connection = SocialConnection(social: social, settings: settings);
|
||||
final saved = SavedOffersStore(InMemorySecretStore());
|
||||
|
||||
await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
|
||||
offer: _offer(),
|
||||
connection: connection,
|
||||
mine: true,
|
||||
savedOffers: saved,
|
||||
)));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('offerDetail.save')), findsNothing);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue