import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';

Future<void> testExecutable(FutureOr<void> Function() testMain) async {
  await loadAppFonts();
  goldenFileComparator =
      CustomGoldenFileComparator(Uri.parse('test/src/goldens'));
  return testMain();
}

class CustomGoldenFileComparator extends LocalFileComparator {
  CustomGoldenFileComparator(Uri testFile) : super(testFile);

  @override
  Future<bool> compare(Uint8List imageBytes, Uri golden) async {
    final result = await GoldenFileComparator.compareLists(
      imageBytes,
      await getGoldenBytes(golden),
    );

    if (!result.passed && result.diffPercent > 0.05) {
      final error = await generateFailureOutput(result, golden, basedir);
      throw FlutterError(error);
    }
    return true;
  }

  @override
  Future<void> update(Uri golden, Uint8List imageBytes) =>
      super.update(golden, imageBytes);
}
