Merge branch 'claude/nostalgic-wright-acfc25': inline offer photos

Market offers carry a small base64 data-URI thumbnail embedded in the Nostr
event (no media server); full photo stays in the encrypted inventory. Blossom
alternative parked on branch parked/offer-photos-blossom.

# Conflicts:
#	apps/app_seeds/lib/i18n/strings.g.dart
This commit is contained in:
vjrj 2026-07-10 21:43:35 +02:00
commit e2b88b4f26
20 changed files with 520 additions and 16 deletions

View file

@ -0,0 +1,61 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:image/image.dart' as img;
import 'package:tane/services/offer_thumbnail.dart';
void main() {
/// A real, decodable photo far bigger than a thumbnail, with varied pixels so
/// JPEG can't compress it to nothing.
Uint8List bigPhoto() {
final image = img.Image(width: 1280, height: 960);
for (var y = 0; y < image.height; y++) {
for (var x = 0; x < image.width; x++) {
image.setPixelRgb(x, y, (x * 7) % 256, (y * 5) % 256, (x + y) % 256);
}
}
return img.encodeJpg(image, quality: 90);
}
test('produces a small jpeg data-URI that fits the byte budget', () {
final uri = offerThumbnailDataUri(bigPhoto(), maxBytes: 40000);
expect(uri, isNotNull);
expect(uri!, startsWith('data:image/jpeg;base64,'));
final b64 = uri.substring('data:image/jpeg;base64,'.length);
expect(b64.length, lessThanOrEqualTo(40000));
// The payload really is a smaller image than the source.
final decoded = img.decodeJpg(base64.decode(b64))!;
expect(decoded.width, lessThanOrEqualTo(320));
expect(decoded.height, lessThanOrEqualTo(320));
});
test('never upscales an already-tiny image', () {
final small = img.encodeJpg(img.Image(width: 64, height: 48), quality: 80);
final uri = offerThumbnailDataUri(small);
expect(uri, isNotNull);
final b64 = uri!.substring('data:image/jpeg;base64,'.length);
final decoded = img.decodeJpg(base64.decode(b64))!;
expect(decoded.width, 64);
expect(decoded.height, 48);
});
test('returns null for bytes that are not an image', () {
expect(offerThumbnailDataUri(Uint8List.fromList([1, 2, 3, 4])), isNull);
});
group('decodeDataUri', () {
test('round-trips a base64 data URI to its bytes', () {
final bytes = Uint8List.fromList([9, 8, 7, 6]);
final uri = 'data:image/jpeg;base64,${base64.encode(bytes)}';
expect(decodeDataUri(uri), bytes);
});
test('returns null for a plain http url', () {
expect(decodeDataUri('https://example.org/a.jpg'), isNull);
});
});
}

