// Copyright 2026 Kerem Bas. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter_test/flutter_test.dart';
import 'package:palette_generator_plus/palette_generator_plus.dart';

import 'encoded_images.dart';

Future<ui.Image> decode(Uint8List png) async {
  final ui.Codec codec = await ui.instantiateImageCodec(png);
  return (await codec.getNextFrame()).image;
}

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  late ui.Image landscape;

  setUpAll(() async {
    landscape = await decode(kImageDataLandscape);
  });

  test('Celebi: isolate output equals main-thread output', () async {
    final PaletteGenerator inIsolate =
        await PaletteGenerator.fromImage(landscape, runInIsolate: true);
    final PaletteGenerator onMain =
        await PaletteGenerator.fromImage(landscape, runInIsolate: false);
    expect(inIsolate.paletteColors, equals(onMain.paletteColors));
    expect(inIsolate.selectedSwatches, equals(onMain.selectedSwatches));
  });

  test('Legacy: isolate output equals main-thread output', () async {
    final PaletteGenerator inIsolate = await PaletteGenerator.fromImage(
      landscape,
      runInIsolate: true,
      quantizer: const LegacyQuantizer(),
    );
    final PaletteGenerator onMain = await PaletteGenerator.fromImage(
      landscape,
      runInIsolate: false,
      quantizer: const LegacyQuantizer(),
    );
    expect(inIsolate.paletteColors, equals(onMain.paletteColors));
    expect(inIsolate.selectedSwatches, equals(onMain.selectedSwatches));
  });
}
