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