View file

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
@ -269,12 +270,14 @@ void main() {
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
category: 'Solanaceae',
),
ShareableLot(
lotId: 'lot-2',
varietyId: 'var-2',
summary: 'Judía',
offerStatus: OfferStatus.exchange,
),
@ -291,6 +294,88 @@ void main() {
await cubit.close();
});
test('publishLots embeds an inline thumbnail data-URI on the offer',
() async {
final transport = FakeOfferTransport();
var thumbnailed = 0;
final cubit = OffersCubit(
transport,
coverPhoto: (id) async => Uint8List.fromList([1, 2, 3]),
thumbnail: (bytes) {
thumbnailed++;
return 'data:image/jpeg;base64,AAAA';
},
);
await cubit.publishLots(
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',
);
await cubit.discover('sp3');
await pumpEventQueue();
expect(thumbnailed, 1);
expect(cubit.state.offers.single.imageUrl, 'data:image/jpeg;base64,AAAA');
await cubit.close();
});
test('publishLots still publishes (without image) when thumbnailing fails',
() async {
final transport = FakeOfferTransport();
final cubit = OffersCubit(
transport,
coverPhoto: (id) async => Uint8List.fromList([1, 2, 3]),
thumbnail: (bytes) => throw Exception('boom'),
);
final count = await cubit.publishLots(
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',
);
expect(count, 1); // the offer still went out
await cubit.discover('sp3');
await pumpEventQueue();
expect(cubit.state.offers.single.imageUrl, isNull);
await cubit.close();
});
test('publishLots without a thumbnailer publishes no image', () async {
final transport = FakeOfferTransport();
final cubit = OffersCubit(transport); // no thumbnail, no coverPhoto
await cubit.publishLots(
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',
);
await cubit.discover('sp3');
await pumpEventQueue();
expect(cubit.state.offers.single.imageUrl, isNull);
await cubit.close();
});
test('publishLots is a no-op offline or with no area', () async {
final offline = OffersCubit(null);
expect(
@ -298,6 +383,7 @@ void main() {
const [
ShareableLot(
lotId: 'x',
varietyId: 'vx',
summary: 'x',
offerStatus: OfferStatus.shared,
),
@ -315,6 +401,7 @@ void main() {
const [
ShareableLot(
lotId: 'x',
varietyId: 'vx',
summary: 'x',
offerStatus: OfferStatus.shared,
),
@ -336,13 +423,20 @@ void main() {
cubit: cubit,
shareableLots: const [
ShareableLot(
lotId: 'lot-1', summary: 'Tomate', offerStatus: OfferStatus.shared),
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared),
ShareableLot(
lotId: 'lot-2',
varietyId: 'var-2',
summary: 'Judía',
offerStatus: OfferStatus.exchange),
ShareableLot(
lotId: 'lot-3', summary: 'Otro', offerStatus: OfferStatus.shared),
lotId: 'lot-3',
varietyId: 'var-3',
summary: 'Otro',
offerStatus: OfferStatus.shared),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',

View file

@ -6,6 +6,7 @@ import 'package:tane/i18n/strings.g.dart';
import 'package:tane/services/social_service.dart';
import 'package:tane/services/social_settings.dart';
import 'package:tane/ui/market_offer_detail_screen.dart';
import 'package:tane/ui/market_widgets.dart';
import '../support/test_support.dart';
@ -25,7 +26,7 @@ Widget _wrap(MarketOfferDetailScreen screen) {
);
}
Offer _offer({bool organic = false}) => Offer(
Offer _offer({bool organic = false, String? imageUrl}) => Offer(
id: 'lot-1',
authorPubkeyHex: 'ab' * 32,
summary: 'Tomate rosa de Barbastro',
@ -33,6 +34,7 @@ Offer _offer({bool organic = false}) => Offer(
category: 'Solanaceae',
approxGeohash: 'sp3e9',
isOrganic: organic,
imageUrl: imageUrl,
);
void main() {
@ -70,4 +72,34 @@ void main() {
expect(find.byKey(const Key('offerDetail.contact')), findsNothing);
});
testWidgets('shows a hero image when the offer has a photo', (tester) async {
final social = await SocialService.fromRootSeedHex('00' * 32);
final settings = SocialSettings(InMemorySecretStore());
await settings.setRelayUrls(const []);
await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
offer: _offer(imageUrl: 'https://media.example/abc.jpg'),
social: social,
settings: settings,
)));
await tester.pumpAndSettle();
expect(find.byType(OfferHeroImage), findsOneWidget);
});
testWidgets('shows no hero image when the offer has none', (tester) async {
final social = await SocialService.fromRootSeedHex('00' * 32);
final settings = SocialSettings(InMemorySecretStore());
await settings.setRelayUrls(const []);
await tester.pumpWidget(_wrap(MarketOfferDetailScreen(
offer: _offer(),
social: social,
settings: settings,
)));
await tester.pumpAndSettle();
expect(find.byType(OfferHeroImage), findsNothing);
});
}

View file

@ -0,0 +1,62 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/ui/market_widgets.dart';
Widget _wrap(Widget child) => MaterialApp(home: Scaffold(body: Center(child: child)));
/// A 1×1 PNG as an inline data URI the shape offers actually publish.
const _dataUri =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lE'
'QVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
void main() {
testWidgets('OfferThumbnail renders an inline data-URI from memory',
(tester) async {
await tester.pumpWidget(
_wrap(const OfferThumbnail(url: _dataUri, semanticLabel: 'Photo')),
);
await tester.pump();
expect(find.byType(Image), findsOneWidget);
// Decoded fine no broken-image placeholder.
expect(find.byIcon(Icons.image_not_supported_outlined), findsNothing);
});
testWidgets('OfferThumbnail builds a network image', (tester) async {
await tester.pumpWidget(
_wrap(const OfferThumbnail(
url: 'https://media.example/abc.jpg',
semanticLabel: 'Photo',
)),
);
// The image widget is present; its loadingBuilder/errorBuilder handle the
// remote fetch, so we only assert the widget is wired (no network in tests).
expect(find.byType(Image), findsOneWidget);
});
testWidgets('OfferThumbnail falls back to a placeholder when the load fails',
(tester) async {
await tester.pumpWidget(
_wrap(const OfferThumbnail(
url: 'https://media.example/missing.jpg',
semanticLabel: 'Photo',
)),
);
// Let the (failed) network fetch resolve; the errorBuilder then shows a
// neutral icon instead of a broken image.
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(find.byIcon(Icons.image_not_supported_outlined), findsOneWidget);
});
testWidgets('OfferHeroImage builds a network image', (tester) async {
await tester.pumpWidget(
_wrap(const OfferHeroImage(
url: 'https://media.example/abc.jpg',
semanticLabel: 'Photo',
)),
);
expect(find.byType(Image), findsOneWidget);
});
}